【发布时间】:2013-12-05 08:36:34
【问题描述】:
我已经坚持了一段时间了。当我通过内核参数将我的结构传递给 CUDA 时,它们不包含任何数据,并且它们内部的一切都是未定义的。
在主机全局空间中
struct matl1
{
static const double cond;
};
const double matl1::cond = 420.5;
然后在 main() 内部
matl1 * h_matl1 = (matl1*)malloc(sizeof(matl1));
matl1 * d_matl1;
cudaMalloc((void**)&d_matl1, sizeof(matl1));
cudaMemcpy(d_matl1, h_matl1, sizeof(matl1), cudaMemcpyHostToDevice);
kernel<<<1,1>>>(d_matl1,...);
然后在 kernel() 内部
__global__ void kernel(matl1* d_matl1,...)
{
double cond = d_matl1->cond;
}
我收到以下错误:
error : identifier "matl1::cond" is undefined in device code
作为一个快速测试,如果我在 main() 中的主机上执行以下操作
cout << h_matl1->cond << endl;
它显示了 420.5 的正确输出。我不确定为什么它没有进入设备。
这是我剩下的输出
uild 开始:项目:test_struct,配置:Debug Win32 ------
正在编译CUDA源文件kernel.cu...
C:\Users\User\Documents\Visual Studio 2012\Projects\test_struct\test_struct>"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\bin\nvcc.exe" -gencode=arch =compute_35,code=\"sm_35,compute_35\" --use-local-env --cl-version 2012 -ccbin "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin" -I"C :\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\include" -G --keep-dir Debug -maxrregcount =0 --machine 32 --compile -cudart static -g -DWIN32 -D_DEBUG -D_CONSOLE -D_MBCS -Xcompiler "/EHsc /W3 /nologo /Od /Zi /RTC1 /MDd " -o Debug\kernel.cu.obj " C:\Users\User\Documents\Visual Studio 2012\Projects\test_struct\test_struct\kernel.cu" 1>C:/Users/User/Documents/Visual Studio 2012/Projects/test_struct/test_struct/kernel.cu(15): 错误:设备代码中未定义标识符“matl1::cond”
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V110\BuildCustomizations\CUDA 5.5.targets(592,9): error MSB3721: The command ""C:\Program Files\NVIDIA GPU 计算工具包\CUDA\v5.5\bin\nvcc.exe" -gencode=arch=compute_35,code=\"sm_35,compute_35\" --use-local-env --cl-version 2012 -ccbin "C: \Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\include" -G --keep-dir Debug -maxrregcount=0 --machine 32 --compile -cudart static -g -DWIN32 -D_DEBUG -D_CONSOLE -D_MBCS -Xcompiler "/EHsc /W3 / nologo /Od /Zi /RTC1 /MDd " -o Debug\kernel.cu.obj "C:\Users\User\Documents\Visual Studio 2012\Projects\test_struct\test_struct\kernel.cu"" 退出,代码为 2。
========== 构建:0 成功,1 失败,0 最新,0 跳过 ==========
【问题讨论】:
-
它看起来对我有用。请参阅here 我正在使用 CUDA 5.5 在 linux 上执行此操作。就像我一样,您需要提供更多细节和一个实际完整的示例,这样我们才能看到编译器的所有代码和输出。识别平台(linux、windows等)、哪个版本的CUDA,并提供完整的代码、编译命令的完整输出,以及完整的编译命令。