在我的 CUDA 程序的主机函数中,我一直无法将单个字符转换为整数。行后 -
token[j] = token[j] * 10 + (buf[i] - '0' );
我使用 cuda-gdb 检查 token[j] 的值,我总是得到似乎没有模式的不同数字。我也尝试过简单的转换,不乘以 10(我在另一个线程中看到),不减去“0”,而且我似乎总是得到不同的结果。任何帮助,将不胜感激。这是我第一次在堆栈溢出上发帖,所以如果我的格式很糟糕,请让我休息一下。
-一个苦苦挣扎的程序员
__global__ void rread(unsigned int *table, char *buf, int *threadbytes, unsigned int *token) {
int i = 0;
int j = 0;
*token = NULL;
int tid = threadIdx.x;
unsigned int key;
char delim = ' ';
for(i = tid * *threadbytes; i <(tid * *threadbytes) + *threadbytes ; i++)
{
if (buf[i] != delim) { //check if its not a delim
token[j] = token[j] * 10 + (buf[i] - '0' );
最佳答案
写入 token 时存在竞争条件。
如果你想每个 block 都有一个本地数组,你可以使用共享内存。如果你想要每个线程的本地数组,你将需要使用本地的每个线程内存并在堆栈上声明数组。在第一种情况下,您还必须处理 block 内的并发性。在后者中你不必这样做,尽管你可能会浪费更多的内存(并减少协作)。
关于c++ - 使用 CUDA 在主机设备中将 char 转换为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22617542/