thrust
Public Member Functions | List of all members
thrust::zip_iterator< IteratorTuple > Class Template Reference

#include <thrust/iterator/zip_iterator.h>

Inherits type< IteratorTuple >.

Public Member Functions

__host__ __device__ zip_iterator ()
 
__host__ __device__ zip_iterator (IteratorTuple iterator_tuple)
 
template<typename OtherIteratorTuple >
__host__ __device__ zip_iterator (const zip_iterator< OtherIteratorTuple > &other, typename thrust::detail::enable_if_convertible< OtherIteratorTuple, IteratorTuple >::type *=0)
 
__host__ __device__ const IteratorTuple & get_iterator_tuple () const
 

Detailed Description

template<typename IteratorTuple>
class thrust::zip_iterator< IteratorTuple >

zip_iterator is an iterator which represents a pointer into a range of tuples whose elements are themselves taken from a tuple of input iterators. This iterator is useful for creating a virtual array of structures while achieving the same performance and bandwidth as the structure of arrays idiom. zip_iterator also facilitates kernel fusion by providing a convenient means of amortizing the execution of the same operation over multiple ranges.

The following code snippet demonstrates how to create a zip_iterator which represents the result of "zipping" multiple ranges together.

#include <thrust/tuple.h>
...
thrust::device_vector<int> int_v(3);
int_v[0] = 0; int_v[1] = 1; int_v[2] = 2;
float_v[0] = 0.0f; float_v[1] = 1.0f; float_v[2] = 2.0f;
char_v[0] = 'a'; char_v[1] = 'b'; char_v[2] = 'c';
// typedef these iterators for shorthand
// typedef a tuple of these iterators
// typedef the zip_iterator of this tuple
// finally, create the zip_iterator
ZipIterator iter(thrust::make_tuple(int_v.begin(), float_v.begin(), char_v.begin()));
*iter; // returns (0, 0.0f, 'a')
iter[0]; // returns (0, 0.0f, 'a')
iter[1]; // returns (1, 1.0f, 'b')
iter[2]; // returns (2, 2.0f, 'c')
thrust::get<0>(iter[2]); // returns 2
thrust::get<1>(iter[0]); // returns 0.0f
thrust::get<2>(iter[1]); // returns 'b'
// iter[3] is an out-of-bounds error

Defining the type of a zip_iterator can be complex. The next code example demonstrates how to use the make_zip_iterator function with the make_tuple function to avoid explicitly specifying the type of the zip_iterator. This example shows how to use zip_iterator to copy multiple ranges with a single call to thrust::copy.

#include <thrust/zip_iterator.h>
#include <thrust/tuple.h>
int main()
{
thrust::device_vector<int> int_in(3), int_out(3);
int_in[0] = 0;
int_in[1] = 1;
int_in[2] = 2;
thrust::device_vector<float> float_in(3), float_out(3);
float_in[0] = 0.0f;
float_in[1] = 10.0f;
float_in[2] = 20.0f;
thrust::copy(thrust::make_zip_iterator(thrust::make_tuple(int_in.begin(), float_in.begin())),
thrust::make_zip_iterator(thrust::make_tuple(int_in.end(), float_in.end())),
thrust::make_zip_iterator(thrust::make_tuple(int_out.begin(),float_out.begin())));
// int_out is now [0, 1, 2]
// float_out is now [0.0f, 10.0f, 20.0f]
return 0;
}
See also
make_zip_iterator
make_tuple
tuple
get

The documentation for this class was generated from the following file: