使用 CUDA Thrust 查找最大元素值及其位置 [英] Finding the maximum element value AND its position using CUDA Thrust

查看:26
本文介绍了使用 CUDA Thrust 查找最大元素值及其位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何不仅获取值,还获取最大(最小)元素(res.valres.pos)的位置?

How do I get not only the value but also the position of the maximum (minimum) element (res.val and res.pos)?

thrust::host_vector<float> h_vec(100);
thrust::generate(h_vec.begin(), h_vec.end(), rand);
thrust::device_vector<float> d_vec = h_vec;

T res = -1;
res = thrust::reduce(d_vec.begin(), d_vec.end(), res, thrust::maximum<T>());

推荐答案

不要使用 thrust::reduce.在 thrust/extrema.h 中使用 thrust::max_element (thrust::min_element):

Don't use thrust::reduce. Use thrust::max_element (thrust::min_element) in thrust/extrema.h:

thrust::host_vector<float> h_vec(100);
thrust::generate(h_vec.begin(), h_vec.end(), rand);
thrust::device_vector<float> d_vec = h_vec;

thrust::device_vector<float>::iterator iter =
  thrust::max_element(d_vec.begin(), d_vec.end());

unsigned int position = iter - d_vec.begin();
float max_val = *iter;

std::cout << "The maximum value is " << max_val << " at position " << position << std::endl;

将空范围传递给 max_element 时要小心——您将无法安全地取消引用结果.

Be careful when passing an empty range to max_element -- you won't be able to safely dereference the result.

这篇关于使用 CUDA Thrust 查找最大元素值及其位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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