thrust::transform_output_iterator
Defined in thrust/iterator/transform_output_iterator.h
-
template<typename UnaryFunction, typename OutputIterator>
class transform_output_iterator : public detail::transform_output_iterator_base::type<UnaryFunction, OutputIterator> transform_output_iterator
is a special kind of output iterator which transforms a value written upon dereference. This iterator is useful for transforming an output from algorithms without explicitly storing the intermediate result in the memory and applying subsequent transformation, thereby avoiding wasting memory capacity and bandwidth. Usingtransform_iterator
facilitates kernel fusion by deferring execution of transformation until the value is written while saving both memory capacity and bandwidth.The following code snippet demonstrated how to create a
transform_output_iterator
which appliessqrtf
to the assigning value.#include <thrust/iterator/transform_output_iterator.h> #include <thrust/device_vector.h> // note: functor inherits form unary function // 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); using FloatIterator = thrust::device_vector<float>::iterator; thrust::transform_output_iterator<square_root, FloatIterator> iter(v.begin(), square_root()); iter[0] = 1.0f; // stores sqrtf( 1.0f) iter[1] = 4.0f; // stores sqrtf( 4.0f) iter[2] = 9.0f; // stores sqrtf( 9.0f) iter[3] = 16.0f; // stores sqrtf(16.0f) // iter[4] is an out-of-bounds error v[0]; // returns 1.0f; v[1]; // returns 2.0f; v[2]; // returns 3.0f; v[3]; // returns 4.0f; }
See also
make_transform_output_iterator