不能在 Cuda 中使用常量
Can't use constants in Cuda
示例:
#include <cuda.h>
#include <stdint.h>
#include <assert.h>
__constant__ int32_t m;
int main(int argc, char* argv[])
{
void* s;
int r = cudaGetSymbolAddress( &s, m);
assert( r == cudaSuccess );
return 0;
}
编译:
$ nvcc test.cu -o test -arch compute_20 -code sm_20
运行:
$ ./test
获取:
test: test.cu:15: int main(int, char**): Assertion `r == cudaSuccess' failed.
Aborted (core dumped)
(如果这有什么不同的话,我在两台不同的计算机上的两张不同的卡上测试了这一点。两种情况下都是Cuda 6。(
怎么了?
正如@sgar91所指出的,问题是编译目标与实际GPU不匹配。
具体来说:您的选项中有-code sm_20
,这将使编译器为sm_20构建二进制文件,并且二进制文件中没有PTX,这意味着它不能为您的设备进行JIT编译(计算能力>2.0(,因此您的GPU操作将失败。您应该有-code compute_20
或一个或多个-gencode
参数(有关更多示例,请参阅nvcc手册(。
一些例子:
$ nvcc test.cu -o test -arch compute_20 -code compute_20
$ nvcc test.cu -o test -gencode="arch=compute_20,code="compute_20,sm_20,sm_30""
$ nvcc test.cu -o test -gencode="arch=compute_20,code="sm_20,sm_21"" -gencode="arch=compute_30,code="compute_30,sm_30""
您不应该对CUDAneneneba API调用进行断言,而应该报告实际错误,因为这在这里会有所帮助。
相关文章:
- #定义c-预处理器常量..我做错了什么
- 用C++中的一个变量定义一个常量
- 编译时未启用intel oneApi CUDA支持
- 什么时候在C++中返回常量引用是个好主意
- 代理对象的常量正确性
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 通过多个头文件使用常量变量
- 在cuda线程之间共享大量常量数据
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 如何在CUDA中使用静态常量成员
- 如何在任何文件中包含带有cuda常量的头文件
- CUDA 分配模板函数中从字符串常量到 'char *' 的已弃用转换
- 在 cuda 中定义模板化常量变量
- Cuda:将设备常量声明为模板
- 如何定义CUDA设备常量,如C++常量/常量表达式
- 将常量参数传递给CUDA内核的最快(或最优雅)的方式
- 不能在 Cuda 中使用常量
- 模板化代码的CUDA中的静态常量int