Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cuda 不调用设备功能_Cuda_Thrust - Fatal编程技术网

Cuda 不调用设备功能

Cuda 不调用设备功能,cuda,thrust,Cuda,Thrust,我有以下简单的CUDA推力代码,它在设备向量中增加了10个,但是这个函数是在主机端而不是设备端被调用的 #include <algorithm> #include <iostream> #include <numeric> #include <vector> #include <stdio.h> #include <thrust/device_vector.h> __host__ __device__ int add(in

我有以下简单的CUDA推力代码,它在设备向量中增加了10个,但是这个函数是在主机端而不是设备端被调用的

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
#include <stdio.h>
#include <thrust/device_vector.h>

__host__ __device__ int add(int x){
    #if defined(__CUDA_ARCH__)
     printf("In device\n");
    #else
     printf("In host\n");
    #endif

    return x+10;
}

int main(void)
{
    thrust::host_vector<int> H(4);
    H[0] = H[1] = H[2] = H[3] = 10;

    thrust::device_vector<int> data=H;

    std::transform(data.begin(), data.end(), data.begin(),add);
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
__主机\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu{
#如果已定义(uuu CUDA_uuarch_uuuuu)
printf(“在设备中\n”);
#否则
printf(“在主机中”);
#恩迪夫
返回x+10;
}
内部主(空)
{
推力:主_矢量H(4);
H[0]=H[1]=H[2]=H[3]=10;
推力:设备_矢量数据=H;
std::transform(data.begin()、data.end()、data.begin()、add);
返回0;
}
我在这里做错了什么?

有很好的例子可以效仿

有些人已经指出,看起来你有几个问题

  • 如果要使用推力,应该使用
    推力::变换
    ,而不是
    std::变换
    std::transform
    不了解GPU、CUDA或推力,并将调度您的
    add
    函数的主机版本。我不确定当你把一个
    推力::设备_向量
    传递给它时,它会做什么

  • 由于Jared指出的原因,推力算法需要使用函数对象(函子),而不是裸CUDA
    \uuuuuuu设备\uuuuuu
    函数(源代码中的推力算法实际上是主机代码。该主机代码无法发现裸CUDA
    \uuuuuuu设备
    函数的地址)。通过此修复,您可以非常确定,在处理设备向量时,推力将分派设备代码路径

  • 下面是对您的代码的修改:

    $ cat t856.cu
    #include <stdio.h>
    #include <thrust/host_vector.h>
    #include <thrust/device_vector.h>
    #include <thrust/transform.h>
    
    struct my_func {
    
    __host__ __device__
      int operator()(int x){
        #if defined(__CUDA_ARCH__)
         printf("In device, x is %d\n", x);
        #else
         printf("In host, x is %d\n", x);
        #endif
    
        return x+10;
      }
    };
    
    int main(void)
    {
        thrust::host_vector<int> H(4);
        H[0] = H[1] = H[2] = H[3] = 10;
    
        thrust::device_vector<int> data=H;
    
        thrust::transform(data.begin(), data.end(), data.begin(),my_func());
        return 0;
    }
    $ nvcc -o t856 t856.cu
    $ ./t856
    In device, x is 10
    In device, x is 10
    In device, x is 10
    In device, x is 10
    $
    
    $cat t856.cu
    #包括
    #包括
    #包括
    #包括
    结构我的函数{
    __主机设备__
    int运算符()(int x){
    #如果已定义(uuu CUDA_uuarch_uuuuu)
    printf(“在设备中,x是%d\n”,x);
    #否则
    printf(“在主机中,x是%d\n”,x);
    #恩迪夫
    返回x+10;
    }
    };
    内部主(空)
    {
    推力:主_矢量H(4);
    H[0]=H[1]=H[2]=H[3]=10;
    推力:设备_矢量数据=H;
    转换(data.begin(),data.end(),data.begin(),my_func());
    返回0;
    }
    $nvcc-o t856 t856.cu
    美元/t856
    在这个设备中,x是10
    在这个设备中,x是10
    在这个设备中,x是10
    在这个设备中,x是10
    $
    
    一个
    \uuuuu设备
    \uuuu主机
    函数(
    添加
    )的地址不能被
    \uuuu主机
    函数(
    )获取。使用函数对象(例如,
    推力::plus
    )而不是自由函数。