Cuda推力定制功能 [英] Cuda Thrust Custom function
本文介绍了Cuda推力定制功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Thrust中实现此功能?
How can I impliment this function in Thrust?
for (i=0;i<n;i++)
if (i==pos)
h1[i]=1/h1[i];
else
h1[i]=-h1[i]/value;
在CUDA中,我喜欢:
In CUDA I did it like:
__global__ void inverse_1(double* h1, double value, int pos, int N)
{
int i = blockDim.x * blockIdx.x + threadIdx.x;
if (i < N){
if (i == pos)
h1[i] = 1 / h1[i];
else
h1[i] = -h1[i] / value;
}
}
谢谢!
推荐答案
您需要创建一个二进制函子来应用操作,然后使用计数迭代器作为第二个输入。你可以将 pos
和 value
传递给函子的构造函数。它看起来像:
You need to create a binary functor to apply the operation, then use a counting iterator as the second input. You can pass pos
and value
into the functor's constructor. It'd look something like:
struct inv1_functor
{
const int pos;
const double value;
inv1_functor(double _value, int _pos) : value(_value), pos(_pos) {}
__host__ __device__
double operator()(const double &x, const int &i) const {
if (i == pos)
return 1.0/x;
else
return -x/value;
}
};
//...
thrust::transform(d_vec.begin(), d_vec.end(), thrust::counting_iterator<int>(), d_vec.begin(), inv1_functor(value, pos));
这篇关于Cuda推力定制功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文