Debian Codegraph @

3rdparty/dissection/src/Driver/DissectionMatrix.hpp

raw
1 /*! \file DissectionMatrix.hpp
2 \brief management of threads for factorization and Fw/Bw substitution
3 \author Atsushi Suzuki, Laboratoire Jacques-Louis Lions
4 \date Jun. 20th 2014
5 \date Jul. 12th 2015
6 \date Nov. 30th 2016
7 */
8
9 // This file is part of Dissection
10 //
11 // Dissection is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 3 of the License, or
14 // (at your option) any later version.
15 //
16 // Linking Dissection statically or dynamically with other modules is making
17 // a combined work based on Disssection. Thus, the terms and conditions of
18 // the GNU General Public License cover the whole combination.
19 //
20 // As a special exception, the copyright holders of Dissection give you
21 // permission to combine Dissection program with free software programs or
22 // libraries that are released under the GNU LGPL and with independent modules
23 // that communicate with Dissection solely through the Dissection-fortran
24 // interface. You may copy and distribute such a system following the terms of
25 // the GNU GPL for Dissection and the licenses of the other code concerned,
26 // provided that you include the source code of that other code when and as
27 // the GNU GPL requires distribution of source code and provided that you do
28 // not modify the Dissection-fortran interface.
29 //
30 // Note that people who make modified versions of Dissection are not obligated
31 // to grant this special exception for their modified versions; it is their
32 // choice whether to do so. The GNU General Public License gives permission to
33 // release a modified version without this exception; this exception also makes
34 // it possible to release a modified version which carries forward this
35 // exception. If you modify the Dissection-fortran interface, this exception
36 // does not apply to your modified version of Dissection, and you must remove
37 // this exception when you distribute your modified version.
38 //
39 // This exception is an additional permission under section 7 of the GNU
40 // General Public License, version 3 ("GPLv3")
41 //
42 // Dissection is distributed in the hope that it will be useful,
43 // but WITHOUT ANY WARRANTY; without even the implied warranty of
44 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45 // GNU General Public License for more details.
46 //
47 // You should have received a copy of the GNU General Public License
48 // along with Dissection. If not, see <http://www.gnu.org/licenses/>.
49 //
50
51 # ifndef _DRIVER_DISSECTIONMATRIX_
52 # define _DRIVER_DISSECTIONMATRIX_
53 #include <complex>
54 #include <vector>
55 #include "Splitters/BisectionTree.hpp"
56 #include "Algebra/PlainMatrix.hpp"
57 #include "Algebra/SquareMatrix.hpp"
58 #include "Algebra/SquareBlockMatrix.hpp"
59 #include "Algebra/RectBlockMatrix.hpp"
60 #include "Algebra/SparseMatrix.hpp"
61 #include "Algebra/ColumnMatrix.hpp"
62 #include "Driver/TridiagBlockMatrix.hpp"
63 #include "Driver/C_threads_tasks.hpp"
64
65 using std::vector;
66
67 template<typename T, typename U = T>
68 class DissectionMatrix
69 {
70 public:
71
72 DissectionMatrix(Dissection::Tree *btree,
73 const int nb, const bool isSym, const bool verbose,
74 FILE *fp);
75
76 DissectionMatrix(const DissectionMatrix<T, U> & im)
77 {
78 (*this) = im;
79 }
80
81 ~DissectionMatrix()
82 {
83 if (_islast) {
84 delete [] _color_mask;
85 for (int i = 0; i < _colors; i++) {
86 _tridiag[i]->free();
87 delete _tridiag[i];
88 }
89 delete [] _tridiag;
90 }
91 _diag->free(); // free() is compatible to zero-sized matrix
92 _lower->free();
93 _upper->free();
94 _localSchur->free(); // this may be redundant : 26 Jun.2014
95 delete _factorize_LDLt;
96 delete _diag;
97 delete _lower;
98 delete _upper;
99 delete _localSchur;
100 }
101
102 DissectionMatrix& operator = ( const DissectionMatrix<T, U> &im)
103 {
104 if ( &im != this) {
105 _nb = im._nb;
106 _level = im._level;
107 _nrow = im._nrow;
108 _ncol_offdiag = im._ncol_offdiag;
109 _nop = im._nop;
110 _diag = im._diag->clone();
111 if (!_isSym) {
112 _lower->copy(*im._lower);
113 }
114 _upper->copy(*im._upper);
115 _isSym = im._isSym;
116 _localSchur = &im.localSchurBlock();
117 _factorize_LDLt = im._factorize_LDLt;
118 // _factorize_LDLt_diag = im._factorize_LDLt_diag;
119 _csr_diag = im._csr_diag;
120 _csr_offdiag = im._csr_offdiag;
121 _alignedFather = im._alignedFather;
122 }
123 return *this;
124 }
125 int ColorTridiagBlockMatrix() { return _colors ; }
126 void setColorTridiagBlockMatrix(int colors) { _colors = colors; }
127 int nrow() { return _nrow; }
128 SquareBlockMatrix<T>* addrdiagBlock() { return _diag; }
129 SquareBlockMatrix<T>*& paddrdiagBlock() { return _diag; }
130 SquareBlockMatrix<T>& diagBlock() { return *_diag; }
131 const SquareBlockMatrix<T>& diagBlock() const { return *_diag;}
132 TridiagBlockMatrix<T, U> **addrtridiagBlock() { return _tridiag; }
133 TridiagBlockMatrix<T, U> **&paddrtridiagBlock() { return _tridiag; }
134 RectBlockMatrix<T>* addrupperBlock() { return _upper; }
135 RectBlockMatrix<T>*& paddrupperBlock() { return _upper; }
136 RectBlockMatrix<T>& upperBlock() { return *_upper; }
137 const RectBlockMatrix<T>& upperBlock() const { return *_upper; }
138 RectBlockMatrix<T>* addrlowerBlock() { return _lower; }
139 RectBlockMatrix<T>*& paddrlowerBlock() { return _lower; }
140 RectBlockMatrix<T>& lowerBlock() { return *_lower; }
141 const RectBlockMatrix<T>& lowerBlock() const { return *_lower; }
142 bool isSym() const { return _isSym; }
143 void setAlignedFather() { _alignedFather = true; }
144 bool isAlignedFather() const { return _alignedFather; }
145 bool isFactorized() const { return _diag->isFactorized(); }
146 PlainMatrix<T>& loclSchurBlock();
147 const vector<int>& singIdxPermute() const { return _diag->getSingIdx(); }
148 vector<int>& singIdxPermute() { return _diag->getSingIdx(); }
149 const vector<int>& singIdx() const { return _diag->getSingIdx0(); }
150 vector<int>& singIdx() { return _diag->getSingIdx0(); }
151 int KernelDetected() const { return _diag->KernelDetected(); }
152 int KernelDim() const { return _diag->dim_kern(); }
153 void SetKernelDim(int nsing) { _diag->set_dim_kernel(nsing); }
154 SquareMatrix<T>& localSchurBlock() { return *_localSchur; }
155 const SquareMatrix<T>& localSchurBlock() const { return *_localSchur; }
156 T getLastPivot() const { return _diag->lastPivot(); }
157 void setLastPivot(T pivot_val) { _diag->set_lastPivot(pivot_val); }
158
159 void C_SparseSymbFact_queue(vector<C_task*>& queue,
160 Dissection::Tree *btree,
161 const bool verbose,
162 FILE **fp);
163
164 void C_SparseNumFact_queue(vector<C_task*>& queue,
165 Dissection::Tree *btree,
166 int nnz,
167 T *coefs,
168 double *eps_pivot,
169 double *pivot,
170 bool *kernel_detection,
171 bool *higher_precision,
172 int *aug_dim,
173 U *eps_machine,
174 vector<C_task*>& task_q,
175 const bool verbose,
176 FILE **fp);
177
178 void C_SparseLocalSchur_queue(vector<C_task*>& queue,
179 Dissection::Tree *btree,
180 int nnz,
181 T *cofes,
182 vector<C_task*>& task_p,
183 const bool verbose,
184 FILE **fp);
185
186 void C_FillMatrix_queue(vector<C_task*>& queue,
187 int nnz,
188 T *cofes,
189 const bool verbose,
190 FILE **fp);
191
192 int C_DFullLDLt_queue(vector<C_task*>& queue,
193 vector<int>& task_indcol,
194 vector<int>& task_ptr,
195 double *eps_piv,
196 bool *kernel_detection,
197 int *aug_dim,
198 U *eps_machine,
199 double *pivot,
200 double *pivot0,
201 double *pivot1,
202 vector<C_task*>& task_p,
203 const bool isChldrnAlgnd,
204 const bool verbose,
205 FILE **fp);
206
207 int C_DTRSMScale_queue(vector<C_task*> &queue,
208 vector<list<int> > &queue_parents_index,
209 vector<int> &queue_index,
210 vector<int>& indcol,
211 vector<int>& ptr,
212 Dissection::Tree *btree,
213 vector<C_task*>& task_o,
214 vector<int>& task_indcol,
215 vector<int>& task_ptr,
216 vector<C_task*>& task_p,
217 const bool verbose,
218 FILE **fp);
219
220 void C_DTRSMScale_rearrange(vector<C_task*> &queue,
221 vector<list<int> > &queue_parents_index,
222 vector<int> &queue_index,
223 vector<int>& indcol,
224 vector<int>& ptr,
225 const bool verbose,
226 FILE *fp);
227
228 int C_DGEMM_local_queue(vector<C_task*> &queue,
229 vector<int> &indcol,
230 RectBlockMatrix<T> *upper1,
231 RectBlockMatrix<T> *lower1,
232 bool isSkip,
233 bool isDirect,
234 SquareBlockMatrix<T>* fdiag,
235 vector<C_task *> &task_p,
236 vector<int> &task_p_index,
237 vector<int> &task_p_indcol,
238 vector<int> &task_p_ptr,
239 vector<C_task *> &task_q,
240 vector<int> &task_q_index,
241 vector<int> &task_q_indcol,
242 vector<int> &task_q_ptr,
243 vector<C_task *> *task_s,
244 const bool verbose,
245 FILE **fp);
246
247 void C_deallocLocalSchur_queue(vector<C_task*> &queue,
248 vector<int> &indcol,
249 const bool verbose,
250 FILE **fp);
251
252 void ChildContrib(list<child_contribution<T> > *child_contribs,
253 Dissection::Tree *btree,
254 vector<DissectionMatrix<T, U>* >& dissectionMatrix,
255 const bool verbose,
256 FILE **fp);
257
258 void deallocLower_queue(C_task*& queue,
259 bool isSym,
260 vector<C_task*> &task_p,
261 bool isDirect,
262 vector<C_task*> &task_q);
263
264 int getUpperNRow() const { return upperBlock().nbRows(); }
265 int getUpperNCol() const { return upperBlock().nbColumns(); }
266
267 unsigned nb() const { return _nb; }
268 unsigned level() const { return _level; }
269 int ncol_offdiag() const {return _ncol_offdiag; }
270 int nop() const { return _nop; }
271 bool islast() const { return _islast; }
272 bool alignedFather() const { return _alignedFather; }
273
274 void setNb(unsigned nb) { _nb = nb; }
275 void setLevel(unsigned level) { _level = level; }
276 void setNrow(int nrow) { _nrow = nrow; }
277 void setNcol_offdiag(int ncol_offdiag) { _ncol_offdiag = ncol_offdiag; }
278 void setNop(int nop) { _nop = nop; }
279 void setIsSym(bool isSym) { _isSym = isSym; }
280 void setIslast(bool islast) { _islast = islast; }
281 void setAlignedFather(bool alignedFather) { _alignedFather = alignedFather; }
282
283 private:
284 unsigned _nb;
285 unsigned _level;
286 int _nrow;
287 int _ncol_offdiag;
288 int _nop; // complexity of factorization
289 SquareBlockMatrix<T>* _diag; // pointer to the diagonal block
290 TridiagBlockMatrix<T, U>** _tridiag; // pointer to the tridiagonal blocks
291 RectBlockMatrix<T>* _lower; // pointer to lower off-diagonal block
292 RectBlockMatrix<T>* _upper; // pointer to upper off-diagonal block
293 bool _isSym;
294 bool _islast;
295 bool _alignedFather;
296 int _colors;
297 int *_color_mask;
298
299 SquareBlockMatrix<T> *_localSchur;
300 ColumnMatrix<T> *_factorize_LDLt;
301 // ColumnMatrix<T> *_factorize_LDLt_diag;
302 const CSR_indirect *_csr_diag;
303 const CSR_indirect *_csr_offdiag;
304 };
305
306 template<typename T>
307 class SchurMatrix
308 {
309 public:
310 SchurMatrix() { _full_pivoting = false;}
311 ~SchurMatrix() { }
312 void free() {
313 _sldu.free();
314 _sldu_list.clear(); //
315 _sldu_list_left.clear();
316 delete _arow;
317 delete _acol;
318 _scol.free();
319 _schur.free();
320 }
321 SubSquareMatrix<T>& getSldu() { return _sldu; }
322 vector<int>& getSlduList() { return _sldu_list; }
323 vector<int>& getSlduListLeft() {
324 return _full_pivoting ? _sldu_list_left : _sldu_list;
325 }
326 SparseMatrix<T>*& getArow() { return _arow; }
327 SparseMatrix<T>*& getAcol() { return _acol; }
328 ColumnMatrix<T>& getScol() { return _scol; }
329 ColumnMatrix<T>& getSchur() { return _schur; } // for debugging
330 bool isFullPivoting() { return _full_pivoting; }
331 void setFullPivoting(const bool full_pivoting) {
332 _full_pivoting = full_pivoting;
333 }
334 private:
335 SubSquareMatrix<T> _sldu;
336 vector<int> _sldu_list; // _sldu_list_right;
337 vector<int> _sldu_list_left;
338 SparseMatrix<T>* _arow;
339 SparseMatrix<T>* _acol;
340 ColumnMatrix<T> _scol;
341 ColumnMatrix<T> _schur;
342 bool _full_pivoting;
343 };
344
345 template<typename T>
346 class KernelMatrix
347 {
348 public:
349 KernelMatrix(int dimension = 0) : _dimension(dimension),
350 _full_pivoting(false) { }
351 ~KernelMatrix() { }
352 void free()
353 {
354 _singIdx.clear();
355 _kern_list_eq.clear();
356 _kern_basis.free();
357 _tkern_basis.free();
358 _kern_proj.free();
359 _tkern_proj.free();
360 _ntkern_proj.free();
361 }
362 int dimension() const { return _dimension; }
363 void set_dimension(int dimension){ _dimension = dimension; }
364 vector<int>& getSingIdx() { return _singIdx; }
365 vector<int>& getKernListEq() {
366 return _kern_list_eq;
367 }
368 vector<int>& getKernListEqLeft() {
369 return _full_pivoting ? _kern_list_eq_left : _kern_list_eq;
370 }
371 ColumnMatrix<T>& getKernBasis() {return _kern_basis; }
372 ColumnMatrix<T>& getTKernBasis() { return _tkern_basis; }
373 SquareMatrix<T>& getKernProj() { return _kern_proj; }
374 SquareMatrix<T>& getTKernProj() { return _tkern_proj; }
375 SquareMatrix<T>& getNTKernProj() { return _ntkern_proj; }
376 void setFullPivoting(const bool full_pivoting) {
377 _full_pivoting = full_pivoting;
378 }
379 private:
380 int _dimension;
381 vector<int> _singIdx;
382 vector<int> _kern_list_eq;
383 vector<int> _kern_list_eq_left;
384 ColumnMatrix<T> _kern_basis;
385 ColumnMatrix<T> _tkern_basis;
386 SquareMatrix<T> _kern_proj;
387 SquareMatrix<T> _tkern_proj;
388 SquareMatrix<T> _ntkern_proj;
389 bool _full_pivoting;
390 };
391
392 #endif