问题是:库达内核中是否存在使用类“向量”的方法?当我尝试时,我会得到以下错误:
error : calling a host function("std::vector<int, std::allocator<int> > ::push_back") from a __device__/__global__ function not allowed那么有一种方法可以在全局区域使用向量吗?我最近尝试了以下几种方法:
.之后,我能够在我的Cuda内核中使用printf标准库函数。
是否有办法按照内核代码中支持printf的方式使用标准库类vector?这是在内核代码中使用printf的一个示例:
// this code only to count the 3s in an array using Cuda
//private_count is an array to hold every thread's result separately
__global__ void countKernel(int *a, int length, int* private_count)
{
printf("%d\n",threadIdx.x); //it's print the thread id and it's working
// vector<int> y;
//y.push_back(0); is there a possibility to do this?
unsigned int offset = threadIdx.x * length;
int i = offset;
for( ; i < offset + length; i++)
{
if(a[i] == 3)
{
private_count[threadIdx.x]++;
printf("%d ",a[i]);
}
}
}发布于 2012-04-29 20:47:34
你不能在数据自动化系统中使用STL,但是你可以使用推力库来做你想做的事情。否则,只需将矢量的内容复制到设备上,并对其进行正常操作。
发布于 2013-05-03 16:02:31
在库达库推力中,可以使用thrust::device_vector<classT>在设备上定义向量,并且主机STL向量和设备向量之间的数据传输非常简单。您可以参考这个有用的链接:http://docs.nvidia.com/cuda/thrust/index.html,以找到一些有用的示例。
发布于 2012-04-30 12:58:53
不能在设备代码中使用std::vector,应该使用数组。
https://stackoverflow.com/questions/10375680
复制相似问题