我試圖打印使用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