GPUMLib  0.2.2
GPU Machine Learning Library
DeviceMatrix.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_DeviceMatrix_h
22 #define GPUMLib_DeviceMatrix_h
23 
24 #include <cublas.h>
25 
26 #include "../common/CudaDefinitions.h"
27 #include "HostMatrix.h"
28 
29 namespace GPUMLib {
30 
33 
35 template <class Type> class DeviceMatrix : public BaseMatrix<Type> {
36  private:
37  void Alloc(int rows, int columns) {
38  assert(rows > 0 && columns > 0);
39 
40  int elements = rows * columns;
41 
42  if (cudaMalloc((void **) &(this->matrixData), elements * sizeof(Type)) == cudaSuccess) {
43  this->rows = rows;
44  this->columns = columns;
45  } else {
46  this->Init();
47  }
48  }
49 
50  void Assign(const HostMatrix<Type> & other) {
51  this->storingOrder = (other.IsRowMajor()) ? RowMajor : ColumnMajor;
52  int elements = this->ResizeWithoutPreservingData(other.Rows(), other.Columns());
53  if (elements > 0) cudaMemcpy(this->matrixData, other.Pointer(), elements * sizeof(Type), cudaMemcpyHostToDevice);
54  }
55 
56  void Assign(const DeviceMatrix<Type> & other) {
57  this->storingOrder = other.storingOrder;
58  int elements = this->ResizeWithoutPreservingData(other.rows, other.columns);
59  if (elements > 0) cudaMemcpy(this->matrixData, other.Pointer(), elements * sizeof(Type), cudaMemcpyDeviceToDevice);
60  }
61 
62  public:
63  void Dispose() {
64  if (this->matrixData != nullptr) cudaFree(this->matrixData);
65  this->Init();
66  }
67 
70  DeviceMatrix(StoringOrder storingOrder = RowMajor) : BaseMatrix<Type>(storingOrder) {}
71 
76  DeviceMatrix(int rows, int columns, StoringOrder storingOrder = RowMajor) : BaseMatrix<Type>(storingOrder) {
77  assert(rows > 0 && columns > 0);
78  this->ResizeWithoutPreservingData(rows, columns);
79  }
80 
84  Assign(other);
85  }
86 
89  DeviceMatrix(const HostMatrix<Type> & other) {
90  Assign(other);
91  }
92 
95  DeviceMatrix(DeviceMatrix<Type> && temporaryMatrix) {
96  this->MoveFrom(temporaryMatrix);
97  }
98 
101  Dispose();
102  }
103 
110  Assign(other);
111  return *this;
112  }
113 
120  Assign(other);
121  return *this;
122  }
123 
128  if (this != &temporaryMatrix) {
129  Dispose();
130  this->MoveFrom(temporaryMatrix);
131  }
132 
133  return *this;
134  }
135 
141  if (this != &other) {
142  Dispose();
143  this->matrixData = other.matrixData;
144  this->rows = other.rows;
145  this->columns = other.columns;
146  this->storingOrder = other.storingOrder;
147 
148  other.Init();
149  }
150  }
151 
157  HostMatrix<Type> transpose(*this);
158  transpose.ReplaceByTranspose();
159 
160  return transpose;
161  }
162 
171  assert(C.rows == this->rows && C.columns == this->rows);
172 
173  if (C.IsRowMajor()) {
174  assert(beta == CUDA_VALUE(0.0));
175  C.storingOrder = ColumnMajor;
176  }
177 
178  int ldAB = this->IsRowMajor() ? this->columns : this->rows;
179  cublasSgemm(this->IsRowMajor() ? 'T' : 'N', this->IsRowMajor() ? 'N' : 'T', C.rows, C.columns, this->columns, alpha, this->matrixData, ldAB, this->matrixData, ldAB, beta, C.matrixData, C.rows);
180  }
181 
193  assert(A.columns == B.rows && C.rows == A.rows && C.columns == B.columns);
194 
195  if (C.IsRowMajor()) {
196  assert(beta == CUDA_VALUE(0.0));
197  C.storingOrder = ColumnMajor;
198  }
199 
200  cublasSgemm(A.IsRowMajor() ? 'T' : 'N', B.IsRowMajor() ? 'T' : 'N', C.rows, C.columns, A.columns, alpha, A.matrixData, A.IsRowMajor() ? A.columns : A.rows, B.matrixData, B.IsRowMajor() ? B.columns : B.rows, beta, C.matrixData, C.rows);
201  }
202 };
203 
205 
206 }
207 
208 #endif
~DeviceMatrix()
Destructor.
Definition: DeviceMatrix.h:100
DeviceMatrix(StoringOrder storingOrder=RowMajor)
Definition: DeviceMatrix.h:70
DeviceMatrix(DeviceMatrix< Type > &&temporaryMatrix)
Definition: DeviceMatrix.h:95
void Dispose()
Disposes the matrix.
Definition: DeviceMatrix.h:63
void ReplaceByTranspose()
Definition: BaseMatrix.h:107
void MultiplyBySelfTranspose(DeviceMatrix< Type > &C, cudafloat alpha=CUDA_VALUE(1.0), cudafloat beta=CUDA_VALUE(0.0))
Definition: DeviceMatrix.h:170
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
DeviceMatrix< Type > Transpose()
Definition: DeviceMatrix.h:156
DeviceMatrix(int rows, int columns, StoringOrder storingOrder=RowMajor)
Definition: DeviceMatrix.h:76
void TransferOwnerShipFrom(DeviceMatrix< Type > &other)
Definition: DeviceMatrix.h:140
Create a matrix of any type, on the device, that automatically manages the memory used to hold its el...
Definition: DeviceMatrix.h:35
DeviceMatrix< Type > & operator=(const HostMatrix< Type > &other)
Definition: DeviceMatrix.h:109
Type * Pointer() const
Definition: BaseMatrix.h:88
int Columns() const
Definition: BaseMatrix.h:80
bool IsRowMajor() const
Definition: BaseMatrix.h:100
DeviceMatrix(const HostMatrix< Type > &other)
Definition: DeviceMatrix.h:89
static void Multiply(DeviceMatrix< Type > &A, DeviceMatrix< Type > &B, DeviceMatrix< Type > &C, cudafloat alpha=CUDA_VALUE(1.0), cudafloat beta=CUDA_VALUE(0.0))
Definition: DeviceMatrix.h:192
int ResizeWithoutPreservingData(int rows, int columns)
Definition: BaseMatrix.h:119
int Rows() const
Definition: BaseMatrix.h:74
Create a matrix of any type, on the host, that automatically manages the memory used to hold its elem...
Definition: HostMatrix.h:41
#define CUDA_VALUE(X)
DeviceMatrix(const DeviceMatrix< Type > &other)
Definition: DeviceMatrix.h:83
float cudafloat