gpt4 book ai didi

cuda - 如何将 device_vector 的每个元素递减一个常数?

转载 作者:行者123 更新时间:2023-12-04 15:02:35 26 4
gpt4 key购买 nike

我正在尝试使用 thrust::transformdevice_vector 的每个元素中递减一个常数值.如您所见,最后一行是不完整的。我正在尝试从所有元素中减少常量 fLowestVal但不知 Prop 体如何。

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(...);

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

谢谢!

最佳答案

您可以从 device_vector 的每个元素中减少一个常数值。通过结合 for_each使用占位符表达式:

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

不寻常的 _1 -= val语法意味着创建一个未命名的仿函数,其工作是将其第一个参数减少 val。 . _1位于命名空间 thrust::placeholders ,我们可以通过 using thrust::placeholders 访问指示。

您也可以通过组合 for_each 来做到这一点。或 transform使用您自己提供的自定义仿函数,但它更冗长。

关于cuda - 如何将 device_vector 的每个元素递减一个常数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9671104/

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