Fermat
strided_iterator.h
1 /*
2  * cugar
3  * Copyright (c) 2011-2018, NVIDIA CORPORATION. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the NVIDIA CORPORATION nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #pragma once
29 
30 #include <cugar/basic/types.h>
31 #include <cugar/basic/iterator.h>
32 
33 namespace cugar {
34 
47 
50 
53 
59 template <typename T>
61 {
62  typedef typename std::iterator_traits<T>::value_type value_type;
63  typedef typename std::iterator_traits<T>::reference reference;
64  typedef typename to_const<reference>::type const_reference;
65  typedef typename std::iterator_traits<T>::pointer pointer;
66  typedef typename std::iterator_traits<T>::difference_type difference_type;
67  //typedef typename std::iterator_traits<T>::distance_type distance_type;
68  typedef typename std::iterator_traits<T>::iterator_category iterator_category;
69 
72  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
74 
77  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
78  strided_iterator(T vec, const uint32 stride) : m_vec( vec ), m_stride( stride ) {}
79 
82  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
83  const_reference operator*() const { return *m_vec; }
84 
87  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
88  reference operator*() { return *m_vec; }
89 
92  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
93  const_reference operator[](const uint32 i) const { return m_vec[i*m_stride]; }
94 
97  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
98  reference operator[](const uint32 i) { return m_vec[i*m_stride]; }
99 
102  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
103  strided_iterator<T> operator+(const uint32 i) const
104  {
105  return strided_iterator( m_vec + i * m_stride, m_stride );
106  }
107 
110  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
111  difference_type operator-(const strided_iterator<T> it) const
112  {
113  return (m_vec - it.m_vec) / m_stride;
114  }
115 
118  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
120  {
121  m_vec += m_stride;
122  return *this;
123  }
124 
125  T m_vec;
126  uint32 m_stride;
127 };
128 
131 template <typename T>
132 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
133 strided_iterator<T> make_strided_iterator(T it, const uint32 stride)
134 {
135  return strided_iterator<T>( it, stride );
136 }
137 
140 template <typename T>
141 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
142 bool operator==(const strided_iterator<T> it1, const strided_iterator<T> it2)
143 {
144  return (it1.m_vec == it2.m_vec) && (it1.m_stride == it2.m_stride);
145 }
148 template <typename T>
149 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
150 bool operator!=(const strided_iterator<T> it1, const strided_iterator<T> it2)
151 {
152  return (it1.m_vec != it2.m_vec) || (it1.m_stride != it2.m_stride);
153 }
156 template <typename T>
157 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
158 bool operator<(const strided_iterator<T> it1, const strided_iterator<T> it2) { return (it1.m_vec < it2.m_vec); }
161 template <typename T>
162 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
163 bool operator<=(const strided_iterator<T> it1, const strided_iterator<T> it2) { return (it1.m_vec <= it2.m_vec); }
166 template <typename T>
167 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
168 bool operator>(const strided_iterator<T> it1, const strided_iterator<T> it2) { return (it1.m_vec > it2.m_vec); }
171 template <typename T>
172 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
173 bool operator>=(const strided_iterator<T> it1, const strided_iterator<T> it2) { return (it1.m_vec >= it2.m_vec); }
174 
175 enum block_strided_layout
176 {
177  ROW_MAJOR_LAYOUT = 0u,
178  COLUMN_MAJOR_LAYOUT = 1u
179 };
180 
192 template <uint32 BLOCKSIZE, typename T, block_strided_layout LAYOUT = ROW_MAJOR_LAYOUT>
194 {
195  typedef typename std::iterator_traits<T>::value_type value_type;
196  typedef typename std::iterator_traits<T>::reference reference;
197  typedef typename to_const<reference>::type const_reference;
198  typedef typename std::iterator_traits<T>::pointer pointer;
199  typedef typename std::iterator_traits<T>::difference_type difference_type;
200  //typedef typename std::iterator_traits<T>::distance_type distance_type;
201  typedef typename std::iterator_traits<T>::iterator_category iterator_category;
202 
205  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
207 
210  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
211  block_strided_iterator(T vec, const uint32 stride, const uint32 offset = 0) : m_vec( vec ), m_offset(offset), m_stride( stride ) {}
212 
215  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
216  const_reference operator*() const { return m_vec[m_offset]; }
217 
220  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
221  const_reference operator[](const uint32 i) const
222  {
223  if (LAYOUT == ROW_MAJOR_LAYOUT)
224  return m_vec[((i+m_offset) / BLOCKSIZE)*m_stride + ((i+m_offset) % BLOCKSIZE)];
225  else
226  return m_vec[((i+m_offset) % BLOCKSIZE)*m_stride + ((i+m_offset) / BLOCKSIZE)];
227  }
228 
231  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
232  reference operator[](const uint32 i)
233  {
234  if (LAYOUT == ROW_MAJOR_LAYOUT)
235  return m_vec[((i+m_offset) / BLOCKSIZE)*m_stride + ((i+m_offset) % BLOCKSIZE)];
236  else
237  return m_vec[((i+m_offset) % BLOCKSIZE)*m_stride + ((i+m_offset) / BLOCKSIZE)];
238  }
239 
242  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
244  {
245  return block_strided_iterator<BLOCKSIZE,T>( m_vec, m_stride, m_offset + i );
246  }
247 
250  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
251  difference_type operator-(const block_strided_iterator<BLOCKSIZE,T> it) const
252  {
253  return (m_vec + m_offset) - (it.m_vec + it.m_offset);
254  }
255 
258  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
260  {
261  ++m_offset;
262  return *this;
263  }
264 
265  T m_vec;
266  uint32 m_offset;
267  uint32 m_stride;
268 };
269 
272 template <uint32 BLOCKSIZE,typename T, block_strided_layout LAYOUT>
273 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
275 {
276  return (it1.m_vec == it2.m_vec) && (it1.m_offset == it2.m_offset) && (it1.m_stride == it2.m_stride);
277 }
280 template <uint32 BLOCKSIZE,typename T, block_strided_layout LAYOUT>
281 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
283 {
284  return (it1.m_vec != it2.m_vec) || (it1.m_offset != it2.m_offset) || (it1.m_stride != it2.m_stride);
285 }
286 
289 
290 } // namespace cugar
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE block_strided_iterator()
Definition: strided_iterator.h:206
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE strided_iterator()
Definition: strided_iterator.h:73
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE const_reference operator*() const
Definition: strided_iterator.h:83
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE difference_type operator-(const block_strided_iterator< BLOCKSIZE, T > it) const
Definition: strided_iterator.h:251
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE const_reference operator*() const
Definition: strided_iterator.h:216
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE const_reference operator[](const uint32 i) const
Definition: strided_iterator.h:221
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE strided_iterator(T vec, const uint32 stride)
Definition: strided_iterator.h:78
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE reference operator*()
Definition: strided_iterator.h:88
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE strided_iterator< T > operator+(const uint32 i) const
Definition: strided_iterator.h:103
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE block_strided_iterator< BLOCKSIZE, T > & operator++()
Definition: strided_iterator.h:259
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE const_reference operator[](const uint32 i) const
Definition: strided_iterator.h:93
Definition: strided_iterator.h:60
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE strided_iterator< T > & operator++()
Definition: strided_iterator.h:119
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE block_strided_iterator< BLOCKSIZE, T > operator+(const uint32 i) const
Definition: strided_iterator.h:243
Define a vector_view POD type and plain_view() for std::vector.
Definition: diff.h:38
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE block_strided_iterator(T vec, const uint32 stride, const uint32 offset=0)
Definition: strided_iterator.h:211
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE reference operator[](const uint32 i)
Definition: strided_iterator.h:232
Definition: strided_iterator.h:193
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE reference operator[](const uint32 i)
Definition: strided_iterator.h:98
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE difference_type operator-(const strided_iterator< T > it) const
Definition: strided_iterator.h:111
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE strided_iterator< T > make_strided_iterator(T it, const uint32 stride)
Definition: strided_iterator.h:133