2011-09-16 66 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