Extended CUDA Library (ecuda)  2.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
device_contiguous_row_matrix.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_contiguous_row_matrix.hpp
32 //
33 // Lowest-level representation of a contiguous row matrix 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_CONTIGUOUS_ROW_MATRIX_HPP
40 #define ECUDA_MODEL_DEVICE_CONTIGUOUS_ROW_MATRIX_HPP
41 
42 #include "../global.hpp"
43 #include "../memory.hpp"
44 #include "../iterator.hpp"
45 
46 #include "device_sequence.hpp"
48 #include "device_matrix.hpp"
49 
50 namespace ecuda {
51 
53 namespace model {
54 
62 template<typename T,class P>
63 class device_contiguous_row_matrix : public device_matrix< T, padded_ptr<T,P> >
64 {
65 private:
66  typedef device_matrix< T, padded_ptr<T,P> > base_type;
67 
68 public:
69  typedef typename base_type::value_type value_type;
70  typedef typename base_type::pointer pointer;
71  typedef typename base_type::reference reference;
72  typedef typename base_type::const_reference const_reference;
73  typedef typename base_type::size_type size_type;
74  typedef typename base_type::difference_type difference_type;
75 
76  typedef device_contiguous_block_iterator<value_type, typename make_unmanaged<P>::type > iterator; // this iterator's 2nd template param is also padded_ptr<T,P>
77  typedef device_contiguous_block_iterator<const value_type,typename make_unmanaged_const<P>::type> const_iterator;
78 
79  typedef reverse_device_iterator<iterator > reverse_iterator;
80  typedef reverse_device_iterator<const_iterator> const_reverse_iterator;
81 
82  typedef device_contiguous_sequence<value_type > row_type;
83  typedef device_contiguous_sequence<const value_type> const_row_type;
84  typedef device_sequence< value_type, striding_padded_ptr< value_type, typename ecuda::add_pointer<value_type>::type > > column_type;
85  typedef device_sequence< const value_type, striding_padded_ptr< const value_type, typename ecuda::add_pointer<const value_type>::type > > const_column_type;
86 
87 public:
88  __HOST__ __DEVICE__ device_contiguous_row_matrix( pointer ptr = pointer(), size_type rows = 0, size_type columns = 0 ) : base_type(ptr,rows,columns) {}
89 
90  __HOST__ __DEVICE__ device_contiguous_row_matrix( const device_contiguous_row_matrix& src ) : base_type(src) {}
91 
92  template<typename U,class PointerType2> __HOST__ __DEVICE__ device_contiguous_row_matrix( const device_contiguous_row_matrix<U,PointerType2>& src ) : base_type(src) {}
93 
94  __HOST__ device_contiguous_row_matrix& operator=( const device_contiguous_row_matrix& src )
95  {
96  base_type::operator=(src);
97  return *this;
98  }
99 
100  #ifdef ECUDA_CPP11_AVAILABLE
101  __HOST__ device_contiguous_row_matrix( device_contiguous_row_matrix&& src ) : base_type(src) {}
102  __HOST__ device_contiguous_row_matrix& operator=( device_contiguous_row_matrix&& src )
103  {
104  base_type::operator=(src);
105  return *this;
106  }
107  #endif
108 
109  __HOST__ __DEVICE__ inline iterator begin() __NOEXCEPT__ { return iterator( unmanaged_cast(base_type::get_pointer()), base_type::number_columns() ); }
110  __HOST__ __DEVICE__ iterator end() __NOEXCEPT__
111  {
112  typedef typename ecuda::make_unmanaged<pointer>::type unmanaged_pointer_type;
113  unmanaged_pointer_type p = unmanaged_cast(base_type::get_pointer());
114  p.skip_bytes( p.get_pitch()*base_type::number_rows() );
115  return iterator( p, base_type::number_columns() );
116  }
117  __HOST__ __DEVICE__ inline const_iterator begin() const __NOEXCEPT__ { return const_iterator( unmanaged_cast(base_type::get_pointer()), base_type::number_columns() ); }
118  __HOST__ __DEVICE__ const_iterator end() const __NOEXCEPT__
119  {
120  typedef typename ecuda::make_unmanaged_const<pointer>::type unmanaged_pointer_type;
121  unmanaged_pointer_type p = unmanaged_cast(base_type::get_pointer());
122  p.skip_bytes( p.get_pitch()*base_type::number_rows() );
123  return const_iterator( p, base_type::number_columns() );
124  }
125  #ifdef ECUDA_CPP11_AVAILABLE
126  __HOST__ __DEVICE__ inline const_iterator cbegin() const __NOEXCEPT__ { return const_iterator( unmanaged_cast(base_type::get_pointer()) ); }
127  __HOST__ __DEVICE__ const_iterator cend() const __NOEXCEPT__
128  {
129  typedef typename ecuda::make_unmanaged_const<pointer>::type unmanaged_pointer_type;
130  unmanaged_pointer_type p = unmanaged_cast(base_type::get_pointer());
131  p.skip_bytes( p.get_pitch()*base_type::number_rows() );
132  return const_iterator( p, base_type::number_columns() );
133  }
134  // return const_iterator( unmanaged_cast(base_type::get_pointer())+base_type::size() ); }
135  #endif
136 
137  __HOST__ __DEVICE__ inline reverse_iterator rbegin() __NOEXCEPT__ { return reverse_iterator(end()); }
138  __HOST__ __DEVICE__ inline reverse_iterator rend() __NOEXCEPT__ { return reverse_iterator(begin()); }
139  __HOST__ __DEVICE__ inline const_reverse_iterator rbegin() const __NOEXCEPT__ { return const_reverse_iterator(end()); }
140  __HOST__ __DEVICE__ inline const_reverse_iterator rend() const __NOEXCEPT__ { return const_reverse_iterator(begin()); }
141  #ifdef ECUDA_CPP11_AVAILABLE
142  __HOST__ __DEVICE__ inline const_reverse_iterator crbegin() const __NOEXCEPT__ { return const_reverse_iterator(end()); }
143  __HOST__ __DEVICE__ inline const_reverse_iterator crend() const __NOEXCEPT__ { return const_reverse_iterator(begin()); }
144  #endif
145 
146  __HOST__ __DEVICE__ inline row_type get_row( const size_type row )
147  {
148  typedef typename make_unmanaged<pointer>::type unmanaged_pointer;
149  unmanaged_pointer up = unmanaged_cast( base_type::get_pointer() ); // strip any mgmt by smart pointer
150  up.skip_bytes( row*up.get_pitch() ); // advance to row start
151  return row_type( naked_cast<typename ecuda::add_pointer<value_type>::type>( up ), base_type::number_columns() ); // provide naked pointer since row is contiguous
152  }
153 
154  __HOST__ __DEVICE__ inline const_row_type get_row( const size_type row ) const
155  {
156  typedef typename make_unmanaged_const<pointer>::type unmanaged_pointer;
157  unmanaged_pointer up = unmanaged_cast( base_type::get_pointer() ); // strip any mgmt by smart pointer
158  up.skip_bytes( row*up.get_pitch() );
159  return const_row_type( naked_cast<typename ecuda::add_pointer<const value_type>::type>( up ), base_type::number_columns() ); // provide naked pointer since row is contiguous
160  }
161 
162  __HOST__ __DEVICE__ inline column_type get_column( const size_type column )
163  {
164  typedef typename ecuda::add_pointer<value_type>::type naked_pointer_type;
165  naked_pointer_type np = naked_cast<naked_pointer_type>( unmanaged_cast(base_type::get_pointer()) + column );
166  return column_type( striding_padded_ptr<value_type,naked_pointer_type>( np, base_type::get_pointer().get_pitch() ), base_type::number_rows() );
167  }
168 
169  __HOST__ __DEVICE__ inline const_column_type get_column( const size_type column ) const
170  {
171  typedef typename ecuda::add_pointer<const value_type>::type naked_pointer_type;
172  naked_pointer_type np = naked_cast<naked_pointer_type>( unmanaged_cast(base_type::get_pointer()) + column );
173  return const_column_type( striding_padded_ptr<const value_type,naked_pointer_type>( np, base_type::get_pointer().get_pitch() ), base_type::number_rows() );
174  }
175 
176  __HOST__ __DEVICE__ inline row_type operator[]( const size_type row ) { return get_row(row); }
177  __HOST__ __DEVICE__ inline const_row_type operator[]( const size_type row ) const { return get_row(row); }
178 
179  __DEVICE__ inline reference at( const size_type row, const size_type column )
180  {
181  typename make_unmanaged<pointer>::type up = unmanaged_cast(base_type::get_pointer());
182  up.skip_bytes( up.get_pitch()*row );
183  up.operator+=( column );
184  return *naked_cast<typename ecuda::add_pointer<value_type>::type>(up);
185  }
186 
187  __DEVICE__ inline const_reference at( const size_type row, const size_type column ) const
188  {
189  typename make_unmanaged_const<pointer>::type up = unmanaged_cast(base_type::get_pointer());
190  up.skip_bytes( up.get_pitch()*row );
191  up.operator+=( column );
192  return *up;
193  //return *naked_cast<typename ecuda::add_pointer<const value_type>::type>(up);
194  }
195 
196 };
197 
198 } // namespace model
200 
201 } // namespace ecuda
202 
203 #endif
#define __NOEXCEPT__
Definition: global.hpp:140
#define __HOST__
Definition: global.hpp:150
#define __DEVICE__
Definition: global.hpp:151