我有一个调用在一个.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编译器不会自动分离这两个代码路径吗?所以,只需使用函数模板的模板定义创建一个普通的头文件,并将其包含到任何地方 - 阻塞是什么? –