roger
roger

Reputation: 11

"invalid device symbol" for pointers that are struct fields

I have the code snippets shown below. The two calls to "cudaMemcpyToSymbol" are essentially identical (all the operations to dev_a, dev_b are also the same), but when I ran the code, it reported an "invalid device symbol" error only for the second call to "cudaMemcpyToSymbol". If I deleted it, then the code ran without any problem. Does anyone know what might be the reason? Thanks.

struct aStruct {
  double *a;
  double *b;
};

__device__ struct aStruct as;

int main(void) {

double *dev_a, *dev_b;
HANDLE_ERROR( cudaMalloc( (void**)&dev_a, N * sizeof(double) ) );
HANDLE_ERROR( cudaMalloc( (void**)&dev_b, N * sizeof(double) ) );
...
HANDLE_ERROR( cudaMemcpyToSymbol(as.a, &dev_a, sizeof(double *)) );
HANDLE_ERROR( cudaMemcpyToSymbol(as.b, &dev_b, sizeof(double *)) );
....
}

Upvotes: 0

Views: 431

Answers (1)

talonmies
talonmies

Reputation: 72352

Exactly as the error says, as.a and as.b are not valid device symbols. as is a device symbol, but members of the structure itself don't have symbol table entries and you cannot directly apply a cudaMemcpyToSymbol on them.

To solve this, do something like the following:

struct aStruct _as;

HANDLE_ERROR( cudaMalloc( (void**)&_as.a, N * sizeof(double) ) );
HANDLE_ERROR( cudaMalloc( (void**)&_as.b, N * sizeof(double) ) );

HANDLE_ERROR( cudaMemcpyToSymbol(as, &_as, sizeof(struct aStruct)) );

[disclaimer written in browser, not compiled or tested, use at own risk]

ie. copy a completely initialised struct aStruct only the symbol, rather than trying to copy member by member.

Upvotes: 2

Related Questions