2011-09-16 67 views
0

编译模板函数在CUDA我有以下CUDA代码:错误使用NVCC

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,我使用的工具包版本)建议模板应该可以工作,但我无法让他们去。

任何人都可以建议对此代码进行更改,使其编译(不删除模板!)吗?

+0

为什么声明'INT test_func ( )'包含模板参数? – talonmies

+0

嗯,我想有许多不同的方法来做不同的事情,并根据模板类型来选择它们。对于时间来说,只有一个简单的模板函数,但是一旦我能够编译它,我将为METH_0和METH_1增加更复杂的特化。 – user664303

回答

1

你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>>>(); 
} 
+0

Doh!感谢那。 – user664303

1

这是你想要的,还是我让你的问题错了?

enum METHOD_E { 
    METH_0 = 0, 
    METH_1 
}; 

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

template <> 
inline __device__ int test_func<METH_0>() 
{ 
    return -42; 
} 
+0

工作,谢谢。我接受了另一个答案,因为它稍微更具解释性。 – user664303