Extended CUDA Library (ecuda)  2.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
striding_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/striding_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_PTR_HPP
41 #define ECUDA_PTR_STRIDING_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  template<typename U,typename Q> friend class striding_ptr;
94 
95 public:
96  __HOST__ __DEVICE__ striding_ptr( pointer ptr = pointer(), size_type stride = 0 ) : ptr(ptr), stride(stride) {}
97 
98  __HOST__ __DEVICE__ striding_ptr( const striding_ptr& src ) : ptr(src.ptr), stride(src.stride) {}
99 
100  template<typename U,typename Q>
101  __HOST__ __DEVICE__ striding_ptr( const striding_ptr<U,Q>& src ) : ptr(src.ptr), stride(src.stride) {}
102 
103  #ifdef ECUDA_CPP11_AVAILABLE
104  __HOST__ __DEVICE__ striding_ptr( striding_ptr&& src ) : ptr(std::move(src.ptr)), stride(std::move(src.stride)) {}
105 
106  __HOST__ __DEVICE__ striding_ptr& operator=( striding_ptr&& src )
107  {
108  ptr = std::move(src.ptr);
109  stride = std::move(src.stride);
110  return *this;
111  }
112  #endif
113 
114  __HOST__ __DEVICE__ inline pointer get() const { return ptr; }
115 
116  __HOST__ __DEVICE__ inline size_type get_stride() const { return stride; }
117 
118  __DEVICE__ inline reference operator*() { return *ptr; }
119  __DEVICE__ inline const_reference operator*() const { return *ptr; }
120  __DEVICE__ inline pointer operator->() const { return ptr; }
121  __DEVICE__ inline reference operator[]( std::size_t i ) { return striding_ptr(*this).operator+=(i).operator*(); }
122  __DEVICE__ inline const_reference operator[]( std::size_t i ) const { return striding_ptr(*this).operator+=(i).operator*(); }
123 
124  __HOST__ __DEVICE__ inline striding_ptr& operator++() { ptr += stride; return *this; }
125  __HOST__ __DEVICE__ inline striding_ptr& operator--() { ptr -= stride; return *this; }
126  __HOST__ __DEVICE__ inline striding_ptr operator++( int ) { striding_ptr tmp(*this); ++(*this); return tmp; }
127  __HOST__ __DEVICE__ inline striding_ptr operator--( int ) { striding_ptr tmp(*this); --(*this); return tmp; }
128 
129  __HOST__ __DEVICE__ inline striding_ptr& operator+=( int x ) { ptr += x*stride; return *this; }
130  __HOST__ __DEVICE__ inline striding_ptr& operator-=( int x ) { ptr -= x*stride; return *this; }
131 
132  __HOST__ __DEVICE__ inline striding_ptr operator+( int x ) const { striding_ptr tmp(*this); tmp += x; return tmp; }
133  __HOST__ __DEVICE__ inline striding_ptr operator-( int x ) const { striding_ptr tmp(*this); tmp -= x; return tmp; }
134 
135  template<typename T2,typename P2> __HOST__ __DEVICE__ inline bool operator==( const striding_ptr<T2,P2>& other ) const { return ptr == other.ptr; }
136  template<typename T2,typename P2> __HOST__ __DEVICE__ inline bool operator!=( const striding_ptr<T2,P2>& other ) const { return ptr != other.ptr; }
137  template<typename T2,typename P2> __HOST__ __DEVICE__ inline bool operator< ( const striding_ptr<T2,P2>& other ) const { return ptr < other.ptr; }
138  template<typename T2,typename P2> __HOST__ __DEVICE__ inline bool operator> ( const striding_ptr<T2,P2>& other ) const { return ptr > other.ptr; }
139  template<typename T2,typename P2> __HOST__ __DEVICE__ inline bool operator<=( const striding_ptr<T2,P2>& other ) const { return ptr <= other.ptr; }
140  template<typename T2,typename P2> __HOST__ __DEVICE__ inline bool operator>=( const striding_ptr<T2,P2>& other ) const { return ptr >= other.ptr; }
141 
142  template<typename U,typename V>
143  friend std::basic_ostream<U,V>& operator<<( std::basic_ostream<U,V>& out, const striding_ptr& ptr )
144  {
145  out << "striding_ptr(ptr=" << ptr.ptr << ";stride=" << ptr.stride << ")";
146  return out;
147  }
148 
149 };
150 
151 } // namespace ecuda
152 
153 #endif
std::ptrdiff_t difference_type
__DEVICE__ const_reference operator*() const
__HOST__ __DEVICE__ striding_ptr & operator+=(int x)
__HOST__ __DEVICE__ bool operator!=(const striding_ptr< T2, P2 > &other) const
__HOST__ __DEVICE__ striding_ptr operator++(int)
__HOST__ __DEVICE__ striding_ptr(const striding_ptr &src)
#define __HOST__
Definition: global.hpp:150
__HOST__ __DEVICE__ striding_ptr & operator++()
__HOST__ __DEVICE__ size_type get_stride() const
__HOST__ __DEVICE__ striding_ptr & operator--()
__DEVICE__ const_reference operator[](std::size_t i) const
std::size_t size_type
__HOST__ __DEVICE__ bool operator>(const striding_ptr< T2, P2 > &other) const
__HOST__ __DEVICE__ striding_ptr(const striding_ptr< U, Q > &src)
__HOST__ __DEVICE__ bool operator>=(const striding_ptr< T2, P2 > &other) const
__HOST__ __DEVICE__ striding_ptr(pointer ptr=pointer(), size_type stride=0)
A specialized pointer to striding memory.
const T & const_reference
__DEVICE__ reference operator*()
__HOST__ __DEVICE__ striding_ptr operator-(int x) const
__DEVICE__ reference operator[](std::size_t i)
__HOST__ __DEVICE__ striding_ptr operator+(int x) const
__DEVICE__ pointer operator->() const
__HOST__ __DEVICE__ bool operator==(const striding_ptr< T2, P2 > &other) const
__HOST__ __DEVICE__ striding_ptr & operator-=(int x)
#define __DEVICE__
Definition: global.hpp:151
__HOST__ __DEVICE__ striding_ptr operator--(int)
friend class striding_ptr