#include #include #include #include #include #include #include #include // this example illustrates how to tile a range multiple times // examples: // tiled_range([0, 1, 2, 3], 1) -> [0, 1, 2, 3] // tiled_range([0, 1, 2, 3], 2) -> [0, 1, 2, 3, 0, 1, 2, 3] // tiled_range([0, 1, 2, 3], 3) -> [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3] // ... template class tiled_range { public: using difference_type = typename cuda::std::iterator_traits::difference_type; struct tile_functor { difference_type tile_size; tile_functor(difference_type tile_size) : tile_size(tile_size) {} __host__ __device__ difference_type operator()(const difference_type& i) const { return i % tile_size; } }; using CountingIterator = typename thrust::counting_iterator; using TransformIterator = typename thrust::transform_iterator; using PermutationIterator = typename thrust::permutation_iterator; // type of the tiled_range iterator using iterator = PermutationIterator; // construct repeated_range for the range [first,last) tiled_range(Iterator first, Iterator last, difference_type tiles) : first(first) , last(last) , tiles(tiles) {} iterator begin() const { return PermutationIterator(first, TransformIterator(CountingIterator(0), tile_functor(last - first))); } iterator end() const { return begin() + tiles * (last - first); } protected: Iterator first; Iterator last; difference_type tiles; }; int main() { thrust::device_vector data{10, 20, 30, 40}; // print the initial data std::cout << "range "; thrust::copy(data.begin(), data.end(), std::ostream_iterator(std::cout, " ")); std::cout << '\n'; using Iterator = thrust::device_vector::iterator; // create tiled_range with two tiles tiled_range two(data.begin(), data.end(), 2); std::cout << "two tiles: "; thrust::copy(two.begin(), two.end(), std::ostream_iterator(std::cout, " ")); std::cout << '\n'; // create tiled_range with three tiles tiled_range three(data.begin(), data.end(), 3); std::cout << "three tiles: "; thrust::copy(three.begin(), three.end(), std::ostream_iterator(std::cout, " ")); std::cout << '\n'; return 0; }