thrust::transform_output_iterator#

template<typename UnaryFunction, typename OutputIterator>
class transform_output_iterator : public thrust::iterator_adaptor<transform_output_iterator<UnaryFunction, OutputIterator>, OutputIterator, use_default, use_default, use_default, transform_output_iterator_proxy<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. Using transform_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 applies sqrtf to the assigning value.

#include <thrust/iterator/transform_output_iterator.h>
#include <thrust/device_vector.h>

struct square_root
{
  __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;

}

Public Types

using base_type = Base#

The type of iterator this iterator_adaptor's adapts.

Public Functions

inline Base const &base() const#
Returns:

A const reference to the Base iterator this iterator_adaptor adapts.