thrust::transform_iterator
Defined in thrust/iterator/transform_iterator.h
-
template<class AdaptableUnaryFunction, class Iterator, class Reference = use_default, class Value = use_default>
class transform_iterator : public detail::make_transform_iterator_base::type<AdaptableUnaryFunction, Iterator, use_default, use_default> 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. Usingtransform_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 ofsqrtf
applied to the contents of adevice_vector
.#include <thrust/iterator/transform_iterator.h> #include <thrust/device_vector.h> // note: functor inherits from unary_function struct square_root : public thrust::unary_function<float,float> { __host__ __device__ float operator()(float x) const { return sqrtf(x); } }; int main() { thrust::device_vector<float> v(4); v[0] = 1.0f; v[1] = 4.0f; v[2] = 9.0f; v[3] = 16.0f; using FloatIterator = thrust::device_vector<float>::iterator; thrust::transform_iterator<square_root, FloatIterator> iter(v.begin(), square_root()); *iter; // returns 1.0f iter[0]; // returns 1.0f; iter[1]; // returns 2.0f; iter[2]; // returns 3.0f; iter[3]; // returns 4.0f; // iter[4] is an out-of-bounds error }
This next example demonstrates how to use a
transform_iterator
with thethrust::reduce
function to compute the sum of squares of a sequence. We will create temporarytransform_iterators
with themake_transform_iterator
function in order to avoid explicitly specifying their type:#include <thrust/iterator/transform_iterator.h> #include <thrust/device_vector.h> #include <thrust/reduce.h> #include <iostream> // note: functor inherits from unary_function struct square : public thrust::unary_function<float,float> { __host__ __device__ float operator()(float x) const { return x * x; } }; int main() { // initialize a device array thrust::device_vector<float> v(4); v[0] = 1.0f; v[1] = 2.0f; v[2] = 3.0f; v[3] = 4.0f; float sum_of_squares = thrust::reduce(thrust::make_transform_iterator(v.begin(), square()), thrust::make_transform_iterator(v.end(), square())); std::cout << "sum of squares: " << sum_of_squares << std::endl; return 0; }
Note that in the previous two examples the transform functor (namely
square_root
andsquare
) inherits fromthrust::unary_function
. Inheriting fromthrust::unary_function
ensures that a functor is a validAdaptableUnaryFunction
and provides all the necessary nested alias. Thetransform_iterator
can also be applied to aUnaryFunction
that does not inherit fromthrust::unary_function
using an optional template argument. The following example illustrates how to use the third template argument to specify theresult_type
of the function.#include <thrust/iterator/transform_iterator.h> #include <thrust/device_vector.h> // note: functor *does not* inherit from unary_function struct square_root { __host__ __device__ float operator()(float x) const { return sqrtf(x); } }; int main() { thrust::device_vector<float> v(4); v[0] = 1.0f; v[1] = 4.0f; v[2] = 9.0f; v[3] = 16.0f; using FloatIterator = thrust::device_vector<float>::iterator; // note: float result_type is specified explicitly thrust::transform_iterator<square_root, FloatIterator, float> iter(v.begin(), square_root()); *iter; // returns 1.0f iter[0]; // returns 1.0f; iter[1]; // returns 2.0f; iter[2]; // returns 3.0f; iter[3]; // returns 4.0f; // iter[4] is an out-of-bounds error }
See also
make_transform_iterator
Public Functions
-
transform_iterator() = default
Null constructor does nothing.
-
transform_iterator(transform_iterator const&) = default
-
inline transform_iterator(Iterator const &x, AdaptableUnaryFunction f)
This constructor takes as arguments an
Iterator
and anAdaptableUnaryFunction
and copies them to a newtransform_iterator
.- Parameters
x – An
Iterator
pointing to the input to thistransform_iterator's
AdaptableUnaryFunction
.f – An
AdaptableUnaryFunction
used to transform the objects pointed to byx
.
-
inline explicit transform_iterator(Iterator const &x)
This explicit constructor copies the value of a given
Iterator
and creates thistransform_iterator's
AdaptableUnaryFunction
using its null constructor.- Parameters
x – An
Iterator
to copy.
-
template<typename OtherAdaptableUnaryFunction, typename OtherIterator, typename OtherReference, typename OtherValue>
inline transform_iterator(const transform_iterator<OtherAdaptableUnaryFunction, OtherIterator, OtherReference, OtherValue> &other, thrust::detail::enable_if_convertible_t<OtherIterator, Iterator>* = 0, thrust::detail::enable_if_convertible_t<OtherAdaptableUnaryFunction, AdaptableUnaryFunction>* = 0) This copy constructor creates a new
transform_iterator
from anothertransform_iterator
.- Parameters
other – The
transform_iterator
to copy.
-
inline transform_iterator &operator=(const transform_iterator &other)
Copy assignment operator copies from another
transform_iterator
.other
The othertransform_iterator
to copy
In any case, this
transform_iterator's
underlying iterator will be copy assigned.Note
If the type of this
transform_iterator's
functor is not copy assignable (for example, if it is a lambda) it is not an error to call this function. In this case, however, the functor will not be modified.- Returns
*this
-
inline AdaptableUnaryFunction functor() const
This method returns a copy of this
transform_iterator's
AdaptableUnaryFunction
.- Returns
A copy of this
transform_iterator's
AdaptableUnaryFunction
.
-
transform_iterator() = default