Extended CUDA Library (ecuda)  2.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
device_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_matrix.hpp
32 //
33 // Lowest-level representation of a 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_MATRIX_HPP
40 #define ECUDA_MODEL_DEVICE_MATRIX_HPP
41 
42 #include "../global.hpp"
43 #include "../memory.hpp"
44 #include "../iterator.hpp"
45 
46 #include "device_sequence.hpp"
47 
48 namespace ecuda {
49 
51 namespace model {
52 
59 template<typename T,class P>
60 class device_matrix : public device_sequence<T,P>
61 {
62 private:
63  typedef device_sequence<T,P> base_type;
64 
65 public:
66  typedef typename base_type::value_type value_type;
67  typedef typename base_type::pointer pointer;
68  typedef typename base_type::reference reference;
69  typedef typename base_type::const_reference const_reference;
70  typedef typename base_type::size_type size_type;
71  typedef typename base_type::difference_type difference_type;
72 
73  typedef typename base_type::iterator iterator;
74  typedef typename base_type::const_iterator const_iterator;
75  typedef typename base_type::reverse_iterator reverse_iterator;
76  typedef typename base_type::const_reverse_iterator const_reverse_iterator;
77 
78  typedef device_sequence< value_type, typename make_unmanaged<pointer>::type > row_type;
79  typedef device_sequence< const value_type, typename make_unmanaged_const<pointer>::type > const_row_type;
80  typedef device_sequence< value_type, striding_ptr<value_type,typename make_unmanaged<pointer>::type> > column_type;
81  typedef device_sequence< const value_type, striding_ptr<const value_type,typename make_unmanaged_const<pointer>::type> > const_column_type;
82 
83 private:
84  size_type rows;
85 
86 public:
87  __HOST__ __DEVICE__ device_matrix( pointer ptr = pointer(), size_type rows = 0, size_type columns = 0 ) : base_type(ptr,rows*columns), rows(rows) {}
88  __HOST__ __DEVICE__ device_matrix( const device_matrix& src ) : base_type(src), rows(src.rows) {}
89  __HOST__ device_matrix& operator=( const device_matrix& src ) {
90  base_type::operator=(src);
91  rows = src.rows;
92  return *this;
93  }
94  #ifdef ECUDA_CPP11_AVAILABLE
95  __HOST__ device_matrix( device_matrix&& src ) : base_type(src), rows(std::move(src.rows)) {}
96  __HOST__ device_matrix& operator=( device_matrix&& src )
97  {
98  base_type::operator=( src );
99  rows = std::move(src.rows);
100  return *this;
101  }
102  #endif
103 
104  __HOST__ __DEVICE__ inline size_type number_rows() const __NOEXCEPT__ { return rows; }
105  __HOST__ __DEVICE__ inline size_type number_columns() const __NOEXCEPT__ { return base_type::size()/rows; }
106 
107  __HOST__ __DEVICE__ inline row_type get_row( const size_type row )
108  {
109  return row_type(
110  unmanaged_cast(base_type::get_pointer())+(row*number_columns()),
111  number_columns()
112  );
113  }
114 
115  __HOST__ __DEVICE__ inline const_row_type get_row( const size_type row ) const
116  {
117  return const_row_type(
118  unmanaged_cast(base_type::get_pointer())+(row*number_columns()),
119  number_columns()
120  );
121  }
122 
123  __HOST__ __DEVICE__ inline column_type get_column( const size_type column )
124  {
125  return column_type(
126  striding_ptr<value_type,typename make_unmanaged<pointer>::type>(
127  unmanaged_cast(base_type::get_pointer())+column, // move to top of column
128  number_columns() // stride by number of columns
129  ),
130  number_rows()
131  );
132  }
133 
134  __HOST__ __DEVICE__ inline const_column_type get_column( const size_type column ) const {
135  return const_column_type(
136  striding_ptr<const value_type,typename make_unmanaged_const<pointer>::type>(
137  unmanaged_cast(base_type::get_pointer())+column, // move to top of column
138  number_columns() // stride by number of columns
139  ),
140  number_rows()
141  );
142  }
143 
144  __HOST__ __DEVICE__ inline row_type operator[]( const size_type row ) { return get_row(row); }
145  __HOST__ __DEVICE__ inline const_row_type operator[]( const size_type row ) const { return get_row(row); }
146 
147  __HOST__ __DEVICE__ void swap( device_matrix& other )
148  {
149  base_type::swap( other );
150  ::ecuda::swap( rows, other.rows );
151  }
152 
153 };
154 
155 } // namespace model
157 
158 } // namespace ecuda
159 
160 #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