thrust::for_each#
Overloads#
for_each(exec, first, last, f)#
-
template<typename DerivedPolicy, typename InputIterator, typename UnaryFunction>
InputIterator thrust::for_each( - const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
- InputIterator first,
- InputIterator last,
- UnaryFunction f,
for_eachapplies the function objectfto each element in the range[first, last);f'sreturn value, if any, is ignored. Unlike the C++ Standard Template Library functionstd::for_each, this version offers no guarantee on order of execution. For this reason, this version offor_eachdoes not return a copy of the function object.The algorithm’s execution is parallelized as determined by
exec.The following code snippet demonstrates how to use
for_eachto print the elements of athrust::device_vectorusing thethrust::deviceparallelization policy:#include <thrust/for_each.h> #include <thrust/device_vector.h> #include <thrust/execution_policy.h> #include <cstdio> ... struct printf_functor { __host__ __device__ void operator()(int x) { // note that using printf in a __device__ function requires // code compiled for a GPU with compute capability 2.0 or // higher (nvcc --arch=sm_20) printf("%d\n", x); } }; ... thrust::device_vector<int> d_vec(3); d_vec[0] = 0; d_vec[1] = 1; d_vec[2] = 2; thrust::for_each(thrust::device, d_vec.begin(), d_vec.end(), printf_functor()); // 0 1 2 is printed to standard output in some unspecified order
See also
- Parameters:
exec – The execution policy to use for parallelization.
first – The beginning of the sequence.
last – The end of the sequence.
f – The function object to apply to the range
[first, last).
- Template Parameters:
DerivedPolicy – The name of the derived execution policy.
InputIterator – is a model of Input Iterator, and
InputIterator'svalue_typeis convertible toUnaryFunction'sargument type.UnaryFunction – does not apply any non-constant operation through its argument.
- Returns:
last
for_each(first, last, f)#
-
template<typename InputIterator, typename UnaryFunction>
InputIterator thrust::for_each( - InputIterator first,
- InputIterator last,
- UnaryFunction f,
for_eachapplies the function objectfto each element in the range[first, last);f'sreturn value, if any, is ignored. Unlike the C++ Standard Template Library functionstd::for_each, this version offers no guarantee on order of execution. For this reason, this version offor_eachdoes not return a copy of the function object.The following code snippet demonstrates how to use
for_eachto print the elements of adevice_vector.#include <thrust/for_each.h> #include <thrust/device_vector.h> #include <stdio.h> struct printf_functor { __host__ __device__ void operator()(int x) { // note that using printf in a __device__ function requires // code compiled for a GPU with compute capability 2.0 or // higher (nvcc --arch=sm_20) printf("%d\n", x); } }; ... thrust::device_vector<int> d_vec(3); d_vec[0] = 0; d_vec[1] = 1; d_vec[2] = 2; thrust::for_each(d_vec.begin(), d_vec.end(), printf_functor()); // 0 1 2 is printed to standard output in some unspecified order
See also
- Parameters:
first – The beginning of the sequence.
last – The end of the sequence.
f – The function object to apply to the range
[first, last).
- Template Parameters:
InputIterator – is a model of Input Iterator, and
InputIterator'svalue_typeis convertible toUnaryFunction'sargument type.UnaryFunction – does not apply any non-constant operation through its argument.
- Returns:
last