Template Function thrust::for_each_n(InputIterator, Size, UnaryFunction)¶
Function Documentation¶
-
template<typename
InputIterator
, typenameSize
, typenameUnaryFunction
>
InputIteratorthrust
::
for_each_n
(InputIterator first, Size n, UnaryFunction f) for_each_n
applies the function objectf
to each element in the range[first, first + n)
;f's
return value, if any, is ignored. Unlike the C++ Standard Template Library functionstd::for_each
, this version offers no guarantee on order of execution.The following code snippet demonstrates how to use
for_each_n
to print the elements of adevice_vector
.- Return
first + n
- Parameters
first
: The beginning of the sequence.n
: The size of the input sequence.f
: The function object to apply to the range[first, first + n)
.
- Template Parameters
InputIterator
: is a model of Input Iterator, andInputIterator's
value_type
is convertible toUnaryFunction's
argument_type
.Size
: is an integral type.UnaryFunction
: is a model of Unary Function, andUnaryFunction
does not apply any non-constant operation through its argument.
#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_n(d_vec.begin(), d_vec.size(), printf_functor()); // 0 1 2 is printed to standard output in some unspecified order
- See
for_each
- See