简体   繁体   English

使用CUDA在主机设备中将char转换为int

[英]char to int conversion in host device with CUDA

I have been having trouble converting from a single character to an integer while in the host function of my CUDA program. 在CUDA程序的宿主函数中,从单个字符转换为整数时遇到了麻烦。 After the line - 后行-

token[j] = token[j] * 10 + (buf[i] - '0' );

I use cuda-gdb check the value for token[j], and I always get different numbers that do not seem to have a pattern. 我使用cuda-gdb检查token [j]的值,并且我总是得到似乎没有模式的不同数字。 I have also tried simple casting, not multiplying by ten (which I saw in another thread), not subtracting '0', and I always seem to get a different result. 我还尝试了简单的强制转换,而不是乘以十(我在另一个线程中看到),而不是减去“ 0”,而且我似乎总是得到不同的结果。 Any help would be appreciated. 任何帮助,将不胜感激。 This is my first time posting on stack overflow, so give me a break if my formatting is awful. 这是我第一次在堆栈溢出时发布消息,因此如果格式很糟糕,请给我休息一下。

-A fellow struggling coder -一位挣扎的编码员

 __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' );

There's a race condition on writing to token . 写入令牌存在竞争条件。

If you want to have a local array per block you can use shared memory. 如果要每个块有一个本地数组,则可以使用共享内存。 If you want a local array per thread, you will need to use local per-thread memory and declare the array on the stack. 如果要每个线程一个本地数组,则需要使用本地每个线程内存,并在堆栈上声明该数组。 In the first case you will have to deal with concurrency inside the block as well. 在第一种情况下,您还必须处理块内的并发。 In the latter you don't have to, although you might potentially waste a lot more memory (and reduce collaboration). 在后者中,您不必这样做,尽管您可能会浪费更多的内存(并减少协作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM