How to reduce each element of device_vector constant?

I am trying to use thrust::transform to reduce a constant value from each device_vector element. As you can see, the last line is incomplete. I'm trying to reduce fLowestVal constant from all elements, but I don’t know how.

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

Another question: as soon as I make my changes to device_vector , will the changes also apply to the p array?

Thanks!

+4
source share
1 answer

You can reduce the constant value from each device_vector element by combining for_each with a placeholder expression:

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

The unusual syntax _1 -= val means creating an unnamed functor whose task is to reduce its first argument by val . _1 lives in the thrust::placeholders namespace, which we have access to through the using thrust::placeholders directive.

You can also do this by combining for_each or transform with a special functor that you have provided for yourself, but it is more verbose.

+5
source

Source: https://habr.com/ru/post/1401030/


All Articles