不能在 Cuda 中使用常量

Can't use constants in Cuda

本文关键字:常量 Cuda 不能      更新时间:2023-10-16

示例:

#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调用进行断言,而应该报告实际错误,因为这在这里会有所帮助。