Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...