It sounds like you might need to brush up a bit on strings and pointers in C. Here is something to ponder while you are doing it:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "cuda_runtime.h"
#ifndef gpuAssert
#define gpuAssert( condition ) { if( (condition) != 0 ) { fprintf( stderr, "\n FAILURE %s in %s, line %d\n", cudaGetErrorString(condition), __FILE__, __LINE__ ); exit( 1 ); } }
#endif
#define nstrings (2)
#define strlenlim (9)
__global__ void stringfoo(char ** instrings)
{
const unsigned int tform = 0xafa00;
if (threadIdx.x < nstrings) {
unsigned int *ival = (unsigned int *)instrings[threadIdx.x];
*ival -= tform;
}
return;
}
int main()
{
char * a[nstrings];
char * _s[nstrings];
char ** _a;
char s1[] = "foo1
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include “cuda_runtime.h”
#ifndef gpuAssert
#define gpuAssert( condition ) { if( (condition) != 0 ) { fprintf( stderr, “\n FAILURE %s in %s, line %d\n”, cudaGetErrorString(condition), FILE, LINE ); exit( 1 ); } }
#endif
#define nstrings (2)
#define strlenlim (9)
global void stringfoo(char ** instrings)
{
const unsigned int tform = 0xafa00;
if (threadIdx.x < nstrings) {
unsigned int *ival = (unsigned int *)instrings[threadIdx.x];
*ival -= tform;
}
return;
}
int main()
{
char * a[nstrings];
char * _s[nstrings];
char ** _a;
char s1[] = "foo1\0";
char s2[] = "foo2\0";
a[0] = s1;
a[1] = s2;
for (int i = 0; i < nstrings; i++) {
size_t slen = strlen(a[i]);
size_t clen = (slen > strlenlim) ? strlenlim : slen;
gpuAssert( cudaMalloc((void **)&_s[i], strlenlim) );
gpuAssert( cudaMemcpy(_s[i], a[i], clen, cudaMemcpyHostToDevice) );
}
size_t alen = size_t(nstrings) * sizeof(char *);
gpuAssert( cudaMalloc((void ***)&_a, alen) );
gpuAssert( cudaMemcpy(_a, _s, alen, cudaMemcpyHostToDevice) );
stringfoo <<< 1, nstrings >>> (_a);
gpuAssert( cudaGetLastError() );
char sout[strlenlim];
for (int i = 0; i < nstrings; i++) {
gpuAssert( cudaMemcpy(sout, _s[i], size_t(strlenlim), cudaMemcpyDeviceToHost) );
fprintf(stdout, "%s %s\n", a[i], sout);
}
}
/* vim:ts=4:sw=4:expandtab:number */
";
char s2[] = "foo2
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include “cuda_runtime.h”
#ifndef gpuAssert
#define gpuAssert( condition ) { if( (condition) != 0 ) { fprintf( stderr, “\n FAILURE %s in %s, line %d\n”, cudaGetErrorString(condition), FILE, LINE ); exit( 1 ); } }
#endif
#define nstrings (2)
#define strlenlim (9)
global void stringfoo(char ** instrings)
{
const unsigned int tform = 0xafa00;
if (threadIdx.x < nstrings) {
unsigned int *ival = (unsigned int *)instrings[threadIdx.x];
*ival -= tform;
}
return;
}
int main()
{
char * a[nstrings];
char * _s[nstrings];
char ** _a;
char s1[] = "foo1\0";
char s2[] = "foo2\0";
a[0] = s1;
a[1] = s2;
for (int i = 0; i < nstrings; i++) {
size_t slen = strlen(a[i]);
size_t clen = (slen > strlenlim) ? strlenlim : slen;
gpuAssert( cudaMalloc((void **)&_s[i], strlenlim) );
gpuAssert( cudaMemcpy(_s[i], a[i], clen, cudaMemcpyHostToDevice) );
}
size_t alen = size_t(nstrings) * sizeof(char *);
gpuAssert( cudaMalloc((void ***)&_a, alen) );
gpuAssert( cudaMemcpy(_a, _s, alen, cudaMemcpyHostToDevice) );
stringfoo <<< 1, nstrings >>> (_a);
gpuAssert( cudaGetLastError() );
char sout[strlenlim];
for (int i = 0; i < nstrings; i++) {
gpuAssert( cudaMemcpy(sout, _s[i], size_t(strlenlim), cudaMemcpyDeviceToHost) );
fprintf(stdout, "%s %s\n", a[i], sout);
}
}
/* vim:ts=4:sw=4:expandtab:number */
";
a[0] = s1;
a[1] = s2;
for (int i = 0; i < nstrings; i++) {
size_t slen = strlen(a[i]);
size_t clen = (slen > strlenlim) ? strlenlim : slen;
gpuAssert( cudaMalloc((void **)&_s[i], strlenlim) );
gpuAssert( cudaMemcpy(_s[i], a[i], clen, cudaMemcpyHostToDevice) );
}
size_t alen = size_t(nstrings) * sizeof(char *);
gpuAssert( cudaMalloc((void ***)&_a, alen) );
gpuAssert( cudaMemcpy(_a, _s, alen, cudaMemcpyHostToDevice) );
stringfoo <<< 1, nstrings >>> (_a);
gpuAssert( cudaGetLastError() );
char sout[strlenlim];
for (int i = 0; i < nstrings; i++) {
gpuAssert( cudaMemcpy(sout, _s[i], size_t(strlenlim), cudaMemcpyDeviceToHost) );
fprintf(stdout, "%s %s\n", a[i], sout);
}
}
/* vim:ts=4:sw=4:expandtab:number */
which does this:
avidday@cuda:~$ nvcc stringfoo.cu -o stringfoo
./stringfoo.cu(19): Warning: Cannot tell what pointer points to, assuming global memory space
./stringfoo.cu(19): Warning: Cannot tell what pointer points to, assuming global memory space
avidday@cuda:~$ ./stringfoo
foo1 fud1
foo2 fud2