Extended CUDA Library (ecuda)  2.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
device_fixed_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_fixed_sequence.hpp
32 //
33 // Lowest-level representation of a fixed-size 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_FIXED_SEQUENCE_HPP
40 #define ECUDA_MODEL_DEVICE_FIXED_SEQUENCE_HPP
41 
42 #include "../global.hpp"
43 #include "../memory.hpp"
44 #include "../iterator.hpp"
45 
46 namespace ecuda {
47 
49 namespace model {
50 
58 template<typename T,std::size_t N,class P=typename ecuda::add_pointer<T>::type>
59 class device_fixed_sequence
60 {
61 
62 public:
63  typedef T value_type;
64  typedef P pointer;
65  typedef typename ecuda::add_lvalue_reference<T>::type reference;
66  typedef typename ecuda::add_lvalue_reference<const T>::type const_reference;
67  typedef std::size_t size_type;
68  typedef std::ptrdiff_t difference_type;
69 
70  typedef device_contiguous_iterator<value_type > iterator;
71  typedef device_contiguous_iterator<const value_type> const_iterator;
72 
73  typedef reverse_device_iterator<iterator > reverse_iterator;
74  typedef reverse_device_iterator<const_iterator> const_reverse_iterator;
75 
76 private:
77  pointer ptr;
78 
79 protected:
80  __HOST__ __DEVICE__ inline pointer& get_pointer() { return ptr; }
81  __HOST__ __DEVICE__ inline const pointer& get_pointer() const { return ptr; }
82 
83 public:
84  __HOST__ __DEVICE__ device_fixed_sequence( pointer ptr = pointer() ) : ptr(ptr) {}
85  __HOST__ __DEVICE__ device_fixed_sequence( const device_fixed_sequence& src ) : ptr(src.ptr) {}
86  __HOST__ device_fixed_sequence& operator=( const device_fixed_sequence& src )
87  {
88  ptr = src.ptr;
89  return *this;
90  }
91  #ifdef ECUDA_CPP11_AVAILABLE
92  __HOST__ device_fixed_sequence( device_fixed_sequence&& src ) { ecuda::swap( ptr, src.ptr ); }
93  __HOST__ device_fixed_sequence& operator=( device_fixed_sequence&& src )
94  {
95  ecuda::swap( ptr, src.ptr );
96  return *this;
97  }
98  #endif
99 
100  __HOST__ __DEVICE__ inline __CONSTEXPR__ size_type size() const { return N; }
101 
102  __DEVICE__ inline reference operator[]( const size_type x ) { return *(unmanaged_cast( ptr ) + x); }
103  __DEVICE__ inline const_reference operator[]( const size_type x ) const { return *(unmanaged_cast( ptr ) + x); }
104 
105  __HOST__ __DEVICE__ inline iterator begin() { return iterator( unmanaged_cast(ptr) ); }
106  __HOST__ __DEVICE__ inline iterator end() { return iterator( unmanaged_cast(ptr) + N ); }
107  __HOST__ __DEVICE__ inline const_iterator begin() const { return const_iterator( unmanaged_cast(ptr) ); }
108  __HOST__ __DEVICE__ inline const_iterator end() const { return const_iterator( unmanaged_cast(ptr) + N ); }
109  #ifdef ECUDA_CPP11_AVAILABLE
110  __HOST__ __DEVICE__ inline const_iterator cbegin() const { return const_iterator( unmanaged_cast(ptr) ); }
111  __HOST__ __DEVICE__ inline const_iterator cend() const { return const_iterator( unmanaged_cast(ptr) + N ); }
112  #endif
113 
114  __HOST__ __DEVICE__ inline reverse_iterator rbegin() { return reverse_iterator(end()); }
115  __HOST__ __DEVICE__ inline reverse_iterator rend() { return reverse_iterator(begin()); }
116  __HOST__ __DEVICE__ inline const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
117  __HOST__ __DEVICE__ inline const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
118  #ifdef ECUDA_CPP11_AVAILABLE
119  __HOST__ __DEVICE__ inline const_reverse_iterator crbegin() const { return const_reverse_iterator(end()); }
120  __HOST__ __DEVICE__ inline const_reverse_iterator crend() const { return const_reverse_iterator(begin()); }
121  #endif
122 
123  __HOST__ __DEVICE__ inline void swap( device_fixed_sequence& other ) { ecuda::swap( ptr, other.ptr ); }
124 
125 };
126 
127 } // namespace model
129 
130 } // namespace ecuda
131 
132 #endif
#define __CONSTEXPR__
Definition: global.hpp:141
#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