ergo
MatrixHierarchicBase.h
Go to the documentation of this file.
1 /* Ergo, version 3.4, a program for linear scaling electronic structure
2  * calculations.
3  * Copyright (C) 2014 Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Primary academic reference:
19  * Kohn−Sham Density Functional Theory Electronic Structure Calculations
20  * with Linearly Scaling Computational Time and Memory Usage,
21  * Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek,
22  * J. Chem. Theory Comput. 7, 340 (2011),
23  * <http://dx.doi.org/10.1021/ct100611z>
24  *
25  * For further information about Ergo, see <http://www.ergoscf.org>.
26  */
27 
38 #ifndef MAT_MATRIXHIERARCHICBASE
39 #define MAT_MATRIXHIERARCHICBASE
40 #include "matInclude.h"
41 #include "allocate.h"
42 namespace mat{
49  template<class Treal, class Telement = Treal>
51  public:
52  /* No public constructors (!) */
53  inline bool operator==(int k) const {
54  if (k == 0)
55  return this->is_zero();
56  else
57  throw Failure("Matrix::operator== only implemented for k == 0");
58  }
59  /* Check if matrix is zero (k = 0) */
60 #if 1
61  inline const int& nScalarsRows() const
62  {return rows.getNScalars();}
63  inline const int& nScalarsCols() const
64  {return cols.getNScalars();}
65 #endif
66 
67  inline const int& nrows() const /* Number of rows in Telement matrix */
68  {return rows.getNBlocks();}
69  inline const int& ncols() const /* Number of cols in Telement matrix */
70  {return cols.getNBlocks();}
71 
72  inline Telement& operator() /* Returns the element A(row, col) */
73  (int row, int col) {
75  assert(row >= 0);
76  assert(col >= 0);
77  assert(row < nrows());
78  assert(col < ncols());
79  return elements[row + col * nrows()];
80  }
81  inline const Telement& operator() /*Write protected reference returned*/
82  (int row, int col) const {
84  assert(row >= 0);
85  assert(col >= 0);
86  assert(row < nrows());
87  assert(col < ncols());
88  return elements[row + col * nrows()];
89  }
90 
91  inline Telement& operator[]
92  (int index) {
94  assert(index >= 0);
95  assert(index < nElements());
96  return elements[index];
97  }
98  inline Telement const & operator[]
99  (int index) const {
100  assert(elements);
101  assert(index >= 0);
102  assert(index < nElements());
103  return elements[index];
104  }
105 
106  inline bool is_zero() const {return !elements;}
107 
108  inline int nElements() const {
109  return rows.getNBlocks() * cols.getNBlocks();
110  }
111 
112  inline void resetRows(SizesAndBlocks const & newRows) {
114  elements = 0;
115  rows = newRows;
116  }
117  inline void resetCols(SizesAndBlocks const & newCols) {
119  elements = 0;
120  cols = newCols;
121  }
122 
123  inline void getRows(SizesAndBlocks & rowsCopy) const {
124  rowsCopy = rows;
125  }
126  inline void getCols(SizesAndBlocks & colsCopy) const {
127  colsCopy = cols;
128  }
129 
130 
131  inline bool highestLevel() const {
132  return (rows.getNTotalScalars() == rows.getNScalars() &&
134  }
135 
141  inline bool is_empty() const {
142  return rows.is_empty() || cols.is_empty();
143  }
144  protected:
145 
146 
148  : elements(0) {}
150  SizesAndBlocks const & colsInp)
151  : rows(rowsInp), cols(colsInp), elements(0) {}
153 
154 
157 
160 
161  virtual ~MatrixHierarchicBase();
164  Telement* elements; /* Length is nRows * nCols unless 0 */
165 
166 
167 
168 #if 0
169  inline void assert_alloc() {
170  if (this->cap < this->nel) {
171  freeElements(this->elements);
172  this->cap = this->nel;
173  this->elements = allocateElements<Telement>(this->cap);
174  for (int ind = 0; ind < this->cap; ind++)
175  this->elements[ind] = 0;
176  }
177  }
178 #endif
179  private:
180 
181  }; /* end class */
182 
183 
184 
185  template<class Treal, class Telement> /* Copy constructor */
188  : rows(mat.rows), cols(mat.cols), elements(0) {
189  if (!mat.is_zero()) {
190  elements = allocateElements<Telement>(nElements());
191  for (int i = 0; i < nElements(); i++)
192  elements[i] = mat.elements[i];
193  }
194  }
195 
196 
197  template<class Treal, class Telement> /*Assignment operator*/
201  if (mat.is_zero()) { /* Condition also matches empty matrices. */
202  rows = mat.rows;
203  cols = mat.cols;
205  elements = 0;
206  return *this;
207  }
208  if (is_zero() || (nElements() != mat.nElements())) {
210  elements = allocateElements<Telement>(mat.nElements());
211  }
212  rows = mat.rows;
213  cols = mat.cols;
214  for (int i = 0; i < nElements(); i++)
215  elements[i] = mat.elements[i];
216  return *this;
217  }
218 
219  template<class Treal, class Telement>
223  assert(A.rows == B.rows && A.cols == B.cols);
224  Telement* elementsTmp = A.elements;
225  A.elements = B.elements;
226  B.elements = elementsTmp;
227  }
228 
229 
230  template<class Treal, class Telement>
233  elements = 0;
234  }
235 
236 
237 } /* end namespace mat */
238 
239 #endif
#define A
SizesAndBlocks cols
Definition: MatrixHierarchicBase.h:163
MatrixHierarchicBase(SizesAndBlocks const &rowsInp, SizesAndBlocks const &colsInp)
Definition: MatrixHierarchicBase.h:149
bool highestLevel() const
Definition: MatrixHierarchicBase.h:131
int getNTotalScalars() const
Definition: SizesAndBlocks.h:76
const Telement int col const
Definition: MatrixHierarchicBase.h:82
MatrixHierarchicBase< Treal, Telement > & operator=(const MatrixHierarchicBase< Treal, Telement > &mat)
Definition: MatrixHierarchicBase.h:200
return elements[row+col *nrows()]
Definition: MatrixHierarchicBase.h:79
const int & ncols() const
Definition: MatrixHierarchicBase.h:69
const int & nScalarsRows() const
Definition: MatrixHierarchicBase.h:61
const int & nrows() const
Definition: MatrixHierarchicBase.h:67
Definition: allocate.cc:30
Describes dimensions of matrix and its blocks on all levels.
Definition: SizesAndBlocks.h:37
bool operator==(int k) const
Definition: MatrixHierarchicBase.h:53
virtual ~MatrixHierarchicBase()
Definition: MatrixHierarchicBase.h:231
bool is_empty() const
Definition: SizesAndBlocks.h:63
Telement * elements
Definition: MatrixHierarchicBase.h:164
Telement int col
Definition: MatrixHierarchicBase.h:73
Base class for Matrix and Matrix specialization.
Definition: MatrixHierarchicBase.h:50
void getRows(SizesAndBlocks &rowsCopy) const
Definition: MatrixHierarchicBase.h:123
void freeElements(float *ptr)
Definition: allocate.cc:40
int nElements() const
Definition: MatrixHierarchicBase.h:108
int const & getNScalars() const
Definition: SizesAndBlocks.h:65
void getCols(SizesAndBlocks &colsCopy) const
Definition: MatrixHierarchicBase.h:126
bool is_empty() const
Check if matrix is empty Empty is different from zero, a zero matrix contains information about block...
Definition: MatrixHierarchicBase.h:141
void resetCols(SizesAndBlocks const &newCols)
Definition: MatrixHierarchicBase.h:117
Copyright(c) Emanuel Rubensson 2006.
Definition: Failure.h:47
int const & getNBlocks() const
Definition: SizesAndBlocks.h:64
MatrixHierarchicBase()
Definition: MatrixHierarchicBase.h:147
Telement & operator()(int row
#define B
bool is_zero() const
Definition: MatrixHierarchicBase.h:106
SizesAndBlocks rows
Definition: MatrixHierarchicBase.h:162
const int & nScalarsCols() const
Definition: MatrixHierarchicBase.h:63
void resetRows(SizesAndBlocks const &newRows)
Definition: MatrixHierarchicBase.h:112
static void swap(MatrixHierarchicBase< Treal, Telement > &A, MatrixHierarchicBase< Treal, Telement > &B)
Definition: MatrixHierarchicBase.h:221