Extended CUDA Library (ecuda)  2.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
striding_padded_ptr.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2015, Scott Zuyderduyn
3 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 
8 1. Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation
12  and/or other materials provided with the distribution.
13 
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 
25 The views and conclusions contained in the software and documentation are those
26 of the authors and should not be interpreted as representing official policies,
27 either expressed or implied, of the FreeBSD Project.
28 */
29 
30 //----------------------------------------------------------------------------
31 // ptr/striding_padded_ptr.hpp
32 //
33 // A pointer specialization that describes device memory where elements are
34 // stored with a constant spacing in-between (hence incrementing/decrementing
35 // the pointer involves "striding" between elements).
36 //
37 // Author: Scott D. Zuyderduyn, Ph.D. (scott.zuyderduyn@utoronto.ca)
38 //----------------------------------------------------------------------------
39 
40 #ifndef ECUDA_PTR_STRIDING_PADDED_PTR_HPP
41 #define ECUDA_PTR_STRIDING_PADDED_PTR_HPP
42 
43 #include <iostream>
44 
45 #include "../global.hpp"
46 #include "../type_traits.hpp"
47 #include "../utility.hpp"
48 #include "common.hpp"
49 
50 namespace ecuda {
51 
77 template<typename T,typename P=typename ecuda::add_pointer<T>::type>
79 {
80 
81 public:
82  typedef T element_type;
83  typedef P pointer;
84  typedef T& reference;
85  typedef const T& const_reference;
86  typedef std::size_t size_type;
87  typedef std::ptrdiff_t difference_type;
88 
89 private:
90  pointer ptr;
91  size_type stride;
92 
93 private:
94  template<typename U> struct char_ptr;
95  template<typename U> struct char_ptr<U*> { typedef char* type; };
96  template<typename U> struct char_ptr<const U*> { typedef const char* type; };
97 
98  template<typename T2,typename P2> friend class striding_padded_ptr;
99 
100 public:
101  __HOST__ __DEVICE__ striding_padded_ptr( pointer ptr = pointer(), size_type stride = 0 ) : ptr(ptr), stride(stride) {}
102 
103  __HOST__ __DEVICE__ striding_padded_ptr( const striding_padded_ptr& src ) : ptr(src.ptr), stride(src.stride) {}
104 
105  template<typename T2,typename P2>
106  __HOST__ __DEVICE__ striding_padded_ptr( const striding_padded_ptr<T2,P2>& src ) : ptr(src.ptr), stride(src.stride) {}
107 
108  #ifdef ECUDA_CPP11_AVAILABLE
109  __HOST__ __DEVICE__ striding_padded_ptr( striding_padded_ptr&& src ) : ptr(std::move(src.ptr)), stride(std::move(src.stride)) {}
110 
112  {
113  ptr = std::move(src.ptr);
114  stride = std::move(src.stride);
115  return *this;
116  }
117  #endif
118 
119  __HOST__ __DEVICE__ inline pointer get() const { return ptr; }
120 
121  __HOST__ __DEVICE__ inline size_type get_stride() const { return stride; }
122 
123  __DEVICE__ inline reference operator*() { return *ptr; }
124  __DEVICE__ inline const_reference operator*() const { return *ptr; }
125  __DEVICE__ inline pointer operator->() const { return ptr; }
126  __DEVICE__ inline reference operator[]( std::size_t i ) { return striding_padded_ptr(*this).operator+=(i).operator*(); }
127  __DEVICE__ inline const_reference operator[]( std::size_t i ) const { return striding_padded_ptr(*this).operator+=(i).operator*(); }
128 
130  {
131  typedef typename char_ptr<pointer>::type char_pointer_type;
132  ptr = reinterpret_cast<pointer>( reinterpret_cast<char_pointer_type>(ptr) + stride );
133  return *this;
134  }
136  {
137  typedef typename char_ptr<pointer>::type char_pointer_type;
138  ptr = reinterpret_cast<pointer>( reinterpret_cast<char_pointer_type>(ptr) - stride );
139  return *this;
140  }
141  __HOST__ __DEVICE__ inline striding_padded_ptr operator++( int ) { striding_padded_ptr tmp(*this); ++(*this); return tmp; }
142  __HOST__ __DEVICE__ inline striding_padded_ptr operator--( int ) { striding_padded_ptr tmp(*this); --(*this); return tmp; }
143 
145  {
146  typedef typename char_ptr<pointer>::type char_pointer_type;
147  ptr = reinterpret_cast<pointer>( reinterpret_cast<char_pointer_type>(ptr) + x*stride );
148  return *this;
149  }
151  {
152  typedef typename char_ptr<pointer>::type char_pointer_type;
153  ptr = reinterpret_cast<pointer>( reinterpret_cast<char_pointer_type>(ptr) - x*stride );
154  return *this;
155  }
156 
157  __HOST__ __DEVICE__ inline striding_padded_ptr operator+( int x ) const { striding_padded_ptr tmp(*this); tmp += x; return tmp; }
158  __HOST__ __DEVICE__ inline striding_padded_ptr operator-( int x ) const { striding_padded_ptr tmp(*this); tmp -= x; return tmp; }
159 
160  template<typename T2,typename P2> __HOST__ __DEVICE__ inline bool operator==( const striding_padded_ptr<T2,P2>& other ) const { return ptr == other.ptr; }
161  template<typename T2,typename P2> __HOST__ __DEVICE__ inline bool operator!=( const striding_padded_ptr<T2,P2>& other ) const { return ptr != other.ptr; }
162  template<typename T2,typename P2> __HOST__ __DEVICE__ inline bool operator< ( const striding_padded_ptr<T2,P2>& other ) const { return ptr < other.ptr; }
163  template<typename T2,typename P2> __HOST__ __DEVICE__ inline bool operator> ( const striding_padded_ptr<T2,P2>& other ) const { return ptr > other.ptr; }
164  template<typename T2,typename P2> __HOST__ __DEVICE__ inline bool operator<=( const striding_padded_ptr<T2,P2>& other ) const { return ptr <= other.ptr; }
165  template<typename T2,typename P2> __HOST__ __DEVICE__ inline bool operator>=( const striding_padded_ptr<T2,P2>& other ) const { return ptr >= other.ptr; }
166 
167  template<typename U,typename V>
168  friend std::basic_ostream<U,V>& operator<<( std::basic_ostream<U,V>& out, const striding_padded_ptr& ptr )
169  {
170  out << "striding_padded_ptr(ptr=" << ptr.ptr << ";stride=" << ptr.stride << ")";
171  return out;
172  }
173 
174 };
175 
176 } // namespace ecuda
177 
178 #endif
__HOST__ __DEVICE__ striding_padded_ptr(const striding_padded_ptr &src)
__HOST__ __DEVICE__ size_type get_stride() const
__HOST__ __DEVICE__ striding_padded_ptr operator+(int x) const
__HOST__ __DEVICE__ striding_padded_ptr(pointer ptr=pointer(), size_type stride=0)
__HOST__ __DEVICE__ bool operator!=(const striding_padded_ptr< T2, P2 > &other) const
__HOST__ __DEVICE__ striding_padded_ptr operator++(int)
__DEVICE__ const_reference operator[](std::size_t i) const
#define __HOST__
Definition: global.hpp:150
__HOST__ __DEVICE__ bool operator>=(const striding_padded_ptr< T2, P2 > &other) const
__DEVICE__ const_reference operator*() const
__HOST__ __DEVICE__ bool operator>(const striding_padded_ptr< T2, P2 > &other) const
__DEVICE__ reference operator[](std::size_t i)
A specialized pointer to striding memory.
__DEVICE__ reference operator*()
__HOST__ __DEVICE__ striding_padded_ptr & operator--()
__DEVICE__ pointer operator->() const
__HOST__ __DEVICE__ striding_padded_ptr operator-(int x) const
__HOST__ __DEVICE__ striding_padded_ptr(const striding_padded_ptr< T2, P2 > &src)
__HOST__ __DEVICE__ striding_padded_ptr & operator-=(int x)
__HOST__ __DEVICE__ striding_padded_ptr & operator++()
__HOST__ __DEVICE__ striding_padded_ptr operator--(int)
#define __DEVICE__
Definition: global.hpp:151
__HOST__ __DEVICE__ bool operator==(const striding_padded_ptr< T2, P2 > &other) const
__HOST__ __DEVICE__ striding_padded_ptr & operator+=(int x)