Problem:
I've a matrix in C++ filled with strings and I want to pass it to cuda kernel function. I know that CUDA can't handle strings so after some research I've tried out with some solutions listed below.
Attempts:
define an array of pointers in C++ containing for each cell a pointer chars (for simplicity tmp[i] is filled with the strings contained into the matrix previously cited)
C++ section
char *tmp[3]; int text_length, array_length; text_length = 4; array_length = 3; tmp[0] = (char*) malloc(text_length*sizeof(char)); tmp[1] = (char*) malloc(text_length*sizeof(char)); tmp[2] = (char*) malloc(text_length*sizeof(char)); tmp[0] = "some"; tmp[1] = "rand"; tmp[2] = "text"; char *a[3]; for(int i=0;i<array_length;i++) { cudaMalloc((void**) &a[i],text_length*sizeof(char)); cudaMemcpy(&a[i],&tmp[i],text_length*sizeof(char),cudaMemcpyHostToDevice); } func<<<blocksPerGrid, threadsPerBlock>>>(a);
CUDA section
__global__ void func(char* a[]){ for(int i=0;i<3;i++) printf("value[%d] = %s \n",i, a[i]); }
Output
value[0] = (null) value[1] = (null) value[2] = (null)
spread the matrix filled with strings to a char pointer and pass it to cuda kernel and there try to retrieve the strings (again code simplified in C++)
C++ section
char *a; int index[6]; a = "somerandtext"; index[0] = 0; // first word start index[1] = 3; // first word end index[2] = 4; // same as first word index[3] = 7; index[4] = 8; index[5] = 1; func<<<blocksPerGrid, threadsPerBlock>>>(a,index);
CUDA section
__global__ void func(char* a,int index[]){ int first_word_start = index[0]; int first_word_end = index[1]; // print first word for(int i=first_word_start;i<=first_word_end;i++) printf("%c",a[i]); }
Output
no output produced
I've tried out with a lot of other solutions but no one works for me... The problem can also re proposed asking: how can i pass 'n' strings to a cuda kernel and print (and compare) all of them there ( keep in mind that I can't pass 'n' variables).