我试图打印使用cuPrint函数作为内核函数参数传递的字符串向量的元素。在内核函数中使用cuPrint打印字符串向量的元素
内核
__global__ void testKernel(string wordList[10000])
{
//access thread id
const unsigned int bid = blockIdx.x;
const unsigned int tid = threadIdx.x;
const unsigned int index = bid * blockDim.x + tid;
cuPrintf("wordList[%d]: %s \n", index, wordList[index]);
}
从主要功能设置执行参数代码的代码和启动内核
//Allocate device memory for word list
string* d_wordList;
cudaMalloc((void**)&d_wordList, sizeof(string)*number_of_words);
//Copy word list from host to device
cudaMemcpy(d_wordList, wordList, sizeof(string)*number_of_words, 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>>>(d_wordList);
cudaDeviceSynchronize();
cudaPrintfDisplay(stdout,true);
cudaPrintfEnd();
我收到错误: “错误44错误:调用主机函数(“std :: basic_string,std :: allocator> ::〜basic_string”)从全球函数(“testKernel”)不被允许D:... \ kernel.cu 44 1 CUDA_BF_lar ge_word_list “
我错过了什么? 在此先感谢。
我正在从这样的文本文件中读取文字 \t //构建包含来自文本文件的文字的字符串数组 \t string wordList [10000]; \t if(file。IS_OPEN()) \t { \t \t \t 为\t(INT I = 0; I>单词一览[I]; \t \t \t // cout << endl << wordList [i] << endl; \t \t} \t \t \t} 会有什么用字符数组的变化? –
2014-09-22 13:30:43
在我的答案中提供了示例代码的链接,其中显示了如何操作C风格的字符串。我假设你可以处理文件I/O。这不是CUDA特有的。 – 2014-09-22 15:20:56
是的,处理文件I/O没有问题。谢谢! – 2014-09-23 06:50:21