简体   繁体   中英

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. 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. 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. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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