MCBooster  1.0.1
Tool to generate MC phase space samples in parallel.
strided_iterator.h
Go to the documentation of this file.
1 /*
2  * strided_iterator.h
3  *
4  * original code: https://github.com/thrust/thrust/blob/master/examples/strided_range.cu
5  *
6  * Created on : Feb 25, 2016
7  * Author: Antonio Augusto Alves Junior
8  */
9 
10 /*
11  * This file is part of MCBooster.
12  *
13  * MCBooster is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation, either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * MCBooster is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with MCBooster. If not, see <http://www.gnu.org/licenses/>.
25  */
26 
27 
28 #ifndef STRIDED_ITERATOR_H_
29 #define STRIDED_ITERATOR_H_
30 
31 #include <thrust/iterator/counting_iterator.h>
32 #include <thrust/iterator/transform_iterator.h>
33 #include <thrust/iterator/permutation_iterator.h>
34 #include <thrust/functional.h>
35 
39 template<typename Iterator>
41 {
42 public:
43 
44  typedef typename thrust::iterator_difference<Iterator>::type difference_type;
45 
46  struct stride_functor: public thrust::unary_function<difference_type,
47  difference_type>
48  {
49  difference_type stride;
50 
51  stride_functor(difference_type stride) :
52  stride(stride)
53  {
54  }
55 
56  __host__ __device__ difference_type operator()(
57  const difference_type& i) const
58  {
59  return stride * i;
60  }
61  };
62 
63  typedef typename thrust::counting_iterator<difference_type> CountingIterator;
64  typedef typename thrust::transform_iterator<stride_functor, CountingIterator> TransformIterator;
65  typedef typename thrust::permutation_iterator<Iterator, TransformIterator> PermutationIterator;
66 
68  typedef PermutationIterator iterator;
69 
71  strided_range(Iterator first, Iterator last, difference_type stride) :
72  first(first), last(last), stride(stride)
73  {
74  }
75 
76  iterator begin(void) const
77  {
80  }
81 
82  iterator end(void) const
83  {
84  return begin() + ((last - first) + (stride - 1)) / stride;
85  }
86 
87 protected:
88  Iterator first;
89  Iterator last;
90  difference_type stride;
91 };
92 
93 #endif /* STRIDED_ITERATOR_H_ */
thrust::transform_iterator< stride_functor, CountingIterator > TransformIterator
iterator begin(void) const
__host__ __device__ difference_type operator()(const difference_type &i) const
difference_type stride
stride_functor(difference_type stride)
thrust::counting_iterator< difference_type > CountingIterator
Strided range iterator original code: https://github.com/thrust/thrust/blob/master/examples/strided_r...
thrust::iterator_difference< Iterator >::type difference_type
PermutationIterator iterator
type of the strided_range iterator
strided_range(Iterator first, Iterator last, difference_type stride)
construct strided_range for the range [first,last)
iterator end(void) const
thrust::permutation_iterator< Iterator, TransformIterator > PermutationIterator