这是一个最小化的示例,使用 Cuda 编译工具版本 6.5.12 编译,在 sync_and_check("cudaMemcpyToSymbol")
处抛出了 cudaInvalidSymbol
异常:
#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");
}