CUDA中的推力:: device_vector [英] thrust::device_vector in CUDA

查看:199
本文介绍了CUDA中的推力:: device_vector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是CUDA的新手,正在尝试学习用法。有人可以帮忙吗?我的主要功能如下(我在Visual Studio中,我的源文件和头文件分别是.cu和.cuh)

i am new to CUDA and is trying to learn the usage. can someone please help. i have the following in the main function (i am in visual studio and my source and header files are .cu and .cuh respectively)

 thrust::device_vector<float> d_vec(100);
 kernel<<<100,1>>>(d_vec);

然后在内核中,我有

    template <typename T> __global__ kernel(thrust::device_vector<T> d_vec)
    {  int tid = threadIdx.x + blockIdx.x*blockDim.x;
       T xxx = 3.0;
       d_vec[tid] = xxx;
     }

我的目标是使用float调用一次内核,使用double调用一次内核。还要注意,在这个简单的示例中,我有变量xxx(在我的实际情况下,这是一些产生双精度或浮点数的计算)。

my objective is to call the kernel once with float and once with double. Also note that in this simple example i have variable xxx (which in my real case is some computation producing double or float numbers).

我得到两个错误:
1>不允许从 __ global __ 函数调用 __ host __ 函数(operator =) >不允许从 __ global __ 函数调用 __ host __ 函数(运算符[])

and i get two error: 1> calling a __host__ function (operator =) from a __global__ function is not allowed 2> calling a __host__ function (operator []) from a __global__ function is not allowed

所以我猜这是问题所在,其中 d_vec [tid] = ..中的 []和 =但是我的问题是如何访问内核中的设备向量。有人可以澄清什么是正确的程序,我做错了什么。预先感谢

so i guess "[]" and "=" in "d_vec[tid] = .." is the problem. But my question is how do i access the device vector inside my kernel. Can someone please clarify what is the correct procedure and what i am doing wrong. thanks in advance

推荐答案

thrust :: device_vector对象/引用不能用作内核参数。
您可以使用原始指针传递设备矢量数据。

thrust::device_vector objects/references can not be used as kernel parameters. You could use raw pointers to pass the device vector data.

thrust::device_vector<float> d_vec(100);
float* pd_vec = thrust::raw_pointer_cast(d_vec.data());
kernel<<<100,1>>>(pd_vec);

这是内核的原型

template <typename T> __global__ kernel(T* pd_vec)

您的Q与此类似。 如何施加推力:: device_vector< int>指向原始指针

这篇关于CUDA中的推力:: device_vector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆