简体   繁体   English

如何使用模板函数和CUDA

[英]How to use template functions and CUDA

So I have the following code: 所以我有以下代码:

File: Cuda.cu 档案:Cuda.cu

template <typename T>
__global__ void xpy( int n, T *x, T *y, T *r )
{
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n) r[i] = x[i] + y[i];
}

mtx_mtx_add( float *a1, float *a2, float *r, const int &numElements )
{
// snip
xpy<<<numBlocks, blockSize>>>(numElements, a1, a2, r); 
}
mtx_mtx_add( int *a1, int *a2, int *r, const int &numElements ) {:::}
mtx_mtx_add( long long *a1, long long *a2, long long *r, const int &numElements ) {:::}

File: Calling Code 文件:调用代码

extern "C" bool mtx_mtx_add( float *a1, float *a2, float *r, int &numElements );
extern "C" bool mtx_mtx_add( float *a1, float *a2, float *r, int &numElements );
extern "C" bool mtx_mtx_add( float *a1, float *a2, float *r, int &numElements );

int main()
{
... ...
mtx_mtx_add(...);
}

Now what I want is for the mtx_mtx_add function to be templated. 现在我想要的是mtx_mtx_add函数是模板化的。 Is this possible and if so how? 这有可能吗?如果可以的话怎么样?

Programming in CUDA is basically C++. CUDA中的编程基本上是C ++。 You can use all the features of the C++ language as you would use in a standard C++ program. 您可以像在标准C ++程序中一样使用C ++语言的所有功能。

You can create the function template as follows: 您可以按如下方式创建功能模板:

template<typename T>
bool mtx_mtx_add(T *a1, T *a2, T *r, const int &numElements)
{
   xpy<T><<<numBlocks, blockSize>>>(numElements, a1, a2, r);
}

Then you can specialize the function template for different data-types as: 然后,您可以将不同数据类型的函数模板专门化为:

template bool mtx_mtx_add<float>(float* a1, float* a2, float* r, const int& numElements);
template bool mtx_mtx_add<int>(int* a1, int* a2, int* r, const int& numElements);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM