How to decrement each element of a device_vector by a constant?

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.

Tags:

Cuda

Thrust