Fermat
pointers.h
1 /*
2  * CUGAR : Cuda Graphics Accelerator
3  *
4  * Copyright (c) 2011-2018, NVIDIA CORPORATION. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the NVIDIA CORPORATION nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #pragma once
30 
31 #if defined(__CUDACC__)
32 #include <cub/cub.cuh>
33 #endif
34 
35 #include <cugar/basic/types.h>
36 #include <cugar/basic/iterator.h>
37 
38 namespace cugar {
39 namespace cuda {
40 
43 
46 
51 {
59 };
60 
65 {
72 };
73 
74 #if defined(__CUDACC__)
75 template <CacheLoadModifier MOD> struct cub_load_mod {};
76 
77 template <> struct cub_load_mod<LOAD_DEFAULT> { static const cub::CacheLoadModifier MOD = cub::LOAD_DEFAULT; };
78 template <> struct cub_load_mod<LOAD_CA> { static const cub::CacheLoadModifier MOD = cub::LOAD_CA; };
79 template <> struct cub_load_mod<LOAD_CG> { static const cub::CacheLoadModifier MOD = cub::LOAD_CG; };
80 template <> struct cub_load_mod<LOAD_CS> { static const cub::CacheLoadModifier MOD = cub::LOAD_CS; };
81 template <> struct cub_load_mod<LOAD_CV> { static const cub::CacheLoadModifier MOD = cub::LOAD_CV; };
82 template <> struct cub_load_mod<LOAD_LDG> { static const cub::CacheLoadModifier MOD = cub::LOAD_LDG; };
83 template <> struct cub_load_mod<LOAD_VOLATILE> { static const cub::CacheLoadModifier MOD = cub::LOAD_VOLATILE;};
84 
85 template <CacheStoreModifier MOD> struct cub_store_mod {};
86 
87 template <> struct cub_store_mod<STORE_DEFAULT> { static const cub::CacheStoreModifier MOD = cub::STORE_DEFAULT; };
88 template <> struct cub_store_mod<STORE_WB> { static const cub::CacheStoreModifier MOD = cub::STORE_WB; };
89 template <> struct cub_store_mod<STORE_CG> { static const cub::CacheStoreModifier MOD = cub::STORE_CG; };
90 template <> struct cub_store_mod<STORE_CS> { static const cub::CacheStoreModifier MOD = cub::STORE_CS; };
91 template <> struct cub_store_mod<STORE_WT> { static const cub::CacheStoreModifier MOD = cub::STORE_WT; };
92 template <> struct cub_store_mod<STORE_VOLATILE> { static const cub::CacheStoreModifier MOD = cub::STORE_VOLATILE;};
93 #endif
94 
97 template <CacheLoadModifier LOAD_MOD, typename T>
98 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
99 T load(const T* ptr)
100 {
101  #if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 350
102  return cub::ThreadLoad<cub_load_mod<LOAD_MOD>::MOD>( const_cast<T*>(ptr) );
103  #else
104  return *ptr;
105  #endif
106 }
107 
110 template <CacheStoreModifier STORE_MOD, typename T>
111 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
112 void store(T* ptr, const T& value)
113 {
114  #if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 350
115  cub::ThreadStore<cub_store_mod<STORE_MOD>::MOD>( ptr, value );
116  #else
117  *ptr = value;
118  #endif
119 }
120 
124 template <typename T, CacheLoadModifier MOD>
126 {
127  typedef T value_type;
128  typedef value_type reference;
129  typedef value_type const_reference;
130  typedef value_type* pointer;
131  typedef typename std::iterator_traits<const T*>::difference_type difference_type;
132  typedef std::random_access_iterator_tag iterator_category;
133 
136  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
138 
141  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
142  load_pointer(const T* base) : m_base( base ) {}
143 
146  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
147  load_pointer(const load_pointer& it) : m_base( it.m_base ) {}
148 
151  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
152  value_type operator[](const uint32 i) const
153  {
154  return load<MOD>( m_base + i );
155  }
156 
159  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
160  value_type operator*() const
161  {
162  return load<MOD>( m_base );
163  }
164 
167  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
169  {
170  ++m_base;
171  return *this;
172  }
173 
176  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
178  {
179  load_pointer<T,MOD> r( m_base );
180  ++m_base;
181  return r;
182  }
183 
186  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
188  {
189  --m_base;
190  return *this;
191  }
192 
195  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
197  {
198  load_pointer<T,MOD> r( m_base );
199  --m_base;
200  return r;
201  }
202 
205  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
206  load_pointer<T,MOD> operator+(const difference_type i) const
207  {
208  return load_pointer( m_base + i );
209  }
210 
213  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
214  load_pointer<T,MOD> operator-(const difference_type i) const
215  {
216  return load_pointer( m_base - i );
217  }
218 
221  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
222  load_pointer<T,MOD>& operator+=(const difference_type i)
223  {
224  m_base += i;
225  return *this;
226  }
227 
230  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
231  load_pointer<T,MOD>& operator-=(const difference_type i)
232  {
233  m_base -= i;
234  return *this;
235  }
236 
239  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
240  difference_type operator-(const load_pointer<T,MOD> it) const
241  {
242  return m_base - it.m_base;
243  }
244 
247  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
249  {
250  m_base = it.m_base;
251  return *this;
252  }
253 
254  const T* m_base;
255 };
256 
259 template <CacheLoadModifier MOD, typename T>
260 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
262 {
263  return load_pointer<T,MOD>( it );
264 }
265 
269 template <typename T, CacheStoreModifier STORE_MOD, CacheLoadModifier LOAD_MOD = LOAD_DEFAULT>
271 {
272  typedef T value_type;
273 
276  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
278 
281  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
282  store_reference(T* base) : m_base( base ) {}
283 
286  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
287  store_reference(const store_reference& it) : m_base( it.m_base ) {}
288 
291  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
292  store_reference& operator=(const value_type value)
293  {
294  store<STORE_MOD>( m_base, value );
295  return *this;
296  }
297 
300  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
301  operator value_type()
302  {
303  return load<LOAD_MOD>( m_base );
304  }
305 
308  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
309  store_reference& operator+=(const value_type value)
310  {
311  const value_type old = load<LOAD_MOD>( m_base );
312  store<STORE_MOD>( m_base, old + value );
313  return *this;
314  }
315 
318  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
319  store_reference& operator-=(const value_type value)
320  {
321  const value_type old = load<LOAD_MOD>( m_base );
322  store<STORE_MOD>( m_base, old - value );
323  return *this;
324  }
325 
326  T* m_base;
327 };
328 
332 template <typename T, CacheStoreModifier STORE_MOD, CacheLoadModifier LOAD_MOD = LOAD_DEFAULT>
334 {
335  typedef T value_type;
337  typedef value_type const_reference;
338  typedef value_type* pointer;
339  typedef typename std::iterator_traits<T*>::difference_type difference_type;
340  typedef std::random_access_iterator_tag iterator_category;
341 
344  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
346 
349  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
350  store_pointer(T* base) : m_base( base ) {}
351 
354  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
355  store_pointer(const store_pointer& it) : m_base( it.m_base ) {}
356 
359  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
360  reference operator[](const uint32 i)
361  {
362  return reference( m_base + i );
363  }
364 
367  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
368  reference operator*()
369  {
370  return reference( m_base );
371  }
372 
375  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
377  {
378  ++m_base;
379  return *this;
380  }
381 
384  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
386  {
388  ++m_base;
389  return r;
390  }
391 
394  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
396  {
397  --m_base;
398  return *this;
399  }
400 
403  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
405  {
407  --m_base;
408  return r;
409  }
410 
413  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
414  store_pointer<T,STORE_MOD,LOAD_MOD> operator+(const difference_type i) const
415  {
416  return store_pointer( m_base + i );
417  }
418 
421  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
422  store_pointer<T,STORE_MOD,LOAD_MOD> operator-(const difference_type i) const
423  {
424  return store_pointer( m_base - i );
425  }
426 
429  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
431  {
432  m_base += i;
433  return *this;
434  }
435 
438  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
440  {
441  m_base -= i;
442  return *this;
443  }
444 
447  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
448  difference_type operator-(const store_pointer<T,STORE_MOD,LOAD_MOD> it) const
449  {
450  return m_base - it.m_base;
451  }
452 
455  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
457  {
458  m_base = it.m_base;
459  return *this;
460  }
461 
462  T* m_base;
463 };
464 
467 template <CacheStoreModifier STORE_MOD, CacheLoadModifier LOAD_MOD, typename T>
468 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
470 {
472 }
473 
476 
477 } // namespace cuda
478 } // namespace cugar
Cache at global level.
Definition: pointers.h:54
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE store_pointer< T, STORE_MOD, LOAD_MOD > operator++(int i)
Definition: pointers.h:385
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE load_pointer(const T *base)
Definition: pointers.h:142
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE store_pointer()
Definition: pointers.h:345
CacheStoreModifier
Enumeration of cache modifiers for memory load operations.
Definition: pointers.h:64
Definition: pointers.h:125
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE load_pointer< T, MOD > & operator-=(const difference_type i)
Definition: pointers.h:231
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE store_reference & operator+=(const value_type value)
Definition: pointers.h:309
Cache as volatile (including cached system lines)
Definition: pointers.h:56
Cache streaming (likely to be accessed once)
Definition: pointers.h:69
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE load_pointer< T, MOD > & operator++()
Definition: pointers.h:168
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE T load(const T *ptr)
Definition: pointers.h:99
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE load_pointer< T, MOD > operator+(const difference_type i) const
Definition: pointers.h:206
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE load_pointer()
Definition: pointers.h:137
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE load_pointer< T, MOD > operator--(int i)
Definition: pointers.h:196
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE load_pointer & operator=(const load_pointer< T, MOD > &it)
Definition: pointers.h:248
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE store_pointer< T, STORE_MOD, LOAD_MOD > operator--(int i)
Definition: pointers.h:404
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE store_pointer< T, STORE_MOD, LOAD_MOD > & operator+=(const difference_type i)
Definition: pointers.h:430
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE store_reference(T *base)
Definition: pointers.h:282
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE store_reference & operator-=(const value_type value)
Definition: pointers.h:319
Volatile shared (any memory space)
Definition: pointers.h:71
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE store_pointer< T, STORE_MOD, LOAD_MOD > operator+(const difference_type i) const
Definition: pointers.h:414
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE store_reference & operator=(const value_type value)
Definition: pointers.h:292
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE load_pointer< T, MOD > operator++(int i)
Definition: pointers.h:177
Default (no modifier)
Definition: pointers.h:52
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE reference operator[](const uint32 i)
Definition: pointers.h:360
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE store_pointer(T *base)
Definition: pointers.h:350
Cache as texture.
Definition: pointers.h:57
Cache at all levels.
Definition: pointers.h:53
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE store_reference()
Definition: pointers.h:277
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE load_pointer< T, MOD > make_load_pointer(const T *it)
Definition: pointers.h:261
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE store_pointer< T, STORE_MOD, LOAD_MOD > & operator-=(const difference_type i)
Definition: pointers.h:439
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE reference operator*()
Definition: pointers.h:368
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE store_pointer< T, STORE_MOD, LOAD_MOD > & operator--()
Definition: pointers.h:395
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE value_type operator*() const
Definition: pointers.h:160
Definition: pointers.h:333
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE store_pointer< T, STORE_MOD, LOAD_MOD > operator-(const difference_type i) const
Definition: pointers.h:422
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE void store(T *ptr, const T &value)
Definition: pointers.h:112
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE store_pointer< T, STORE_MOD, LOAD_MOD > & operator++()
Definition: pointers.h:376
Default (no modifier)
Definition: pointers.h:66
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE store_reference(const store_reference &it)
Definition: pointers.h:287
CacheLoadModifier
Enumeration of cache modifiers for memory load operations.
Definition: pointers.h:50
Cache write-through (to system memory)
Definition: pointers.h:70
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE difference_type operator-(const load_pointer< T, MOD > it) const
Definition: pointers.h:240
Define a vector_view POD type and plain_view() for std::vector.
Definition: diff.h:38
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE load_pointer< T, MOD > & operator+=(const difference_type i)
Definition: pointers.h:222
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE store_pointer< T, STORE_MOD, LOAD_MOD > make_store_pointer(const T *it)
Definition: pointers.h:469
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE load_pointer< T, MOD > operator-(const difference_type i) const
Definition: pointers.h:214
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE difference_type operator-(const store_pointer< T, STORE_MOD, LOAD_MOD > it) const
Definition: pointers.h:448
Volatile (any memory space)
Definition: pointers.h:58
Cache streaming (likely to be accessed once)
Definition: pointers.h:55
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE store_pointer & operator=(const store_pointer< T, STORE_MOD, LOAD_MOD > &it)
Definition: pointers.h:456
Cache at global level.
Definition: pointers.h:68
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE load_pointer(const load_pointer &it)
Definition: pointers.h:147
Definition: pointers.h:270
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE value_type operator[](const uint32 i) const
Definition: pointers.h:152
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE load_pointer< T, MOD > & operator--()
Definition: pointers.h:187
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE store_pointer(const store_pointer &it)
Definition: pointers.h:355
Cache write-back all coherent levels.
Definition: pointers.h:67