【发布时间】:2015-07-12 11:49:25
【问题描述】:
问题是:有没有办法在 Cuda 内核中使用“向量”类?当我尝试时,我收到以下错误:
error : calling a host function("std::vector<int, std::allocator<int> > ::push_back") from a __device__/__global__ function not allowed
那么有办法在全局部分使用向量吗? 我最近尝试了以下方法:
- 创建一个新的 Cuda 项目
- 转到项目的属性
- 打开 Cuda C/C++
- 转到设备
- 将“代码生成”中的值更改为以下值: 计算_20,sm_20
........之后我就可以在我的 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]);
}
}
}
【问题讨论】:
-
+1 完全合法的问题(不知道为什么它被否决。不幸的是,目前的答案是否定的。
-
抱歉发布了 necroposting。只是想知道现在是否有任何答案
标签: cuda