ergo
sparse_matrix.h
Go to the documentation of this file.
1 /* Ergo, version 3.7, a program for linear scaling electronic structure
2  * calculations.
3  * Copyright (C) 2018 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 
30 #if !defined(_SPARSE_MATRIX_H_)
31 #define _SPARSE_MATRIX_H_ 1
32 
38 #include <stdio.h>
39 #include <vector>
40 #include <algorithm>
41 
42 
43 #include "realtype.h"
44 #include "matrix_typedefs.h"
45 #include "basisinfo.h"
46 #include "sparse_pattern.h"
47 
48 #if !defined(BEGIN_NAMESPACE)
49 #define BEGIN_NAMESPACE(x) namespace x {
50 #define END_NAMESPACE(x) } /* x */
51 #endif
52 
54 
55 
56 class SparseMatrix {
57  class Exception : public std::exception {
58  const char *msg;
59  public:
60  explicit Exception(const char *msg_) : msg(msg_) {}
61  virtual const char *what() const throw() { return msg; }
62  };
63 
66  int **offsets;
67  int **his;
68  int *cnt;
69  int n;
71  void createOffsets(const SparsePattern& pattern);
72  public:
75  explicit SparseMatrix(const SparsePattern& pattern_);
76  SparseMatrix(const SparsePattern& pattern_,
77  const symmMatrix& m, const int *aoMap,
78  std::vector<int> const & permutationHML);
79 
81  for(int i=0; i<n; i++) {
82  delete [](columns[i]);
83  delete [](offsets[i]);
84  delete [](his[i]);
85  }
86  delete []columns;
87  delete []offsets;
88  delete []his;
89  delete []cnt;
90  }
91 
92  void print(const char *title) const;
93 
95  void addSymmetrizedTo(symmMatrix& sMat,
96  const int *aoMap,
97  std::vector<int> const & permutationHML) const;
98 
102  void add(int row, int col, ergo_real val) {
103  ergo_real *columnData = columns[col];
104  const int *hi = his[col];
105  int idx;
106  for(idx = 0; idx < cnt[col] && row >hi[idx]; ++idx);
107  //int idx = std::upper_bound(hi, hi+cnt[col], row)-hi;
108  if(idx >= cnt[col])
109  throw Exception("SparseMatrix::add called with incorrect args");
110  int offset = offsets[col][idx];
111  /* Add it... */
112  //assert(row-offset>=0);
113  //assert(row-offset<pattern.getColumnSize(col));
114  columnData[row-offset] += val;
115  }
116 
117  /* This operator[] syntax glue that we could in principle use can be
118  expensive performance-wise, do it the old-fashioned way.
119  Checking against intervals.end() is *terribly* expensive!!!
120  */
121  ergo_real at(int row, int col) const {
122  const ergo_real *columnData = columns[col];
123  const int *hi = his[col];
124  int idx; for(idx = 0; idx < cnt[col] && row >hi[idx]; ++idx);
125  if(idx >= cnt[col])
126  throw Exception("SparseMatrix::at called with incorrect args");
127  //int idx = std::upper_bound(hi, hi+cnt[col], row)-hi;
128  int offset = offsets[col][idx];
129  /* return it... */
130  //assert(row-offset>=0);
131  //assert(row-offset<pattern.getColumnSize(col));
132  return columnData[row-offset];
133  }
134 };
135 
136 
138 
139 void
140 getrho_blocked_lda(int nbast, const Dft::SparseMatrix& dmat,
141  const ergo_real * gao,
142  const int* nblocks, const int (*iblocks)[2],
143  int ldaib, ergo_real *tmp, int nvclen, ergo_real *rho);
144 void
145 getrho_blocked_gga(int nbast, const Dft::SparseMatrix& dmat,
146  const ergo_real * gao,
147  const int* nblocks, const int (*iblocks)[2],
148  int ldaib, ergo_real *tmp, int nvclen,
149  ergo_real *rho, ergo_real (*grad)[3]);
150 
151 #endif /* _SPARSE_MATRIX_H_ */
double ergo_real
Definition: realtype.h:69
int n
Definition: sparse_matrix.h:69
#define END_NAMESPACE(x)
Definition: sparse_pattern.h:42
#define BEGIN_NAMESPACE(x)
Definition: sparse_pattern.h:41
A way to store sparse matrix patterns.
Definition: sparse_pattern.h:53
MatrixSymmetric< real, matri > symmMatrix
Definition: test_LanczosSeveralLargestEig.cc:69
Definition: sparse_matrix.h:57
Class that can be used to store sparse matrix patterns.
Code for setting up basis functions starting from shells.
const SparsePattern & pattern
Definition: sparse_matrix.h:64
Definition of the main floating-point datatype used; the ergo_real type.
int ** offsets
for accelerated at() and add() methods.
Definition: sparse_matrix.h:66
ergo_real ** columns
Definition: sparse_matrix.h:65
Sparse matrix structure optimized for XC data access pattern.
Definition: sparse_matrix.h:56
void getrho_blocked_lda(int nbast, const Dft::SparseMatrix &dmat, const ergo_real *gao, const int *nblocks, const int(*iblocks)[2], int ldaib, ergo_real *tmp, int nvclen, ergo_real *rho)
Definition: sparse_matrix.cc:290
Header file with typedefs for matrix and vector types.
ergo_real at(int row, int col) const
Definition: sparse_matrix.h:121
const char * msg
Definition: sparse_matrix.h:58
~SparseMatrix()
Definition: sparse_matrix.h:80
void add(int row, int col, ergo_real val)
Adds given value to an element in given row and column.
Definition: sparse_matrix.h:102
Exception(const char *msg_)
Definition: sparse_matrix.h:60
int * cnt
for accelerated at() and add() methods.
Definition: sparse_matrix.h:68
void getrho_blocked_gga(int nbast, const Dft::SparseMatrix &dmat, const ergo_real *gao, const int *nblocks, const int(*iblocks)[2], int ldaib, ergo_real *tmp, int nvclen, ergo_real *rho, ergo_real(*grad)[3])
Definition: sparse_matrix.cc:336
int ** his
for accelerated at() and add() methods.
Definition: sparse_matrix.h:67
Definition: grid_matrix.h:42
virtual const char * what() const
Definition: sparse_matrix.h:61