thrust

◆ scatter() [1/2]

template<typename DerivedPolicy , typename InputIterator1 , typename InputIterator2 , typename RandomAccessIterator >
__host__ __device__ void thrust::scatter ( const thrust::detail::execution_policy_base< DerivedPolicy > &  exec,
InputIterator1  first,
InputIterator1  last,
InputIterator2  map,
RandomAccessIterator  result 
)

scatter copies elements from a source range into an output array according to a map. For each iterator i in the range [first, last), the value *i is assigned to output[*(map + (i - first))]. The output iterator must permit random access. If the same index appears more than once in the range [map, map + (last - first)), the result is undefined.

The algorithm's execution is parallelized as determined by exec.

Parameters
execThe execution policy to use for parallelization.
firstBeginning of the sequence of values to scatter.
lastEnd of the sequence of values to scatter.
mapBeginning of the sequence of output indices.
resultDestination of the source elements.
Template Parameters
DerivedPolicyThe name of the derived execution policy.
InputIterator1must be a model of Input Iterator and InputIterator1's value_type must be convertible to RandomAccessIterator's value_type.
InputIterator2must be a model of Input Iterator and InputIterator2's value_type must be convertible to RandomAccessIterator's difference_type.
RandomAccessIteratormust be a model of Random Access iterator.
Precondition
The iterator result + i shall not refer to any element referenced by any iterator j in the range [first,last) for all iterators i in the range [map,map + (last - first)).
The iterator result + i shall not refer to any element referenced by any iterator j in the range [map,map + (last - first)) for all iterators i in the range [map,map + (last - first)).
The expression result[*i] shall be valid for all iterators in the range [map,map + (last - first)).

The following code snippet demonstrates how to use scatter to reorder a range using the thrust::device execution policy for parallelization:

#include <thrust/scatter.h>
...
// mark even indices with a 1; odd indices with a 0
int values[10] = {1, 0, 1, 0, 1, 0, 1, 0, 1, 0};
thrust::device_vector<int> d_values(values, values + 10);
// scatter all even indices into the first half of the
// range, and odd indices vice versa
int map[10] = {0, 5, 1, 6, 2, 7, 3, 8, 4, 9};
thrust::device_vector<int> d_map(map, map + 10);
d_values.begin(), d_values.end(),
d_map.begin(), d_output.begin());
// d_output is now {1, 1, 1, 1, 1, 0, 0, 0, 0, 0}
Note
scatter is the inverse of thrust::gather.