ergo
MatrixBase.h
Go to the documentation of this file.
1/* Ergo, version 3.8.2, a program for linear scaling electronic structure
2 * calculations.
3 * Copyright (C) 2023 Elias Rudberg, Emanuel H. Rubensson, Pawel Salek,
4 * and Anastasia Kruchinina.
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Primary academic reference:
20 * Ergo: An open-source program for linear-scaling electronic structure
21 * calculations,
22 * Elias Rudberg, Emanuel H. Rubensson, Pawel Salek, and Anastasia
23 * Kruchinina,
24 * SoftwareX 7, 107 (2018),
25 * <http://dx.doi.org/10.1016/j.softx.2018.03.005>
26 *
27 * For further information about Ergo, see <http://www.ergoscf.org>.
28 */
29
38#ifndef MAT_MATRIXBASE
39#define MAT_MATRIXBASE
40#include <iostream>
41#include <fstream>
42#include <ios>
43#include "FileWritable.h"
44#include "matrix_proxy.h"
45#include "ValidPtr.h"
46#include "SizesAndBlocks.h"
47namespace mat {
48 template<typename Treal, typename Tmatrix>
49 class MatrixGeneral;
50 template<typename Treal, typename Tmatrix>
51 class MatrixSymmetric;
52 template<typename Treal, typename Tmatrix>
53 class MatrixTriangular;
54 template<typename Treal, typename Tvector>
55 class VectorGeneral;
57
68 template<typename Treal, typename Tmatrix>
69 class MatrixBase : public FileWritable {
70 public:
71 friend class MatrixGeneral<Treal, Tmatrix>;
72 friend class MatrixSymmetric<Treal, Tmatrix>;
73 friend class MatrixTriangular<Treal, Tmatrix>;
74
75
77 SizesAndBlocks const & newCols) {
79 matrixPtr->resetRows(newRows);
80 matrixPtr->resetCols(newCols);
81 }
82 inline void getRows(SizesAndBlocks & rowsCopy) const {
83 matrixPtr->getRows(rowsCopy);
84 }
85 inline void getCols(SizesAndBlocks & colsCopy) const {
86 matrixPtr->getCols(colsCopy);
87 }
88
93 inline bool is_empty() const {
95 }
96
97 inline Treal trace() const {
98 return matrixPtr->trace();
99 }
100
101 inline void add_identity(Treal alpha) {
102 matrixPtr->addIdentity(alpha);
103 }
105 *matrixPtr *= alpha;
106 return *this;
107 }
108
109 inline bool operator==(int k) const {
110 if (k == 0)
111 return *matrixPtr == 0;
112 else
113 throw Failure("MatrixBase::operator== only implemented for k == 0");
114 }
115
116
117
118 inline void clear() {
119 if (is_empty())
120 // This means that the object's data structure has not been set
121 // There is nothing to clear and the matrixPtr is not valid either
122 return;
123 matrixPtr->clear();
124 }
125
126 inline size_t memory_usage() const {
127 return matrixPtr->memory_usage();
128 }
129
130 inline void write_to_buffer_count(int& n_bytes) const {
131 int ib_length = 3;
132 int vb_length = 0;
133 this->matrixPtr->write_to_buffer_count(ib_length, vb_length);
134 n_bytes = vb_length * sizeof(Treal) + ib_length * sizeof(int);
135 }
136
137#if 1
138 inline int get_nrows() const {
139 return matrixPtr->nScalarsRows();
140 }
141 inline int get_ncols() const {
142 return matrixPtr->nScalarsCols();
143 }
144#endif
145
146 inline Tmatrix const & getMatrix() const {return *matrixPtr;}
147 inline Tmatrix & getMatrix() {return *matrixPtr;}
148
150 inline Treal maxAbsValue() const {return matrixPtr->maxAbsValue();}
151
152 protected:
154
158 matrixPtr.haveDataStructureSet(other.matrixPtr.haveDataStructureGet());
159 /* getConstRefForCopying() is used here to make sure it works
160 also in the case when the matrix is written to file. */
162 matrixPtr.inMemorySet(other.matrixPtr.inMemoryGet());
163 }
164
167 FileWritable::operator=(other); /* Allows us to copy mat on file */
168 matrixPtr.haveDataStructureSet(other.matrixPtr.haveDataStructureGet());
169 /* getConstRefForCopying() is used here to make sure it works
170 also in the case when the matrix is written to file. */
172 matrixPtr.inMemorySet(other.matrixPtr.inMemoryGet());
173 return *this;
174 }
175
178 if (mt.A.matrixPtr.haveDataStructureGet()) {
180 }
181 if (mt.tA)
182 Tmatrix::transpose(*mt.A.matrixPtr, *this->matrixPtr);
183 else
184 *this->matrixPtr = *mt.A.matrixPtr;
185 return *this;
186 // FileWritable::operator=(other);/*Could be used to copy mat on file*/
187 }
188
189
190 void write_to_buffer_base(void* buffer, const int n_bytes,
191 const matrix_type mattype) const;
192 void read_from_buffer_base(void* buffer, const int n_bytes,
193 const matrix_type mattype);
194
195 void writeToFileBase(std::ofstream & file,
196 matrix_type const mattype) const;
197 void readFromFileBase(std::ifstream & file,
198 matrix_type const mattype);
199
200 std::string obj_type_id() const {return "MatrixBase";}
201 inline void inMemorySet(bool inMem) {
203 }
204
205 static void getPermutedIndexes(std::vector<int> const & index,
206 std::vector<int> const & permutation,
207 std::vector<int> & newIndex) {
208 newIndex.resize(index.size());
209 for (unsigned int i = 0; i < index.size(); ++i)
210 newIndex[i] = permutation[index[i]];
211 }
212
213
214 private:
215
216 };
217
218
219 template<typename Treal, typename Tmatrix>
221 writeToFileBase(std::ofstream & file,
222 matrix_type const mattype) const {
223 int type = (int)mattype;
224 file.write((char*)&type,sizeof(int));
225
226 if (is_empty())
227 // This means that the object's data structure has not been set
228 // The ValidPtr prevents setting the data structure between
229 // calls to writeToFile and readFromFile
230 return;
231 matrixPtr->writeToFile(file);
232 }
233
234 template<typename Treal, typename Tmatrix>
236 readFromFileBase(std::ifstream & file,
237 matrix_type const mattype) {
238 char type[sizeof(int)];
239 file.read(type, sizeof(int));
240 if (((int)*type) != mattype)
241 throw Failure("MatrixBase<Treal, Tmatrix>::"
242 "readFromFile(std::ifstream &, "
243 "matrix_type const): Wrong matrix type");
244 if (is_empty())
245 // This means that the object's data structure has not been set
246 return;
247 matrixPtr->readFromFile(file);
248 }
249
250
251
252 template<typename Treal, typename Tmatrix>
254 write_to_buffer_base(void* buffer, const int n_bytes,
255 const matrix_type mattype) const {
256 int ib_length = 3; /* Length of integer buffer, at least 3: matrix_type, */
257 /* ib_length and vb_length */
258 int vb_length = 0; /* Length of value buffer */
259 this->matrixPtr->write_to_buffer_count(ib_length, vb_length);
260 if (n_bytes >=
261 (int)(vb_length * sizeof(Treal) + ib_length * sizeof(int))) {
262 int* int_buf = (int*)buffer;
263 int_buf[0] = mattype;
264 int_buf[1] = ib_length;
265 int_buf[2] = vb_length;
266 Treal* value_buf = (Treal*)&(int_buf[ib_length]); /* Value buffer */
267 /* begins after integer buffer end */
268 int ib_index = 0;
269 int vb_index = 0;
270 this->matrixPtr->write_to_buffer(&int_buf[3], ib_length - 3,
273 }
274 else {
275 throw Failure("MatrixBase::write_to_buffer: Buffer is too small");
276 }
277 }
278
279 template<typename Treal, typename Tmatrix>
281 read_from_buffer_base(void* buffer, const int n_bytes,
282 const matrix_type mattype) {
283 int* int_buf = (int*)buffer;
284 if(int_buf[0] == mattype) {
285 int ib_length = int_buf[1];
286 int vb_length = int_buf[2];
287 int ib_index = 0;
288 int vb_index = 0;
290 this->matrixPtr->read_from_buffer(&int_buf[3], ib_length - 3,
293 }
294 else {
295 throw Failure("MatrixBase::read_from_buffer: Wrong matrix type");
296 }
297 }
298
299
300} /* end namespace mat */
301#endif
302
303
Abstract class for simple writing and reading of objects to/from file.
Class used to keep track of the block sizes used at different levels in the hierarchical matrix data ...
Smart pointer class to control access to object.
Definition Failure.h:57
Write and read objects to/from file.
Definition FileWritable.h:56
FileWritable & operator=(FileWritable const &)
Definition FileWritable.cc:478
Base class for matrix API.
Definition MatrixBase.h:69
MatrixBase< Treal, Tmatrix > & operator*=(Treal const alpha)
Definition MatrixBase.h:104
size_t memory_usage() const
Definition MatrixBase.h:126
int get_ncols() const
Definition MatrixBase.h:141
bool is_empty() const
Check if matrix is empty.
Definition MatrixBase.h:93
void getCols(SizesAndBlocks &colsCopy) const
Definition MatrixBase.h:85
bool operator==(int k) const
Definition MatrixBase.h:109
std::string obj_type_id() const
Definition MatrixBase.h:200
Tmatrix const & getMatrix() const
Definition MatrixBase.h:146
static void getPermutedIndexes(std::vector< int > const &index, std::vector< int > const &permutation, std::vector< int > &newIndex)
Definition MatrixBase.h:205
void read_from_buffer_base(void *buffer, const int n_bytes, const matrix_type mattype)
Definition MatrixBase.h:281
int get_nrows() const
Definition MatrixBase.h:138
MatrixBase(const MatrixBase< Treal, Tmatrix > &other)
Definition MatrixBase.h:156
void add_identity(Treal alpha)
Definition MatrixBase.h:101
void writeToFileBase(std::ofstream &file, matrix_type const mattype) const
Definition MatrixBase.h:221
Tmatrix & getMatrix()
Definition MatrixBase.h:147
Treal trace() const
Definition MatrixBase.h:97
void inMemorySet(bool inMem)
Make object invalid (false) via this function when object is written to file and valid (true) when ob...
Definition MatrixBase.h:201
MatrixBase< Treal, Tmatrix > & operator=(const MatrixBase< Treal, Tmatrix > &other)
Definition MatrixBase.h:166
void getRows(SizesAndBlocks &rowsCopy) const
Definition MatrixBase.h:82
Treal maxAbsValue() const
Get largest absolute value of matrix element in the matrix.
Definition MatrixBase.h:150
void resetSizesAndBlocks(SizesAndBlocks const &newRows, SizesAndBlocks const &newCols)
Definition MatrixBase.h:76
void clear()
Release memory for the information written to file.
Definition MatrixBase.h:118
MatrixBase< Treal, Tmatrix > & operator=(const Xtrans< MatrixGeneral< Treal, Tmatrix > > &mt)
Definition MatrixBase.h:177
void write_to_buffer_base(void *buffer, const int n_bytes, const matrix_type mattype) const
Definition MatrixBase.h:254
void write_to_buffer_count(int &n_bytes) const
Definition MatrixBase.h:130
ValidPtr< Tmatrix > matrixPtr
Definition MatrixBase.h:153
void readFromFileBase(std::ifstream &file, matrix_type const mattype)
Definition MatrixBase.h:236
MatrixBase()
Definition MatrixBase.h:155
Normal matrix.
Definition MatrixGeneral.h:59
Symmetric matrix.
Definition MatrixSymmetric.h:68
Upper non-unit triangular matrix.
Definition MatrixTriangular.h:59
Describes dimensions of matrix and its blocks on all levels.
Definition SizesAndBlocks.h:45
Smart pointer class to control access to object.
Definition ValidPtr.h:50
const Tobj & getConstRefForCopying() const
getConstRefForCopying() is provided to make it possible to copy the object also when it is written to...
Definition ValidPtr.h:89
void haveDataStructureSet(bool val)
Definition ValidPtr.h:99
bool haveDataStructureGet() const
Definition ValidPtr.h:102
void inMemorySet(bool val)
Definition ValidPtr.h:93
Proxy structs used by the matrix API.
Definition allocate.cc:39
matrix_type
Definition MatrixBase.h:56
@ matrix_triang
Definition MatrixBase.h:56
@ matrix_matr
Definition MatrixBase.h:56
@ matrix_symm
Definition MatrixBase.h:56
static Treal getMachineEpsilon()
Definition matInclude.h:147
normalMatrix MatrixGeneral
Definition random_matrices.h:74
This proxy expresses the result of transposition of an object of type TX.
Definition matrix_proxy.h:118