gpt4 book ai didi

templates - 使用nvcc在CUDA中编译模板函数时出错

转载 作者:行者123 更新时间:2023-12-02 10:49:59 36 4
gpt4 key购买 nike

我有以下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,我正在使用的工具包的版本)建议模板可以工作,但我无法使用它们。

谁能建议对此代码进行更改以使其编译(无需删除模板!)?

最佳答案

您的test_func定义是错误的:

test_func()应该只是test_func()

这对我有用:

enum METHOD_E {
METH_0 = 0,
METH_1
};

template < enum METHOD_E METH>
__device__
inline
int test_func ()
{
return int(METH);
}

__global__ void test_kernel()
{
test_func<METH_0>();
}

void test()
{
test_kernel<<<1, 1>>>();
}

关于templates - 使用nvcc在CUDA中编译模板函数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7446536/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com