CUDA 中全局变量的字符串查找错误?
我有类似的东西:
__constant__ double PNT[ NUMCOORDS ];
__device__ double PNT[ NUMCOORDS ];
取决于某些预处理器选择。然后我使用这个变量:
cudaMemcpyToSymbol("PNT", point, pntSize)
但是,有时(我真的不能说何时真正让我困惑)我得到了错误信息:
按字符串名称查找重复的全局变量
检查 CUDA 错误时 。我尝试用 PNT
替换 "PNT"
,奇怪的是,这有效:
cudaMemcpyToSymbol(PNT, point, pntSize)
我在实践中使用这个解决方案(而不是使用字符串“PNT”
)?
I have something like either:
__constant__ double PNT[ NUMCOORDS ];
__device__ double PNT[ NUMCOORDS ];
depending upon some preprocessor selections. I then use this variable:
cudaMemcpyToSymbol("PNT", point, pntSize)
However, sometimes (and I really CAN'T say when which really confuses me) I get the error message:
duplicate global variable looked up by string name
when checking for CUDA errors. I tried replacing "PNT"
with PNT
and strangely, this works:
cudaMemcpyToSymbol(PNT, point, pntSize)
Shound I use this solution in practice (instead of using a string "PNT"
)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根本问题与 cudaMemcpyToSymbol 无关。您看到的错误是由 CUDA 运行时搜索您提供的符号时生成的,因为它是在代码运行的上下文中多重定义的。 CUDA 运行时版本在检测重复定义(例如 __constant__ 声明、纹理、__device__ 函数等)方面已经变得越来越好。
解决方案是重构代码,以便符号在应用程序中仅定义一次。由于 CUDA 没有链接器,因此如果在两个文件中定义符号,则不会出现编译时错误。但是,当 CUDA 运行时将最终链接的应用程序中生成的二进制有效负载加载到上下文中时,可能会发生重复符号冲突,并可能导致运行时错误。
The underlying problem has nothing to do with
cudaMemcpyToSymbol
. The error you are seeing is generated by the CUDA runtime when it searches for the symbol you have supplied because it is multiply defined in the context in which your code is running. CUDA runtime versions have been getting progressively better at detecting duplicate definitions (things like__constant__
declarations, textures,__device__
functions).The solution is to refactor your code so that symbols are only ever defined once within an application. Because CUDA doesn't have a linker, there will be no compilation time errors if you define a symbol in two files. But when the CUDA runtime loads the resulting binary payloads from the final linked application into a context, duplicate symbol conflicts can occur and runtime errors can result.