ergo
sparse_matrix.h
Go to the documentation of this file.
1 /* Ergo, version 3.8, a program for linear scaling electronic structure
2  * calculations.
3  * Copyright (C) 2019 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_ */
template_blas_sqrt
Treal template_blas_sqrt(Treal x)
ShellSpecStruct::coeffList
ergo_real coeffList[MAX_NO_OF_CONTR_GAUSSIANS]
Definition: basisinfo.h:72
sparse_matrix.h
realtype.h
Definition of the main floating-point datatype used; the ergo_real type.
NeighbourList::setOverlappingWith
void setOverlappingWith(const std::vector< NeighbourList > &list)
Definition: sparse_matrix.cc:84
dft_common.h
Common DFT routines. Mostly functional mixing.
SparsePattern::Column::end
Iterator end() const
Definition: sparse_pattern.h:115
SparseMatrix::cnt
int * cnt
for accelerated at() and add() methods.
Definition: sparse_matrix.h:68
SparseMatrix::print
void print(const char *title) const
Definition: sparse_matrix.cc:215
NeighbourList::extent
ergo_real extent
an approximation for the shell extent.
Definition: sparse_matrix.cc:68
SparseMatrix::offsets
int ** offsets
for accelerated at() and add() methods.
Definition: sparse_matrix.h:66
getrho_blocked_lda
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
SparseMatrix::Exception::msg
const char * msg
Definition: sparse_matrix.h:58
ergo_real
double ergo_real
Definition: realtype.h:69
END_NAMESPACE
#define END_NAMESPACE(x)
Definition: sparse_pattern.h:42
SparseMatrix
Sparse matrix structure optimized for XC data access pattern.
Definition: sparse_matrix.h:56
symmMatrix
MatrixSymmetric< real, matri > symmMatrix
Definition: test_LanczosSeveralLargestEig.cc:69
SparseMatrix::Exception::what
virtual const char * what() const
Definition: sparse_matrix.h:61
ShellSpecStruct
Definition: basisinfo.h:71
SparseMatrix::SparseMatrix
SparseMatrix(const SparsePattern &pattern_)
Constructs a square matrix and preallocate according to the specified pattern.
Definition: sparse_matrix.cc:134
ShellSpecStruct::centerCoords
ergo_real centerCoords[3]
Definition: basisinfo.h:76
real
ergo_real real
Definition: test.cc:46
sparse_pattern.h
Class that can be used to store sparse matrix patterns.
ShellSpecStruct::exponentList
ergo_real exponentList[MAX_NO_OF_CONTR_GAUSSIANS]
Definition: basisinfo.h:73
SparseMatrix::addSymmetrizedTo
void addSymmetrizedTo(symmMatrix &sMat, const int *aoMap, std::vector< int > const &permutationHML) const
Assigns itself to a given hierarchic matrix.
Definition: sparse_matrix.cc:239
template_blas_fabs
Treal template_blas_fabs(Treal x)
SparseMatrix::n
int n
Definition: sparse_matrix.h:69
SparseMatrix::add
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
SparseMatrix::Exception::Exception
Exception(const char *msg_)
Definition: sparse_matrix.h:60
SparseMatrix::his
int ** his
for accelerated at() and add() methods.
Definition: sparse_matrix.h:67
NeighbourList
Definition: sparse_matrix.cc:65
NeighbourList::size
size_t size() const
Definition: sparse_matrix.cc:105
SparsePattern::size
int size() const
Returns the dimension of the pattern.
Definition: sparse_pattern.h:156
SparsePattern::Column::begin
Iterator begin() const
Definition: sparse_pattern.h:109
SparsePattern
A way to store sparse matrix patterns.
Definition: sparse_pattern.h:53
NeighbourList::begin
std::list< int >::iterator begin()
Definition: sparse_matrix.cc:99
ShellSpecStruct::noOfContr
int noOfContr
Definition: basisinfo.h:77
NeighbourList::neighbours
std::list< int > neighbours
Definition: sparse_matrix.cc:67
SparseMatrix::Exception
Definition: sparse_matrix.h:57
NeighbourList::shellInfo
const ShellSpecStruct * shellInfo
Definition: sparse_matrix.cc:66
SparseMatrix::createOffsets
void createOffsets(const SparsePattern &pattern)
Fills in offsets and his based on pattern.
Definition: sparse_matrix.cc:189
template_blas_log
Treal template_blas_log(Treal x)
SparsePattern::Column::Iterator
Definition: sparse_pattern.h:66
sqDist
static ergo_real sqDist(const ergo_real a[], const ergo_real b[])
computes a squared distance between two points.
Definition: sparse_matrix.cc:55
matrix_typedefs.h
Header file with typedefs for matrix and vector types. The levels of hierarchic matrices are defined ...
basisinfo.h
Code for setting up basis functions starting from shells.
SparseMatrix::columns
ergo_real ** columns
Definition: sparse_matrix.h:65
zeroorbs
static void zeroorbs(real *tmp, const int *nblocks, const int(*iblocks)[2], int ldaib, int nvclen)
Definition: sparse_matrix.cc:278
getrho_blocked_lda
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
NeighbourList::end
std::list< int >::iterator end()
Definition: sparse_matrix.cc:102
SparseMatrix::pattern
const SparsePattern & pattern
Definition: sparse_matrix.h:64
SparsePattern::getColumnSize
int getColumnSize(int col) const
returns the number of stored elements for specified column.
Definition: sparse_pattern.h:151
Dft
Definition: grid_matrix.h:42
ErgoRealPtr
ergo_real * ErgoRealPtr
Definition: sparse_matrix.cc:133
NeighbourList::NeighbourList
NeighbourList(const ShellSpecStruct *sis, ergo_real thr)
Definition: sparse_matrix.cc:70
getrho_blocked_gga
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
getrho_blocked_gga
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
SparsePattern::Column
Definition: sparse_pattern.h:61
SparseMatrix::at
ergo_real at(int row, int col) const
Definition: sparse_matrix.h:121
SparsePattern::IntervalList
std::vector< Interval > IntervalList
Definition: sparse_pattern.h:60
output.h
Functionality for writing output messages to a text file.
SparseMatrix::~SparseMatrix
~SparseMatrix()
Definition: sparse_matrix.h:80
BEGIN_NAMESPACE
#define BEGIN_NAMESPACE(x)
Definition: sparse_pattern.h:41