SpikeGPU  1.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
strided_range.h
Go to the documentation of this file.
1 // ============================================================================
2 // This file implements the strided_range class to allow non-contiguous
3 // access to data in a thrust vector.
4 //
5 // This class is the same as the strided_range thrust example provided
6 // by Nathan Bell at
7 // (https://code.google.com/p/thrust/source/browse/examples/strided_range.cu)
8 // ============================================================================
9 
10 
11 #ifndef SPIKE_STRIDED_RANGE
12 #define SPIKE_STRIDED_RANGE
13 
14 #include <thrust/iterator/counting_iterator.h>
15 #include <thrust/iterator/transform_iterator.h>
16 #include <thrust/iterator/permutation_iterator.h>
17 #include <thrust/functional.h>
18 
19 
20 namespace spike {
21 
22 template <typename Iterator>
24 {
25 public:
26  typedef typename thrust::iterator_difference<Iterator>::type difference_type;
27 
28  struct stride_functor : public thrust::unary_function<difference_type, difference_type>
29  {
31 
32  __host__ __device__
33  difference_type operator()(const difference_type& i) const {return m_stride * i;}
34 
36  };
37 
38  typedef typename thrust::counting_iterator<difference_type> CountingIterator;
39  typedef typename thrust::transform_iterator<stride_functor, CountingIterator> TransformIterator;
40  typedef typename thrust::permutation_iterator<Iterator,TransformIterator> PermutationIterator;
41 
42  // Type of the strided_range iterator
44 
45  // Construct strided_range for the range [first,last)
46  strided_range(Iterator first, Iterator last, difference_type stride)
47  : m_first(first), m_last(last), m_stride(stride) {}
48 
49  iterator begin(void) const
50  {
52  }
53 
54  iterator end(void) const
55  {
56  return begin() + ((m_last - m_first) + (m_stride - 1)) / m_stride;
57  }
58 
59 protected:
60  Iterator m_first;
61  Iterator m_last;
63 };
64 
65 } // namespace spike
66 
67 
68 
69 #endif
thrust::counting_iterator< difference_type > CountingIterator
Definition: strided_range.h:38
Definition: strided_range.h:28
strided_range(Iterator first, Iterator last, difference_type stride)
Definition: strided_range.h:46
thrust::permutation_iterator< Iterator, TransformIterator > PermutationIterator
Definition: strided_range.h:40
iterator end(void) const
Definition: strided_range.h:54
__host__ __device__ difference_type operator()(const difference_type &i) const
Definition: strided_range.h:33
PermutationIterator iterator
Definition: strided_range.h:43
Definition: strided_range.h:23
thrust::transform_iterator< stride_functor, CountingIterator > TransformIterator
Definition: strided_range.h:39
difference_type m_stride
Definition: strided_range.h:62
stride_functor(difference_type stride)
Definition: strided_range.h:30
Iterator m_first
Definition: strided_range.h:60
difference_type m_stride
Definition: strided_range.h:35
iterator begin(void) const
Definition: strided_range.h:49
Iterator m_last
Definition: strided_range.h:61
thrust::iterator_difference< Iterator >::type difference_type
Definition: strided_range.h:26