GPUMLib  0.2.2
GPU Machine Learning Library
BaseMatrix.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_BaseMatrix_h
22 #define GPUMLib_BaseMatrix_h
23 
24 namespace GPUMLib {
25 
28 
30 typedef enum {
31  RowMajor,
32  ColumnMajor
33 } StoringOrder;
34 
35 template <class Type> class CudaMatrix;
36 
38 template <class Type> class BaseMatrix {
39  friend class CudaMatrix<Type>;
40 
41  protected:
42  Type * matrixData;
43  int rows;
44  int columns;
45  StoringOrder storingOrder;
46 
47  void Init() {
48  matrixData = nullptr;
49  rows = columns = 0;
50  }
51 
52  BaseMatrix(StoringOrder storingOrder = RowMajor) {
53  this->storingOrder = storingOrder;
54  Init();
55  }
56 
57  void MoveFrom(BaseMatrix<Type> & other) {
58  matrixData = other.matrixData;
59  rows = other.rows;
60  columns = other.columns;
61  storingOrder = other.storingOrder;
62 
63  other.Init();
64  }
65 
66  virtual void Alloc(int rows, int columns) = 0;
67 
68  public:
70  virtual void Dispose() = 0;
71 
74  int Rows() const {
75  return rows;
76  }
77 
80  int Columns() const {
81  return columns;
82  }
83 
88  Type * Pointer() const {
89  return (Elements() > 0) ? matrixData : nullptr;
90  }
91 
94  int Elements() const {
95  return rows * columns;
96  }
97 
100  bool IsRowMajor() const {
101  return (storingOrder == RowMajor);
102  }
103 
108  int newRows = columns;
109 
110  columns = rows;
111  rows = newRows;
112  storingOrder = (IsRowMajor()) ? ColumnMajor : RowMajor;
113  }
114 
119  int ResizeWithoutPreservingData(int rows, int columns) {
120  int newElements = rows * columns;
121 
122  if (newElements != Elements()) {
123  Dispose();
124  Alloc(rows, columns);
125  } else {
126  this->rows = rows;
127  this->columns = columns;
128  }
129 
130  return Elements();
131  }
132 };
133 
135 
136 }
137 
138 #endif
void ReplaceByTranspose()
Definition: BaseMatrix.h:107
Base class for HostMatrix and DeviceMatrix classes (Matrix base class)
Definition: BaseMatrix.h:38
int Elements() const
Definition: BaseMatrix.h:94
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, that automatically manages the memory used to hold its elements (data wi...
Definition: BaseMatrix.h:35
virtual void Dispose()=0
Disposes the matrix.
Type * Pointer() const
Definition: BaseMatrix.h:88
int Columns() const
Definition: BaseMatrix.h:80
bool IsRowMajor() const
Definition: BaseMatrix.h:100
int ResizeWithoutPreservingData(int rows, int columns)
Definition: BaseMatrix.h:119
int Rows() const
Definition: BaseMatrix.h:74