GPUMLib  0.2.2
GPU Machine Learning Library
CudaMatrix.h
1 /*
2  Noel Lopes is a Professor at the Polytechnic of Guarda, Portugal
3  Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Noel de Jesus Mendonça Lopes
4 
5  This file is part of GPUMLib.
6 
7  GPUMLib is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef GPUMLib_CudaMatrix_h
22 #define GPUMLib_CudaMatrix_h
23 
24 #include "DeviceMatrix.h"
25 
26 namespace GPUMLib {
27 
30 
32 template <class Type> class CudaMatrix {
33  private:
34  HostMatrix<Type> h;
35  DeviceMatrix<Type> d;
36 
37  void MoveFrom(BaseMatrix<Type> & other) {
38  h.MoveFrom(other.h);
39  d.MoveFrom(other.d);
40  }
41 
42  public:
45  CudaMatrix(StoringOrder storingOrder = RowMajor) : h(storingOrder), d(storingOrder) { }
46 
51  CudaMatrix(int rows, int columns, StoringOrder storingOrder = RowMajor) : h(rows, columns, storingOrder), d(rows, columns, storingOrder) { }
52 
55  CudaMatrix(const CudaMatrix<Type> & other) : h(other.h), d(other.d) { }
56 
59  CudaMatrix(const HostMatrix<Type> & other) : h(other), d(other) { }
60 
63  CudaMatrix(const DeviceMatrix<Type> & other) : h(other), d(other) { }
64 
67  CudaMatrix(CudaMatrix<Type> && temporaryMatrix) {
68  MoveFrom(temporaryMatrix);
69  }
70 
77  h = other;
78  d = other;
79 
80  return *this;
81  }
82 
89  h = other;
90  d = other;
91 
92  return *this;
93  }
94 
101  h = other.h;
102  d = other.d;
103 
104  return *this;
105  }
106 
112  CudaMatrix<Type> & operator = (const CudaMatrix<Type> && temporaryMatrix) {
113  if (this != &temporaryMatrix) {
114  Dispose();
115  MoveFrom(temporaryMatrix);
116  }
117 
118  return *this;
119  }
120 
124  void UpdateDevice() {
125  d = h;
126  }
127 
131  void UpdateHost() {
132  h = d;
133  }
134 
139  Type * HostPointer() const {
140  return h.Pointer();
141  }
142 
146  Type * DevicePointer() const {
147  return d.Pointer();
148  }
149 
154  return d;
155  }
156 
161  return h;
162  }
163 
168  Type & operator()(int row, int column) {
169  return h(row, column);
170  }
171 
176  Type operator()(int row, int column) const {
177  return h(row, column);
178  }
179 
182  int Rows() const {
183  return h.Rows();
184  }
185 
188  int Columns() const {
189  return h.Columns();
190  }
191 
194  int Elements() const {
195  return h.Elements();
196  }
197 
200  bool IsRowMajor() const {
201  return h.IsRowMajor();
202  }
203 
208  h.ReplaceByTranspose();
209  d.ReplaceByTranspose();
210  }
211 
217  CudaMatrix<Type> transpose(h.Transpose());
218 
219  return transpose;
220  }
221 
226  int ResizeWithoutPreservingData(int rows, int columns) {
227  int he = h.ResizeWithoutPreservingData(rows, columns);
228  int de = d.ResizeWithoutPreservingData(rows, columns);
229 
230  return (he > de) ? de : he;
231  }
232 
234  void Dispose() {
235  d.Dispose();
236  h.Dispose();
237  }
238 
240  void DisposeDevice() {
241  UpdateHost();
242  d.Dispose();
243  }
244 
246  void DisposeHost() {
247  UpdateDevice();
248  h.Dispose();
249  }
250 };
251 
253 
254 }
255 
256 #endif
int Rows() const
Definition: CudaMatrix.h:182
void DisposeHost()
Updates the device information and disposes the host matrix.
Definition: CudaMatrix.h:246
Type & operator()(int row, int column)
Definition: CudaMatrix.h:168
int Columns() const
Definition: CudaMatrix.h:188
bool IsRowMajor() const
Definition: CudaMatrix.h:200
CudaMatrix< Type > Transpose()
Definition: CudaMatrix.h:216
StoringOrder
Defines the methods for storing information in the matrix (either row-major or column-major).
Definition: BaseMatrix.h:30
CudaMatrix(StoringOrder storingOrder=RowMajor)
Definition: CudaMatrix.h:45
Create a matrix of any type, that automatically manages the memory used to hold its elements (data wi...
Definition: BaseMatrix.h:35
DeviceMatrix< Type > & GetDeviceMatrix()
Definition: CudaMatrix.h:153
Type operator()(int row, int column) const
Definition: CudaMatrix.h:176
CudaMatrix(const CudaMatrix< Type > &other)
Definition: CudaMatrix.h:55
HostMatrix< Type > & GetHostMatrix()
Definition: CudaMatrix.h:160
Create a matrix of any type, on the device, that automatically manages the memory used to hold its el...
Definition: DeviceMatrix.h:35
void DisposeDevice()
//! Updates the host information and disposes the device matrix
Definition: CudaMatrix.h:240
CudaMatrix(int rows, int columns, StoringOrder storingOrder=RowMajor)
Definition: CudaMatrix.h:51
CudaMatrix< Type > & operator=(const HostMatrix< Type > &other)
Definition: CudaMatrix.h:76
void ReplaceByTranspose()
Definition: CudaMatrix.h:207
Type * HostPointer() const
Definition: CudaMatrix.h:139
CudaMatrix(const HostMatrix< Type > &other)
Definition: CudaMatrix.h:59
void Dispose()
Disposes the matrix.
Definition: CudaMatrix.h:234
int Elements() const
Definition: CudaMatrix.h:194
Type * DevicePointer() const
Definition: CudaMatrix.h:146
int ResizeWithoutPreservingData(int rows, int columns)
Definition: CudaMatrix.h:226
Create a matrix of any type, on the host, that automatically manages the memory used to hold its elem...
Definition: HostMatrix.h:41
CudaMatrix(CudaMatrix< Type > &&temporaryMatrix)
Definition: CudaMatrix.h:67
CudaMatrix(const DeviceMatrix< Type > &other)
Definition: CudaMatrix.h:63