【发布时间】:2016-03-08 02:21:00
【问题描述】:
在 CUDA C 项目中,我想尝试使用 Thrust 库来查找浮点数组中的最大元素。似乎推力函数推力::max_element() 是我需要的。我想在其上使用此函数的数组是 cuda 内核的结果(似乎工作正常),因此在调用推力::max_element() 时它已经存在于设备内存中。 我对 Thrust 库不是很熟悉,但是在查看了thrust::max_element() 的文档并阅读了该站点上类似问题的答案后,我想我已经掌握了这个过程的工作原理。不幸的是,我得到了错误的结果,而且似乎我没有正确使用库函数。有人可以告诉我我的代码有什么问题吗?
float* deviceArray;
float* max;
int length = 1025;
*max = 0.0f;
size = (int) length*sizeof(float);
cudaMalloc(&deviceArray, size);
cudaMemset(deviceArray, 0.0f, size);
// here I launch a cuda kernel which modifies deviceArray
thrust::device_ptr<float> d_ptr = thrust::device_pointer_cast(deviceArray);
*max = *(thrust::max_element(d_ptr, d_ptr + length));
我使用以下标题:
#include <thrust/extrema.h>
#include <thrust/device_ptr.h>
即使我确信在运行内核后 deviceArray 包含非零值,我仍不断得到 *max 的零值。 我使用 nvcc 作为编译器 (CUDA 7.0),并且在计算能力为 3.5 的设备上运行代码。
任何帮助将不胜感激。谢谢。
【问题讨论】: