thrust
|
#include <thrust/iterator/transform_iterator.h>
Inherits type< AdaptableUnaryFunction, Iterator, Reference, Value >.
Public Member Functions | |
__host__ __device__ | transform_iterator () |
__host__ __device__ | transform_iterator (Iterator const &x, AdaptableUnaryFunction f) |
__host__ __device__ | transform_iterator (Iterator const &x) |
template<typename OtherAdaptableUnaryFunction , typename OtherIterator , typename OtherReference , typename OtherValue > | |
__host__ __device__ | transform_iterator (const transform_iterator< OtherAdaptableUnaryFunction, OtherIterator, OtherReference, OtherValue > &other, typename thrust::detail::enable_if_convertible< OtherIterator, Iterator >::type *=0, typename thrust::detail::enable_if_convertible< OtherAdaptableUnaryFunction, AdaptableUnaryFunction >::type *=0) |
__host__ __device__ transform_iterator & | operator= (const transform_iterator &other) |
__host__ __device__ AdaptableUnaryFunction | functor () const |
transform_iterator
is an iterator which represents a pointer into a range of values after transformation by a function. This iterator is useful for creating a range filled with the result of applying an operation to another range without either explicitly storing it in memory, or explicitly executing the transformation. Using transform_iterator
facilitates kernel fusion by deferring the execution of a transformation until the value is needed while saving both memory capacity and bandwidth.
The following code snippet demonstrates how to create a transform_iterator
which represents the result of sqrtf
applied to the contents of a device_vector
.
This next example demonstrates how to use a transform_iterator
with the thrust::reduce
function to compute the sum of squares of a sequence. We will create temporary transform_iterators
with the make_transform_iterator
function in order to avoid explicitly specifying their type:
Note that in the previous two examples the transform functor (namely square_root
and square
) inherits from thrust::unary_function
. Inheriting from thrust::unary_function
ensures that a functor is a valid AdaptableUnaryFunction
and provides all the necessary typedef
declarations. The transform_iterator
can also be applied to a UnaryFunction
that does not inherit from thrust::unary_function
using an optional template argument. The following example illustrates how to use the third template argument to specify the result_type
of the function.