Greens-code
A modular quantum transport code
supermatrix.hpp
1 #ifndef SUPER_MATRIX_CPP_H
2 #define SUPER_MATRIX_CPP_H
3 
4 #include "parallel.hpp"
5 #include <cstdlib>
6 #include <exception>
7 #include <iostream>
8 #include <fstream>
9 #include <list>
10 #include <map>
11 #include <vector>
12 #include <memory>
13 
14 #include "matrix.hpp"
15 
16 class super_Matrix;
17 
19 public:
20  virtual size_t nnz() const = 0;
21 };
22 
24 private:
27 protected:
28 
29  super_Matrix* my_sM;
30 
31  void fail(const char* msg) const{
32  throw ( super_Matrix_Invalid_request(msg));
33  }
34 
35  super_Matrix_strategy(super_Matrix* m): my_sM(m){};
36 
37 public:
38  typedef STD_TR1::shared_ptr<super_Matrix_iterator_factory> p_iterator_factory;
39  typedef STD_TR1::shared_ptr<const super_Matrix_iterator_factory> p_const_iterator_factory;
40 
41  enum status {SetUp, Baked, Multiply, Factorized, Factorized_dist};
42 
43  virtual void addmyselfto(Matrix& m) const = 0;
44 
45  virtual ~super_Matrix_strategy(){};
46 
48  virtual void obtain_Diagonal(complex *) = 0;
49 
50  virtual void Insert(row, column, complex) = 0;
51  virtual void Add(row, column, complex) = 0;
52  virtual complex Get(row, column) const = 0;
53 
54  virtual void Apply(const complex* ,
55  complex* ) const = 0;
56  virtual void Apply(complex*) const = 0;
57 
58  virtual void Scale(complex) = 0;
59 
60  virtual void Output(std::ostream& os) const = 0;
61 
63  virtual p_iterator_factory provide_iterator() = 0;
64  virtual p_const_iterator_factory provide_const_iterator() const = 0;
65 
66  virtual std::unique_ptr<super_Matrix_strategy> deep_copy(
67  STD_TR1::shared_ptr<super_Matrix> new_mat) const = 0;
68 
69  virtual status my_status() const = 0;
70 };
71 
73 
107 public:
108  typedef STD_TR1::shared_ptr<super_Matrix_iterator_factory> p_iterator_factory;
109  typedef STD_TR1::shared_ptr<const super_Matrix_iterator_factory> p_const_iterator_factory;
110 
111  typedef std::pair<int,int> indexpair;
112 
113  typedef super_Matrix_strategy::status status;
114 
115  typedef STD_TR1::shared_ptr<super_Matrix> ref;
116  typedef STD_TR1::shared_ptr<const super_Matrix> const_ref;
117 
119  explicit super_Matrix(MPI_Comm comm,
120  const int N);
121  virtual ~super_Matrix() throw();
122 
124  status get_status(){return strategy->my_status();};
125 
127  p_iterator_factory provide_iterator(){ return strategy->provide_iterator();};
128  p_const_iterator_factory provide_const_iterator() const { return strategy->provide_const_iterator();};
129 
131  void set_param(const std::string&, double);
132  void set_param(const std::string&, int);
133 
134  bool get_double_param(const std::string&, double&) const;
135  bool get_int_param(const std::string&, int&) const;
136 
138  void obtain_Diagonal(complex *);
139 
141  void Add_To(Matrix& m) const;
142 
144  void Output(std::ostream& os) const {strategy->Output(os);};
145 
147  void Bake_to_Multiply(bool use_mkl = true);
149  void Bake_to_Distributed_Multiply();
151  void Bake_to_Factorize(bool distributed = false);
152 
154  void Factorize();
156  void Reset();
157 
159  ref deep_copy() const;
160 
162  MPI_Comm get_comm() const { return comm;};
163 
164 
165 private:
166  int NN;
167 
168  MPI_Comm comm;
169 
170  std::map<std::string, int> iparams;
171  std::map<std::string, double> dparams;
172 
173  std::unique_ptr<super_Matrix_strategy> strategy;
174 
175 // super_Matrix(const super_Matrix&);
176 
177  void scale(complex v) { strategy->Scale(v);};
178  void insert(row r, column c, complex v){ strategy->Insert(r,c,v);};
179  void add(row r, column c, complex v){ strategy->Add(r,c,v);};
180  complex get(row r, column c) const { return strategy->Get(r,c);};
181 
182  void apply_two(const complex* phi,
183  complex* result) const {strategy->Apply(phi, result);};
184  void apply_one(complex* phi) const {strategy->Apply(phi);};
185 
186  size_t num_rows() const {return NN;};
187  size_t num_cols() const {return NN;};
188 };
189 
190 /*-----------------*
191  * SETUP STRATEGY *
192  *-----------------*/
193 
195 public:
196  typedef std::pair<int,int> indexpair;
197 private:
198  const std::map<indexpair, complex>& data;
199 public:
200  super_Matrix_SetUp_iterator(const std::map<indexpair, complex>& d):
201  data(d){}
202 
203  size_t nnz() const{ return data.size();};
204 
205  std::map<std::pair<int,int>,complex>::const_iterator begin() const {return data.begin();};
206  std::map<std::pair<int,int>,complex>::const_iterator end() const {return data.end();};
207 };
208 
210 
211 
212  typedef std::pair<int,int> indexpair;
213  std::map<indexpair, complex> data;
214  int NN;
215 
216  indexpair index(row r, column c) const { return std::pair<int,int>(r.get(),c.get());};
217 
218  int get_row(const indexpair& i) const {return i.first;};
219  int get_col(const indexpair& i) const {return i.second;};
220 
221  friend class matrix_factory;
222 
223  void Scale(complex v);
224 
225 
226 public:
227 
228  p_iterator_factory provide_iterator();
229  p_const_iterator_factory provide_const_iterator() const;
230 
233  NN(m->num_of_cols()){};
234 
235  ~super_Matrix_SetUp(){};
236 
237  void addmyselfto(Matrix&) const;
238 
239  void Insert(row, column, complex);
240  void Add(row, column, complex);
241  complex Get(row, column) const;
242 
243  void obtain_Diagonal(complex* diag);
244 
245  void Apply(const complex*,
246  complex*) const{
247  fail("Apply not valid in SetUp Phase!\n");
248  };
249  void Apply( complex*) const{
250  fail("Apply not valid in SetUp Phase!\n");
251  };
252 
253  std::unique_ptr<super_Matrix_strategy> deep_copy(
254  STD_TR1::shared_ptr<super_Matrix> new_mat
255  ) const;
256 
257  void Output(std::ostream& os) const;
258 
259  status my_status() const {return SetUp;};
260 };
261 #endif
p_iterator_factory provide_iterator()
Return an iterator factory over non-zero elements.
Definition: supermatrix.hpp:127
Definition: supermatrix_MUMPS.hpp:304
Definition: supermatrix.hpp:18
void Output(std::ostream &os) const
Output the matrix.
Definition: supermatrix.hpp:144
General interface to inversion of large, sparse Matrices.
Definition: supermatrix.hpp:106
An invalid request for matrix inversion.
Definition: exceptions.hpp:13
MPI_Comm get_comm() const
Return MPI Communicator.
Definition: supermatrix.hpp:162
Definition: supermatrix.hpp:209
Base Matrix class for access to 2D number grids.
Definition: matrix.hpp:82
Definition: supermatrix.hpp:194
status get_status()
Return the current phase.
Definition: supermatrix.hpp:124
This Base-Class offers Matrix-Vector multiplication and scaling.
Definition: matrix.hpp:322
Definition: supermatrix.hpp:23