使用 nvcc 在 CUDA 中编译模板函数时出错
我有以下 CUDA 代码:
enum METHOD_E {
METH_0 = 0,
METH_1
};
template <enum METHOD_E METH>
inline __device__ int test_func<METH>()
{
return int(METH);
}
__global__ void test_kernel()
{
test_func<METH_0>();
}
void test()
{
test_kernel<<<1, 1>>>();
}
编译时出现以下错误:
>nvcc --cuda test.cu
test.cu
test.cu(7): error: test_func is not a template
test.cu(14): error: identifier "test_func" is undefined
test.cu(14): error: expected an expression
3 errors detected in the compilation of "C:/Users/BLAH45~1/AppData/Local/Temp/tm
pxft_00000b60_00000000-6_test.cpp1.ii".
《编程指南》的 D.1.4 节(4.0,我正在使用的工具包版本)建议模板应该可以工作,但我无法让它们。
任何人都可以建议对此代码进行更改以使其编译(不删除模板!)?
I have the following CUDA code:
enum METHOD_E {
METH_0 = 0,
METH_1
};
template <enum METHOD_E METH>
inline __device__ int test_func<METH>()
{
return int(METH);
}
__global__ void test_kernel()
{
test_func<METH_0>();
}
void test()
{
test_kernel<<<1, 1>>>();
}
When I compile I get the following error:
>nvcc --cuda test.cu
test.cu
test.cu(7): error: test_func is not a template
test.cu(14): error: identifier "test_func" is undefined
test.cu(14): error: expected an expression
3 errors detected in the compilation of "C:/Users/BLAH45~1/AppData/Local/Temp/tm
pxft_00000b60_00000000-6_test.cpp1.ii".
Section D.1.4 of the Programming Guide (4.0, the version of the toolkit I'm using) suggests templates should work, but I can't get them to.
Can anyone suggest a change to this code which makes it compile (without removing the templating!)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
test_func
定义是错误的:test_func()
应该简单地test_func()
。这对我有用:
Your
test_func
definition is wrong:test_func<METH>()
should be simplytest_func()
.This works for me:
这是你想要的,还是我弄错了你的问题?
Is this what you want, or did I get your problem wrong?