GPUMLib  0.2.2
GPU Machine Learning Library
HostMatrix.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_HostMatrix_h
22 #define GPUMLib_HostMatrix_h
23 
24 #include <new>
25 #include <assert.h>
26 #include <cuda_runtime.h>
27 #include <string.h>
28 
29 #include "BaseMatrix.h"
30 
31 using namespace std;
32 
33 namespace GPUMLib {
34 
37 
38 template <class Type> class DeviceMatrix;
39 
41 template <class Type> class HostMatrix : public BaseMatrix<Type> {
42  private :
43  void Alloc(int rows, int columns) {
44  assert(rows > 0 && columns > 0);
45 
46  int elements = rows * columns;
47  this->matrixData = new (nothrow) Type [elements];
48  if (this->matrixData != nullptr) {
49  this->rows = rows;
50  this->columns = columns;
51  } else {
52  this->Init();
53  }
54  }
55 
56  void Assign(const HostMatrix<Type> & other) {
57  this->storingOrder = other.storingOrder;
58  int elements = this->ResizeWithoutPreservingData(other.rows, other.columns);
59  if (elements > 0) memcpy(this->matrixData, other.matrixData, elements * sizeof(Type));
60  }
61 
62  void Assign(const DeviceMatrix<Type> & other) {
63  this->storingOrder = (other.IsRowMajor()) ? RowMajor : ColumnMajor;
64  int elements = this->ResizeWithoutPreservingData(other.Rows(), other.Columns());
65  if (elements > 0) cudaMemcpy(this->matrixData, other.Pointer(), elements * sizeof(Type), cudaMemcpyDeviceToHost);
66  }
67 
68  public :
69  void Dispose() {
70  if (this->matrixData != nullptr) delete [] this->matrixData;
71  this->Init();
72  }
73 
76  HostMatrix(StoringOrder storingOrder = RowMajor) : BaseMatrix<Type>(storingOrder) {}
77 
82  HostMatrix(int rows, int columns, StoringOrder storingOrder = RowMajor) : BaseMatrix<Type>(storingOrder) {
83  assert(rows > 0 && columns > 0);
84  this->ResizeWithoutPreservingData(rows, columns);
85  }
86 
89  HostMatrix(const HostMatrix<Type> & other) {
90  Assign(other);
91  }
92 
95  HostMatrix(const DeviceMatrix<Type> & other) {
96  Assign(other);
97  }
98 
101  HostMatrix(HostMatrix<Type> && temporaryMatrix) {
102  this->MoveFrom(temporaryMatrix);
103  }
104 
107  Dispose();
108  }
109 
115  HostMatrix<Type> & operator = (const HostMatrix<Type> & other) {
116  Assign(other);
117  return *this;
118  }
119 
125  HostMatrix<Type> & operator = (const DeviceMatrix<Type> & other) {
126  Assign(other);
127  return *this;
128  }
129 
133  HostMatrix<Type> & operator = (HostMatrix<Type> && temporaryMatrix) {
134  if (this != &temporaryMatrix) {
135  Dispose();
136  this->MoveFrom(temporaryMatrix);
137  }
138 
139  return *this;
140  }
141 
147  if (this != &other) {
148  Dispose();
149  this->matrixData = other.matrixData;
150  this->rows = other.rows;
151  this->columns = other.columns;
152  this->storingOrder = other.storingOrder;
153 
154  other.Init();
155  }
156  }
157 
163  HostMatrix<Type> transpose(*this);
164  transpose.ReplaceByTranspose();
165 
166  return transpose;
167  }
168 
173  Type & operator()(int row, int column) {
174  assert(row >= 0 && row < this->rows && column >= 0 && column < this->columns);
175 
176  int index = (this->IsRowMajor()) ? row * this->columns + column : column * this->rows + row;
177 
178  return this->matrixData[index];
179  }
180 
185  Type operator()(int row, int column) const {
186  assert(row >= 0 && row < this->rows && column >= 0 && column < this->columns);
187 
188  int index = (this->IsRowMajor()) ? row * this->columns + column : column * this->rows + row;
189 
190  return this->matrixData[index];
191  }
192 };
193 
195 
196 }
197 
198 #endif
HostMatrix(const HostMatrix< Type > &other)
Definition: HostMatrix.h:89
void ReplaceByTranspose()
Definition: BaseMatrix.h:107
~HostMatrix()
Destructor.
Definition: HostMatrix.h:106
HostMatrix< Type > Transpose()
Definition: HostMatrix.h:162
HostMatrix(int rows, int columns, StoringOrder storingOrder=RowMajor)
Definition: HostMatrix.h:82
void Dispose()
Disposes the matrix.
Definition: HostMatrix.h:69
Base class for HostMatrix and DeviceMatrix classes (Matrix base class)
Definition: BaseMatrix.h:38
StoringOrder
Defines the methods for storing information in the matrix (either row-major or column-major).
Definition: BaseMatrix.h:30
Create a matrix of any type, on the device, that automatically manages the memory used to hold its el...
Definition: DeviceMatrix.h:35
void TransferOwnerShipFrom(HostMatrix< Type > &other)
Definition: HostMatrix.h:146
Type * Pointer() const
Definition: BaseMatrix.h:88
int Columns() const
Definition: BaseMatrix.h:80
bool IsRowMajor() const
Definition: BaseMatrix.h:100
HostMatrix(const DeviceMatrix< Type > &other)
Definition: HostMatrix.h:95
HostMatrix(HostMatrix< Type > &&temporaryMatrix)
Definition: HostMatrix.h:101
Type & operator()(int row, int column)
Definition: HostMatrix.h:173
int Rows() const
Definition: BaseMatrix.h:74
Type operator()(int row, int column) const
Definition: HostMatrix.h:185
Create a matrix of any type, on the host, that automatically manages the memory used to hold its elem...
Definition: HostMatrix.h:41
HostMatrix(StoringOrder storingOrder=RowMajor)
Definition: HostMatrix.h:76