如何通过常量减少device_vector的每个元素?

iga*_*l k 6 cuda thrust

我试图用来thrust::transform减少a的每个元素的常量值device_vector.如您所见,最后一行不完整.我试图从所有元素减去常数,fLowestVal但不知道究竟是多少.

thrust::device_ptr<float> pWrapper(p);
thrust::device_vector<float> dVector(pWrapper, pWrapper + MAXX * MAXY);
float fLowestVal = *thrust::min_element(dVector.begin(), dVector.end(),thrust::minimum<float>());

// XXX What goes here?
thrust::transform(...);
Run Code Online (Sandbox Code Playgroud)

另一个问题:一旦我对其进行了更改device_vector,更改是否也适用于p阵列?

谢谢!

Jar*_*ock 6

您可以device_vector通过for_each与占位符表达式组合来减少a的每个元素的常量值:

#include <thrust/functional.h>
...
using thrust::placeholders;
thrust::for_each(vec.begin(), vec.end(), _1 -= val);
Run Code Online (Sandbox Code Playgroud)

不寻常的_1 -= val语法意味着创建一个未命名的仿函数,其作用是减少其第一个参数val. _1生活在命名空间中thrust::placeholders,我们可以通过using thrust::placeholders指令访问它.

您也可以通过组合for_eachtransform使用自己提供的自定义仿函数来完成此操作,但它更详细.