gpt4 book ai didi

cuda - 如何使用 Thrust 计算 int2 数组的平均值

转载 作者:行者123 更新时间:2023-12-02 22:36:08 25 4
gpt4 key购买 nike

我正在尝试计算包含点 (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;
}

关于cuda - 如何使用 Thrust 计算 int2 数组的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9364435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com