我有一個調用在一個.CU文件中定義這樣CUDA和模板:需要專業化聲明?
template<typename T, class M>
__global__ void compute_kernel(T* input, T* output, n) {
M m;
// compute stuff using m
};
template<typename T, class M>
void compute(T* input, T* output, int n) {
// ... compute blocks, threads, etc.
compute_kernel<T,M> <<<dim_grid, dim_block>>>(input, output, n);
// ...
};
和一個頭文件是內核(__global__
)將被包括在主機代碼模板化包裝函數僅具有聲明
template<typename T, class M>
void compute(T* input, T* output, int n);
但是,從具有任意模板參數的主機調用compute()
時,編譯失敗,並且undefined reference to 'void reduce(...)'
並且僅當我在代碼編譯時向.cu
文件的末尾添加專門化聲明:
template void
compute<int, Method1<int> >(int* input, int* output, int n);
template void
compute<float, Method1<float> >(float* input, float* output, int n);
template void
compute<int, Method2<int> >(int* input, int* output, int n);
template void
compute<float, Method2<float> >(float* input, float* output, int n);
那麼,是否有必要專門化每個模板化的函數以使其可以從主機調用? (這是一個很大的缺點)
感謝您的意見!
與任何模板一樣,您必須確保整個模板定義在每個*實例化站點都可見(除非您故意提供明確的實例化)。 –
@KerrekSB:我認爲你是對的,這是問題所在。但是,要分離主機和設備代碼(以及編譯),似乎無法將整個模板定義無處不在... – bbtrb
我不確定問題出在哪裏。 CUDA編譯器不會自動分離這兩個代碼路徑嗎?所以,只需使用函數模板的模板定義創建一個普通的頭文件,並將其包含到任何地方 - 阻塞是什麼? –