Extended CUDA Library (ecuda)  2.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
device_sequence.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 // model/device_sequence.hpp
32 //
33 // Lowest-level representation of a sequence in device memory.
34 //
35 // Author: Scott D. Zuyderduyn, Ph.D. (scott.zuyderduyn@utoronto.ca)
36 //----------------------------------------------------------------------------
37 
38 #pragma once
39 #ifndef ECUDA_MODEL_DEVICE_SEQUENCE_HPP
40 #define ECUDA_MODEL_DEVICE_SEQUENCE_HPP
41 
42 #include "../global.hpp"
43 #include "../memory.hpp"
44 #include "../iterator.hpp"
45 
46 namespace ecuda {
47 
49 namespace model {
50 
65 template<typename T,class P>
66 class device_sequence
67 {
68 
69 public:
70  typedef T value_type;
71  typedef P pointer;
72  typedef typename ecuda::add_lvalue_reference<T>::type reference;
73  typedef typename ecuda::add_lvalue_reference<const T>::type const_reference;
74  typedef std::size_t size_type;
75  typedef std::ptrdiff_t difference_type;
76 
77  typedef device_iterator< value_type,typename make_unmanaged<pointer>::type > iterator;
78  typedef device_iterator<const value_type,typename make_unmanaged_const<pointer>::type> const_iterator;
79 
80  typedef reverse_device_iterator<iterator > reverse_iterator;
81  typedef reverse_device_iterator<const_iterator> const_reverse_iterator;
82 
83 private:
84  pointer ptr;
85  size_type length;
86 
87  template<typename U,class Q> friend class device_sequence;
88 
89 protected:
90  __HOST__ __DEVICE__ inline pointer& get_pointer() __NOEXCEPT__ { return ptr; }
91  __HOST__ __DEVICE__ inline const pointer& get_pointer() const __NOEXCEPT__ { return ptr; }
92 
93 public:
94  __HOST__ __DEVICE__ device_sequence( pointer ptr = pointer(), size_type length = 0 ) : ptr(ptr), length(length) {}
95 
96  __HOST__ __DEVICE__ device_sequence( const device_sequence& src ) : ptr(src.ptr), length(src.length) {}
97 
98  template<typename U,class Q> __HOST__ __DEVICE__ device_sequence( const device_sequence<U,Q>& src ) : ptr(src.ptr), length(src.length) {}
99 
100  __HOST__ device_sequence& operator=( const device_sequence& src )
101  {
102  ptr = src.ptr;
103  length = src.length;
104  return *this;
105  }
106 
107  #ifdef ECUDA_CPP11_AVAILABLE
108  __HOST__ device_sequence( device_sequence&& src ) : ptr(std::move(src.ptr)), length(std::move(src.length)) {}
109  __HOST__ device_sequence& operator=( device_sequence&& src )
110  {
111  ptr = std::move(src.ptr);
112  length = std::move(src.length);
113  return *this;
114  }
115  #endif
116 
117  __HOST__ __DEVICE__ inline size_type size() const __NOEXCEPT__ { return length; }
118 
119  __DEVICE__ inline reference operator[]( const size_type x ) { return *(unmanaged_cast( ptr ) + x); }
120  __DEVICE__ inline const_reference operator[]( const size_type x ) const { return *(unmanaged_cast( ptr ) + x); }
121 
122  __HOST__ __DEVICE__ inline iterator begin() __NOEXCEPT__ { return iterator( unmanaged_cast(ptr) ); }
123  __HOST__ __DEVICE__ inline iterator end() __NOEXCEPT__ { return iterator( unmanaged_cast(ptr) + size() ); }
124  __HOST__ __DEVICE__ inline const_iterator begin() const __NOEXCEPT__ { return const_iterator( unmanaged_cast(ptr) ); }
125  __HOST__ __DEVICE__ inline const_iterator end() const __NOEXCEPT__ { return const_iterator( unmanaged_cast(ptr) + size() ); }
126  #ifdef ECUDA_CPP11_AVAILABLE
127  __HOST__ __DEVICE__ inline const_iterator cbegin() const __NOEXCEPT__ { return const_iterator( unmanaged_cast(ptr) ); }
128  __HOST__ __DEVICE__ inline const_iterator cend() const __NOEXCEPT__ { return const_iterator( unmanaged_cast(ptr) + size() ); }
129  #endif
130 
131  __HOST__ __DEVICE__ inline reverse_iterator rbegin() __NOEXCEPT__ { return reverse_iterator(end()); }
132  __HOST__ __DEVICE__ inline reverse_iterator rend() __NOEXCEPT__ { return reverse_iterator(begin()); }
133  __HOST__ __DEVICE__ inline const_reverse_iterator rbegin() const __NOEXCEPT__ { return const_reverse_iterator(end()); }
134  __HOST__ __DEVICE__ inline const_reverse_iterator rend() const __NOEXCEPT__ { return const_reverse_iterator(begin()); }
135  #ifdef ECUDA_CPP11_AVAILABLE
136  __HOST__ __DEVICE__ inline const_reverse_iterator crbegin() const __NOEXCEPT__ { return const_reverse_iterator(end()); }
137  __HOST__ __DEVICE__ inline const_reverse_iterator crend() const __NOEXCEPT__ { return const_reverse_iterator(begin()); }
138  #endif
139 
140  //TODO: think about this - worth considering for user access
141  //__HOST__ __DEVICE__ inline typename ecuda::add_pointer<value_type>::type data() const { return naked_cast<typename ecuda::add_pointer<value_type>::type>( ptr ); }
142  //__HOST__ __DEVICE__ inline pointer data() const { return ptr; }
143 
144  __HOST__ __DEVICE__ void swap( device_sequence& other )
145  {
146  ::ecuda::swap( ptr, other.ptr );
147  ::ecuda::swap( length, other.length );
148  }
149 
150 };
151 
152 } // namespace model
154 
155 } // namespace ecuda
156 
157 #endif
#define __NOEXCEPT__
Definition: global.hpp:140
#define __HOST__
Definition: global.hpp:150
__HOST__ __DEVICE__ void swap(T &a, T &b) __NOEXCEPT__
Definition: algorithm.hpp:54
#define __DEVICE__
Definition: global.hpp:151