Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

c++ - CUDA 5.5 cudaMemcpyToSymbol, __constant__ and out of scope error

I'm trying to compile a CUDA example which has;

cuda.cu:

__constant__ unsigned VERTICES;
__constant__ unsigned TRIANGLES;

and the corresponding code in main.cpp;

cudaMemcpyToSymbol(VERTICES, &verticesNo, sizeof(int));
cudaMemcpyToSymbol(TRIANGLES, &trianglesNo, sizeof(int));

How to avoid "VERTICES not declared in this scope" error when compiling the main.cpp?

TIA.

cheers,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

CUDA __constant__ variables have a file scope linkage. That means that the cudaMemcpyToSymbol have to be in the same .cu file where the __constant__ variable is defined.

You can add a wrapper function to the .cu file and call this one from your .cpp file.

sample for cuda.cu:

__constant__ unsigned VERTICES;
__constant__ unsigned TRIANGLES;

void wrapper_fn(unsigned *verticesNo, unsigned *trianglesNo)
{
  cudaMemcpyToSymbol(VERTICES, verticesNo, sizeof(unsigned));
  cudaMemcpyToSymbol(TRIANGLES, trianglesNo, sizeof(unsigned));
}

Then only call wrapper_fn in your main.cpp.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...