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.
source share