Search code examples
c++cudanvcc

C++ with CUDA: how to express a byte as a char or set of chars?


INSIDE THE CUDA KERNEL

Suppose I have a byte that can have a binary value between 0 and 255.

I have a character array (char *) length three:

char * c = (char *) malloc(300000000*sizeof(char)); // 30 mb

Short of the following (as in, I would like to rule out “solutions” that involve a manual byte to char representation):

switch(my_byte){
    case 0:
       c[0] = '0';
    case 1:
       c[1] = '1';
    ...
    case 255:
       c[0] = '2';
       c[1] = '5';
       c[2] = '5';
}

How do I convert the byte to a char * style string in a Cuda kernel?


Solution

  • This is my solution, for now, in an effort to avoid the flow control issue in the vectorized code.

    /*! \brief byte to raw chars; this is not a string! */
    __device__ void byte_to_chars(uint8_t b,char * str_arr_ptr){
      uint8_t buf[4];
    
      buf[0] = b / 100;
      buf[1] = (b % 100 - b % 10) / 10;      
      buf[2] = b % 10;
    
      buf[3] = 3 - !buf[0] + !buf[0]*!buf[1]; // size
    
      // buf[3] = sz
      // 3 - buf[3] = missing digits; i.e., 1 for 023, 2 for 003
      for(int i = 0; i < buf[3]; i++) str_arr_ptr[0][i] = buf[ i + 3 - buf[3] ]+'0';              
    
      // modify function signature as needed -- i.e., return
      // useful info 
    }
    

    However, a solution based on library calls would be best.