Reputation: 1934
I'm trying to use thrust::transform
to decrement a constant value from each element of a device_vector
. As you can see, the last line is incomplete. I'm trying to decrement from all elements the constant fLowestVal
but dont know how exactly.
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: Once I do my changes on the device_vector
, will the changes apply also to the p
array?
Thanks!
Upvotes: 6
Views: 1544
Reputation: 1532
One option which could be useful in a wide range of contexts is to use one of Thrust's fancy iterators as an argument of thrust::transform
. Here, you would do so along with the thrust::minus
binary function object, like this:
#include <thrust/transform.h>
#include <thrust/iterator/constant_iterator.h>
#include <thrust/functional.h>
...
thrust::transform(vec.begin(),
vec.end(),
thrust::make_constant_iterator(value),
vec.begin(),
thrust::minus<float>());
The documentation for fancy iterators can be found here.
Upvotes: 0
Reputation: 11416
You can decrement a constant value from each element of a device_vector
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 _1 -= val
syntax means to create an unnamed functor whose job is to decrement its first argument by val
. _1
lives in the namespace thrust::placeholders
, which we have access to via the using thrust::placeholders
directive.
You could also do this by combining for_each
or transform
with a custom functor you provided yourself, but it's more verbose.
Upvotes: 7