Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cudaInvalidSymbol due to cudaMemcpyToSymbol to a struct

Tags:

cuda

Here is the minimum example that throws cudaInvalidSymbol at sync_and_check("cudaMemcpyToSymbol"), compiled using Cuda compilation tools, release 6.5, V6.5.12:

#include<stdio.h>

__inline __host__ void gpuAssert(cudaError_t code, const char* command, const char *file, int line, 
                 bool abort=true)
{
   if (code != cudaSuccess) 
   {
      fprintf(stderr,"Error while calling %s in %s line %d: %s\n", command,
          file, line, cudaGetErrorString(code));
      if (abort) exit(code);
   }
}

#define sync_and_check(command) { gpuAssert(cudaGetLastError(), command, __FILE__, __LINE__); }

struct S
{
    float one;
};

__device__ __constant__ S d_s;

__global__ void kernel(float* f)
{
    (*f) = d_s.one;
}

int main()
{
    cudaGetLastError();

    S s;
    s.one = 1.f;
    cudaMemcpyToSymbol(&d_s, &s, sizeof(S));
    sync_and_check("cudaMemcpyToSymbol");

    float* d_f;
    cudaMalloc(&d_f, sizeof(float));
    sync_and_check("cudaMalloc");

    dim3 dimGrid(1), dimBlock(32);
    kernel<<<dimGrid, dimBlock>>>(d_f);
    sync_and_check("kernel");

    cudaFree(d_f);
    sync_and_check("cudaFree");
}
like image 888
Leo Avatar asked Dec 11 '25 11:12

Leo


1 Answers

The problem lies in referencing. The correct invocation is

cudaMemcpyToSymbol(d_s, &s, sizeof(S));

I was confused because the man page says

cudaError_t cudaMemcpyToSymbol (const void * symbol, const void * src,
   size_t count, size_t offset = 0, enum cudaMemcpyKind kind =
   cudaMemcpyHostToDevice)
   Copies count bytes from the memory area pointed to by src to the memory
   area pointed to by offset bytes from the start of symbol symbol. The
   memory areas may not overlap. symbol is a variable that resides in
   global or constant memory space. kind can be either
   cudaMemcpyHostToDevice or cudaMemcpyDeviceToDevice.

   Parameters:
       symbol - Device symbol address

Whatever they mean by "symbol" :) Copying to struct elements

    cudaMemcpyToSymbol(d_s.one, &(s.one), sizeof(S));

works just as well.

like image 127
Leo Avatar answered Dec 14 '25 04:12

Leo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!