Как засунуть :: make_transform_iterator, который разыменовывает device_ptr? - PullRequest
0 голосов
/ 04 декабря 2018

Новое в C ++ и CUDA.Использование MSVS 2015 Community и CUDA 9.2.

Я попытался создать transform_iterator, который просто разыменовывает device_ptr.

Я получаю ошибку компиляции: функция "dereference_device_double_functor :: operator ()" не может быть вызвана с помощьюзаданный список аргументов

Я также сделал версию, в которой используется host_vector и просто обычный двойной указатель, чтобы убедиться, что мое использование функтора правильно.

    #include <iostream>
    #include "thrust\device_vector.h"
    #include "thrust\host_vector.h"

    struct dereference_device_double_functor
    {
        dereference_device_double_functor() {}

        typedef thrust::device_reference<thrust::device_ptr<double>> argument_type;
        typedef double result_type;

        __host__ __device__
            double operator()(thrust::device_reference<thrust::device_ptr<double>> xDpRef) const {
            thrust::device_ptr<double> xDp = (thrust::device_ptr<double>)xDpRef;
            return *xDp;
        }
    };

    struct dereference_host_double_functor
    {
        dereference_host_double_functor() {}

        typedef double* argument_type;
        typedef double result_type;

        __host__ __device__
            double operator()(double* const& xPtr) const {
            return *xPtr;
        }
    };

    int main()
    {
        // Create double
        thrust::device_vector<double> dv(1, 5);
        thrust::host_vector<double> hv(1, 6);

        // Make sure its there
        std::cout << dv[0] << std::endl;
        std::cout << hv[0] << std::endl;

        // Create pointers to doubles
        thrust::device_vector<thrust::device_ptr<double>> dvPtr(1);
        thrust::device_vector<double*> hvPtr(1);

        // Assign pointers to doubles
        dvPtr[0] = &(dv[0]);
        hvPtr[0] = &(hv[0]);

        // Make sure pointers point correctly
        std::cout << *((thrust::device_ptr<double>)dvPtr[0]) << std::endl;
        std::cout << *(hvPtr[0]) << std::endl;

        // Test functor with iterator
        auto dvi = dvPtr.begin();
        double dvd = dereference_device_double_functor()(*dvi);
        auto hvi = hvPtr.begin();
        double hvd = dereference_host_double_functor()(*hvi);

        // Make sure it worked with iterator
        std::cout << dvd << std::endl;
        std::cout << hvd << std::endl;

        // Make dereferencing transfom iterators
        auto tik = thrust::make_transform_iterator(dvPtr.begin(), dereference_device_double_functor());
        auto tij = thrust::make_transform_iterator(hvPtr.begin(), dereference_host_double_functor());

        // Check that transform iterators work
        //std::cout << *tik << std::endl; // Will cause compile error: function "dereference_device_double_functor::operator()" cannot be called with the given argument list
        std::cout << *tij << std::endl;

        return 0;
    }

Спасибо за вашу помощь!

1 Ответ

0 голосов
/ 04 декабря 2018

В вашем вопросе вы утверждаете это:

Я попытался создать transform_iterator, который просто разыменовывает device_ptr.

Это не то, что я вижу в вашем коде, однако:

    __host__ __device__
        double operator()(thrust::device_reference<thrust::device_ptr<double>> xDpRef) const {

Когда я компилирую ваш код в linux, я получаю следующее (взято из сообщения об ошибке компиляции):

$ nvcc -std=c++11 -o t351 t351.cu
/usr/local/cuda/bin/..//include/thrust/iterator/transform_iterator.h(312): error: function "dereference_device_double_functor::operator()" cannot be called with the given argument list
            argument types are: (thrust::device_ptr<double>)
            object type is: dereference_device_double_functor
...

Так что тяга передает вам thrust::device_ptr<double>.Но ваш оператор функтора настроен на thrust::device_reference<thrust::device_ptr<double>>

Когда я изменяю ваш код с этого:

    __host__ __device__
        double operator()(thrust::device_reference<thrust::device_ptr<double>> xDpRef) const {

на это:

    __host__ __device__
        double operator()(thrust::device_ptr<double> xDpRef) const {

Он компилируется иработает правильно для меня (на Linux):

$ cat t351.cu
 #include <iostream>
    #include <thrust/device_vector.h>
    #include <thrust/host_vector.h>

    struct dereference_device_double_functor
    {
        dereference_device_double_functor() {}

        typedef thrust::device_reference<thrust::device_ptr<double>> argument_type;
        typedef double result_type;

        __host__ __device__
            double operator()(thrust::device_ptr<double> xDpRef) const {
            thrust::device_ptr<double> xDp = (thrust::device_ptr<double>)xDpRef;
            return *xDp;
        }
    };

    struct dereference_host_double_functor
    {
        dereference_host_double_functor() {}

        typedef double* argument_type;
        typedef double result_type;

        __host__ __device__
            double operator()(double* const& xPtr) const {
            return *xPtr;
        }
    };

    int main()
    {
        // Create double
        thrust::device_vector<double> dv(1, 5);
        thrust::host_vector<double> hv(1, 6);

        // Make sure its there
        std::cout << dv[0] << std::endl;
        std::cout << hv[0] << std::endl;

        // Create pointers to doubles
        thrust::device_vector<thrust::device_ptr<double>> dvPtr(1);
        thrust::device_vector<double*> hvPtr(1);

        // Assign pointers to doubles
        dvPtr[0] = &(dv[0]);
        hvPtr[0] = &(hv[0]);

        // Make sure pointers point correctly
        std::cout << *((thrust::device_ptr<double>)dvPtr[0]) << std::endl;
        std::cout << *(hvPtr[0]) << std::endl;

        // Test functor with iterator
        auto dvi = dvPtr.begin();
        double dvd = dereference_device_double_functor()(*dvi);
        auto hvi = hvPtr.begin();
        double hvd = dereference_host_double_functor()(*hvi);

        // Make sure it worked with iterator
        std::cout << dvd << std::endl;
        std::cout << hvd << std::endl;

        // Make dereferencing transfom iterators
        auto tik = thrust::make_transform_iterator(dvPtr.begin(), dereference_device_double_functor());
        auto tij = thrust::make_transform_iterator(hvPtr.begin(), dereference_host_double_functor());

        // Check that transform iterators work
        std::cout << *tik << std::endl; // Will cause compile error: function "dereference_device_double_functor::operator()" cannot be called with the given argument list
        std::cout << *tij << std::endl;

        return 0;
    }
$ nvcc -std=c++11 -o t351 t351.cu
$ cuda-memcheck ./t351
========= CUDA-MEMCHECK
5
6
5
6
5
6
5
6
========= ERROR SUMMARY: 0 errors
$
...