我有一个模板化的包装函数,它调用__global__
像这样的 .cu 文件中定义的内核 ( )
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);
// ...
};
Run Code Online (Sandbox Code Playgroud)
和一个包含在主机代码中的头文件,它只有声明
template<typename T, class M>
void compute(T* input, T* output, int n);
Run Code Online (Sandbox Code Playgroud)
但是,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);
Run Code Online (Sandbox Code Playgroud)
那么,是否有必要专门化每个模板化函数以使其可从主机调用?(这是一个相当大的缺点)
感谢您的意见!
归档时间: |
|
查看次数: |
3809 次 |
最近记录: |