将char数组传递给CUDA内核
Passing char array to CUDA Kernel
我正试图将一个包含10000字的char数组从主函数中的txt文件中读取到CUDA内核函数。
单词从主机传输到设备,如下所示:
(主要功能代码:)
//.....
const int text_length = 20;
char (*wordList)[text_length] = new char[10000][text_length];
char *dev_wordList;
for(int i=0; i<number_of_words; i++)
{
file>>wordList[i];
cout<<wordList[i]<<endl;
}
cudaMalloc((void**)&dev_wordList, 20*number_of_words*sizeof(char));
cudaMemcpy(dev_wordList, &(wordList[0][0]), 20 * number_of_words * sizeof(char), cudaMemcpyHostToDevice);
//Setup execution parameters
int n_blocks = (number_of_words + 255)/256;
int threads_per_block = 256;
dim3 grid(n_blocks, 1, 1);
dim3 threads(threads_per_block, 1, 1);
cudaPrintfInit();
testKernel<<<grid, threads>>>(dev_wordList);
cudaDeviceSynchronize();
cudaPrintfDisplay(stdout,true);
cudaPrintfEnd();
(内核功能代码:)
__global__ void testKernel(char* d_wordList)
{
//access thread id
const unsigned int bid = blockIdx.x;
const unsigned int tid = threadIdx.x;
const unsigned int index = bid * blockDim.x + tid;
cuPrintf("!! %c%c%c%c%c%c%c%c%c%c n" , d_wordList[index * 20 + 0],
d_wordList[index * 20 + 1],
d_wordList[index * 20 + 2],
d_wordList[index * 20 + 3],
d_wordList[index * 20 + 4],
d_wordList[index * 20 + 5],
d_wordList[index * 20 + 6],
d_wordList[index * 20 + 7],
d_wordList[index * 20 + 8],
d_wordList[index * 20 + 9]);
}
有没有办法更容易地操纵它们?(我希望每个元素/位置都有一个单词)我尝试过<string>
,但我不能在CUDA设备代码中使用它们。
cuPrintf("%sn", d_wordlist+(index*20));
应该工作吗?(如果您的字符串为零终止)
更新:
此行:
char (*wordList)[text_length] = new char[10000][text_length];
在我看来很奇怪。一般来说,指向char的指针数组会这样分配:
char** wordList = new char*[10000];
for (int i=0;i<10000;i++) wordList[i] = new char[20];
在这种情况下,wordList[i]将是指向字符串编号i的指针。
更新#2:
如果你需要将字符串存储为一个连续的块,并且你确信没有一个字符串超过text_length+1,那么你可以这样做:
char *wordList = new char[10000*text_length];
for(int i=0; i<number_of_words; i++)
{
file>>wordList+(i*text_length);
cout<<wordList+(i*text_length)<<endl;
}
在这种情况下,wordList+(i*text_length)将指向字符串编号i的开头,它将以0结尾,因为这就是您从文件中读取它的方式,并且您将能够按照此答案中指定的方式打印出来。但是,如果您的任何字符串比text_length-1长,您仍然会遇到问题。
相关文章:
- CUDA内核和数学函数的显式命名空间
- 将 2D 推力::d evice_vector 复矩阵传递给 CUDA 内核函数
- 如何将矢量的数据传递给 CUDA 内核?
- 无法在 cuda 内核中使用我的模板类
- CUDA内核printf()在终端中不产生输出,在探查器中工作
- 编译为 cuda 内核调用提供了"expression must have integral or unscoped enum type"
- 使用模板模式优化 CUDA 内核
- 带有大结构变量的 CUDA 内核函数给出了错误的结果
- CUDA 内核在第二次运行时运行得更快 - 为什么?
- 在CUDA内核中传递一个常数整数
- 如何将函数作为CUDA内核参数传递
- 验证调用 cuda 内核的次数
- cuda 内核调用/传递参数中的编译错误
- 预期;在 CUDA 内核上
- CUDA 内核"Only a single pack parameter is allowed"解决方法?
- 降低CUDA内核运行时:内核中矩阵的动态内存分配
- 如何在多个 GPU 上同时执行 cufftXt 和 CUDA 内核
- 执行 CUDA 内核时黑屏C++输出正确的结果
- CUDA 内核中的竞争条件
- 优化三角矩阵计算的 CUDA 内核执行