Search code examples
cudacharmemcpy

I have problems copying char on the GPU with CUDA


Host:

unsigned char exp[128];
unsigned char __e;

i = cudaMalloc( (void**)&__e, 128 * sizeof(unsigned char) );
if(i != cudaSuccess)
    printf("cudaMalloc __e FAIL! Code: %d\n", i);

BN_bn2bin128B(e, exp);  // copy data from e to exp, no problems since here

i = cudaMemcpy( &__e, &exp, 128 * sizeof(unsigned char), cudaMemcpyHostToDevice);
if(i != cudaSuccess)
    printf("cudaMemcpy __e FAIL! Code: %d\n", i);

Output:

cudaMemcpy __e FAIL! Code: 11

Error 11 corresponds to:

cudaErrorInvalidValue = 11, ///< Invalid value

Why? Where is the error?


Solution

  • You have declared __e incorrectly. It must be a pointer. Try this:

    unsigned char exp[128];
    unsigned char * __e;
    
    i = cudaMalloc( (void**)&__e, 128 * sizeof(unsigned char) );
    if(i != cudaSuccess)
        printf("cudaMalloc __e FAIL! Code: %d\n", i);
    
    // whatever goes here to set exp
    
    i = cudaMemcpy( __e, &exp[0], 128 * sizeof(unsigned char), cudaMemcpyHostToDevice);
    if(i != cudaSuccess)
        printf("cudaMemcpy __e FAIL! Code: %d\n", i);