cudaMemcpyToSymbol using or not using string

前端 未结 2 827
离开以前
离开以前 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 <stdio.h>
    #include <stdlib.h>
    #include <cuda.h>
    
    __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.

    0 讨论(0)
  • 2021-01-06 06:58

    As of CUDA 5, using a string for symbol names is no longer supported. This is covered in the CUDA 5 release notes here

    •The use of a character string to indicate a device symbol, which was possible with certain API functions, is no longer supported. Instead, the symbol should be used directly.

    One of the reasons for this has to do with enabling of a true device linker, which is new functionality in CUDA 5.

    0 讨论(0)
提交回复
热议问题