Debian Codegraph @

3rdparty/dissection/src/Driver/TridiagBlockMatrix.cpp

raw
1 /*! \file TridiagBlockMatrix.cpp
2 \brief tridiagonal factorization algorithm with Cuthill-McKee
3 \author François-Xavier Roux, ONERA, Laboratoire Jacques-Louis Lions
4 \author Atsushi Suzuki, Laboratoire Jacques-Louis Lions
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 #include "Compiler/OptionCompiler.hpp"
52 #include "Compiler/OptionLibrary.hpp"
53 #include <cstdlib>
54 #include <algorithm>
55 #include "Driver/TridiagBlockMatrix.hpp"
56 #include "Driver/C_BlasRoutines.hpp"
57 #include "Driver/C_KernDetect.hpp"
58 #include "Driver/DissectionDefault.hpp"
59 #include "Compiler/arithmetic.hpp"
60 #include "Compiler/elapsed_time.hpp"
61 #include "Algebra/SparseRenumbering.hpp"
62 #include "Algebra/VectorArray.hpp"
63 #include "Compiler/DissectionIO.hpp"
64
65 template<typename T, typename U>
66 const T TridiagBlockMatrix<T, U>::_one = T(1.0);
67 template<typename T, typename U>
68 const T TridiagBlockMatrix<T, U>::_none = T(-1.0);
69 template<typename T, typename U>
70 const T TridiagBlockMatrix<T, U>::_zero = T(0.0);
71
72 template<typename T, typename U>
73 void TridiagBlockMatrix<T, U>::SymbolicFact(const int color,
74 const int color_max,
75 int *color_mask,
76 const int dim_,
77 const int nnz_,
78 const int *prow_,
79 const int *indcols_,
80 const int *indvals_)
81 {
82 long long n1, n2;
83 // const void *fp = (void *)fp_;
84 int dim1, dim2, dim3;
85 _color = color;
86 _color_max = color_max;
87 // need count _dim and _nnz
88 _nnz = 0;
89 dim1 = 0;
90 for (int i = 0; i < dim_; i++) {
91 if (color_mask[i] == color) {
92 _nnz += prow_[i + 1] - prow_[i];
93 dim1++;
94 }
95 }
96 // isolated entries are dealt by color 1
97 dim3 = 0;
98 if (color == 1) {
99 for (int i = 0; i < dim_; i++) {
100 if (color_mask[i] == 0) {
101 _nnz++;
102 dim3++;
103 }
104 }
105 }
106 dim2 = 0;
107 for (int i = 0; i < dim_; i++) { // treatment fused block whose size is
108 // less than DIM_AUG_KERN
109 if (color_mask[i] == (-color)) {
110 _nnz += prow_[i + 1] - prow_[i];
111 dim2++;
112 }
113 }
114 _dim = dim1 + dim2 + dim3;
115 _new2old.resize(_dim);
116
117 vector<int> remap_eqn, map_eqn, prow1, indcols1;
118 remap_eqn.resize(dim_);
119 map_eqn.resize(dim_);
120 prow1.resize(dim_ + 1);
121 indcols1.resize(nnz_);
122 {
123 int i0, i1, i2, i3;
124 i0 = 0;
125 i1 = dim1;
126 i2 = dim1 + dim2;
127 i3 = _dim; // dim1 + dim2 + dim3
128 for (int i = 0; i < dim_; i++) {
129 if (color_mask[i] == color) {
130 remap_eqn[i0++] = i;
131 }
132 else if (color_mask[i] == (-color)) {
133 remap_eqn[i1++] = i;
134 }
135 else if ((color == 1) && (color_mask[i] == 0)) {
136 remap_eqn[i2++] = i;
137 }
138 else {
139 remap_eqn[i3++] = i;
140 }
141 }
142 }
143 for (int i = 0; i < dim_; i++) {
144 map_eqn[remap_eqn[i]] = i; // map : old(original) to new (mapped) index
145 }
146 RenumberCSR(dim_, remap_eqn, map_eqn,
147 prow_, indcols_, prow1, indcols1, _verbose, _fp);
148
149 vector<int> new2old, old2new;
150 new2old.resize(dim_);
151 old2new.resize(dim_);
152
153 if (dim1 > MIN_TRIDIAG_SIZE) {
154 CMK_number(dim1, &prow1[0], &indcols1[0], new2old, _verbose, _fp);
155
156 for (int i = 0; i < dim3; i++) {
157 _new2old[i] = remap_eqn[i + dim1 + dim2];
158 }
159 for (int i = 0; i < dim1; i++) {
160 _new2old[i + dim3] = remap_eqn[new2old[i]];
161 }
162 for (int i = 0; i < dim2; i++) {
163 _new2old[i + dim1 + dim3] = remap_eqn[i + dim1];
164 }
165 _nfront = point_front(dim1, &prow1[0], &indcols1[0], new2old,
166 _p_front, _verbose, _fp);
167 }
168 else {
169 _nfront = 1;
170 _p_front.resize(2);
171 _p_front[0] = 0;
172 _p_front[1] = dim1 + dim2;
173 for (int i = 0; i < _dim; i++) {
174 _new2old[i] = remap_eqn[i];
175 }
176 }
177 if (dim3 > 0) {
178 for (int i = 1; i < _nfront; i++) {
179 _p_front[i] += dim3;
180 }
181 }
182 _p_front[_nfront] = _dim; // replace the end of block
183
184 prow1.clear();
185 indcols1.clear();
186 // remap_eqn.clear();
187 map_eqn.clear();
188
189 diss_printf(_verbose, _fp,
190 "%s %d : SymbolicFact : %d ", __FILE__, __LINE__, _nb);
191 diss_printf(_verbose, _fp,
192 "color = %d dim = %d = %d + %d + %d nfront = %d : ",
193 color, _dim, dim1, dim2, dim3, _nfront);
194 for (int i = 0; i <= _nfront; i++) {
195 diss_printf(_verbose, _fp, "%d ", _p_front[i]);
196 }
197 diss_printf(_verbose, _fp, "\n");
198
199 _ptRows.resize(_dim + 1);
200 _indCols.resize(_nnz);
201 _indVals.resize(_nnz);
202
203 for (int i = 0; i < _dim; i++) {
204 new2old[i] = _new2old[i];
205 }
206 for (int i = _dim; i < dim_; i++) {
207 new2old[i] = remap_eqn[i];
208 }
209 for (int i = 0; i < dim_; i++) {
210 old2new[new2old[i]] = i;
211 }
212 {
213 bool shrink_flag = (_dim < dim_);
214 RenumberCSR(shrink_flag,
215 _dim, nnz_, new2old, old2new, prow_, indcols_, indvals_,
216 _ptRows, _indCols, _indVals, _verbose, _fp);
217 }
218 new2old.clear();
219 old2new.clear();
220
221 _p_diag.resize(_dim);
222 _p_upper.resize(_dim);
223
224 TridiagStruct(_ptRows, _indCols, _nfront, _p_front, _p_diag, _p_upper);
225
226 if (_isSymmetric) {
227 _nop = 0.0;
228 n2 = (long long)(_p_front[1] - _p_front[0]);
229 _nop += (n2 * n2 * n2) / 3.0;
230 for (int n = 1; n < _nfront; n++) {
231 n1 = n2;
232 n2 = (long long)(_p_front[n + 1] - _p_front[n]);
233 _nop += (n1 * n1 * n2) / 3.0;
234 _nop += (n1 * n2 * n2) / 3.0;
235 _nop += (n2 * n2 * n2) / 3.0;
236 }
237 }
238 else {
239 _nop = 0.0;
240 n2 = (long long)(_p_front[1] - _p_front[0]);
241 _nop += (2.0 * n2 * n2 * n2) / 3.0;
242 for (int n = 1; n < _nfront; n++) {
243 n1 = n2;
244 n2 = (long long)(_p_front[n + 1] - _p_front[n]);
245 _nop += (2.0 * n1 * n1 * n2) / 3.0;
246 _nop += (2.0 * n1 * n2 * n2) / 3.0;
247 _nop += (2.0 * n2 * n2 * n2) / 3.0;
248 }
249 }
250 _diag_blocks = new ColumnMatrix<T>[_nfront];
251 #ifndef SPARSE_OFFDIAG
252 _upper_blocks = new ColumnMatrix<T>[_nfront]; // 27 Nov.2015
253 _lower_blocks = new ColumnMatrix<T>[_nfront]; // 27 Nov.2015
254 #endif
255 #ifdef STORE_WHOLE_FACTORIZED
256 _factorized_whole.init(_dim, _dim);
257 _factorized_whole.ZeroClear();
258 #endif
259 _diag_block_alloc_status = true;
260 _maxdim = 0;
261 for (int n = 0; n < _nfront; n++) {
262 const int itmp = _p_front[n + 1] - _p_front[n];
263 _maxdim = _maxdim > itmp ? _maxdim : itmp;
264 }
265 _n0 = 0; // initialization
266 _nscol = 0;
267 remap_eqn.clear();
268 }
269
270 template
271 void TridiagBlockMatrix<double>::SymbolicFact(const int color,
272 const int color_max,
273 int *color_mask,
274 const int dim_,
275 const int nnz_,
276 const int *prow_,
277 const int *indcols_,
278 const int *indvals_);
279 template
280 void TridiagBlockMatrix<quadruple>::SymbolicFact(const int color,
281 const int color_max,
282 int *color_mask,
283 const int dim_,
284 const int nnz_,
285 const int *prow_,
286 const int *indcols_,
287 const int *indvals_);
288 template
289 void TridiagBlockMatrix<float>::SymbolicFact(const int color,
290 const int color_max,
291 int *color_mask,
292 const int dim_,
293 const int nnz_,
294 const int *prow_,
295 const int *indcols_,
296 const int *indvals_);
297
298 template void
299 TridiagBlockMatrix<complex<double>, double>::
300 SymbolicFact(const int color,
301 const int color_max,
302 int *color_mask,
303 const int dim_,
304 const int nnz_,
305 const int *prow_,
306 const int *indcols_,
307 const int *indvals_);
308
309 template
310 void TridiagBlockMatrix<complex<quadruple>, quadruple>::
311 SymbolicFact(const int color,
312 const int color_max,
313 int *color_mask,
314 const int dim_,
315 const int nnz_,
316 const int *prow_,
317 const int *indcols_,
318 const int *indvals_);
319
320 template void
321 TridiagBlockMatrix<complex<float>, float>::
322 SymbolicFact(const int color,
323 const int color_max,
324 int *color_mask,
325 const int dim_,
326 const int nnz_,
327 const int *prow_,
328 const int *indcols_,
329 const int *indvals_);
330 //
331
332
333 template<typename T, typename U>
334 void TridiagBlockMatrix<T, U>::
335 TridiagNumericFact(double *pivot,
336 const double eps_pivot,
337 const int dim_aug_kern,
338 ColumnMatrix<T> *diag_block_save,
339 vector<int> &num_null_aug,
340 double *nopd, const bool higher_precision)
341 {
342 fprintf(stderr, "%s %d : only specialized template version is impelemneted\n",
343 __FILE__, __LINE__);
344 }
345
346 template<>
347 void TridiagBlockMatrix<double>::
348 TridiagNumericFact(double *pivot,
349 const double eps_pivot,
350 const int dim_aug_kern,
351 ColumnMatrix<double> *diag_block_save,
352 vector<int> &num_null_aug,
353 double *nopd, const bool higher_precision)
354 {
355 #if 1
356 ColumnMatrix<quadruple>* diag_blocks_high;
357 list<int> high_blocks;
358 diag_blocks_high = new ColumnMatrix<quadruple>[_nfront];
359 TridiagNumericFact_<double, double,
360 quadruple, quadruple>(pivot,
361 #else
362 ColumnMatrix<double>* diag_blocks_high;
363 list<int> high_blocks;
364 diag_blocks_high = new ColumnMatrix<double>[_nfront];
365 TridiagNumericFact_<double, double,
366 double, double>(pivot,
367 #endif
368 eps_pivot,
369 dim_aug_kern,
370 diag_block_save,
371 num_null_aug,
372 nopd,
373 higher_precision,
374 _verbose,
375 _fp,
376 _dim,
377 _isSymmetric,
378 _nfront,
379 _p_front,
380 _p_diag,
381 _p_upper,
382 _coef,
383 _ptRows,
384 _indCols, // column_numb
385 _indVals, // column_numb
386 _num_null,
387 _permute,
388 _permute_ginv,
389 _diag_blocks,
390 diag_blocks_high,
391 _upper_blocks,
392 _lower_blocks,
393 high_blocks
394 #ifdef STORE_WHOLE_FACTORIZED
395 ,
396 _factorized_whole
397 #endif
398 );
399 for (list<int>::iterator it = high_blocks.begin(); it != high_blocks.end();
400 ++it) {
401 // fprintf(stderr, "%s %d : %d\n", __FILE__, __LINE__, (*it));
402 diag_blocks_high[(*it)].free();
403 }
404 delete [] diag_blocks_high;
405 high_blocks.clear();
406 #if 0
407 fprintf(stderr, "%s %d : _num_null[%d]= ", __FILE__, __LINE__, _nfront);
408 for (int i = 0; i < _nfront; i++) {
409 fprintf(stderr, "%d ", _num_null[i]);
410 }
411 fprintf(stderr, "\n");
412 #endif
413 }
414
415 template<>
416 void TridiagBlockMatrix<complex<double>, double>::
417 TridiagNumericFact(double *pivot,
418 const double eps_pivot,
419 const int dim_aug_kern,
420 ColumnMatrix<complex<double> > *diag_block_save,
421 vector<int> &num_null_aug,
422 double *nopd, const bool higher_precision)
423 {
424 ColumnMatrix<complex<quadruple> >* diag_blocks_high;
425 list<int> high_blocks;
426 diag_blocks_high = new ColumnMatrix<complex<quadruple> >[_nfront];
427 TridiagNumericFact_<complex<double>, double,
428 complex<quadruple>, quadruple>(pivot,
429 eps_pivot,
430 dim_aug_kern,
431 diag_block_save,
432 num_null_aug,
433 nopd,
434 higher_precision,
435 _verbose,
436 _fp,
437 _dim,
438 _isSymmetric,
439 _nfront,
440 _p_front,
441 _p_diag,
442 _p_upper,
443 _coef,
444 _ptRows,
445 _indCols, // column_numb
446 _indVals, // column_numb
447 _num_null,
448 _permute,
449 _permute_ginv,
450 _diag_blocks,
451 diag_blocks_high,
452 _upper_blocks,
453 _lower_blocks,
454 high_blocks
455 #ifdef STORE_WHOLE_FACTORIZED
456 ,
457 _factorized_whole
458 #endif
459 );
460 for (list<int>::iterator it = high_blocks.begin(); it != high_blocks.end();
461 ++it) {
462 diag_blocks_high[(*it)].free();
463 }
464 delete [] diag_blocks_high;
465 high_blocks.clear();
466 }
467
468 template<>
469 void TridiagBlockMatrix<quadruple>::
470 TridiagNumericFact(double *pivot,
471 const double eps_pivot,
472 const int dim_aug_kern,
473 ColumnMatrix<quadruple> *diag_block_save,
474 vector<int> &num_null_aug,
475 double *nopd, const bool higher_precision)
476 {
477 ColumnMatrix<quadruple>* diag_blocks_high;
478 list<int> high_blocks;
479 diag_blocks_high = new ColumnMatrix<quadruple>[_nfront];
480 TridiagNumericFact_<quadruple, quadruple,
481 quadruple, quadruple>(pivot,
482 eps_pivot,
483 dim_aug_kern,
484 diag_block_save,
485 num_null_aug,
486 nopd,
487 false, // higher_precision,
488 _verbose,
489 _fp,
490 _dim,
491 _isSymmetric,
492 _nfront,
493 _p_front,
494 _p_diag,
495 _p_upper,
496 _coef,
497 _ptRows,
498 _indCols, // column_numb
499 _indVals, // column_numb
500 _num_null,
501 _permute,
502 _permute_ginv,
503 _diag_blocks,
504 diag_blocks_high,
505 _upper_blocks,
506 _lower_blocks,
507 high_blocks
508 #ifdef STORE_WHOLE_FACTORIZED
509 ,
510 _factorized_whole
511 #endif
512 );
513 for (list<int>::iterator it = high_blocks.begin(); it != high_blocks.end();
514 ++it) {
515 diag_blocks_high[(*it)].free();
516 }
517 delete [] diag_blocks_high;
518 high_blocks.clear();
519
520 }
521
522 template<>
523 void TridiagBlockMatrix<complex<quadruple>, quadruple>::
524 TridiagNumericFact(double *pivot,
525 const double eps_pivot,
526 const int dim_aug_kern,
527 ColumnMatrix<complex<quadruple> >*diag_block_save,
528 vector<int> &num_null_aug,
529 double *nopd, const bool higher_precision)
530 {
531 ColumnMatrix<complex<quadruple> >* diag_blocks_high;
532 list<int> high_blocks;
533 diag_blocks_high = new ColumnMatrix<complex<quadruple> >[_nfront];
534 TridiagNumericFact_<complex<quadruple>, quadruple,
535 complex<quadruple>, quadruple>(pivot,
536 eps_pivot,
537 dim_aug_kern,
538 diag_block_save,
539 num_null_aug,
540 nopd,
541 false, // higher_precision,
542 _verbose,
543 _fp,
544 _dim,
545 _isSymmetric,
546 _nfront,
547 _p_front,
548 _p_diag,
549 _p_upper,
550 _coef,
551 _ptRows,
552 _indCols, // column_numb
553 _indVals, // column_numb
554 _num_null,
555 _permute,
556 _permute_ginv,
557 _diag_blocks,
558 diag_blocks_high,
559 _upper_blocks,
560 _lower_blocks,
561 high_blocks
562 #ifdef STORE_WHOLE_FACTORIZED
563 ,
564 _factorized_whole
565 #endif
566 );
567 for (list<int>::iterator it = high_blocks.begin(); it != high_blocks.end();
568 ++it) {
569 diag_blocks_high[(*it)].free();
570 }
571 delete [] diag_blocks_high;
572 high_blocks.clear();
573
574 }
575
576 template<>
577 void TridiagBlockMatrix<float>::
578 TridiagNumericFact(double *pivot,
579 const double eps_pivot,
580 const int dim_aug_kern,
581 ColumnMatrix<float> *diag_block_save,
582 vector<int> &num_null_aug,
583 double *nopd, const bool higher_precision)
584 {
585 ColumnMatrix<double>* diag_blocks_high;
586 list<int> high_blocks;
587 diag_blocks_high = new ColumnMatrix<double>[_nfront];
588 TridiagNumericFact_<float, float,
589 double, double>(pivot,
590 eps_pivot,
591 dim_aug_kern,
592 diag_block_save,
593 num_null_aug,
594 nopd,
595 higher_precision,
596 _verbose,
597 _fp,
598 _dim,
599 _isSymmetric,
600 _nfront,
601 _p_front,
602 _p_diag,
603 _p_upper,
604 _coef,
605 _ptRows,
606 _indCols, // column_numb
607 _indVals, // column_numb
608 _num_null,
609 _permute,
610 _permute_ginv,
611 _diag_blocks,
612 diag_blocks_high,
613 _upper_blocks,
614 _lower_blocks,
615 high_blocks
616 #ifdef STORE_WHOLE_FACTORIZED
617 ,
618 _factorized_whole
619 #endif
620 );
621 for (list<int>::iterator it = high_blocks.begin(); it != high_blocks.end();
622 ++it) {
623 // fprintf(stderr, "%s %d : %d\n", __FILE__, __LINE__, (*it));
624 diag_blocks_high[(*it)].free();
625 }
626 delete [] diag_blocks_high;
627 high_blocks.clear();
628 #if 0
629 fprintf(stderr, "%s %d : _num_null[%d]= ", __FILE__, __LINE__, _nfront);
630 for (int i = 0; i < _nfront; i++) {
631 fprintf(stderr, "%d ", _num_null[i]);
632 }
633 fprintf(stderr, "\n");
634 #endif
635 }
636
637 template<>
638 void TridiagBlockMatrix<complex<float>, float>::
639 TridiagNumericFact(double *pivot,
640 const double eps_pivot,
641 const int dim_aug_kern,
642 ColumnMatrix<complex<float> > *diag_block_save,
643 vector<int> &num_null_aug,
644 double *nopd, const bool higher_precision)
645 {
646 ColumnMatrix<complex<double> >* diag_blocks_high;
647 list<int> high_blocks;
648 diag_blocks_high = new ColumnMatrix<complex<double> >[_nfront];
649 TridiagNumericFact_<complex<float>, float,
650 complex<double>, double>(pivot,
651 eps_pivot,
652 dim_aug_kern,
653 diag_block_save,
654 num_null_aug,
655 nopd,
656 higher_precision,
657 _verbose,
658 _fp,
659 _dim,
660 _isSymmetric,
661 _nfront,
662 _p_front,
663 _p_diag,
664 _p_upper,
665 _coef,
666 _ptRows,
667 _indCols, // column_numb
668 _indVals, // column_numb
669 _num_null,
670 _permute,
671 _permute_ginv,
672 _diag_blocks,
673 diag_blocks_high,
674 _upper_blocks,
675 _lower_blocks,
676 high_blocks
677 #ifdef STORE_WHOLE_FACTORIZED
678 ,
679 _factorized_whole
680 #endif
681 );
682 for (list<int>::iterator it = high_blocks.begin(); it != high_blocks.end();
683 ++it) {
684 diag_blocks_high[(*it)].free();
685 }
686 delete [] diag_blocks_high;
687 high_blocks.clear();
688 }
689
690 template<typename T, typename U>
691 void TridiagBlockMatrix<T, U>::NumericFact(const T* coef,
692 const double eps_pivot,
693 double *pivot,
694 const bool kernel_detection,
695 const bool higher_precision,
696 const int dim_aug_kern,
697 const U eps_machine,
698 double *nopd)
699 {
700 int nsing, n0, nn0;
701 ColumnMatrix<T> a12, a21, a22, s22, b12, b21, b22;
702 double eps_piv, pivot_val;
703 bool flag_repeat_piv;
704 vector<int> perm;
705 ColumnMatrix<T>* diag_block_save;
706 vector<int> num_null_aug;
707 vector<int> list_sing;
708 const double machine_eps = todouble<U>(eps_machine);
709
710 _coef = coef;
711 diag_block_save = new ColumnMatrix<T>[_nfront];
712 TridiagNumericFact(pivot, eps_pivot,
713 dim_aug_kern,
714 diag_block_save,
715 num_null_aug, nopd, higher_precision);
716 nsing = 0;
717 for (int n = 0; n < _nfront; n++) {
718 nsing += _num_null[n];
719 }
720
721 if (nsing == 0) {
722 _nscol = 0;
723 _detected = true; // without using kernel detection
724 _n0 = 0;
725 _list_elim.clear();
726 _list_schur.clear();
727 _a12.free();
728 _a21.free();
729 _s22.free();
730 }
731 else { // if (nsing > 0)
732 _detected = false; // initialization : detection is not activated
733 diss_printf(_verbose, _fp, "%s %d nsing = %d nfront=%d : ",
734 __FILE__, __LINE__, nsing, _nfront);
735 for (int n = 0; n < _nfront; n++) {
736 diss_printf(_verbose, _fp, "%d/%d ",
737 _num_null[n], (_p_front[n + 1] - _p_front[n]));
738 if (num_null_aug[n] > 0) {
739 diss_printf(_verbose, _fp, "(+%d)", num_null_aug[n]);
740 }
741 }
742 diss_printf(_verbose, _fp, "\n");
743 s22.init(nsing, nsing);
744 perm.resize(nsing);
745 list_sing.resize(nsing);
746 nsing = 0;
747 for (int n = 0; n < _nfront; n++) {
748 const int offset = _p_front[n];
749 const int dim1 = _p_front[n + 1] - offset;
750 const int num_nonsing = dim1 - _num_null[n];
751 for (int i = num_nonsing; i < dim1; i++, nsing++) { // singular entries
752 list_sing[nsing] = _permute[i + offset] + offset;
753 }
754 }
755 a12.init(_dim, nsing);
756 a21.init(_dim, nsing);
757 a22.init(nsing, nsing);
758 ComputeSchurComplement(nsing, list_sing, a12, a21, a22);
759
760 if (higher_precision) {
761 eps_piv = machine_epsilon<double, double>();
762 }
763 else {
764 eps_piv = eps_pivot; // initial is given by user and then repeated
765 }
766 flag_repeat_piv = true;
767 int count_repeat = 0;
768 while (flag_repeat_piv) {
769 s22.copy(a22);
770 // find maximum diagonal from s22[]
771 pivot_val = 0.0;
772 for (int i = 0; i < nsing; i++) {
773 // pivot value in double
774 const double tmp = blas_abs<T, double>(s22(i, i));
775 pivot_val = tmp > pivot_val ? tmp : pivot_val;
776 }
777 n0 = 0; // by assuming the matrix is invertible
778 if (_isSymmetric) {
779 double fop;
780 full_ldlt_permute<T, U>(&nn0, n0, nsing, s22.addrCoefs(), nsing,
781 &pivot_val, &perm[0], eps_piv, &fop);
782 }
783 else {
784 double fop;
785 full_ldu_permute<T, U>(&nn0, n0, nsing, s22.addrCoefs(), nsing,
786 &pivot_val, &perm[0], eps_piv, &fop);
787 }
788 diss_printf(_verbose, _fp,
789 "%s %d factorize dim = %d eps_piv = %g -> sing = %d\n",
790 __FILE__, __LINE__, nsing, eps_piv, nn0);
791 //
792 if (((nsing - dim_aug_kern) >= nn0) || (eps_piv < TOL_PIVOT)) {
793 flag_repeat_piv = false;
794 break;
795 }
796 else {
797 eps_piv /= 10.0;
798 count_repeat++;
799 }
800 } // while (flag_repeat_piv)
801 n0 = nn0;
802 diss_printf(_verbose, _fp,
803 "%s %d : n0 = %d count_repeat = %d\n", __FILE__, __LINE__,
804 n0, count_repeat);
805 if ((n0 == 0) && (count_repeat == 0)) {
806 _n0 = 0;
807 _detected = true; // without kernel check
808 }
809 else if (!kernel_detection) {
810 _n0 = (-1); // for safety
811 _detected = false;
812 }
813 else { // if ((n0 == 0) && (count_repeat == 0)) && kernel_detection
814 int nsing1, nsing2;
815 if (n0 == 0) {
816 nsing1 = nsing;
817 b22.init(nsing1, nsing1);
818 b22.copy(a22); //
819 }
820 else { // if (n0 > 0) { // compute Schur complement again
821 nsing1 = n0 + dim_aug_kern;
822 nsing2 = nsing - nsing1;
823 if (nsing2 > 0) {
824 b21.init(nsing2, nsing1);
825 b12.init(nsing2, nsing1);
826 b22.init(nsing1, nsing1);
827 if (_isSymmetric) {
828 for (int j = 0; j < nsing1; j++) {
829 const int jj = perm[nsing2 + j];
830 for (int i = 0; i <= j; i++) {
831 const int ii = perm[nsing2 + i];
832 b22(i, j) = a22(ii, jj);
833 }
834 }
835 // symmetrize
836 for (int j = 0 ; j < nsing1; j++) {
837 for (int i = 0; i < j; i++) {
838 b22(j, i) = b22(i, j);
839 }
840 }
841 for (int i = 0; i < nsing2; i++) {
842 const int ii = perm[i];
843 for (int j = 0; j < nsing1; j++) {
844 const int jj = perm[nsing2 + j];
845 b12(i, j) = a22(ii, jj);
846 }
847 }
848 // alpha = 1
849 blas_trsm<T>(CblasLeft, CblasLower, CblasNoTrans, CblasUnit,
850 nsing2, nsing1, _one, s22.addrCoefs(), nsing,
851 b12.addrCoefs(), nsing2);
852 for (int i = 0; i < nsing2; i++) {
853 for (int j = 0; j < nsing1; j++) {
854 b21(i, j) = b12(i, j) * s22(i, i);
855 }
856 }
857 } // _isSymmetric
858 else {
859 for (int j = 0; j < nsing1; j++) {
860 const int jj = perm[nsing2 + j];
861 for (int i = 0; i < nsing1; i++) {
862 const int ii = perm[nsing2 + i];
863 b22(i, j) = a22(ii, jj);
864 }
865 }
866 for (int i = 0; i < nsing2; i++) {
867 const int ii = perm[i];
868 for (int j = 0; j < nsing1; j++) {
869 const int jj = perm[nsing2 + j];
870 b12(i, j) = a22(ii, jj);
871 b21(i, j) = a22(jj, ii);
872 }
873 }
874 // alpha = T(1)
875 blas_trsm<T>(CblasLeft, CblasLower, CblasNoTrans, CblasUnit,
876 nsing2, nsing1, _one, s22.addrCoefs(), nsing,
877 b12.addrCoefs(), nsing2);
878 blas_trsm<T>(CblasLeft, CblasLower, CblasTrans, CblasUnit,
879 nsing2, nsing1, _one, s22.addrCoefs(), nsing,
880 b21.addrCoefs(), nsing2);
881 for (int i = 0; i < nsing2; i++) {
882 for (int j = 0; j < nsing1; j++) {
883 b12(i, j) *= s22(i, i);
884 }
885 }
886 } // _isSymmetric
887 // alpha = -1
888 // beta = 1
889 blas_gemm<T>(CblasTrans, CblasNoTrans, nsing1, nsing1, nsing2, _none,
890 b21.addrCoefs(), nsing2, b12.addrCoefs(), nsing2, _one,
891 b22.addrCoefs(), nsing1);
892 b21.free();
893 b12.free();
894 } // if (nsing2 > 0)
895 else {
896 b22.init(nsing1, nsing1);
897 b22.copy(a22); //
898 }
899 } // if (n0 > 0)
900 bool flag, flag_2x2;
901 flag = ComputeDimKernel<T, U>(&nn0, &flag_2x2, b22.addrCoefs(),
902 nsing1, _isSymmetric,
903 dim_aug_kern, eps_machine, eps_piv,
904 _verbose, _fp);
905 if (!flag) {
906 _n0 = (-1); // for safety
907 _detected = false;
908 }
909 else {
910 _n0 = nn0;
911 _detected = true;
912 }
913 } // if ((n0 == 0) && (count_repeat == 0)) && kernel_detection
914 for (int n = 0; n < _nfront; n++) {
915 if (diag_block_save[n].size() > 0) {
916 _diag_blocks[n].copy(diag_block_save[n]);
917 _num_null[n] -= num_null_aug[n];
918 num_null_aug[n] = 0; // for safety
919 }
920 diag_block_save[n].free(); // release memory
921 }
922 // recounting "nsing" after determinated singularity with restoring aug_dim
923 nsing = 0;
924 for (int n = 0; n < _nfront; n++) {
925 const int offset = _p_front[n];
926 const int dim1 = _p_front[n + 1] - offset;
927 const int num_nonsing = dim1 - _num_null[n];
928 for (int i = num_nonsing; i < dim1; i++, nsing++) {
929 list_sing[nsing] = _permute[i + offset] + offset;
930 }
931 }
932 a12.free();
933 a21.free();
934 a21.free();
935 s22.free();
936 if ((nsing == _n0) || (!_detected)) { // nsing >= _n0
937 _nscol = 0;
938 _list_schur.clear();
939 _list_elim.resize(nsing);
940 for (int i = 0; i < nsing; i++){
941 _list_elim[i] = list_sing[i];
942 }
943 _a12.free();
944 _a21.free();
945 _s22.free();
946 if (!_detected) {
947 _n0 = nsing;
948 }
949 }
950 else { // if ((nsing < _n0) && _detected)
951 a12.init(_dim, nsing);
952 a21.init(_dim, nsing);
953 a22.init(nsing, nsing);
954 ComputeSchurComplement(nsing, list_sing, a12, a21, a22);
955 // debug : end
956 pivot_val = 0.0;
957 for (int i = 0; i < nsing; i++) {
958 // pivot value in double
959 const double tmp = blas_abs<T, double>(a22(i, i));
960 pivot_val = tmp > pivot_val ? tmp : pivot_val;
961 }
962 diss_printf(_verbose, _fp, "%s %d : _n0 = %d nsing = %d\n",
963 __FILE__, __LINE__, _n0, nsing);
964 // factorize Schur complement by knowing the dimension of the kernle : _n0
965 if (_isSymmetric) {
966 double fop;
967 bool flag;
968 flag = full_ldlt_permute<T, U>(&nn0, _n0, nsing, a22.addrCoefs(), nsing,
969 &pivot_val, &perm[0], machine_eps, &fop);
970 if (!flag) {
971 diss_printf(_verbose, _fp,
972 "%s %d : full_ldlt_permute fails : %d != %d\n",
973 __FILE__, __LINE__, _n0, nn0);
974 }
975 }
976 else {
977 double fop;
978 bool flag;
979 flag = full_ldu_permute<T, U>(&nn0, _n0, nsing, a22.addrCoefs(), nsing,
980 &pivot_val, &perm[0], machine_eps, &fop);
981 if (!flag) {
982 diss_printf(_verbose, _fp,
983 "%s %d : full_ldu_permute fails : %d != %d\n",
984 __FILE__, __LINE__, _n0, nn0);
985 }
986 }
987 _nscol = nsing - _n0;
988 diss_printf(_verbose, _fp, "%s %d : detected = %s : _nscol = %d _n0 = %d : perm[] = ",
989 __FILE__, __LINE__, (_detected ? "true" : "false"),
990 _nscol, _n0);
991 for (int i = 0; i < nsing; i++) {
992 diss_printf(_verbose, _fp, "%d ", perm[i]);
993 }
994 diss_printf(_verbose, _fp, "\n");
995 _list_schur.resize(_nscol);
996 if (_n0 > 0) {
997 _list_elim.resize(_n0);
998 }
999 for (int i = 0; i < _nscol; i++){
1000 _list_schur[i] = _permute_ginv[list_sing[perm[i]]]; // regular part
1001 }
1002 for (int i = 0; i < _n0; i++){
1003 _list_elim[i] = list_sing[perm[i + _nscol]];
1004 }
1005
1006 diss_printf(_verbose, _fp, "%s %d : _nscol = %d : _list_schur[] = ",
1007 __FILE__, __LINE__, _nscol);
1008 for (int i = 0; i < _nscol; i++) {
1009 diss_printf(_verbose, _fp, "%d ", _list_schur[i]);
1010 }
1011 diss_printf(_verbose, _fp, "\n");
1012 if (_n0 > 0) {
1013 diss_printf(_verbose, _fp, "%s %d : n0 = %d : _list_elim[] = ",
1014 __FILE__, __LINE__, _n0);
1015 for (int i = 0; i < _n0; i++) {
1016 diss_printf(_verbose, _fp, "%d ", _list_elim[i]);
1017 }
1018 diss_printf(_verbose, _fp, "\n");
1019
1020 diss_printf(_verbose, _fp,
1021 "%s %d : n0 = %d : _new2old[_list_elim[]] = ",
1022 __FILE__, __LINE__, _n0);
1023 for (int i = 0; i < _n0; i++) {
1024 diss_printf(_verbose, _fp, "%d ", _new2old[_list_elim[i]]);
1025 }
1026 diss_printf(_verbose, _fp, "\n");
1027 } // if (_n0 > 0)
1028
1029 _a12.init(_dim, _nscol);
1030 for (int j = 0; j < _nscol; j++) {
1031 for (int i = 0; i < _dim; i++) {
1032 _a12(_permute_ginv[i], j) = a12(i, perm[j]);
1033 }
1034 }
1035 if (!_isSymmetric) {
1036 _a21.init(_dim, _nscol);
1037 for (int j = 0; j < _nscol; j++) {
1038 for (int i = 0; i < _dim; i++) {
1039 _a21(_permute_ginv[i], j) = a21(i, perm[j]);
1040 }
1041 }
1042 }
1043 else {
1044 _a21.free();
1045 }
1046 _s22.init(_nscol, _nscol);
1047 // a22 is already permuted by perm[] during LDLt/LDU,
1048 // _s22 is smaller than a22
1049 for (int i = 0; i < _nscol; i++) {
1050 for (int j = 0; j < _nscol; j++) {
1051 _s22(i, j) = a22(i, j);
1052 }
1053 }
1054 a12.free();
1055 a21.free();
1056 a22.free();
1057 }
1058 perm.clear();
1059 } // if (nsing > 0)
1060 delete [] diag_block_save;
1061 }
1062
1063 template
1064 void TridiagBlockMatrix<double>::
1065 NumericFact(const double* coef,
1066 const double eps_pivot,
1067 double *pivot,
1068 const bool kernel_detection,
1069 const bool higher_precision,
1070 const int dim_aug_kern,
1071 const double eps_machine,
1072 double *nopd);
1073
1074 template
1075 void TridiagBlockMatrix<quadruple>::
1076 NumericFact(const quadruple* coef,
1077 const double eps_pivot,
1078 double *pivot,
1079 const bool kernel_detection,
1080 const bool higher_precision,
1081 const int dim_aug_kern,
1082 const quadruple eps_machine,
1083 double *nopd);
1084
1085 template void
1086 TridiagBlockMatrix<complex<double>, double>::
1087 NumericFact(const complex<double>* coef,
1088 const double eps_pivot,
1089 double *pivot,
1090 const bool kernel_detection,
1091 const bool higher_precision,
1092 const int dim_aug_kern,
1093 const double eps_machine,
1094 double *nopd);
1095
1096 template
1097 void TridiagBlockMatrix<complex<quadruple>, quadruple>::
1098 NumericFact(const complex<quadruple>* coef,
1099 const double eps_pivot,
1100 double *pivot,
1101 const bool kernel_detection,
1102 const bool higher_precision,
1103 const int dim_aug_kern,
1104 const quadruple eps_machine,
1105 double *nopd);
1106
1107 template
1108 void TridiagBlockMatrix<float>::
1109 NumericFact(const float* coef,
1110 const double eps_pivot,
1111 double *pivot,
1112 const bool kernel_detection,
1113 const bool higher_precision,
1114 const int dim_aug_kern,
1115 const float eps_machine,
1116 double *nopd);
1117
1118 template void
1119 TridiagBlockMatrix<complex<float>, float>::
1120 NumericFact(const complex<float>* coef,
1121 const double eps_pivot,
1122 double *pivot,
1123 const bool kernel_detection,
1124 const bool higher_precision,
1125 const int dim_aug_kern,
1126 const float eps_machine,
1127 double *nopd);
1128 //
1129
1130 template<typename T, typename U>
1131 void TridiagBlockMatrix<T, U>::ComputeSchurComplement(const int nsing,
1132 vector<int> &list_sing,
1133 ColumnMatrix<T>& a12,
1134 ColumnMatrix<T>& a21,
1135 ColumnMatrix<T>& a22)
1136 {
1137 // _nscol should be set 0 before calling
1138 a12.ZeroClear();
1139 a21.ZeroClear();
1140 a22.ZeroClear();
1141 for (int j = 0; j < nsing; j++) {
1142 extract_column(list_sing[j], a12.addrCoefs() + (j * _dim));
1143 }
1144 for (int j = 0; j < nsing; j++) {
1145 for (int i = 0; i < nsing; i++) {
1146 a22(i, j) = a12(list_sing[i], j);
1147 }
1148 }
1149 for (int j = 0; j < nsing; j++) {
1150 for (int i = 0; i < nsing; i++) {
1151 a12(list_sing[i], j) = _zero;
1152 }
1153 }
1154 if (_isSymmetric) {
1155 a21.copy(a12);
1156 }
1157 else {
1158 for (int j = 0; j < nsing; j++) {
1159 extract_row(list_sing[j], a21.addrCoefs() + (j * _dim));
1160 }
1161 for (int j = 0; j < nsing; j++) {
1162 for (int i = 0; i < nsing; i++) {
1163 a21(list_sing[i], j) = _zero;
1164 }
1165 }
1166 }
1167 SolveMulti(false, false, nsing, a12, 0);
1168 // alpha = -1
1169 // beta = 1
1170 blas_gemm<T>(CblasTrans, CblasNoTrans, nsing, nsing, _dim, _none,
1171 a21.addrCoefs(), _dim, a12.addrCoefs(), _dim, _one,
1172 a22.addrCoefs(), nsing);
1173 if (_isSymmetric) {
1174 // symmetrize
1175 for (int i = 0; i < nsing; i++) {
1176 for (int j = i + 1; j < nsing; j++) {
1177 a22(j, i) = a22(i, j); // acess lower
1178 }
1179 }
1180 }
1181 else {
1182 SolveMulti(false, true, nsing, a21, 0);
1183 }
1184 }
1185
1186 template
1187 void TridiagBlockMatrix<double>::
1188 ComputeSchurComplement(const int nsing, vector<int> &list_sing,
1189 ColumnMatrix<double>& a12,
1190 ColumnMatrix<double>& a21,
1191 ColumnMatrix<double>& a22);
1192
1193 template
1194 void TridiagBlockMatrix<quadruple>::
1195 ComputeSchurComplement(const int nsing, vector<int> &list_sing,
1196 ColumnMatrix<quadruple>& a12,
1197 ColumnMatrix<quadruple>& a21,
1198 ColumnMatrix<quadruple>& a22);
1199
1200 template void
1201 TridiagBlockMatrix<complex<double>, double>::
1202 ComputeSchurComplement(const int nsing, vector<int> &list_sing,
1203 ColumnMatrix<complex<double> >& a12,
1204 ColumnMatrix<complex<double> >& a21,
1205 ColumnMatrix<complex<double> >& a22);
1206
1207
1208 template
1209 void TridiagBlockMatrix<complex<quadruple>, quadruple>::
1210 ComputeSchurComplement(const int nsing, vector<int> &list_sing,
1211 ColumnMatrix<complex<quadruple> >& a12,
1212 ColumnMatrix<complex<quadruple> >& a21,
1213 ColumnMatrix<complex<quadruple> >& a22);
1214
1215 template
1216 void TridiagBlockMatrix<float>::
1217 ComputeSchurComplement(const int nsing, vector<int> &list_sing,
1218 ColumnMatrix<float>& a12,
1219 ColumnMatrix<float>& a21,
1220 ColumnMatrix<float>& a22);
1221
1222 template void
1223 TridiagBlockMatrix<complex<float>, float>::
1224 ComputeSchurComplement(const int nsing, vector<int> &list_sing,
1225 ColumnMatrix<complex<float> >& a12,
1226 ColumnMatrix<complex<float> >& a21,
1227 ColumnMatrix<complex<float> >& a22);
1228
1229 //
1230
1231 template<typename T, typename U>
1232 void TridiagBlockMatrix<T, U>::SingularNode(vector<int> &list_sing)
1233 {
1234 if (_n0 >= 0) {
1235 list_sing.resize(_n0);
1236 for (int i = 0; i < _n0; i++) {
1237 list_sing[i] = _new2old[_list_elim[i]];
1238 }
1239 }
1240 else {
1241 diss_printf(_verbose, _fp, "%s %d : _n0 = %d\n", __FILE__, __LINE__, _n0);
1242 }
1243 }
1244
1245 template
1246 void TridiagBlockMatrix<double>::
1247 SingularNode(vector<int> &list_sing);
1248
1249 template
1250 void TridiagBlockMatrix<quadruple>::
1251 SingularNode(vector<int> &list_sing);
1252
1253 template void
1254 TridiagBlockMatrix<complex<double>, double>::
1255 SingularNode(vector<int> &list_sing);
1256
1257 template
1258 void TridiagBlockMatrix<complex<quadruple>, quadruple>::
1259 SingularNode(vector<int> &list_sing);
1260
1261 template
1262 void TridiagBlockMatrix<float>::
1263 SingularNode(vector<int> &list_sing);
1264
1265 template void
1266 TridiagBlockMatrix<complex<float>, float>::
1267 SingularNode(vector<int> &list_sing);
1268
1269 //
1270
1271 //#define DEBUG_FACTORIZATION
1272 template<typename T, typename U, typename W, typename Z>
1273 void TridiagNumericFact_(double *pivot,
1274 const double eps_pivot,
1275 const int dim_aug_kern,
1276 ColumnMatrix<T> *diag_block_save,
1277 vector<int> &num_null_aug,
1278 double *nopd,
1279 const bool higher_precision,
1280 bool &_verbose,
1281 FILE* &_fp,
1282 int &_dim,
1283 bool &_isSymmetric,
1284 int &_nfront,
1285 vector<int> &_p_front,
1286 vector<int> &_p_diag,
1287 vector<int> &_p_upper,
1288 const T* &_coef,
1289 vector<int> &_ptRows,
1290 vector<int> &_indCols, // column_numb
1291 vector<int> &_indVals, // column_numb
1292 vector<int> &_num_null,
1293 vector<int> &_permute,
1294 vector<int> &_permute_ginv,
1295 ColumnMatrix<T>* &_diag_blocks,
1296 ColumnMatrix<W>* &_diag_blocks_high,
1297 ColumnMatrix<T>* &_upper_blocks,
1298 ColumnMatrix<T>* &_lower_blocks,
1299 list<int> &_high_blocks
1300 #ifdef STORE_WHOLE_FACTORIZED
1301 ,
1302 ColumnMatrix<T> &_factorized_whole
1303 #endif
1304 )
1305 {
1306 int dim1, dim2, nn0;
1307 const int n0 = 0;
1308 int n0_total = 0;
1309 ColumnMatrix<T> diag_works, diag_origs; // supposing "dim2 <= size_b2"
1310 #ifdef SPARSE_OFFDIAG
1311 ColumnMatrix<T> upper, lower;
1312 ColumnMatrix<W> upper_high, lower_high;
1313 #endif
1314 int upper_size_max;
1315 int diag_size_max;
1316 int *permute_diag, *permute_offdiag, *permute_work;
1317 int *permute_diag_inv, *permute_offdiag_inv;
1318 double fop;
1319 //const double eps_pivot_high = machine_epsilon<double, quadruple>();
1320 const double eps_pivot_high = machine_epsilon<double, double>();
1321 const T _zero(0.0);
1322 double pivot_save;
1323 _num_null.resize(_nfront);
1324 // _null_lists_local.resize(_nfront);
1325 _permute.resize(_dim);
1326 *pivot = 1.0;
1327 upper_size_max = 0;
1328 for (int n = 1; n < _nfront; n++) {
1329 const int itmp = ((_p_front[n] - _p_front[n - 1]) *
1330 (_p_front[n + 1] - _p_front[n]));
1331 upper_size_max = itmp > upper_size_max ? itmp : upper_size_max;
1332 }
1333 #ifdef SPARSE_OFFDIAG
1334 VectorArray<T> upper_work(upper_size_max);
1335 VectorArray<T> lower_work(upper_size_max);
1336 if (higher_precision) {
1337 VectorArray<W> upper_high_work(upper_size_max);
1338 VectorArray<W> lower_high_work(upper_size_max);
1339 }
1340 #else
1341 VectorArray<T> permute_vals(upper_size_max);
1342 #endif
1343 bool high_factorization = false;
1344 bool high_schur_complement = false;
1345 diag_size_max = 0;
1346 for (int n = 0; n < _nfront; n++) {
1347 const int itmp = _p_front[n + 1] - _p_front[n];
1348 _diag_blocks[n].init(itmp, itmp);
1349 diag_size_max = itmp > diag_size_max ? itmp : diag_size_max;
1350 }
1351 permute_offdiag = new int[diag_size_max];
1352 permute_offdiag_inv = new int[diag_size_max];
1353 permute_diag_inv = new int[diag_size_max];
1354 permute_work = new int[diag_size_max];
1355
1356 { // n = 0
1357 dim2 = _p_front[1] - _p_front[0];
1358 ColumnMatrix<T> &diag = _diag_blocks[0];
1359 diag.ZeroClear();
1360 permute_diag = &_permute[0] + _p_front[0];
1361
1362 for (int i = _p_front[0]; i < _p_front[1]; i++) {
1363 for (int k = _p_diag[i]; k < _p_upper[i]; k++) {
1364 const int ii = i - _p_front[0];
1365 const int jj = _indCols[k] - _p_front[0];
1366 diag(ii, jj) = _coef[_indVals[k]];
1367 }
1368 }
1369 if (higher_precision) { // copy to higher precision
1370 ColumnMatrix<W> &diag_high = _diag_blocks_high[0];
1371 diag_high.init(dim2, dim2);
1372 for (int j = 0; j < dim2; j++) {
1373 for (int i = 0; i < dim2; i++) {
1374 diag_high(i, j) = conv_prec<W, T>(diag(i, j));
1375 }
1376 }
1377 pivot_save = *pivot;
1378 }
1379 if (_isSymmetric) {
1380 bool flag;
1381 flag = full_ldlt_permute<T, U>(&nn0, n0, dim2,
1382 diag.addrCoefs(), dim2, pivot,
1383 permute_diag, eps_pivot, &fop);
1384 }
1385 else {
1386 bool flag;
1387 flag = full_ldu_permute<T, U>(&nn0, n0, dim2,
1388 diag.addrCoefs(), dim2, pivot,
1389 permute_diag, eps_pivot, &fop);
1390 }
1391 if (higher_precision) {
1392 high_factorization = false;
1393 if (nn0 > 0) {
1394 // if (true) {
1395 fprintf(stderr, "%s %d : switch to quadruple at %d %d/%d ",
1396 __FILE__, __LINE__, 0, nn0, dim2);
1397 high_factorization = true;
1398 _high_blocks.push_back(0);
1399 ColumnMatrix<W> &diag_high = _diag_blocks_high[0];
1400 *pivot = pivot_save;
1401 if (_isSymmetric) {
1402 bool flag;
1403 flag = full_ldlt_permute<W, Z>(&nn0, n0, dim2,
1404 diag_high.addrCoefs(), dim2, pivot,
1405 permute_diag, eps_pivot_high, &fop);
1406 }
1407 else {
1408 bool flag;
1409 flag = full_ldu_permute<W, Z>(&nn0, n0, dim2,
1410 diag_high.addrCoefs(), dim2, pivot,
1411 permute_diag, eps_pivot_high, &fop);
1412 }
1413 fprintf(stderr, "%d\n", nn0);
1414 #if 0
1415 for (int i = 0; i < dim2; i++) {
1416 fprintf(stderr, "%d %s %s\n", i,
1417 tostring<T>(diag(i,i)).c_str(),
1418 tostring<W>(diag_high(i,i)).c_str());
1419 }
1420 #endif
1421 for (int j = 0; j < dim2; j++) {
1422 for (int i = 0; i < dim2; i++) {
1423 diag(i, j) = conv_prec<T, W>(diag_high(i, j));
1424 }
1425 }
1426 }
1427 }
1428 {
1429 _num_null[0] = nn0;
1430 *nopd += fop;
1431 for (int i = 0; i < nn0; i++) {
1432 // _null_lists_local[0].push_back(i + (dim2 - nn0));
1433 // nullifying rows and columns
1434 for (int i = (dim2 - nn0); i < dim2; i++) {
1435 for (int j = 0; j < dim2; j++) {
1436 diag(i, j) = _zero;
1437 diag(j, i) = _zero;
1438 }
1439 }
1440 }
1441 n0_total += nn0;
1442 }
1443 } // n = 0
1444 for (int n = 1; n < _nfront; n++) {
1445 ColumnMatrix<T> &diag1 = _diag_blocks[n - 1];
1446 vector<int> i0;
1447 dim1 = dim2;
1448 dim2 = _p_front[n + 1] - _p_front[n];
1449 #ifdef SPARSE_OFFDIAG
1450 upper.init(dim1, dim2, upper_work.addrCoefs(), false);
1451 lower.init(dim1, dim2, lower_work.addrCoefs(), false);
1452 upper_high.init(dim1, dim2, upper_high_work.addrCoefs(), false);
1453 lower_high.init(dim1, dim2, lower_high_work.addrCoefs(), false);
1454 #else
1455 ColumnMatrix<T> &upper = _upper_blocks[n]; // 27 Nov.2015
1456 ColumnMatrix<T> &lower = _lower_blocks[n]; // 27 Nov.2015
1457 upper.init(dim1, dim2);
1458 lower.init(dim1, dim2);
1459 ColumnMatrix<W> upper_high;
1460 ColumnMatrix<W> lower_high;
1461 if (high_factorization) {
1462 upper_high.init(dim1, dim2);
1463 lower_high.init(dim1, dim2);
1464 }
1465 #endif
1466 // ColumnMatrix<T> upper, lower
1467 // permute_diag[] is defined in the previous step n-1
1468 // generate permute_offdiag[] from previous permute_diag[]
1469 GenPermuteOffdiag(_p_front[n - 1], _p_front[n], _p_front[n + 1],
1470 _ptRows, _p_diag, _indCols,
1471 permute_diag, permute_diag_inv,
1472 permute_offdiag, permute_offdiag_inv,
1473 i0);
1474 upper.ZeroClear();
1475 if (_isSymmetric) {
1476 for (int i = _p_front[n]; i < _p_front[n + 1]; i++) {
1477 const int jj = permute_offdiag_inv[i - _p_front[n]];
1478 for (int k = _ptRows[i]; k < _p_diag[i]; k++) {
1479 // access lower block to create upper block
1480 const int ii = permute_diag_inv[_indCols[k] - _p_front[n - 1]];
1481 upper(ii, jj) = _coef[_indVals[k]];
1482 }
1483 }
1484 }
1485 else {
1486 lower.ZeroClear();
1487 for (int i = _p_front[n - 1]; i < _p_front[n]; i++) {
1488 const int ii = permute_diag_inv[i - _p_front[n - 1]];
1489 for (int k = _p_upper[i]; k < _ptRows[i + 1]; k++) {
1490 const int jj = permute_offdiag_inv[_indCols[k] - _p_front[n]];
1491 upper(ii, jj) = _coef[_indVals[k]];
1492 }
1493 }
1494 for (int i = _p_front[n]; i < _p_front[n + 1]; i++) {
1495 const int jj = permute_offdiag_inv[i - _p_front[n]];
1496 for (int k = _ptRows[i]; k < _p_diag[i]; k++) {
1497 // same as upper() of _isSymmetric
1498 const int ii = permute_diag_inv[_indCols[k] - _p_front[n - 1]];
1499 lower(ii, jj) = _coef[_indVals[k]];
1500 }
1501 }
1502 }
1503 ColumnMatrix<T> &diag = _diag_blocks[n];
1504 if (high_factorization) {
1505 ColumnMatrix<W> &diag1_high = _diag_blocks_high[n - 1];
1506 for (int j = 0; j < dim2; j++) {
1507 for (int i = 0; i < dim1; i++) {
1508 upper_high(i, j)=conv_prec<W, T>(upper(i,j));
1509 lower_high(i, j)=conv_prec<W, T>(lower(i,j));
1510 }
1511 }
1512 full_fw_multiprofile<W>(false, dim1, _num_null[n - 1], dim2,
1513 diag1_high.addrCoefs(), dim1,
1514 upper_high.addrCoefs(), dim1, i0, &fop);
1515 if (_isSymmetric) { // reduce arithmeic by setting T(0) for 0 <=i< i0[j]
1516 for (int j = 0; j < dim2; j++) {
1517 for (int i = 0; i < dim1; i++) {
1518 lower_high(i, j) = upper_high(i, j) * diag1_high(i, i);
1519 }
1520 }
1521 }
1522 else {
1523 full_fw_multiprofile<W>(true, dim1, _num_null[n - 1], dim2,
1524 diag1_high.addrCoefs(), dim1,
1525 lower_high.addrCoefs(), dim1, i0, &fop);
1526 for (int j = 0; j < dim2; j++) {
1527 for (int i = 0; i < dim1; i++) {
1528 lower_high(i, j) *= diag1_high(i, i);
1529 }
1530 }
1531 }
1532 ColumnMatrix<W> &diag_high = _diag_blocks_high[n];
1533 diag_high.init(dim2, dim2);
1534 // alpha = -1, beta = 0
1535 SparseSchur<W>(_isSymmetric, dim2, dim1, i0, upper_high, lower_high,
1536 diag_high, &fop);
1537 for (int i = _p_front[n]; i < _p_front[n + 1]; i++) {
1538 for (int k = _p_diag[i]; k < _p_upper[i]; k++) {
1539 const int ii = permute_offdiag_inv[i - _p_front[n]];
1540 const int jj = permute_offdiag_inv[_indCols[k] - _p_front[n]];
1541 diag_high(ii, jj) += conv_prec<W, T>(_coef[_indVals[k]]);
1542 }
1543 }
1544 for (int j = 0; j < dim2; j++) {
1545 for (int i = 0; i < dim2; i++) {
1546 diag(i, j) = conv_prec<T, W>(diag_high(i, j));
1547 }
1548 }
1549 #ifndef SPARSE_OFFDIAG
1550 for (int j = 0; j < dim2; j++) {
1551 for (int i = 0; i < dim1; i++) {
1552 upper_high(i, j) *= diag1_high(i, i);
1553 }
1554 }
1555 #endif
1556 for (int j = 0; j < dim2; j++) {
1557 for (int i = 0; i < dim1; i++) {
1558 upper(i, j)=conv_prec<T, W>(upper_high(i,j));
1559 lower(i, j)=conv_prec<T, W>(lower_high(i,j));
1560 }
1561 }
1562 #ifndef SPARSE_OFFDIAG
1563 upper_high.free();
1564 lower_high.free();
1565 #endif
1566 high_schur_complement = true;
1567 // diag_high.free();
1568 } // if (high_factorization)
1569 else {
1570 full_fw_multiprofile<T>(false, dim1, _num_null[n - 1], dim2,
1571 diag1.addrCoefs(), dim1,
1572 upper.addrCoefs(), dim1, i0, &fop);
1573 if (_isSymmetric) { // reduce arithmeic by setting T(0) for 0 <=i< i0[j]
1574 for (int j = 0; j < dim2; j++) {
1575 for (int i = 0; i < dim1; i++) {
1576 lower(i, j) = upper(i, j) * diag1(i, i);
1577 }
1578 }
1579 }
1580 else {
1581 full_fw_multiprofile<T>(true, dim1, _num_null[n - 1], dim2,
1582 diag1.addrCoefs(), dim1,
1583 lower.addrCoefs(), dim1, i0, &fop);
1584 for (int j = 0; j < dim2; j++) {
1585 for (int i = 0; i < dim1; i++) {
1586 lower(i, j) *= diag1(i, i);
1587 }
1588 }
1589 }
1590 // alpha = -1, beta = 0
1591 SparseSchur<T>(_isSymmetric, dim2, dim1, i0, upper, lower, diag, &fop);
1592 for (int i = _p_front[n]; i < _p_front[n + 1]; i++) {
1593 for (int k = _p_diag[i]; k < _p_upper[i]; k++) {
1594 const int ii = permute_offdiag_inv[i - _p_front[n]];
1595 const int jj = permute_offdiag_inv[_indCols[k] - _p_front[n]];
1596 diag(ii, jj) += _coef[_indVals[k]];
1597 }
1598 }
1599 #ifndef SPARSE_OFFDIAG
1600 for (int j = 0; j < dim2; j++) {
1601 for (int i = 0; i < dim1; i++) {
1602 upper(i, j) *= diag1(i, i);
1603 }
1604 }
1605 #endif
1606 } // if (high_factorization)
1607 if (higher_precision) { // copy to higher precision
1608 ColumnMatrix<W> &diag_high = _diag_blocks_high[n];
1609 if (!high_schur_complement) {
1610 diag_high.init(dim2, dim2);
1611 for (int j = 0; j < dim2; j++) {
1612 for (int i = 0; i < dim2; i++) {
1613 diag_high(i, j) = conv_prec<W, T>(diag(i, j));
1614 }
1615 }
1616 }
1617 pivot_save = *pivot;
1618 }
1619 if (_isSymmetric) {
1620 bool flag;
1621 flag = full_ldlt_permute<T, U>(&nn0, n0, dim2, diag.addrCoefs(), dim2,
1622 pivot, permute_work, eps_pivot, &fop);
1623 }
1624 else {
1625 bool flag;
1626 flag = full_ldu_permute<T, U>(&nn0, n0, dim2, diag.addrCoefs(), dim2,
1627 pivot, permute_work, eps_pivot, &fop);
1628 }
1629
1630 if (higher_precision) {
1631 high_factorization = false;
1632 if (nn0 > 0) {
1633 // if (true) {
1634 fprintf(stderr, "%s %d : switch to quadruple at %d %d/%d : ",
1635 __FILE__, __LINE__, n, nn0, dim2);
1636 high_factorization = true;
1637 _high_blocks.push_back(n);
1638 ColumnMatrix<W> &diag_high = _diag_blocks_high[n];
1639 *pivot = pivot_save;
1640 if (_isSymmetric) {
1641 bool flag;
1642 flag = full_ldlt_permute<W, Z>(&nn0, n0, dim2,
1643 diag_high.addrCoefs(), dim2, pivot,
1644 permute_work, eps_pivot_high, &fop);
1645 }
1646 else {
1647 bool flag;
1648 flag = full_ldu_permute<W, Z>(&nn0, n0, dim2,
1649 diag_high.addrCoefs(), dim2, pivot,
1650 permute_work, eps_pivot_high, &fop);
1651 }
1652 fprintf(stderr, "%d\n", nn0);
1653 #if 0
1654 for (int i = 0; i < dim2; i++) {
1655 fprintf(stderr, "%d %s %s\n", i,
1656 tostring<T>(diag(i,i)).c_str(),
1657 tostring<W>(diag_high(i,i)).c_str());
1658 }
1659 #endif
1660 for (int j = 0; j < dim2; j++) {
1661 for (int i = 0; i < dim2; i++) {
1662 diag(i, j) = conv_prec<T, W>(diag_high(i, j));
1663 }
1664 }
1665 }
1666 else {
1667 #if 0
1668 fprintf(stderr, "%s %d : not using quadruple at %d %d/%d : ",
1669 __FILE__, __LINE__, n, nn0, dim2);
1670 fprintf(stderr, "%d\n", nn0);
1671 // if (nn0 > 0) {
1672 {
1673 for (int i = 0; i < dim2; i++) {
1674 fprintf(stderr, "%d %s\n", i,
1675 tostring<T>(diag(i,i)).c_str());
1676 }
1677 }
1678 #endif
1679 ColumnMatrix<W> &diag_high = _diag_blocks_high[n];
1680 diag_high.free();
1681 }
1682 high_schur_complement = false;
1683 } // if (higher_precision)
1684 permute_diag = &_permute[0] + _p_front[n];
1685 // generate permute_diag from pemrute_work + perumte_upper
1686 for (int i = 0; i < dim2; i++) {
1687 permute_diag[i] = permute_offdiag[permute_work[i]];
1688 }
1689 #ifndef SPARSE_OFFDIAG
1690 for (int j = 0; j < dim2; j++) {
1691 for (int i = 0; i < dim1; i++) {
1692 permute_vals[i + j * dim1] = upper(i, permute_work[j]);
1693 }
1694 }
1695 for (int j = 0; j < dim2; j++) {
1696 for (int i = 0; i < dim1; i++) {
1697 upper(i, j) = permute_vals[i + j * dim1];
1698 }
1699 }
1700 for (int j = 0; j < dim2; j++) {
1701 for (int i = 0; i < dim1; i++) {
1702 permute_vals[i + j * dim1] = lower(i, permute_work[j]);
1703 }
1704 }
1705 for (int j = 0; j < dim2; j++) {
1706 for (int i = 0; i < dim1; i++) {
1707 lower(i, j) = permute_vals[i + j * dim1];
1708 }
1709 }
1710 #endif
1711 {
1712 _num_null[n] = nn0;
1713 *nopd += fop;
1714 for (int i = 0; i < nn0; i++) {
1715 // nullifying rows and columns
1716 for (int i = (dim2 - nn0); i < dim2; i++) {
1717 for (int j = 0; j < dim2; j++) {
1718 diag(i, j) = _zero;
1719 diag(j, i) = _zero;
1720 }
1721 }
1722 } // loop : i
1723 n0_total += nn0;
1724 }
1725 } // loop : n
1726 if ((n0_total > 0) && (dim_aug_kern > 0)) {
1727 int count = 0;
1728 bool flag = false;
1729 num_null_aug.clear();
1730 num_null_aug.resize(_nfront, 0);
1731 for (int n = (_nfront - 1); n >= 0; n--) {
1732 dim1 = _p_front[n + 1] - _p_front[n];
1733 for (int k = (dim1 - _num_null[n] - 1); k >= 0; k--) {
1734 num_null_aug[n]++;
1735 count++;
1736 if (count >= dim_aug_kern) {
1737 flag = true;
1738 break;
1739 }
1740 }
1741 if (flag) {
1742 break;
1743 }
1744 }
1745 if (flag == false) {
1746 diss_printf(_verbose, _fp, "%s %d : dim_aug_kern = %d > dim = %d?\n",
1747 __FILE__, __LINE__, dim_aug_kern, _dim);
1748 }
1749 for (int n = (_nfront - 1); n >= 0; n--) {
1750 diag_block_save[n].free();
1751 if (num_null_aug[n] > 0) {
1752 dim1 = _p_front[n + 1] - _p_front[n];
1753 diag_block_save[n].init(dim1, dim1);
1754 diag_block_save[n].copy(_diag_blocks[n]);
1755 _num_null[n] += num_null_aug[n]; //
1756 for (int j = 0; j < num_null_aug[n]; j++) {
1757 const int jj = dim1 - _num_null[n] + j;
1758 for (int i = 0; i < dim1; i++) {
1759 _diag_blocks[n](i, jj) = _zero;
1760 _diag_blocks[n](jj, i) = _zero;
1761 }
1762 }
1763 }
1764 } // loop : n
1765 } // if ((n0_total > 0) && kernel_detection) {
1766 _permute_ginv.resize(_dim);
1767 for (int n = 0; n < _nfront; n++) {
1768 const int offset = _p_front[n];
1769 for (int i = offset; i < _p_front[n + 1]; i++) {
1770 _permute_ginv[_permute[i] + offset] = i;
1771 }
1772 }
1773 //#ifdef SPARSE_OFFDIAG
1774 // delete [] upper_work;
1775 // delete [] lower_work;
1776 //#else
1777 // delete [] permute_vals;
1778 //#endif
1779 delete [] permute_offdiag;
1780 delete [] permute_diag_inv;
1781 delete [] permute_offdiag_inv;
1782 delete [] permute_work;
1783 //#ifdef DEBUG_FACTORIZATION
1784 // delete [] diag_orig;
1785 //#endif
1786
1787 }
1788
1789 template void
1790 TridiagNumericFact_<double, double,
1791 quadruple, quadruple>(double *pivot,
1792 const double eps_pivot,
1793 const int dim_aug_kern,
1794 ColumnMatrix<double> *diag_block_save,
1795 vector<int> &num_null_aug,
1796 double *nopd,
1797 const bool higher_precision,
1798 bool &_verbose,
1799 FILE* &_fp,
1800 int &_dim,
1801 bool &_isSymmetric,
1802 int &_nfront,
1803 vector<int> &_p_front,
1804 vector<int> &_p_diag,
1805 vector<int> &_p_upper,
1806 const double* &_coef,
1807 vector<int> &_ptRows,
1808 vector<int> &_indCols, // column_numb
1809 vector<int> &_indVals, // column_numb
1810 vector<int> &_num_null,
1811 vector<int> &_permute,
1812 vector<int> &_permute_ginv,
1813 ColumnMatrix<double>* &_diag_blocks,
1814 ColumnMatrix<quadruple>* &_diag_blocks_high,
1815 ColumnMatrix<double>* &_upper_blocks,
1816 ColumnMatrix<double>* &_lower_blocks,
1817 list<int> &_high_blocks
1818 #ifdef STORE_WHOLE_FACTORIZED
1819 ,
1820 ColumnMatrix<double> &_factorized_whole
1821 #endif
1822 );
1823
1824 template void
1825 TridiagNumericFact_<complex<double>, double,
1826 complex<quadruple>, quadruple>(double *pivot,
1827 const double eps_pivot,
1828 const int dim_aug_kern,
1829 ColumnMatrix<complex<double> > *diag_block_save,
1830 vector<int> &num_null_aug,
1831 double *nopd,
1832 const bool higher_precision,
1833 bool &_verbose,
1834 FILE* &_fp,
1835 int &_dim,
1836 bool &_isSymmetric,
1837 int &_nfront,
1838 vector<int> &_p_front,
1839 vector<int> &_p_diag,
1840 vector<int> &_p_upper,
1841 const complex<double>* &_coef,
1842 vector<int> &_ptRows,
1843 vector<int> &_indCols, // column_numb
1844 vector<int> &_indVals, // column_numb
1845 vector<int> &_num_null,
1846 vector<int> &_permute,
1847 vector<int> &_permute_ginv,
1848 ColumnMatrix<complex<double> >* &_diag_blocks,
1849 ColumnMatrix<complex<quadruple> >* &_diag_blocks_high,
1850 ColumnMatrix<complex<double> >* &_upper_blocks,
1851 ColumnMatrix<complex<double> >* &_lower_blocks,
1852 list<int> &_high_blocks
1853 #ifdef STORE_WHOLE_FACTORIZED
1854 ,
1855 ColumnMatrix<complex<double> > &_factorized_whole
1856 #endif
1857 );
1858
1859 template void
1860 TridiagNumericFact_<float, float, double, double>
1861 (double *pivot,
1862 const double eps_pivot,
1863 const int dim_aug_kern,
1864 ColumnMatrix<float> *diag_block_save,
1865 vector<int> &num_null_aug,
1866 double *nopd,
1867 const bool higher_precision,
1868 bool &_verbose,
1869 FILE* &_fp,
1870 int &_dim,
1871 bool &_isSymmetric,
1872 int &_nfront,
1873 vector<int> &_p_front,
1874 vector<int> &_p_diag,
1875 vector<int> &_p_upper,
1876 const float* &_coef,
1877 vector<int> &_ptRows,
1878 vector<int> &_indCols, // column_numb
1879 vector<int> &_indVals, // column_numb
1880 vector<int> &_num_null,
1881 vector<int> &_permute,
1882 vector<int> &_permute_ginv,
1883 ColumnMatrix<float>* &_diag_blocks,
1884 ColumnMatrix<double>* &_diag_blocks_high,
1885 ColumnMatrix<float>* &_upper_blocks,
1886 ColumnMatrix<float>* &_lower_blocks,
1887 list<int> &_high_blocks
1888 #ifdef STORE_WHOLE_FACTORIZED
1889 ,
1890 ColumnMatrix<float> &_factorized_whole
1891 #endif
1892 );
1893
1894 template void
1895 TridiagNumericFact_<complex<float>, float,
1896 complex<double>, double>
1897 (double *pivot,
1898 const double eps_pivot,
1899 const int dim_aug_kern,
1900 ColumnMatrix<complex<float> > *diag_block_save,
1901 vector<int> &num_null_aug,
1902 double *nopd,
1903 const bool higher_precision,
1904 bool &_verbose,
1905 FILE* &_fp,
1906 int &_dim,
1907 bool &_isSymmetric,
1908 int &_nfront,
1909 vector<int> &_p_front,
1910 vector<int> &_p_diag,
1911 vector<int> &_p_upper,
1912 const complex<float>* &_coef,
1913 vector<int> &_ptRows,
1914 vector<int> &_indCols, // column_numb
1915 vector<int> &_indVals, // column_numb
1916 vector<int> &_num_null,
1917 vector<int> &_permute,
1918 vector<int> &_permute_ginv,
1919 ColumnMatrix<complex<float> >* &_diag_blocks,
1920 ColumnMatrix<complex<double> >* &_diag_blocks_high,
1921 ColumnMatrix<complex<float> >* &_upper_blocks,
1922 ColumnMatrix<complex<float> >* &_lower_blocks,
1923 list<int> &_high_blocks
1924 #ifdef STORE_WHOLE_FACTORIZED
1925 ,
1926 ColumnMatrix<complex<float> > &_factorized_whole
1927 #endif
1928 );
1929
1930
1931 template<typename T, typename U>
1932 void TridiagBlockMatrix<T, U>::ComputeSchur(const int dim_,
1933 int* color_mask,
1934 const int ncol_,
1935 const int *ptrow1,
1936 const int *indcols1,
1937 const int *indvals1,
1938 const int *indvals2,
1939 const T *coef,
1940 const int size_b1,
1941 SquareBlockMatrix<T> &local_s,
1942 double *nopd,
1943 elapsed_t *tt)
1944 {
1945 //input : CSR data of upper block : ptrow1, indcols1, indvals1, indvals2, coef
1946 //output: Schur complment : -c^T a^-1 b
1947 // : permutation of upper block : old2new_j
1948 ColumnMatrix<T> b, c, y, z;
1949 vector<T> dscale;
1950 // const int nrow = _dim;
1951 vector<int> i0;
1952 vector<int> permute_diag_inv, permute_upper, permute_upper_inv;
1953 // T *s;
1954 int ncol;
1955 vector<int> map_eqn, remap_eqn;
1956
1957 get_realtime(&tt[0]); // entering
1958 map_eqn.resize(dim_);
1959 remap_eqn.resize(dim_);
1960
1961 int jtmp = _dim;
1962 int itmp = 0;
1963 for (int i = 0; i < dim_; i++) {
1964 if ((color_mask[i] == _color) || (color_mask[i] == (-_color)) ||
1965 ((_color == 1) && color_mask[i] == 0)) {
1966 map_eqn[itmp++] = i;
1967 }
1968 else {
1969 map_eqn[jtmp++] = i;
1970 }
1971 }
1972 for (int i = 0; i < dim_; i++) {
1973 remap_eqn[map_eqn[i]] = i;
1974 }
1975 const int nrow = _dim;
1976 GenPermuteUpper(nrow, remap_eqn, ncol_, ptrow1, indcols1, _nfront, _p_front,
1977 _new2old, _permute, permute_diag_inv,
1978 permute_upper,
1979 permute_upper_inv, i0, _verbose, _fp);
1980 ncol = 0;
1981 for (vector<int>::const_iterator it = i0.begin(); it != i0.end(); ++it) {
1982 ncol++;
1983 if (*it >= nrow) {
1984 break;
1985 }
1986 }
1987 #ifdef PERMUTE_UPPER
1988 ColumnMatrix<T> s(ncol, ncol);
1989 #endif
1990 b.init(nrow, ncol);
1991 c.init(nrow, ncol);
1992 dscale.resize(nrow);
1993 b.ZeroClear();
1994 FillBlockSparse<T>(coef, nrow, map_eqn, ptrow1, indcols1, indvals1,
1995 permute_diag_inv, permute_upper_inv, b);
1996 if (!_isSymmetric) {
1997 c.ZeroClear();
1998 FillBlockSparse<T>(coef, nrow, map_eqn, ptrow1, indcols1, indvals2,
1999 permute_diag_inv, permute_upper_inv, c);
2000 }
2001 get_realtime(&tt[1]); // after filling enteries
2002 // A_12' = A_11^-1 A_12 A_21'^T = A_11^-T A_21^T
2003 //[L_11 ][D_11 ][U_11 A_12'] forward : x_1 = L_11^-1 b_1
2004 //[A_21' L_22][ D_22][ U_22 ] x_2 = L_22^-1 (b_2 - A_21' x_1)
2005 // backward: x_1 = U_11^-T b_1
2006 // x_2 = U_22^-1 (b_2 - A_12'^T x_1)
2007 // symmetric => A_21' = A_12'^T
2008 // unsymmetric A_21'^T is stored ==> DGEMM('T','N',...,(tri%a12) or (tri%a21),
2009 if (_nscol > 0) {
2010 y.init(_nscol, ncol);
2011 z.init(_nscol, ncol);
2012 for (int j = 0; j < ncol; j++) {
2013 for (int i = 0; i < _nscol; i++) {
2014 y(i, j) = b(_list_schur[i], j);
2015 // nullifying
2016 b(_list_schur[i], j) = _zero;
2017 }
2018 }
2019 // alpha = -1; beta = 1;
2020 if (_isSymmetric) {
2021 // y -= A_12 ^T b
2022 blas_gemm<T>(CblasTrans, CblasNoTrans, _nscol, ncol, nrow, _none,
2023 _a12.addrCoefs(), nrow, b.addrCoefs(), nrow, _one,
2024 y.addrCoefs(), _nscol);
2025 }
2026 else {
2027 // y -= A_21 ^T b
2028 blas_gemm<T>(CblasTrans, CblasNoTrans, _nscol, ncol, nrow, _none,
2029 _a21.addrCoefs(), nrow, b.addrCoefs(), nrow, _one,
2030 y.addrCoefs(), _nscol);
2031 }
2032 } // if (_nscol > 0)
2033 ForwardUpper(false, ncol, b, i0, dscale); // normal
2034 if (_nscol > 0) {
2035 // alpha = 1.0
2036 blas_trsm<T>(CblasLeft, CblasLower, CblasNoTrans, CblasUnit, _nscol, ncol,
2037 _one, _s22.addrCoefs(), _nscol, y.addrCoefs(), _nscol);
2038 if (_isSymmetric) {
2039 for (int i = 0; i < _nscol; i++) { // exclude _2 entries
2040 dscale[_list_schur[i]] = _zero;
2041 }
2042 for (int j = 0; j < ncol; j++) {
2043 for (int i = 0; i < _nscol; i++) {
2044 z(i, j) = y(i, j) * _s22(i, i);
2045 }
2046 }
2047 }
2048 }
2049 if (_isSymmetric) {
2050 // ddtimesu
2051 for (int j = 0; j < ncol; j++) {
2052 for (int i = 0; i < i0[j]; i++) {
2053 c(i, j) = _zero;
2054 }
2055 for (int i = i0[j]; i < nrow; i++) {
2056 c(i, j) = dscale[i] * b(i, j);
2057 }
2058 // fop += (nrow-j0(j)-1)
2059 }
2060 }
2061 else { // if (isSymmetric)
2062 if (_nscol > 0) {
2063 for (int j = 0; j < ncol; j++) {
2064 for (int i = 0; i < _nscol; i++) {
2065 z(i, j) = c(_list_schur[i], j);
2066 // nullifying : easier than changing entries in dscle[]
2067 c(_list_schur[i], j) = _zero;
2068 }
2069 }
2070 // alpha = -1; beta = 1;
2071 // z -= A_12 ^T c
2072 blas_gemm<T>(CblasTrans, CblasNoTrans, _nscol, ncol, nrow, _none,
2073 _a12.addrCoefs(), nrow, c.addrCoefs(), nrow, _one,
2074 z.addrCoefs(), _nscol);
2075 }
2076 ForwardUpper(true, ncol, c, i0, dscale); // Transposed
2077 if (_nscol > 0) {
2078 // alpha = 1.0
2079 blas_trsm<T>(CblasLeft, CblasUpper, CblasTrans, CblasUnit,
2080 _nscol, ncol, _one,
2081 _s22.addrCoefs(), _nscol, z.addrCoefs(), _nscol);
2082 for (int i = 0; i < _nscol; i++) {
2083 dscale[_list_schur[i]] = _zero;
2084 }
2085 for (int j = 0; j < ncol; j++) {
2086 for (int i = 0; i < _nscol; i++) {
2087 z(i, j) *= _s22(i, i);
2088 }
2089 }
2090 }
2091 // ddtimesu
2092 for (int j = 0; j < ncol; j++) {
2093 for (int i = 0; i < i0[j]; i++) {
2094 c(i, j) = _zero;
2095 }
2096 for (int i = i0[j]; i < nrow; i++) {
2097 c(i, j) *= dscale[i];
2098 }
2099 // fop += (nrow-j0(j)-1)
2100 }
2101 } // if (_isSymmetric)
2102 //nops += fop;
2103 double fop;
2104 get_realtime(&tt[2]); // after tridiag forward
2105 #ifdef PERMUTE_UPPER
2106 // s = b * c : alpha = 1.0; beta = 0.0
2107 if (_isSymmetric) {
2108 SchurProfileSym<T>(nrow, ncol, i0, b, c, s.addrCoefs(), size_b1, &fop);
2109 }
2110 else {
2111 SchurProfileUnSym<T>(nrow, ncol, i0, b, c, s.addrCoefs(), size_b1, &fop);
2112 }
2113 if (_nscol > 0) {
2114 // s += z y : alpha = 1.0; beta = 1.0;
2115 blas_gemm<T>(CblasTrans, CblasNoTrans, ncol, ncol, _nscol, _one,
2116 z.addrCoefs(), _nscol, y.addrCoefs(), _nscol, _one,
2117 s.addrCoefs(), ncol);
2118 }
2119 get_realtime(&tt[3]); // after sparse dgemm
2120 // nops = tri%nop
2121 if (_color == 1) {
2122 local_s.ZeroClear();
2123 if (_isSymmetric) {
2124 for (int i = 0; i < ncol; i++) {
2125 const int ii = permute_upper[i];
2126 for (int j = i; j < ncol; j++) {
2127 const int jj = permute_upper[j];
2128 const int iii = (ii <= jj ? ii : jj);
2129 const int jjj = (ii <= jj ? jj : ii);
2130 local_s(iii, jjj) = s(i, j);
2131 }
2132 }
2133 }
2134 else {
2135 for (int i = 0; i < ncol; i++) {
2136 const int ii = permute_upper[i];
2137 for (int j = 0; j < ncol; j++) {
2138 const int jj = permute_upper[j];
2139 local_s(ii, jj) = s(i, j);
2140 }
2141 }
2142 }
2143 // local_s.copyFromArrayPermute(s, ncol, &permute_upper_inv[0]);
2144 }
2145 else {
2146 if (_isSymmetric) {
2147 for (int i = 0; i < ncol; i++) {
2148 const int ii = permute_upper[i];
2149 for (int j = i; j < ncol; j++) {
2150 const int jj = permute_upper[j];
2151 const int iii = (ii <= jj ? ii : jj);
2152 const int jjj = (ii <= jj ? jj : ii);
2153 local_s(iii, jjj) += s(i, +j);
2154 }
2155 }
2156 }
2157 else {
2158 for (int i = 0; i < ncol; i++) {
2159 const int ii = permute_upper[i];
2160 for (int j = 0; j < ncol; j++) {
2161 const int jj = permute_upper[j];
2162 local_s(ii, jj) += s(i, j);
2163 }
2164 }
2165 }
2166 // local_s.addFromArrayPermute(s, ncol, &permute_upper_inv[0]);
2167 } // if (_color == 1)
2168 #else
2169 // s = b * c : alpha = 1.0; beta = 0.0
2170 if (_isSymmetric) {
2171 for (int j = 0; j < local_s.num_blocks(); j++) {
2172 for (int i = 0; i < j; i++) {
2173 const int nnrow = local_s.nrowBlock(i, j);
2174 const int nncol = local_s.ncolBlock(i, j);
2175 const int ishift = local_s.IndexBlock(i) * nrow;
2176 const int jshift = local_s.IndexBlock(j) * nrow;
2177 blas_gemm<T>(CblasTrans, CblasNoTrans,
2178 nnrow, nncol, nrow, _one,
2179 c.addrCoefs() + ishift, nrow,
2180 b.addrCoefs() + jshift, nrow,
2181 (_color == 1 ? _zero : _one),
2182 local_s.addrCoefBlock(i, j), nnrow);
2183 if (_nscol > 0) {
2184 const int ishift1 = local_s.IndexBlock(i) * _nscol;
2185 const int jshift1 = local_s.IndexBlock(j) * _nscol;
2186 // s += z y : alpha = 1.0; beta = 1.0;
2187 blas_gemm<T>(CblasTrans, CblasNoTrans, nnrow, nncol, _nscol, _one,
2188 z.addrCoefs() + ishift1, _nscol,
2189 y.addrCoefs() + jshift1, _nscol, _one,
2190 local_s.addrCoefBlock(i, j), nnrow);
2191 }
2192 }
2193 {
2194 const int nnrow = local_s.nrowBlock(j, j);
2195 const int jshift = local_s.IndexBlock(j) * nrow;
2196 C_gemm_symm<T>(nnrow, nrow, _one,
2197 c.addrCoefs() + jshift, nrow,
2198 b.addrCoefs() + jshift, nrow,
2199 (_color == 1 ? _zero : _one),
2200 local_s.addrCoefBlock(j, j), nnrow);
2201 if (_nscol > 0) {
2202 const int jshift1 = local_s.IndexBlock(j) * _nscol;
2203 // s += z y : alpha = 1.0; beta = 1.0;
2204 C_gemm_symm<T>(nnrow, _nscol, _one,
2205 z.addrCoefs() + jshift1, _nscol,
2206 y.addrCoefs() + jshift1, _nscol, _one,
2207 local_s.addrCoefBlock(j, j), nnrow);
2208 }
2209 }
2210 } // loop : j
2211 } // if (_isSymmetric)
2212 else {
2213 for (int j = 0; j < local_s.num_blocks(); j++) {
2214 for (int i = 0; i <= j; i++) {
2215 const int nnrow = local_s.nrowBlock(i, j);
2216 const int nncol = local_s.ncolBlock(i, j);
2217 const int ishift = local_s.IndexBlock(i) * nrow;
2218 const int jshift = local_s.IndexBlock(j) * nrow;
2219 blas_gemm<T>(CblasTrans, CblasNoTrans,
2220 nnrow, nncol, nrow, _one,
2221 c.addrCoefs() + ishift, nrow,
2222 b.addrCoefs() + jshift, nrow,
2223 (_color == 1 ? _zero : _one),
2224 local_s.addrCoefBlock(i, j), nnrow);
2225 if (_nscol > 0) {
2226 const int ishift1 = local_s.IndexBlock(i) * _nscol;
2227 const int jshift1 = local_s.IndexBlock(j) * _nscol;
2228 // s += z y : alpha = 1.0; beta = 1.0;
2229 blas_gemm<T>(CblasTrans, CblasNoTrans, nnrow, nncol, _nscol, _one,
2230 z.addrCoefs() + ishift1, _nscol,
2231 y.addrCoefs() + jshift1, _nscol, _one,
2232 local_s.addrCoefBlock(i, j), nnrow);
2233 }
2234 } // lower part of local_s stores data in transposed way
2235 for (int i = (j + 1); i < local_s.num_blocks(); i++) {
2236 const int nnrow = local_s.nrowBlock(i, j);
2237 const int nncol = local_s.ncolBlock(i, j);
2238 const int ishift = local_s.IndexBlock(i) * nrow;
2239 const int jshift = local_s.IndexBlock(j) * nrow;
2240 blas_gemm<T>(CblasTrans, CblasNoTrans,
2241 nnrow, nncol, nrow, _one,
2242 b.addrCoefs() + jshift, nrow,
2243 c.addrCoefs() + ishift, nrow,
2244 (_color == 1 ? _zero : _one),
2245 local_s.addrCoefBlock(i, j), nnrow);
2246 if (_nscol > 0) {
2247 const int ishift1 = local_s.IndexBlock(i) * _nscol;
2248 const int jshift1 = local_s.IndexBlock(j) * _nscol;
2249 // s += z y : alpha = 1.0; beta = 1.0;
2250 blas_gemm<T>(CblasTrans, CblasNoTrans, nnrow, nncol, _nscol, _one,
2251 y.addrCoefs() + jshift1, _nscol,
2252 z.addrCoefs() + ishift1, _nscol, _one,
2253 local_s.addrCoefBlock(i, j), nnrow);
2254 }
2255 }
2256 }
2257 } // if (_isSymmetric)
2258 get_realtime(&tt[3]); // after sparse dgemm
2259 #endif
2260 b.free();
2261 c.free();
2262 y.free();
2263 z.free();
2264 dscale.clear();
2265 permute_diag_inv.clear();
2266 permute_upper_inv.clear();
2267 #ifdef PERMUTE_UPPER
2268 s.free(); // delete [] s;
2269 #endif
2270 get_realtime(&tt[4]); // end of the routine
2271 }
2272
2273 template
2274 void TridiagBlockMatrix<double>::
2275 ComputeSchur(const int dim_,
2276 int* color_mask,
2277 const int ncol,
2278 const int *ptrow1,
2279 const int *indcols1,
2280 const int *indvals1,
2281 const int *indvals2,
2282 const double *coef,
2283 const int size_b1,
2284 SquareBlockMatrix<double> &local_s,
2285 double *nopd,
2286 elapsed_t *tt);
2287
2288 template
2289 void TridiagBlockMatrix<quadruple>::
2290 ComputeSchur(const int dim_,
2291 int* color_mask,
2292 const int ncol,
2293 const int *ptrow1,
2294 const int *indcols1,
2295 const int *indvals1,
2296 const int *indvals2,
2297 const quadruple *coef,
2298 const int size_b1,
2299 SquareBlockMatrix<quadruple> &local_s,
2300 double *nopd,
2301 elapsed_t *tt);
2302
2303 template void
2304 TridiagBlockMatrix<complex<double>, double>::
2305 ComputeSchur(const int dim_,
2306 int* color_mask,
2307 const int ncol,
2308 const int *ptrow1, const int *indcols1,
2309 const int *indvals1, const int *indvals2,
2310 const complex<double> *coef,
2311 const int size_b1,
2312 SquareBlockMatrix<complex<double> > &local_s,
2313 double *nopd,
2314 elapsed_t *tt);
2315
2316 template
2317 void TridiagBlockMatrix<complex<quadruple>, quadruple>::
2318 ComputeSchur(const int dim_,
2319 int* color_mask,
2320 const int ncol,
2321 const int *ptrow1, const int *indcols1,
2322 const int *indvals1, const int *indvals2,
2323 const complex<quadruple> *coef,
2324 const int size_b1,
2325 SquareBlockMatrix<complex<quadruple> > &local_s,
2326 double *nopd,
2327 elapsed_t *tt);
2328
2329 template
2330 void TridiagBlockMatrix<float>::
2331 ComputeSchur(const int dim_,
2332 int* color_mask,
2333 const int ncol,
2334 const int *ptrow1,
2335 const int *indcols1,
2336 const int *indvals1,
2337 const int *indvals2,
2338 const float *coef,
2339 const int size_b1,
2340 SquareBlockMatrix<float> &local_s,
2341 double *nopd,
2342 elapsed_t *tt);
2343
2344 template void
2345 TridiagBlockMatrix<complex<float>, float>::
2346 ComputeSchur(const int dim_,
2347 int* color_mask,
2348 const int ncol,
2349 const int *ptrow1, const int *indcols1,
2350 const int *indvals1, const int *indvals2,
2351 const complex<float> *coef,
2352 const int size_b1,
2353 SquareBlockMatrix<complex<float> > &local_s,
2354 double *nopd,
2355 elapsed_t *tt);
2356
2357 //
2358
2359 template<typename T, typename U>
2360 void TridiagBlockMatrix<T, U>::SolveMulti(const bool flag_new2old,
2361 const bool isTrans,
2362 const int nrhs, ColumnMatrix<T>& x,
2363 const int nscol_)
2364 {
2365 ColumnMatrix<T> xx, y, zz;
2366 double fop;
2367 const int nscol = (nscol_ == (-1) ? _nscol : nscol_);
2368 xx.init(_dim, nrhs);
2369 if (flag_new2old) {
2370 for (int m = 0; m < nrhs; m++) {
2371 for (int i = 0; i < _dim; i++) {
2372 xx(_permute_ginv[i], m) = x(_new2old[i], m);
2373 }
2374 }
2375 }
2376 else {
2377 for (int m = 0; m < nrhs; m++) {
2378 for (int i = 0; i < _dim; i++) {
2379 xx(_permute_ginv[i], m) = x(i, m);
2380 }
2381 }
2382 }
2383 // x(, m) is reorderd to follow the permutation generated by
2384 // TridiagBlockFactorization
2385 if (nscol > 0) {
2386 y.init(nscol, nrhs);
2387 for (int m = 0; m < nrhs; m++) {
2388 for (int i = 0; i < nscol; i++) {
2389 y(i, m) = xx(_list_schur[i], m);
2390 xx(_list_schur[i], m) = _zero; // nullifying
2391 }
2392 }
2393 // alpha = (-1.0); beta = 1.0
2394 if (_isSymmetric) {
2395 blas_gemm<T>(CblasTrans, CblasNoTrans, nscol, nrhs, _dim, _none,
2396 _a12.addrCoefs(), _dim,
2397 xx.addrCoefs(), _dim, _one, y.addrCoefs(), nscol);
2398 }
2399 else {
2400 if (isTrans) {
2401 blas_gemm<T>(CblasTrans, CblasNoTrans, nscol, nrhs, _dim, _none,
2402 _a12.addrCoefs(), _dim,
2403 xx.addrCoefs(), _dim, _one, y.addrCoefs(), nscol);
2404 }
2405 else {
2406 blas_gemm<T>(CblasTrans, CblasNoTrans, nscol, nrhs, _dim, _none,
2407 _a21.addrCoefs(), _dim,
2408 xx.addrCoefs(), _dim, _one, y.addrCoefs(), nscol);
2409 }
2410 }
2411 blas_trsm<T>(CblasLeft,
2412 isTrans ? CblasUpper : CblasLower,
2413 isTrans ? CblasTrans : CblasNoTrans,
2414 CblasUnit, nscol, nrhs,
2415 _one, _s22.addrCoefs(), nscol, y.addrCoefs(), nscol);
2416 for (int m = 0; m < nrhs; m++) {
2417 for (int i = 0; i < nscol; i++) {
2418 y(i, m) *= _s22(i, i);
2419 }
2420 }
2421 blas_trsm<T>(CblasLeft,
2422 isTrans ? CblasLower : CblasUpper,
2423 isTrans ? CblasTrans : CblasNoTrans,
2424 CblasUnit, nscol, nrhs,
2425 _one, _s22.addrCoefs(), nscol, y.addrCoefs(), nscol);
2426 } // if (nscol > 0)
2427 zz.init(_maxdim, nrhs);
2428 // forward substitution
2429 for (int n = 0; n < (_nfront - 1); n++) {
2430 // copy to working array zz
2431 const int dim1 = _p_front[n + 1] - _p_front[n];
2432 for (int m = 0; m < nrhs; m++) {
2433 for (int i = 0; i < dim1; i++) {
2434 zz(i, m) = xx(_p_front[n] + i, m);
2435 }
2436 }
2437 #ifdef SPARSE_OFFDIAG
2438 full_fwbw_multi<T>(isTrans, dim1, _num_null[n],
2439 _diag_blocks[n].addrCoefs(), dim1,
2440 nrhs,
2441 zz.addrCoefs(), _maxdim);
2442 // sparse matrix * dense matrix
2443 if (isTrans) {
2444 for (int m = 0; m < nrhs; m++) {
2445 for (int i = _p_front[n]; i < _p_front[n + 1]; i++) {
2446 const int ii = _permute_ginv[i] - _p_front[n];
2447 for (int k = _p_upper[i]; k < _ptRows[i + 1]; k++) {
2448 const int jj = _permute_ginv[_indCols[k]];
2449 xx(jj, m) -= _coef[_indVals[k]] * zz(ii, m);
2450 }
2451 }
2452 }
2453 }
2454 else {
2455 for (int m = 0; m < nrhs; m++) {
2456 for (int i = _p_front[n + 1]; i < _p_front[n + 2]; i++) {
2457 const int ii = _permute_ginv[i];
2458 for (int k = _ptRows[i]; k < _p_diag[i]; k++) {
2459 const int jj = _permute_ginv[_indCols[k]] - _p_front[n];
2460 xx(ii, m) -= _coef[_indVals[k]] * zz(jj, m);
2461 }
2462 }
2463 }
2464 } // if (isTrans)
2465 #else
2466 full_fw_multi<T>(isTrans, dim1, _num_null[n],
2467 _diag_blocks[n].addrCoefs(), dim1,
2468 nrhs,
2469 zz.addrCoefs(), _maxdim, &fop);
2470 int dim2 = _p_front[n + 2] - _p_front[n + 1];
2471 if (isTrans) {
2472 ColumnMatrix<T> &upper = _upper_blocks[n + 1];
2473 blas_gemm<T>(CblasTrans, CblasNoTrans, dim2, nrhs, dim1,
2474 _none, upper.addrCoefs(), upper.nbRows(),
2475 zz.addrCoefs(), _maxdim,
2476 _one,
2477 xx.addrCoefs() + _p_front[n + 1], _dim);
2478 }
2479 else {
2480 ColumnMatrix<T> &lower = _lower_blocks[n + 1];
2481 blas_gemm<T>(CblasTrans, CblasNoTrans, dim2, nrhs, dim1,
2482 _none, lower.addrCoefs(), lower.nbRows(),
2483 zz.addrCoefs(), _maxdim,
2484 _one,
2485 xx.addrCoefs() + _p_front[n + 1], _dim);
2486 }
2487 for (int m = 0; m < nrhs; m++) {
2488 for (int i = 0; i < dim1; i++) {
2489 xx(_p_front[n] + i, m) = zz(i, m) * _diag_blocks[n](i, i);
2490 }
2491 }
2492 #endif
2493 }
2494 #ifndef SPARSE_OFFDIAG
2495 {
2496 const int n = _nfront - 1;
2497 const int dim1 = _p_front[n + 1] - _p_front[n];
2498 for (int m = 0; m < nrhs; m++) {
2499 for (int i = 0; i < dim1; i++) {
2500 zz(i, m) = xx(_p_front[n] + i, m);
2501 }
2502 }
2503 full_fw_multi<T>(isTrans, dim1, _num_null[n],
2504 _diag_blocks[n].addrCoefs(), dim1,
2505 nrhs,
2506 zz.addrCoefs(), _maxdim, &fop);
2507 for (int m = 0; m < nrhs; m++) {
2508 for (int i = 0; i < dim1; i++) {
2509 xx(_p_front[n] + i, m) = zz(i, m) * _diag_blocks[n](i, i);
2510 }
2511 }
2512 }
2513 #endif
2514 // backward substitution
2515 for (int n = (_nfront - 1); n > 0; n--) {
2516 const int dim1 = _p_front[n + 1] - _p_front[n];
2517 #ifdef SPARSE_OFFDIAG
2518 full_fwbw_multi<T>(isTrans, dim1, _num_null[n],
2519 _diag_blocks[n].addrCoefs(), dim1,
2520 nrhs,
2521 xx.addrCoefs() + _p_front[n], _dim);
2522 if (_isSymmetric) {
2523 // Transposed sparse matrix * dense matrix
2524 for (int m = 0; m < nrhs; m++) {
2525 for (int i = _p_front[n]; i < _p_front[n + 1]; i++) {
2526 const int ii = _permute_ginv[i];
2527 for (int k = _ptRows[i]; k < _p_diag[i]; k++) {
2528 const int jj = _permute_ginv[_indCols[k]];
2529 xx(jj, m) -= _coef[_indVals[k]] * xx(ii, m);
2530 }
2531 }
2532 }
2533 }
2534 else {
2535 if (isTrans) {
2536 for (int m = 0; m < nrhs; m++) {
2537 for (int i = _p_front[n]; i < _p_front[n + 1]; i++) {
2538 const int ii = _permute_ginv[i];
2539 for (int k = _ptRows[i]; k < _p_diag[i]; k++) {
2540 const int jj = _permute_ginv[_indCols[k]];
2541 xx(jj, m) -= _coef[_indVals[k]] * xx(ii, m);
2542 }
2543 }
2544 }
2545 }
2546 else {
2547 for (int m = 0; m < nrhs; m++) {
2548 for (int i = _p_front[n - 1]; i < _p_front[n]; i++) {
2549 const int ii = _permute_ginv[i];
2550 for (int k = _p_upper[i]; k < _ptRows[i + 1]; k++) {
2551 const int jj = _permute_ginv[_indCols[k]];
2552 xx(ii, m) -= _coef[_indVals[k]] * xx(jj, m);
2553 }
2554 }
2555 }
2556 } // if (isTrans)
2557 }
2558 #else
2559 full_bw_multi<T>(isTrans, dim1, _num_null[n],
2560 _diag_blocks[n].addrCoefs(), dim1,
2561 nrhs,
2562 xx.addrCoefs() + _p_front[n], _dim, &fop);
2563 int dim2 = _p_front[n] - _p_front[n - 1];
2564 if (isTrans) {
2565 ColumnMatrix<T> &lower = _lower_blocks[n];
2566 blas_gemm<T>(CblasNoTrans, CblasNoTrans, dim2, nrhs, dim1,
2567 _none, lower.addrCoefs(), lower.nbRows(),
2568 xx.addrCoefs() + _p_front[n], _dim,
2569 _one,
2570 xx.addrCoefs() + _p_front[n - 1], _dim);
2571 }
2572 else {
2573 ColumnMatrix<T> &upper = _upper_blocks[n];
2574 blas_gemm<T>(CblasNoTrans, CblasNoTrans, dim2, nrhs, dim1,
2575 _none, upper.addrCoefs(), upper.nbRows(),
2576 xx.addrCoefs() + _p_front[n], _dim,
2577 _one,
2578 xx.addrCoefs() + _p_front[n - 1], _dim);
2579 }
2580 #endif
2581 } // loop : n
2582 {
2583 const int dim1 = _p_front[1] - _p_front[0];
2584 #ifdef SPARSE_OFFDIAG
2585 full_fwbw_multi<T>(isTrans, dim1, _num_null[0],
2586 _diag_blocks[0].addrCoefs(), dim1,
2587 nrhs,
2588 xx.addrCoefs() + _p_front[0], _dim);
2589 #else
2590 full_bw_multi<T>(isTrans, dim1, _num_null[0],
2591 _diag_blocks[0].addrCoefs(), dim1,
2592 nrhs,
2593 xx.addrCoefs() + _p_front[0], _dim, &fop);
2594 #endif
2595 }
2596 if (nscol > 0) {
2597 // alpha = -1.0; beta = 1.0
2598 if (isTrans) {
2599 blas_gemm<T>(CblasNoTrans, CblasNoTrans, _dim, nrhs, nscol, _none,
2600 _a21.addrCoefs(), _dim, y.addrCoefs(), nscol, _one,
2601 xx.addrCoefs(), _dim);
2602 }
2603 else {
2604 blas_gemm<T>(CblasNoTrans, CblasNoTrans, _dim, nrhs, nscol, _none,
2605 _a12.addrCoefs(), _dim, y.addrCoefs(), nscol, _one,
2606 xx.addrCoefs(), _dim);
2607 }
2608 for (int m = 0; m < nrhs; m++) {
2609 for (int i = 0; i < nscol; i++) {
2610 xx(_list_schur[i], m) = y(i, m);
2611 }
2612 }
2613 y.free();
2614 }
2615 if (flag_new2old) {
2616 for (int j = 0; j < nrhs; j++) {
2617 for (int i = 0; i < _dim; i++) {
2618 x(_new2old[i], j) = xx(_permute_ginv[i], j);
2619 }
2620 }
2621 }
2622 else {
2623 for (int j = 0; j < nrhs; j++) {
2624 for (int i = 0; i < _dim; i++) {
2625 x(i, j) = xx(_permute_ginv[i], j);
2626 }
2627 }
2628 }
2629 }
2630
2631 template
2632 void TridiagBlockMatrix<double>::
2633 SolveMulti(const bool flag_new2old,
2634 const bool isTrans,
2635 const int nhrs,
2636 ColumnMatrix<double>& x,
2637 const int nscol_);
2638
2639 template
2640 void TridiagBlockMatrix<quadruple>::
2641 SolveMulti(const bool flag_new2old,
2642 const bool isTrans,
2643 const int nhrs,
2644 ColumnMatrix<quadruple>& x,
2645 const int nscol_);
2646
2647 template void
2648 TridiagBlockMatrix<complex<double>, double>::
2649 SolveMulti(const bool flag_new2old, const bool isTrans,
2650 const int nhrs, ColumnMatrix<complex<double> >& x,
2651 const int nscol_);
2652
2653 template
2654 void TridiagBlockMatrix<complex<quadruple>, quadruple>::
2655 SolveMulti(const bool flag_new2old, const bool isTrans,
2656 const int nhrs, ColumnMatrix<complex<quadruple> >& x,
2657 const int nscol_);
2658
2659 template
2660 void TridiagBlockMatrix<float>::
2661 SolveMulti(const bool flag_new2old,
2662 const bool isTrans,
2663 const int nhrs,
2664 ColumnMatrix<float>& x,
2665 const int nscol_);
2666
2667 template void
2668 TridiagBlockMatrix<complex<float>, float>::
2669 SolveMulti(const bool flag_new2old, const bool isTrans,
2670 const int nhrs, ColumnMatrix<complex<float> >& x,
2671 const int nscol_);
2672
2673 //
2674
2675 template<typename T, typename U>
2676 void TridiagBlockMatrix<T, U>::SolveSingle(const bool flag_new2old,
2677 const bool isTrans, T* x_,
2678 const int nscol_)
2679 {
2680 const int nscol = (nscol_ == (-1) ? _nscol : nscol_);
2681 double fop;
2682
2683 // zz.resize(_maxdim);
2684 VectorArray<T> xx(_dim);
2685 VectorArray<T> zz(_maxdim);
2686 VectorArray<T> y; // allocated only for nscol > 0
2687 if (flag_new2old) {
2688 for (int i = 0; i < _dim; i++) {
2689 xx[_permute_ginv[i]] = x_[_new2old[i]];
2690 }
2691 }
2692 else {
2693 for (int i = 0; i < _dim; i++) {
2694 xx[_permute_ginv[i]] = x_[i];
2695 }
2696 }
2697 // xx[ ] is reorderd to follow the permutation generated by
2698 // TridiagBlockFactorization
2699 if (nscol > 0) {
2700 y.init(nscol);
2701 for (int i = 0; i < nscol; i++) {
2702 y[i] = xx[_list_schur[i]];
2703 xx[_list_schur[i]] = _zero;
2704 }
2705 if (_isSymmetric) {
2706 blas_gemv<T>(CblasTrans, _dim, nscol, _none, _a12.addrCoefs(), _dim,
2707 xx.addrCoefs(), 1, _one, y.addrCoefs(), 1);
2708 }
2709 else {
2710 if (isTrans) {
2711 blas_gemv<T>(CblasTrans, _dim, nscol, _none, _a12.addrCoefs(), _dim,
2712 xx.addrCoefs(), 1, _one, y.addrCoefs(), 1);
2713 }
2714 else {
2715 blas_gemv<T>(CblasTrans, _dim, nscol, _none, _a21.addrCoefs(), _dim,
2716 xx.addrCoefs(), 1, _one, y.addrCoefs(), 1);
2717 }
2718 }
2719
2720 blas_trsv<T>(isTrans ? CblasUpper : CblasLower,
2721 isTrans ? CblasTrans : CblasNoTrans,
2722 CblasUnit, nscol,
2723 _s22.addrCoefs(), nscol, y.addrCoefs(), 1);
2724 for (int i = 0; i < nscol; i++) {
2725 y[i] *= _s22(i, i);
2726 }
2727 blas_trsv<T>(isTrans ? CblasLower : CblasUpper,
2728 isTrans ? CblasTrans : CblasNoTrans,
2729 CblasUnit, nscol,
2730 _s22.addrCoefs(), nscol, y.addrCoefs(), 1);
2731 } // if (nscol > 0)
2732 // forward substitution
2733 for (int n = 0; n < (_nfront - 1); n++) {
2734 // copy to working array zz
2735 const int dim1 = _p_front[n + 1] - _p_front[n];
2736 for (int i = 0; i < dim1; i++) {
2737 zz[i] = xx[_p_front[n] + i];
2738 }
2739 #ifdef SPARSE_OFFDIAG
2740 full_fwbw_single<T>(isTrans, dim1, _num_null[n],
2741 _diag_blocks[n].addrCoefs(), dim1,
2742 &zz[0]);
2743 // sparse matrix * vector
2744 if (isTrans) {
2745 for (int i = _p_front[n]; i < _p_front[n + 1]; i++) {
2746 const int ii = _permute_ginv[i] - _p_front[n];
2747 for (int k = _p_upper[i]; k < _ptRows[i + 1]; k++) {
2748 const int jj = _permute_ginv[_indCols[k]];
2749 xx[jj] -= _coef[_indVals[k]] * zz[ii];
2750 }
2751 }
2752 }
2753 else {
2754 for (int i = _p_front[n + 1]; i < _p_front[n + 2]; i++) {
2755 const int ii = _permute_ginv[i];
2756 for (int k = _ptRows[i]; k < _p_diag[i]; k++) {
2757 const int jj = _permute_ginv[_indCols[k]] - _p_front[n];
2758 xx[ii] -= _coef[_indVals[k]] * zz[jj];
2759 }
2760 }
2761 }
2762 #else
2763 full_fw_single<T>(isTrans, dim1, _num_null[n],
2764 _diag_blocks[n].addrCoefs(), dim1,
2765 &zz[0], &fop);
2766 int dim2 = _p_front[n + 2] - _p_front[n + 1];
2767 if (isTrans) {
2768 ColumnMatrix<T> &upper = _upper_blocks[n + 1];
2769 blas_gemv<T>(CblasTrans, dim1, dim2,
2770 _none, upper.addrCoefs(), upper.nbRows(),
2771 &zz[0], 1,
2772 _one,
2773 xx.addrCoefs() + _p_front[n + 1], 1);
2774 }
2775 else {
2776 ColumnMatrix<T> &lower = _lower_blocks[n + 1];
2777 blas_gemv<T>(CblasTrans, dim1, dim2,
2778 _none, lower.addrCoefs(), lower.nbRows(),
2779 &zz[0], 1,
2780 _one,
2781 xx.addrCoefs() + _p_front[n + 1], 1);
2782 }
2783 for (int i = 0; i < dim1; i++) {
2784 xx[_p_front[n] + i] = zz[i] * _diag_blocks[n](i, i);
2785 }
2786 #endif
2787 }
2788 #ifndef SPARSE_OFFDIAG
2789 {
2790 const int n = _nfront - 1;
2791 const int dim1 = _p_front[n + 1] - _p_front[n];
2792 for (int i = 0; i < dim1; i++) {
2793 zz[i] = xx[_p_front[n] + i];
2794 }
2795 full_fw_single<T>(isTrans, dim1, _num_null[n],
2796 _diag_blocks[n].addrCoefs(), dim1,
2797 &zz[0], &fop);
2798 for (int i = 0; i < dim1; i++) {
2799 xx[_p_front[n] + i] = zz[i] * _diag_blocks[n](i, i);
2800 }
2801 }
2802 #endif
2803 // backward substitution
2804 for (int n = (_nfront - 1); n > 0; n--) {
2805 const int dim1 = _p_front[n + 1] - _p_front[n];
2806 #ifdef SPARSE_OFFDIAG
2807 full_fwbw_single<T>(isTrans, dim1, _num_null[n],
2808 _diag_blocks[n].addrCoefs(), dim1,
2809 xx.addrCoefs() + _p_front[n]);
2810 if (_isSymmetric) {
2811 // Transposed sparse matrix * vector
2812 for (int i = _p_front[n]; i < _p_front[n + 1]; i++) {
2813 const int ii = _permute_ginv[i];
2814 for (int k = _ptRows[i]; k < _p_diag[i]; k++) {
2815 const int jj = _permute_ginv[_indCols[k]];
2816 xx[jj] -= _coef[_indVals[k]] * xx[ii];
2817 }
2818 }
2819 }
2820 else {
2821 if (isTrans) {
2822 for (int i = _p_front[n]; i < _p_front[n + 1]; i++) {
2823 const int ii = _permute_ginv[i];
2824 for (int k = _ptRows[i]; k < _p_diag[i]; k++) {
2825 const int jj = _permute_ginv[_indCols[k]];
2826 xx[jj] -= _coef[_indVals[k]] * xx[ii];
2827 }
2828 }
2829 }
2830 else {
2831 for (int i = _p_front[n - 1]; i < _p_front[n]; i++) {
2832 const int ii = _permute_ginv[i];
2833 for (int k = _p_upper[i]; k < _ptRows[i + 1]; k++) {
2834 const int jj = _permute_ginv[_indCols[k]];
2835 xx[ii] -= _coef[_indVals[k]] * xx[jj];
2836 }
2837 }
2838 }
2839 }
2840 #else
2841 full_bw_single<T>(isTrans, dim1, _num_null[n],
2842 _diag_blocks[n].addrCoefs(), dim1,
2843 xx.addrCoefs() + _p_front[n], &fop);
2844 int dim2 = _p_front[n] - _p_front[n - 1];
2845 if (isTrans) {
2846 ColumnMatrix<T> &lower = _lower_blocks[n];
2847 blas_gemv<T>(CblasNoTrans, dim2, dim1,
2848 _none, lower.addrCoefs(), lower.nbRows(),
2849 xx.addrCoefs() + _p_front[n], 1,
2850 _one,
2851 xx.addrCoefs() + _p_front[n - 1], 1);
2852 }
2853 else {
2854 ColumnMatrix<T> &upper = _upper_blocks[n];
2855 blas_gemv<T>(CblasNoTrans, dim2, dim1,
2856 _none, upper.addrCoefs(), upper.nbRows(),
2857 xx.addrCoefs() + _p_front[n], 1,
2858 _one,
2859 xx.addrCoefs() + _p_front[n - 1], 1);
2860 }
2861 #endif
2862 } // loop : n
2863 {
2864 const int dim1 = _p_front[1] - _p_front[0];
2865 #ifdef SPARSE_OFFDIAG
2866 full_fwbw_single<T>(isTrans, dim1, _num_null[0],
2867 _diag_blocks[0].addrCoefs(), dim1,
2868 xx.addrCoefs() + _p_front[0]);
2869 #else
2870 full_bw_single<T>(isTrans, dim1, _num_null[0],
2871 _diag_blocks[0].addrCoefs(), dim1,
2872 xx.addrCoefs() + _p_front[0], &fop);
2873 #endif
2874 }
2875 if (nscol > 0) {
2876 // alpha = -1.0; beta = 1.0
2877 if (isTrans) {
2878 blas_gemv<T>(CblasNoTrans, _dim, nscol, _none, _a21.addrCoefs(), _dim,
2879 y.addrCoefs(), 1, _one, xx.addrCoefs(), 1);
2880 }
2881 else {
2882 blas_gemv<T>(CblasNoTrans, _dim, nscol, _none, _a12.addrCoefs(), _dim,
2883 y.addrCoefs(), 1, _one, xx.addrCoefs(), 1);
2884 }
2885 for (int i = 0; i < nscol; i++) {
2886 xx[_list_schur[i]] = y[i];
2887 }
2888 }
2889 if (flag_new2old) {
2890 for (int i = 0; i < _dim; i++) {
2891 x_[_new2old[i]] = xx[_permute_ginv[i]];
2892 }
2893 }
2894 else {
2895 for (int i = 0; i < _dim; i++) {
2896 x_[i] = xx[_permute_ginv[i]];
2897 }
2898 }
2899 // delete [] xx;
2900 }
2901
2902 template
2903 void TridiagBlockMatrix<double>::
2904 SolveSingle(const bool flag_new2old,
2905 const bool isTrans,
2906 double *x, const int nscol_);
2907
2908 template
2909 void TridiagBlockMatrix<quadruple>::
2910 SolveSingle(const bool flag_new2old,
2911 const bool isTrans,
2912 quadruple *x, const int nscol_);
2913
2914 template void
2915 TridiagBlockMatrix<complex<double>, double>::
2916 SolveSingle(const bool flag_new2old,
2917 const bool isTrans,
2918 complex<double> *x, const int nscol_);
2919
2920 template
2921 void TridiagBlockMatrix<complex<quadruple>, quadruple>::
2922 SolveSingle(const bool flag_new2old,
2923 const bool isTrans,
2924 complex<quadruple> *x, const int nscol_);
2925
2926 template
2927 void TridiagBlockMatrix<float>::
2928 SolveSingle(const bool flag_new2old,
2929 const bool isTrans,
2930 float *x, const int nscol_);
2931
2932 template void
2933 TridiagBlockMatrix<complex<float>, float>::
2934 SolveSingle(const bool flag_new2old,
2935 const bool isTrans,
2936 complex<float> *x, const int nscol_);
2937
2938 //
2939
2940 template<typename T, typename U>
2941 void TridiagBlockMatrix<T, U>::ForwardUpper(bool isTransposed,
2942 int ncol, ColumnMatrix<T> &b,
2943 vector<int>& i0,
2944 vector<T> &dscale)
2945 {
2946 int ncol0;
2947 double fop;
2948 #ifdef STORE_WHOLE_FACTORIZED
2949 blas_trsm<T>(CblasLeft,
2950 (isTransposed ? CblasUpper : CblasLower),
2951 (isTransposed ? CblasTrans : CblasNoTrans),
2952 CblasUnit,
2953 _dim, ncol, _one, _factorized_whole.addrCoefs(), _dim,
2954 b.addrCoefs(), b.nbRows());
2955 for (int i = 0; i < _dim; i++) {
2956 dscale[i] = _factorized_whole(i, i);
2957 }
2958 #else // #ifdef STORE_WHOLE_FACTORIZED
2959 ColumnMatrix<T> zz;
2960 vector<int> i0_local;
2961 i0_local.resize(ncol);
2962
2963 zz.init(_maxdim, ncol);
2964 // zz.ZeroClear();
2965 for (int n = 0; n < (_nfront - 1); n++) {
2966 const int dim1 = _p_front[n + 1] - _p_front[n];
2967 ColumnMatrix<T> &diag = _diag_blocks[n];
2968 ncol0 = ncol;
2969 for (int j = 0; j < ncol; j++) {
2970 int itmp = std::max<int>((int)(i0[j] - _p_front[n]), 0);
2971 if (i0[j] >= _p_front[n + 1]) {
2972 ncol0 = j;
2973 break;
2974 }
2975 i0_local[j] = itmp;
2976 }
2977 if (ncol0 > 0) {
2978 full_fw_multiprofile<T>(isTransposed,
2979 dim1, _num_null[n], ncol0, diag.addrCoefs(), dim1,
2980 b.addrCoefs() + _p_front[n], _dim,
2981 i0_local, &fop);
2982 }
2983 #ifdef SPARSE_OFFDIAG
2984 // dddiag_times_s1
2985 for (int j = 0; j < ncol0; j++) {
2986 for (int i = 0; i < i0_local[j]; i++) {
2987 zz(i, j) = _zero;
2988 }
2989 for (int i = i0_local[j]; i < dim1; i++) {
2990 zz(i, j) = b(i + _p_front[n], j) * diag(i, i);
2991 }
2992 fop += (double)(dim1 - i0[j]);
2993 }
2994 if (ncol0 > 0) {
2995 full_bw_multi<T>(isTransposed,
2996 dim1, _num_null[n], diag.addrCoefs(), dim1, ncol0,
2997 zz.addrCoefs(), _maxdim,
2998 &fop);
2999 }
3000 // sparse matrix * dense matrix
3001 if (isTransposed) {
3002 for (int j = 0; j < ncol0; j++) {
3003 for (int i = _p_front[n]; i < _p_front[n + 1]; i++) {
3004 const int i1 = _permute_ginv[i] - _p_front[n];
3005 for (int k = _p_upper[i]; k < _ptRows[i + 1]; k++) {
3006 const int ii = _permute_ginv[_indCols[k]];
3007 b(ii, j) -= _coef[_indVals[k]] * zz(i1, j);
3008 }
3009 }
3010 }
3011 }
3012 else {
3013 for (int j = 0; j < ncol0; j++) {
3014 for (int i = _p_front[n + 1]; i < _p_front[n + 2]; i++) {
3015 const int ii = _permute_ginv[i];
3016 for (int k = _ptRows[i]; k < _p_diag[i]; k++) {
3017 const int i1 = _permute_ginv[_indCols[k]] - _p_front[n];
3018 b(ii, j) -= _coef[_indVals[k]] * zz(i1, j);
3019 }
3020 }
3021 }
3022 }
3023 #else // #ifdef SPARSE_OFFDIAG
3024 int dim2 = _p_front[n + 2] - _p_front[n + 1];
3025 if (isTransposed) {
3026 ColumnMatrix<T> &upper = _upper_blocks[n + 1];
3027 blas_gemm<T>(CblasTrans, CblasNoTrans, dim2, ncol0, dim1,
3028 _none, upper.addrCoefs(), upper.nbRows(),
3029 b.addrCoefs() + _p_front[n], b.nbRows(),
3030 _one,
3031 b.addrCoefs() + _p_front[n + 1], b.nbRows());
3032 }
3033 else {
3034 ColumnMatrix<T> &lower = _lower_blocks[n + 1];
3035 blas_gemm<T>(CblasTrans, CblasNoTrans, dim2, ncol0, dim1,
3036 _none, lower.addrCoefs(), lower.nbRows(),
3037 b.addrCoefs() + _p_front[n], b.nbRows(),
3038 _one,
3039 b.addrCoefs() + _p_front[n + 1], b.nbRows());
3040 }
3041 #endif // #ifdef SPARSE_OFFDIAG
3042 } // loop : n
3043 {
3044 int n = (_nfront - 1);
3045 const int dim1 = _p_front[n + 1] - _p_front[n];
3046 ColumnMatrix<T> &diag = _diag_blocks[n];
3047 ncol0 = ncol;
3048 for (int j = 0; j < ncol; j++) {
3049 int itmp = std::max<int>((int)(i0[j] - _p_front[n]), 0);
3050 if (i0[j] >= _p_front[n + 1]) {
3051 ncol0 = j;
3052 break;
3053 }
3054 i0_local[j] = itmp;
3055 }
3056 if (ncol0 > 0) {
3057 full_fw_multiprofile<T>(isTransposed,
3058 dim1, _num_null[n], ncol0, diag.addrCoefs(), dim1,
3059 b.addrCoefs() + _p_front[n], _dim,
3060 i0_local, &fop);
3061 }
3062 }
3063 for (int n = 0; n < _nfront; n++) {
3064 const int dim1 = _p_front[n + 1] - _p_front[n];
3065 ColumnMatrix<T> &diag = _diag_blocks[n];
3066 for (int i = 0; i < dim1; i++) {
3067 dscale[_p_front[n] + i] = diag(i, i);
3068 }
3069 }
3070 zz.free();
3071 #endif // #ifdef STORE_WHOLE_FACTORIZED
3072 }
3073
3074 template
3075 void TridiagBlockMatrix<double>::
3076 ForwardUpper(bool isTransposed,
3077 int ncol, ColumnMatrix<double> &b,
3078 vector<int>& i0,
3079 vector<double> &dscale);
3080
3081 template
3082 void TridiagBlockMatrix<quadruple>::ForwardUpper(bool isTransposed,
3083 int ncol,
3084 ColumnMatrix<quadruple> &b,
3085 vector<int>& i0,
3086 vector<quadruple> &dscale);
3087 template void
3088 TridiagBlockMatrix<complex<double>, double>::
3089 ForwardUpper(bool isTransposed,
3090 int ncol, ColumnMatrix<complex<double> > &b,
3091 vector<int>& i0,
3092 vector<complex<double> > &dscale);
3093
3094 template
3095 void TridiagBlockMatrix<complex<quadruple>, quadruple>::
3096 ForwardUpper(bool isTransposed,
3097 int ncol, ColumnMatrix<complex<quadruple> > &b,
3098 vector<int>& i0,
3099 vector<complex<quadruple> > &dscale);
3100
3101 template
3102 void TridiagBlockMatrix<float>::
3103 ForwardUpper(bool isTransposed,
3104 int ncol, ColumnMatrix<float> &b,
3105 vector<int>& i0,
3106 vector<float> &dscale);
3107
3108 template void
3109 TridiagBlockMatrix<complex<float>, float>::
3110 ForwardUpper(bool isTransposed,
3111 int ncol, ColumnMatrix<complex<float> > &b,
3112 vector<int>& i0,
3113 vector<complex<float> > &dscale);
3114
3115 //
3116
3117 void RenumberCSR(const bool shrink_flag,
3118 const int dim,
3119 const int nnz,
3120 vector<int> &b2a,
3121 vector<int> &a2b,
3122 const int *aptrows,
3123 const int *aindcols,
3124 const int *aindvals,
3125 vector<int> &bptrows,
3126 vector<int> &bindcols, vector<int> &bindvals,
3127 const bool verbose, FILE *fp)
3128 {
3129 vector<int> *bbindcols, *bbindvals;
3130 if (shrink_flag) {
3131 bbindcols = new vector<int>;
3132 bbindvals = new vector<int>;
3133 (*bbindcols).resize(nnz);
3134 (*bbindvals).resize(nnz);
3135 }
3136 else {
3137 bbindcols = &bindcols;
3138 bbindvals = &bindvals;
3139 }
3140 bptrows[0] = 0;
3141 for (int i = 0; i < dim; i++) {
3142 const int ii = b2a[i];
3143 bptrows[i + 1] = bptrows[i] + (aptrows[ii + 1] - aptrows[ii]);
3144 }
3145 for (int i = 0; i < dim; i++) {
3146 const int ii = b2a[i];
3147 list<int> local_indcols;
3148 list<int> local_indvals;
3149 for (int kk = aptrows[ii]; kk < aptrows[ii + 1]; kk++) {
3150 const int jj = aindcols[kk];
3151 const int j = a2b[jj];
3152 const int ll = aindvals[kk];
3153 list<int>::iterator ic = local_indcols.begin();
3154 list<int>::iterator iv = local_indvals.begin();
3155 bool flag = false;
3156 for ( ; ic != local_indcols.end(); ++ic, ++iv) {
3157 if (*ic > j) {
3158 local_indcols.insert(ic, j);
3159 local_indvals.insert(iv, ll);
3160 flag = true;
3161 break;
3162 }
3163 }
3164 if (flag == false) {
3165 local_indcols.push_back(j);
3166 local_indvals.push_back(ll);
3167 }
3168 } // loop : kk
3169 {
3170 list<int>::const_iterator ic = local_indcols.begin();
3171 list<int>::const_iterator iv = local_indvals.begin();
3172 int k = bptrows[i];
3173 for ( ; k < bptrows[i + 1]; k++, ++ic, ++iv) {
3174 (*bbindcols)[k] = *ic;
3175 (*bbindvals)[k] = *iv;
3176 }
3177 }
3178 local_indcols.clear();
3179 local_indvals.clear();
3180 } // loop : i
3181 if (shrink_flag) {
3182 int nnz1 = bptrows[dim];
3183 if (nnz1 != bindcols.size()) {
3184 fprintf(fp, "%s %d : %d != %d\n",
3185 __FILE__, __LINE__, nnz1, (int)bindcols.size());
3186 }
3187 for (int i = 0; i < nnz1; i++) {
3188 bindcols[i] = (*bbindcols)[i];
3189 bindvals[i] = (*bbindvals)[i];
3190 }
3191 (*bbindcols).clear();
3192 (*bbindvals).clear();
3193 delete bbindcols;
3194 delete bbindvals;
3195 }
3196 }
3197
3198 void RenumberCSR(const int dim,
3199 vector<int> &b2a,
3200 vector<int> &a2b,
3201 const int *aptrows,
3202 const int *aindcols,
3203 vector<int> &bptrows,
3204 vector<int> &bindcols,
3205 const bool verbose,
3206 FILE *fp)
3207 {
3208 // const int dim = b2a.size();
3209 bptrows[0] = 0;
3210 for (int i = 0; i < dim; i++) {
3211 const int ii = b2a[i];
3212 bptrows[i + 1] = bptrows[i] + (aptrows[ii + 1] - aptrows[ii]);
3213 }
3214 for (int i = 0; i < dim; i++) {
3215 const int ii = b2a[i];
3216 list<int> local_indcols;
3217 for (int kk = aptrows[ii]; kk < aptrows[ii + 1]; kk++) {
3218 const int jj = aindcols[kk];
3219 const int j = a2b[jj];
3220 list<int>::iterator ic = local_indcols.begin();
3221 bool flag = false;
3222 for ( ; ic != local_indcols.end(); ++ic) {
3223 if (*ic > j) {
3224 local_indcols.insert(ic, j);
3225 flag = true;
3226 break;
3227 }
3228 }
3229 if (flag == false) {
3230 local_indcols.push_back(j);
3231 }
3232 } // loop : kk
3233 {
3234 list<int>::const_iterator ic = local_indcols.begin();
3235 int k = bptrows[i];
3236 for ( ; k < bptrows[i + 1]; k++, ++ic) {
3237 bindcols[k] = *ic;
3238 }
3239 }
3240 local_indcols.clear();
3241 } // loop : i
3242 }
3243
3244 void TridiagStruct(vector<int> &ptrow, vector<int> &indcols,
3245 const int nfront,
3246 vector<int> &pfront, vector<int> &p_diag,
3247 vector<int> &p_upp)
3248 //, vector<int> &p_dia_blo)
3249 {
3250 for (int n = 0; n < nfront; n++) {
3251 const int i1 = pfront[n];
3252 const int i2 = pfront[n + 1];
3253 for (int i = i1; i < i2; i ++) {
3254 for (int k = ptrow[i]; k < ptrow[i + 1]; k++) {
3255 if (indcols[k] >= i1) {
3256 p_diag[i] = k;
3257 break;
3258 }
3259 }
3260 } // loop : i
3261 }
3262 for (int n = 0; n < nfront; n++) {
3263 const int i1 = pfront[n];
3264 const int i2 = pfront[n + 1];
3265 for (int i = i1; i < i2; i ++) {
3266 bool flag = false;
3267 for (int k = ptrow[i]; k < ptrow[i + 1]; k++) {
3268 if (indcols[k] >= i2) {
3269 p_upp[i] = k;
3270 flag = true;
3271 break;
3272 }
3273 }
3274 if (flag == false) {
3275 p_upp[i] = ptrow[i + 1];
3276 }
3277 } // loop : i
3278 }
3279 }
3280
3281 void GenPermuteOffdiag(const int pfront0, const int pfront1, const int pfront2,
3282 vector<int> &ptrow, vector<int> &ptdiag,
3283 vector<int> &indcols,
3284 const int *permute_diag, int *permute_diag_inv,
3285 int *permute_offdiag, int *permute_offdiag_inv,
3286 vector<int> &i0)
3287 {
3288 const int dim1 = pfront1 - pfront0;
3289 const int dim2 = pfront2 - pfront1;
3290 vector<int> i0_loc;
3291 i0_loc.resize(dim2);
3292 i0.resize(dim2);
3293 for (int i = 0; i < dim1; i++) {
3294 permute_diag_inv[permute_diag[i]] = i;
3295 }
3296 list<int> perm_offdg;
3297 for (int j = 0; j < dim2; j++) {
3298 const int jj = j + pfront1;
3299 int itmp = dim1;
3300 for (int k = ptrow[jj]; k < ptdiag[jj]; k++) {
3301 const int i = permute_diag_inv[indcols[k] - pfront0];
3302 itmp = itmp > i ? i : itmp;
3303 }
3304 bool flag = false;
3305 i0_loc[j] = itmp;
3306 for (list<int>::iterator it = perm_offdg.begin(); it != perm_offdg.end();
3307 ++it) {
3308 if (itmp < i0_loc[(*it)]) {
3309 perm_offdg.insert(it, j);
3310 flag = true;
3311 break;
3312 }
3313 }
3314 if (flag == false) {
3315 perm_offdg.push_back(j);
3316 }
3317 } // loop : j
3318 { // copy list<int> to vector<int>
3319 list<int>::const_iterator jt = perm_offdg.begin();
3320 int j = 0;
3321 for (; j < dim2; j++, ++jt) {
3322 permute_offdiag[j] = (*jt);
3323 i0[j] = i0_loc[(*jt)];
3324 }
3325 for (int i = 0 ; i < dim2; i++) {
3326 permute_offdiag_inv[permute_offdiag[i]] = i;
3327 }
3328 }
3329 perm_offdg.clear();
3330 i0_loc.clear();
3331 }
3332
3333 void GenPermuteUpper(const int nrow,
3334 vector<int> &remap_eqn,
3335 const int ncol,
3336 const int *ptrow, const int *indcols,
3337 const int nfront,
3338 vector<int> &p_fronts,
3339 vector<int> &new2old,
3340 vector<int> &permute_diag,
3341 vector<int> &permute_diag_inv,
3342 vector<int> &permute_upper,
3343 vector<int> &permute_upper_inv,
3344 vector<int> &i0,
3345 const bool verbose,
3346 FILE *fp)
3347 {
3348 vector<bool> colflag;
3349 vector<int> permute_tmp;
3350 colflag.resize(ncol, false);
3351 permute_diag_inv.resize(nrow);
3352 permute_tmp.resize(nrow);
3353 permute_upper.resize(ncol);
3354 permute_upper_inv.resize(ncol);
3355 i0.resize(ncol);
3356 for (int n = 0; n < nfront; n++) {
3357 for (int i = p_fronts[n]; i < p_fronts[n + 1]; i++) {
3358 permute_tmp[i] = new2old[permute_diag[i] + p_fronts[n]];
3359 }
3360 }
3361 #ifdef PERMUTE_UPPER
3362 int l = 0;
3363 for (int i = 0; i < nrow; i++) {
3364 const int ii = permute_tmp[i];
3365 for (int k = ptrow[ii]; k < ptrow[ii + 1]; k++) {
3366 const int jj = indcols[k];
3367 if (colflag[jj] == false) {
3368 colflag[jj] = true;
3369 i0[l] = i;
3370 permute_upper[l++] = jj;
3371 }
3372 }
3373 }
3374 for(int j = 0; j < ncol; j++) {
3375 if (colflag[j] == false) {
3376 i0[l] = nrow;
3377 permute_upper[l++] = j;
3378 }
3379 }
3380 for (int j = 0; j < ncol; j++) {
3381 permute_upper_inv[permute_upper[j]] = j;
3382 }
3383 #else
3384 for(int j = 0; j < ncol; j++) {
3385 permute_upper[j] = j;
3386 permute_upper_inv[j] = j;
3387 }
3388 #endif
3389 for (int i = 0; i < nrow; i++) {
3390 permute_diag_inv[remap_eqn[permute_tmp[i]]] = i;
3391 }
3392 colflag.clear();
3393 permute_tmp.clear();
3394 }
3395
3396 template<typename T, typename U>
3397 void TridiagBlockMatrix<T, U>::extract_column(const int jcol, T *dcol)
3398 {
3399 for (int i = 0; i < _dim; i++) {
3400 for (int k = _ptRows[i]; k < _ptRows[i + 1]; k++) {
3401 if (_indCols[k] == jcol) {
3402 dcol[i] = _coef[_indVals[k]];
3403 break;
3404 }
3405 }
3406 }
3407 }
3408
3409 template
3410 void TridiagBlockMatrix<double>::
3411 extract_column(const int jcol, double *dcol);
3412
3413 template
3414 void TridiagBlockMatrix<quadruple>::
3415 extract_column(const int jcol, quadruple *dcol);
3416
3417 template void
3418 TridiagBlockMatrix<complex<double>, double>::
3419 extract_column(const int jcol, complex<double> *dcol);
3420
3421 template
3422 void TridiagBlockMatrix<complex<quadruple>, quadruple>::
3423 extract_column(const int jcol, complex<quadruple> *dcol);
3424
3425 template
3426 void TridiagBlockMatrix<float>::
3427 extract_column(const int jcol, float *dcol);
3428
3429 template void
3430 TridiagBlockMatrix<complex<float>, float>::
3431 extract_column(const int jcol, complex<float> *dcol);
3432
3433 //
3434
3435 template<typename T, typename U>
3436 void TridiagBlockMatrix<T, U>::extract_row(const int irow, T *drow)
3437 {
3438 for (int k = _ptRows[irow]; k < _ptRows[irow + 1]; k++) {
3439 const int jj = _indCols[k];
3440 drow[jj] = _coef[_indVals[k]];
3441 }
3442 }
3443
3444 template
3445 void TridiagBlockMatrix<double>::
3446 extract_row(const int irow, double *drow);
3447
3448 template
3449 void TridiagBlockMatrix<quadruple>::extract_row(const int irow,
3450 quadruple *drow);
3451
3452 template void
3453 TridiagBlockMatrix<complex<double>, double>::
3454 extract_row(const int irow, complex<double> *drow);
3455
3456 template
3457 void TridiagBlockMatrix<complex<quadruple>, quadruple>::
3458 extract_row(const int irow,
3459 complex<quadruple> *drow);
3460
3461 template
3462 void TridiagBlockMatrix<float>::
3463 extract_row(const int irow, float *drow);
3464
3465 template void
3466 TridiagBlockMatrix<complex<float>, float>::
3467 extract_row(const int irow, complex<float> *drow);
3468 //
3469
3470 template<typename T>
3471 void FillBlockSparse(const T *coef,
3472 const int dim,
3473 vector<int>& map_eqn,
3474 const int *ptrow,
3475 const int *indcols,
3476 const int *indvals,
3477 vector<int> &old2new_i,
3478 vector<int> &old2new_j,
3479 ColumnMatrix<T> &b)
3480 {
3481 for (int i = 0; i < dim; i++) {
3482 const int ii = old2new_i[i];
3483 const int i0 = map_eqn[i];
3484 for (int k = ptrow[i0]; k < ptrow[i0 + 1]; k++) {
3485 const int jj = old2new_j[indcols[k]];
3486 b(ii, jj) = coef[indvals[k]];
3487 }
3488 } // loop : i
3489 }
3490
3491 template
3492 void FillBlockSparse<double>(const double *coef,
3493 const int dim,
3494 vector<int>& map_eqn,
3495 const int *ptrow,
3496 const int *indcols,
3497 const int *indvals,
3498 vector<int> &old2new_i,
3499 vector<int> &old2new_j,
3500 ColumnMatrix<double> &b);
3501
3502 template
3503 void FillBlockSparse<quadruple>(const quadruple *coef,
3504 const int dim,
3505 vector<int>& map_eqn,
3506 const int *ptrow,
3507 const int *indcols,
3508 const int *indvals,
3509 vector<int> &old2new_i,
3510 vector<int> &old2new_j,
3511 ColumnMatrix<quadruple> &b);
3512
3513 template
3514 void FillBlockSparse<complex<double> >(const complex<double> *coef,
3515 const int dim,
3516 vector<int>& map_eqn,
3517 const int *ptrow,
3518 const int *indcols,
3519 const int *indvals,
3520 vector<int> &old2new_i,
3521 vector<int> &old2new_j,
3522 ColumnMatrix<complex<double> > &b);
3523
3524
3525 template
3526 void FillBlockSparse<complex<quadruple> >(const complex<quadruple> *coef,
3527 const int dim,
3528 vector<int>& map_eqn,
3529 const int *ptrow,
3530 const int *indcols,
3531 const int *indvals,
3532 vector<int> &old2new_i,
3533 vector<int> &old2new_j,
3534 ColumnMatrix<complex<quadruple> > &b);
3535
3536 template
3537 void FillBlockSparse<float>(const float *coef,
3538 const int dim,
3539 vector<int>& map_eqn,
3540 const int *ptrow,
3541 const int *indcols,
3542 const int *indvals,
3543 vector<int> &old2new_i,
3544 vector<int> &old2new_j,
3545 ColumnMatrix<float> &b);
3546
3547 template
3548 void FillBlockSparse<complex<float> >(const complex<float> *coef,
3549 const int dim,
3550 vector<int>& map_eqn,
3551 const int *ptrow,
3552 const int *indcols,
3553 const int *indvals,
3554 vector<int> &old2new_i,
3555 vector<int> &old2new_j,
3556 ColumnMatrix<complex<float> > &b);
3557
3558 //
3559
3560 bool isnegative(const double x) {
3561 return (x < 0.0);
3562 }
3563
3564 bool isnegative(const quadruple x) {
3565 return (x < quadruple(0.0));
3566 }
3567
3568 bool isnegative(const complex<double> &x) {
3569 return ((x.real() < 0.0) && (x.imag() == 0.0));
3570 }
3571
3572 bool isnegative(const complex<quadruple> &x) {
3573 return (x.real() < quadruple(0.0)) && (x.imag() == quadruple(0.0));
3574 }
3575
3576 bool isnegative(const float x) {
3577 return (x < 0.0);
3578 }
3579
3580 bool isnegative(const complex<float> &x) {
3581 return ((x.real() < 0.0) && (x.imag() == 0.0));
3582 }
3583
3584 template<typename T, typename U>
3585 int TridiagBlockMatrix<T, U>::NumNegativeDiags()
3586 {
3587 int count = 0 ;
3588 for (int n = 0; n < _nfront; n++) {
3589 ColumnMatrix<T> &diag = _diag_blocks[n];
3590 for (int i = _p_front[n]; i < _p_front[n + 1]; i++) {
3591 const int ii = i - _p_front[n];
3592 if (isnegative(diag(ii, ii))) {
3593 count++;
3594 }
3595 }
3596 }
3597 for (int i = 0; i < _nscol; i++) {
3598 if (isnegative(_s22(i, i))) {
3599 count++;
3600 }
3601 }
3602 return count;
3603 }
3604
3605 template
3606 int TridiagBlockMatrix<double>::
3607 NumNegativeDiags();
3608
3609 template
3610 int TridiagBlockMatrix<quadruple>::NumNegativeDiags();
3611
3612 template int
3613 TridiagBlockMatrix<complex<double>, double>::
3614 NumNegativeDiags();
3615
3616 template
3617 int TridiagBlockMatrix<complex<quadruple>, quadruple>::NumNegativeDiags();
3618 //
3619
3620 template
3621 int TridiagBlockMatrix<float>::
3622 NumNegativeDiags();
3623
3624 template int
3625 TridiagBlockMatrix<complex<float>, float>::
3626 NumNegativeDiags();
3627
3628 template<typename T, typename U>
3629 void TridiagBlockMatrix<T, U>::KernelBasis(const bool isTrans,
3630 ColumnMatrix<T> &a12)
3631 {
3632 // const T zero(0.0);
3633 // const T none(-1.0);
3634
3635 vector<T> v;
3636 v.resize(_dim);
3637 a12.free();
3638 a12.init(_dim, _n0);
3639 a12.ZeroClear();
3640 for (int j = 0; j < _n0; j++) {
3641 if (isTrans) {
3642 extract_row(_list_elim[j], a12.addrCoefs() + (j * _dim));
3643 }
3644 else {
3645 extract_column(_list_elim[j], a12.addrCoefs() + (j * _dim));
3646 }
3647 for (int i = 0; i < _n0; i++) {
3648 a12(_list_elim[i], j) = _zero;
3649 }
3650 }
3651 if (_n0 > 1) {
3652 SolveMulti(false, isTrans, _n0, a12);
3653 }
3654 else {
3655 SolveSingle(false, isTrans, a12.addrCoefs());
3656 }
3657 for (int j = 0; j < _n0; j++) {
3658 for (int i = 0; i < _dim; i++) {
3659 v[i] = a12(i, j);
3660 }
3661 for (int i = 0; i < _n0; i++) {
3662 v[_list_elim[i]] = _zero;
3663 }
3664 v[_list_elim[j]] = _none;
3665 for (int i = 0; i < _dim; i++) {
3666 a12(_new2old[i], j) = v[i];
3667 }
3668 }
3669
3670 v.clear();
3671 }
3672
3673 template
3674 void TridiagBlockMatrix<double>::
3675 KernelBasis(const bool isTrans,
3676 ColumnMatrix<double> & a12);
3677
3678 template
3679 void TridiagBlockMatrix<quadruple>::KernelBasis(const bool isTrans,
3680 ColumnMatrix<quadruple> &a12);
3681
3682 template void
3683 TridiagBlockMatrix<complex<double>, double>::
3684 KernelBasis(const bool isTrans,
3685 ColumnMatrix<complex<double> > & a12);
3686
3687 template
3688 void TridiagBlockMatrix<complex<quadruple>, quadruple >::
3689 KernelBasis(const bool isTrans,
3690 ColumnMatrix<complex<quadruple> > & a12);
3691
3692 template
3693 void TridiagBlockMatrix<float>::
3694 KernelBasis(const bool isTrans,
3695 ColumnMatrix<float> & a12);
3696
3697 template void
3698 TridiagBlockMatrix<complex<float>, float>::
3699 KernelBasis(const bool isTrans,
3700 ColumnMatrix<complex<float> > & a12);
3701 //