如何使用Thrust从int2数组计算平均值 [英] how to calculate an average from a int2 array using Thrust

查看:179
本文介绍了如何使用Thrust从int2数组计算平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图计算包含点(x,y)的某个数组的平均值。

可以使用推力来找到表示为(x,y)的平均点,点?当每个单元包含该点的绝对位置时,
i也可以将该数组表示为 thrust :: device_vector< int> ,意味着 i * numColumns + j ,但我不确定平均数字是否代表平均单元格。

谢谢!

I'm trying to calculate the average of a certain array which contains points (x,y).
is it possible to use thrust to find the average point represented as a (x,y) point? i could also represent the array as a thrust::device_vector<int>when each cell contains the absolute position of the point, meaning i*numColumns + j though I'm not sure that the average number represents the average cell.
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;
}

这篇关于如何使用Thrust从int2数组计算平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆