我正在尝试计算包含点 (x,y) 的某个数组的平均值。
是否可以使用推力找到表示为 (x,y) 点的平均点?
我也可以将数组表示为thrust::device_vector<int>
当每个单元格包含点的绝对位置时,意味着i*numColumns + j
虽然我不确定平均数是否代表平均单元格。
Thanks!
#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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)