Extended CUDA Library (ecuda)  2.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
padded_ptr.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2014-2016, 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/padded_ptr.hpp
32 //
33 // A pointer specialization that describes device memory that is padded to
34 // comform to a given memory alignment.
35 //
36 // Author: Scott D. Zuyderduyn, Ph.D. (scott.zuyderduyn@utoronto.ca)
37 //----------------------------------------------------------------------------
38 
39 #ifndef ECUDA_PTR_PADDED_PTR_HPP
40 #define ECUDA_PTR_PADDED_PTR_HPP
41 
42 #include <iostream>
43 
44 #include "../global.hpp"
45 #include "../type_traits.hpp"
46 #include "../utility.hpp"
47 #include "common.hpp"
48 
49 namespace ecuda {
50 
78 template<typename T,class P=typename ecuda::add_pointer<T>::type>
79 class padded_ptr
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  template<typename U> struct char_pointer;
91  template<typename U> struct char_pointer<U*> { typedef char* type; };
92  template<typename U> struct char_pointer<const U*> { typedef const char* type; };
93  template<typename U> __HOST__ __DEVICE__ typename char_pointer<U*>::type char_cast( U* ptr ) const { return reinterpret_cast<char*>(ptr); }
94  template<typename U> __HOST__ __DEVICE__ typename char_pointer<const U*>::type char_cast( const U* ptr ) const { return reinterpret_cast<const char*>(ptr); }
95 
96 private:
97  pointer ptr;
98  size_type pitch;
99 
100 public:
101  __HOST__ __DEVICE__ padded_ptr( pointer ptr = pointer(), size_type pitch = size_type() ) : ptr(ptr), pitch(pitch) {}
102 
103  __HOST__ __DEVICE__ padded_ptr( const padded_ptr& src ) : ptr(src.ptr), pitch(src.pitch) {}
104 
106  {
107  ptr = src.ptr;
108  pitch = src.pitch;
109  return *this;
110  }
111 
112  template<typename T2,class P2>
113  __HOST__ __DEVICE__ padded_ptr( const padded_ptr<T2,P2>& src ) : ptr(src.get()), pitch(src.get_pitch()) {}
114 
115  #ifdef ECUDA_CPP11_AVAILABLE
116  __HOST__ __DEVICE__ padded_ptr( padded_ptr&& src ) : ptr(std::move(src.ptr)), pitch(std::move(src.pitch)) {}
118  {
119  ptr = std::move(src.ptr);
120  pitch = std::move(src.pitch);
121  return *this;
122  }
123  #endif
124 
125  __HOST__ __DEVICE__ inline size_type get_pitch() const { return pitch; }
126  __HOST__ __DEVICE__ inline pointer get() const { return ptr; }
127 
128  __DEVICE__ inline reference operator*() { return *ptr; }
129  __DEVICE__ inline const_reference operator*() const { return *ptr; }
130  __DEVICE__ inline pointer operator->() const { return ptr; }
131  __DEVICE__ inline reference operator[]( std::size_t i ) { return padded_ptr(*this).operator+=(i).operator*(); }
132  __DEVICE__ inline const_reference operator[]( std::size_t i ) const { return padded_ptr(*this).operator+=(i).operator*(); }
133 
134  #ifdef ECUDA_CPP11_AVAILABLE
135  __HOST__ __DEVICE__ explicit operator bool() const __NOEXCEPT__ { return naked_cast<typename ecuda::add_pointer<const element_type>::type>( ptr ) != NULL; }
141  #else
142  __HOST__ __DEVICE__ operator bool() const __NOEXCEPT__ { return naked_cast<typename ecuda::add_pointer<const element_type>::type>( ptr ) != NULL; }
148  #endif
149 
150  __HOST__ __DEVICE__ inline padded_ptr& operator++() { ++ptr; return *this; }
151  __HOST__ __DEVICE__ inline padded_ptr& operator--() { --ptr; return *this; }
152 
153  __HOST__ __DEVICE__ inline padded_ptr operator++( int ) { padded_ptr tmp(*this); ++(*this); return tmp; }
154  __HOST__ __DEVICE__ inline padded_ptr operator--( int ) { padded_ptr tmp(*this); --(*this); return tmp; }
155 
156  __HOST__ __DEVICE__ inline padded_ptr& operator+=( int x ) { ptr += x; return *this; }
157  __HOST__ __DEVICE__ inline padded_ptr& operator-=( int x ) { ptr -= x; return *this; }
158 
159  __HOST__ __DEVICE__ inline padded_ptr operator+( int x ) const
160  {
161  padded_ptr tmp( *this );
162  tmp += x;
163  return tmp;
164  }
165 
166  __HOST__ __DEVICE__ inline padded_ptr operator-( int x ) const
167  {
168  padded_ptr tmp( *this );
169  tmp -= x;
170  return tmp;
171  }
172 
173  __HOST__ __DEVICE__ inline padded_ptr operator+( std::size_t x ) const { return operator+( static_cast<int>(x) ); }
174  __HOST__ __DEVICE__ inline padded_ptr operator-( std::size_t x ) const { return operator-( static_cast<int>(x) ); }
175 
176  template<typename T2,typename P2> __HOST__ __DEVICE__ bool operator==( const padded_ptr<T2,P2>& other ) const { return ptr == other.ptr; }
177  template<typename T2,typename P2> __HOST__ __DEVICE__ bool operator!=( const padded_ptr<T2,P2>& other ) const { return ptr != other.ptr; }
178  template<typename T2,typename P2> __HOST__ __DEVICE__ bool operator< ( const padded_ptr<T2,P2>& other ) const { return ptr < other.ptr; }
179  template<typename T2,typename P2> __HOST__ __DEVICE__ bool operator> ( const padded_ptr<T2,P2>& other ) const { return ptr > other.ptr; }
180  template<typename T2,typename P2> __HOST__ __DEVICE__ bool operator<=( const padded_ptr<T2,P2>& other ) const { return ptr <= other.ptr; }
181  template<typename T2,typename P2> __HOST__ __DEVICE__ bool operator>=( const padded_ptr<T2,P2>& other ) const { return ptr >= other.ptr; }
182 
192  {
193  typedef typename ecuda::add_pointer<element_type>::type raw_pointer_type;
194  typedef typename char_pointer<raw_pointer_type>::type char_pointer_type;
195  char_pointer_type charPtr = char_cast( naked_cast<raw_pointer_type>(ptr) );
196  charPtr += x;
197  ptr = pointer( naked_cast<raw_pointer_type>(charPtr) );
198  }
199 
200  template<typename U,typename V>
201  friend std::basic_ostream<U,V>& operator<<( std::basic_ostream<U,V>& out, const padded_ptr& ptr )
202  {
203  out << "padded_ptr(ptr=" << ptr.ptr << ";pitch=" << ptr.pitch << ")";
204  return out;
205  }
206 
207 };
208 
209 
210 } // namespace ecuda
211 
212 #endif
__HOST__ __DEVICE__ void skip_bytes(difference_type x)
Move the pointer some number of bytes.
Definition: padded_ptr.hpp:191
__HOST__ __DEVICE__ padded_ptr operator+(int x) const
Definition: padded_ptr.hpp:159
__HOST__ __DEVICE__ bool operator>=(const padded_ptr< T2, P2 > &other) const
Definition: padded_ptr.hpp:181
__HOST__ __DEVICE__ bool operator==(const padded_ptr< T2, P2 > &other) const
Definition: padded_ptr.hpp:176
__HOST__ __DEVICE__ padded_ptr operator-(std::size_t x) const
Definition: padded_ptr.hpp:174
__HOST__ __DEVICE__ padded_ptr(pointer ptr=pointer(), size_type pitch=size_type())
Definition: padded_ptr.hpp:101
__HOST__ __DEVICE__ padded_ptr operator-(int x) const
Definition: padded_ptr.hpp:166
const T & const_reference
Definition: padded_ptr.hpp:85
__HOST__ __DEVICE__ padded_ptr & operator++()
Definition: padded_ptr.hpp:150
#define __NOEXCEPT__
Definition: global.hpp:140
__DEVICE__ reference operator*()
Definition: padded_ptr.hpp:128
__HOST__ __DEVICE__ bool operator>(const padded_ptr< T2, P2 > &other) const
Definition: padded_ptr.hpp:179
__DEVICE__ pointer operator->() const
Definition: padded_ptr.hpp:130
__HOST__ __DEVICE__ padded_ptr operator+(std::size_t x) const
Definition: padded_ptr.hpp:173
#define __HOST__
Definition: global.hpp:150
A specialized pointer to padded memory.
Definition: iterator.hpp:52
__HOST__ __DEVICE__ pointer get() const
Definition: padded_ptr.hpp:126
__DEVICE__ const_reference operator[](std::size_t i) const
Definition: padded_ptr.hpp:132
__HOST__ __DEVICE__ padded_ptr(const padded_ptr< T2, P2 > &src)
Definition: padded_ptr.hpp:113
__HOST__ __DEVICE__ padded_ptr & operator-=(int x)
Definition: padded_ptr.hpp:157
__HOST__ __DEVICE__ padded_ptr operator++(int)
Definition: padded_ptr.hpp:153
__HOST__ __DEVICE__ padded_ptr & operator+=(int x)
Definition: padded_ptr.hpp:156
__HOST__ __DEVICE__ padded_ptr & operator--()
Definition: padded_ptr.hpp:151
__HOST__ __DEVICE__ size_type get_pitch() const
Definition: padded_ptr.hpp:125
__HOST__ __DEVICE__ bool operator!=(const padded_ptr< T2, P2 > &other) const
Definition: padded_ptr.hpp:177
__DEVICE__ const_reference operator*() const
Definition: padded_ptr.hpp:129
#define __DEVICE__
Definition: global.hpp:151
__HOST__ __DEVICE__ padded_ptr(const padded_ptr &src)
Definition: padded_ptr.hpp:103
std::size_t size_type
Definition: padded_ptr.hpp:86
__HOST__ __DEVICE__ padded_ptr & operator=(const padded_ptr &src)
Definition: padded_ptr.hpp:105
__HOST__ __DEVICE__ padded_ptr operator--(int)
Definition: padded_ptr.hpp:154
__DEVICE__ reference operator[](std::size_t i)
Definition: padded_ptr.hpp:131
std::ptrdiff_t difference_type
Definition: padded_ptr.hpp:87