cudaMemcpyToSymbol using or not using string

前端 未结 2 828
离开以前
离开以前 2021-01-06 06:22

I was trying to copy a structure to constant memory in this way:

struct Foo {
    int a, b, c;
};

__constant__ Foo cData;

int main() {
    Foo hData = {1,          


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-06 06:50

    Because of getting the same error again and again, I want to share this sample code that shows nearly all of the example cases for this problem (so I may refer here later when I make same mistakes again).

    //file: main.cu
    #include 
    #include 
    #include 
    
    __constant__ float constData[256];
    __device__ float devData;
    __device__ float* devPointer;
    
    int main(int argc, char **argv)
    {
      cudaFree(0);
    
      float data[256];
      cudaError_t err = cudaMemcpyToSymbol(constData, data, sizeof(data));
      printf("Err id: %d, str: %s\n", err, cudaGetErrorString(err));
    
      float value = 3.14f;
      err = cudaMemcpyToSymbol(devData, &value, sizeof(float));
      printf("Err id: %d, str: %s\n", err, cudaGetErrorString(err));
    
      float* ptr;
      cudaMalloc(&ptr, 256 * sizeof(float));
      err = cudaMemcpyToSymbol(devPointer, &ptr, sizeof(ptr));
      printf("Err id: %d, str: %s\n", err, cudaGetErrorString(err));
      cudaFree(ptr);
    
      return EXIT_SUCCESS;
    }
    

    I was getting "invalid device symbol" and many others which are related to _constant_ _device_ memory usage. This code gives no such errors at runtime.

提交回复
热议问题