iga*_*l k 5 cuda average thrust
我正在尝试计算包含点(x,y)的某个数组的平均值.
是否有可能使用推力来找到表示为(x,y)点的平均点?我也可以将数组表示为thrust::device_vector<int>每个单元格包含点的绝对位置的时间,这意味着i*numColumns + j虽然我不确定平均数字代表平均单元格.
谢谢!
#include <iostream>
#include <thrust/device_vector.h>
#include <thrust/reduce.h>
struct add_int2 {
__device__
int2 operator()(const int2& a, const int2& b) const {
int2 r;
r.x = a.x + b.x;
r.y = a.y + b.y;
return r;
}
};
#define N 20
int main()
{
thrust::host_vector<int2> a(N);
for (unsigned i=0; i<N; ++i) {
a[i].x = i;
a[i].y = i+1;
}
thrust::device_vector<int2> b = a;
int2 init;
init.x = init.y = 0;
int2 ave = thrust::reduce(b.begin(), b.end(), init, add_int2());
ave.x /= N;
ave.y /= N;
std::cout << ave.x << " " << ave.y << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Keveman的回答是正确的,我只想添加一个需要代码的有用提示,所以我会把它放在这里,而不是在评论中.
Thrust 1.5增加了lambda占位符,这可以使@keveman的方法更简单.而不是一个仿函数,只需定义operator+for int2,然后用_1 + _2lambda占位符表达式替换仿函数的实例化.您还可以init通过调用make_int2()(由CUDA提供)替换显式声明.注意:int2 operator+在CUDA代码示例SDK的"vector_math.h"标题中定义,但我在下面定义它以使其清楚(因为该文件不是CUDA的标准部分).
#include <iostream>
#include <thrust/device_vector.h>
#include <thrust/reduce.h>
using namespace thrust::placeholders;
__device__
int2 operator+(const int2& a, const int2& b) {
return make_int2(a.x+b.x, a.y+b.y);
}
#define N 20
int main()
{
thrust::host_vector<int2> a(N);
for (unsigned i=0; i<N; ++i) {
a[i].x = i;
a[i].y = i+1;
}
thrust::device_vector<int2> b = a;
int2 ave = thrust::reduce(b.begin(), b.end(), make_int2(0, 0), _1 + _2);
ave.x /= N;
ave.y /= N;
std::cout << ave.x << " " << ave.y << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)