/*! \file TridiagBlockMatrix.cpp
\brief tridiagonal factorization algorithm with Cuthill-McKee
\author François-Xavier Roux, ONERA, Laboratoire Jacques-Louis Lions
\author Atsushi Suzuki, Laboratoire Jacques-Louis Lions
\date Jul. 12th 2015
\date Nov. 30th 2016
*/
// This file is part of Dissection
//
// Dissection is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Linking Dissection statically or dynamically with other modules is making
// a combined work based on Disssection. Thus, the terms and conditions of
// the GNU General Public License cover the whole combination.
//
// As a special exception, the copyright holders of Dissection give you
// permission to combine Dissection program with free software programs or
// libraries that are released under the GNU LGPL and with independent modules
// that communicate with Dissection solely through the Dissection-fortran
// interface. You may copy and distribute such a system following the terms of
// the GNU GPL for Dissection and the licenses of the other code concerned,
// provided that you include the source code of that other code when and as
// the GNU GPL requires distribution of source code and provided that you do
// not modify the Dissection-fortran interface.
//
// Note that people who make modified versions of Dissection are not obligated
// to grant this special exception for their modified versions; it is their
// choice whether to do so. The GNU General Public License gives permission to
// release a modified version without this exception; this exception also makes
// it possible to release a modified version which carries forward this
// exception. If you modify the Dissection-fortran interface, this exception
// does not apply to your modified version of Dissection, and you must remove
// this exception when you distribute your modified version.
//
// This exception is an additional permission under section 7 of the GNU
// General Public License, version 3 ("GPLv3")
//
// Dissection is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Dissection. If not, see .
//
#include "Compiler/OptionCompiler.hpp"
#include "Compiler/OptionLibrary.hpp"
#include
#include
#include "Driver/TridiagBlockMatrix.hpp"
#include "Driver/C_BlasRoutines.hpp"
#include "Driver/C_KernDetect.hpp"
#include "Driver/DissectionDefault.hpp"
#include "Compiler/arithmetic.hpp"
#include "Compiler/elapsed_time.hpp"
#include "Algebra/SparseRenumbering.hpp"
#include "Algebra/VectorArray.hpp"
#include "Compiler/DissectionIO.hpp"
template
const T TridiagBlockMatrix::_one = T(1.0);
template
const T TridiagBlockMatrix::_none = T(-1.0);
template
const T TridiagBlockMatrix::_zero = T(0.0);
template
void TridiagBlockMatrix::SymbolicFact(const int color,
const int color_max,
int *color_mask,
const int dim_,
const int nnz_,
const int *prow_,
const int *indcols_,
const int *indvals_)
{
long long n1, n2;
// const void *fp = (void *)fp_;
int dim1, dim2, dim3;
_color = color;
_color_max = color_max;
// need count _dim and _nnz
_nnz = 0;
dim1 = 0;
for (int i = 0; i < dim_; i++) {
if (color_mask[i] == color) {
_nnz += prow_[i + 1] - prow_[i];
dim1++;
}
}
// isolated entries are dealt by color 1
dim3 = 0;
if (color == 1) {
for (int i = 0; i < dim_; i++) {
if (color_mask[i] == 0) {
_nnz++;
dim3++;
}
}
}
dim2 = 0;
for (int i = 0; i < dim_; i++) { // treatment fused block whose size is
// less than DIM_AUG_KERN
if (color_mask[i] == (-color)) {
_nnz += prow_[i + 1] - prow_[i];
dim2++;
}
}
_dim = dim1 + dim2 + dim3;
_new2old.resize(_dim);
vector remap_eqn, map_eqn, prow1, indcols1;
remap_eqn.resize(dim_);
map_eqn.resize(dim_);
prow1.resize(dim_ + 1);
indcols1.resize(nnz_);
{
int i0, i1, i2, i3;
i0 = 0;
i1 = dim1;
i2 = dim1 + dim2;
i3 = _dim; // dim1 + dim2 + dim3
for (int i = 0; i < dim_; i++) {
if (color_mask[i] == color) {
remap_eqn[i0++] = i;
}
else if (color_mask[i] == (-color)) {
remap_eqn[i1++] = i;
}
else if ((color == 1) && (color_mask[i] == 0)) {
remap_eqn[i2++] = i;
}
else {
remap_eqn[i3++] = i;
}
}
}
for (int i = 0; i < dim_; i++) {
map_eqn[remap_eqn[i]] = i; // map : old(original) to new (mapped) index
}
RenumberCSR(dim_, remap_eqn, map_eqn,
prow_, indcols_, prow1, indcols1, _verbose, _fp);
vector new2old, old2new;
new2old.resize(dim_);
old2new.resize(dim_);
if (dim1 > MIN_TRIDIAG_SIZE) {
CMK_number(dim1, &prow1[0], &indcols1[0], new2old, _verbose, _fp);
for (int i = 0; i < dim3; i++) {
_new2old[i] = remap_eqn[i + dim1 + dim2];
}
for (int i = 0; i < dim1; i++) {
_new2old[i + dim3] = remap_eqn[new2old[i]];
}
for (int i = 0; i < dim2; i++) {
_new2old[i + dim1 + dim3] = remap_eqn[i + dim1];
}
_nfront = point_front(dim1, &prow1[0], &indcols1[0], new2old,
_p_front, _verbose, _fp);
}
else {
_nfront = 1;
_p_front.resize(2);
_p_front[0] = 0;
_p_front[1] = dim1 + dim2;
for (int i = 0; i < _dim; i++) {
_new2old[i] = remap_eqn[i];
}
}
if (dim3 > 0) {
for (int i = 1; i < _nfront; i++) {
_p_front[i] += dim3;
}
}
_p_front[_nfront] = _dim; // replace the end of block
prow1.clear();
indcols1.clear();
// remap_eqn.clear();
map_eqn.clear();
diss_printf(_verbose, _fp,
"%s %d : SymbolicFact : %d ", __FILE__, __LINE__, _nb);
diss_printf(_verbose, _fp,
"color = %d dim = %d = %d + %d + %d nfront = %d : ",
color, _dim, dim1, dim2, dim3, _nfront);
for (int i = 0; i <= _nfront; i++) {
diss_printf(_verbose, _fp, "%d ", _p_front[i]);
}
diss_printf(_verbose, _fp, "\n");
_ptRows.resize(_dim + 1);
_indCols.resize(_nnz);
_indVals.resize(_nnz);
for (int i = 0; i < _dim; i++) {
new2old[i] = _new2old[i];
}
for (int i = _dim; i < dim_; i++) {
new2old[i] = remap_eqn[i];
}
for (int i = 0; i < dim_; i++) {
old2new[new2old[i]] = i;
}
{
bool shrink_flag = (_dim < dim_);
RenumberCSR(shrink_flag,
_dim, nnz_, new2old, old2new, prow_, indcols_, indvals_,
_ptRows, _indCols, _indVals, _verbose, _fp);
}
new2old.clear();
old2new.clear();
_p_diag.resize(_dim);
_p_upper.resize(_dim);
TridiagStruct(_ptRows, _indCols, _nfront, _p_front, _p_diag, _p_upper);
if (_isSymmetric) {
_nop = 0.0;
n2 = (long long)(_p_front[1] - _p_front[0]);
_nop += (n2 * n2 * n2) / 3.0;
for (int n = 1; n < _nfront; n++) {
n1 = n2;
n2 = (long long)(_p_front[n + 1] - _p_front[n]);
_nop += (n1 * n1 * n2) / 3.0;
_nop += (n1 * n2 * n2) / 3.0;
_nop += (n2 * n2 * n2) / 3.0;
}
}
else {
_nop = 0.0;
n2 = (long long)(_p_front[1] - _p_front[0]);
_nop += (2.0 * n2 * n2 * n2) / 3.0;
for (int n = 1; n < _nfront; n++) {
n1 = n2;
n2 = (long long)(_p_front[n + 1] - _p_front[n]);
_nop += (2.0 * n1 * n1 * n2) / 3.0;
_nop += (2.0 * n1 * n2 * n2) / 3.0;
_nop += (2.0 * n2 * n2 * n2) / 3.0;
}
}
_diag_blocks = new ColumnMatrix[_nfront];
#ifndef SPARSE_OFFDIAG
_upper_blocks = new ColumnMatrix[_nfront]; // 27 Nov.2015
_lower_blocks = new ColumnMatrix[_nfront]; // 27 Nov.2015
#endif
#ifdef STORE_WHOLE_FACTORIZED
_factorized_whole.init(_dim, _dim);
_factorized_whole.ZeroClear();
#endif
_diag_block_alloc_status = true;
_maxdim = 0;
for (int n = 0; n < _nfront; n++) {
const int itmp = _p_front[n + 1] - _p_front[n];
_maxdim = _maxdim > itmp ? _maxdim : itmp;
}
_n0 = 0; // initialization
_nscol = 0;
remap_eqn.clear();
}
template
void TridiagBlockMatrix::SymbolicFact(const int color,
const int color_max,
int *color_mask,
const int dim_,
const int nnz_,
const int *prow_,
const int *indcols_,
const int *indvals_);
template
void TridiagBlockMatrix::SymbolicFact(const int color,
const int color_max,
int *color_mask,
const int dim_,
const int nnz_,
const int *prow_,
const int *indcols_,
const int *indvals_);
template
void TridiagBlockMatrix::SymbolicFact(const int color,
const int color_max,
int *color_mask,
const int dim_,
const int nnz_,
const int *prow_,
const int *indcols_,
const int *indvals_);
template void
TridiagBlockMatrix, double>::
SymbolicFact(const int color,
const int color_max,
int *color_mask,
const int dim_,
const int nnz_,
const int *prow_,
const int *indcols_,
const int *indvals_);
template
void TridiagBlockMatrix, quadruple>::
SymbolicFact(const int color,
const int color_max,
int *color_mask,
const int dim_,
const int nnz_,
const int *prow_,
const int *indcols_,
const int *indvals_);
template void
TridiagBlockMatrix, float>::
SymbolicFact(const int color,
const int color_max,
int *color_mask,
const int dim_,
const int nnz_,
const int *prow_,
const int *indcols_,
const int *indvals_);
//
template
void TridiagBlockMatrix::
TridiagNumericFact(double *pivot,
const double eps_pivot,
const int dim_aug_kern,
ColumnMatrix *diag_block_save,
vector &num_null_aug,
double *nopd, const bool higher_precision)
{
fprintf(stderr, "%s %d : only specialized template version is impelemneted\n",
__FILE__, __LINE__);
}
template<>
void TridiagBlockMatrix::
TridiagNumericFact(double *pivot,
const double eps_pivot,
const int dim_aug_kern,
ColumnMatrix *diag_block_save,
vector &num_null_aug,
double *nopd, const bool higher_precision)
{
#if 1
ColumnMatrix* diag_blocks_high;
list high_blocks;
diag_blocks_high = new ColumnMatrix[_nfront];
TridiagNumericFact_(pivot,
#else
ColumnMatrix* diag_blocks_high;
list high_blocks;
diag_blocks_high = new ColumnMatrix[_nfront];
TridiagNumericFact_(pivot,
#endif
eps_pivot,
dim_aug_kern,
diag_block_save,
num_null_aug,
nopd,
higher_precision,
_verbose,
_fp,
_dim,
_isSymmetric,
_nfront,
_p_front,
_p_diag,
_p_upper,
_coef,
_ptRows,
_indCols, // column_numb
_indVals, // column_numb
_num_null,
_permute,
_permute_ginv,
_diag_blocks,
diag_blocks_high,
_upper_blocks,
_lower_blocks,
high_blocks
#ifdef STORE_WHOLE_FACTORIZED
,
_factorized_whole
#endif
);
for (list::iterator it = high_blocks.begin(); it != high_blocks.end();
++it) {
// fprintf(stderr, "%s %d : %d\n", __FILE__, __LINE__, (*it));
diag_blocks_high[(*it)].free();
}
delete [] diag_blocks_high;
high_blocks.clear();
#if 0
fprintf(stderr, "%s %d : _num_null[%d]= ", __FILE__, __LINE__, _nfront);
for (int i = 0; i < _nfront; i++) {
fprintf(stderr, "%d ", _num_null[i]);
}
fprintf(stderr, "\n");
#endif
}
template<>
void TridiagBlockMatrix, double>::
TridiagNumericFact(double *pivot,
const double eps_pivot,
const int dim_aug_kern,
ColumnMatrix > *diag_block_save,
vector &num_null_aug,
double *nopd, const bool higher_precision)
{
ColumnMatrix >* diag_blocks_high;
list high_blocks;
diag_blocks_high = new ColumnMatrix >[_nfront];
TridiagNumericFact_, double,
complex, quadruple>(pivot,
eps_pivot,
dim_aug_kern,
diag_block_save,
num_null_aug,
nopd,
higher_precision,
_verbose,
_fp,
_dim,
_isSymmetric,
_nfront,
_p_front,
_p_diag,
_p_upper,
_coef,
_ptRows,
_indCols, // column_numb
_indVals, // column_numb
_num_null,
_permute,
_permute_ginv,
_diag_blocks,
diag_blocks_high,
_upper_blocks,
_lower_blocks,
high_blocks
#ifdef STORE_WHOLE_FACTORIZED
,
_factorized_whole
#endif
);
for (list::iterator it = high_blocks.begin(); it != high_blocks.end();
++it) {
diag_blocks_high[(*it)].free();
}
delete [] diag_blocks_high;
high_blocks.clear();
}
template<>
void TridiagBlockMatrix::
TridiagNumericFact(double *pivot,
const double eps_pivot,
const int dim_aug_kern,
ColumnMatrix *diag_block_save,
vector &num_null_aug,
double *nopd, const bool higher_precision)
{
ColumnMatrix* diag_blocks_high;
list high_blocks;
diag_blocks_high = new ColumnMatrix[_nfront];
TridiagNumericFact_(pivot,
eps_pivot,
dim_aug_kern,
diag_block_save,
num_null_aug,
nopd,
false, // higher_precision,
_verbose,
_fp,
_dim,
_isSymmetric,
_nfront,
_p_front,
_p_diag,
_p_upper,
_coef,
_ptRows,
_indCols, // column_numb
_indVals, // column_numb
_num_null,
_permute,
_permute_ginv,
_diag_blocks,
diag_blocks_high,
_upper_blocks,
_lower_blocks,
high_blocks
#ifdef STORE_WHOLE_FACTORIZED
,
_factorized_whole
#endif
);
for (list::iterator it = high_blocks.begin(); it != high_blocks.end();
++it) {
diag_blocks_high[(*it)].free();
}
delete [] diag_blocks_high;
high_blocks.clear();
}
template<>
void TridiagBlockMatrix, quadruple>::
TridiagNumericFact(double *pivot,
const double eps_pivot,
const int dim_aug_kern,
ColumnMatrix >*diag_block_save,
vector &num_null_aug,
double *nopd, const bool higher_precision)
{
ColumnMatrix >* diag_blocks_high;
list high_blocks;
diag_blocks_high = new ColumnMatrix >[_nfront];
TridiagNumericFact_, quadruple,
complex, quadruple>(pivot,
eps_pivot,
dim_aug_kern,
diag_block_save,
num_null_aug,
nopd,
false, // higher_precision,
_verbose,
_fp,
_dim,
_isSymmetric,
_nfront,
_p_front,
_p_diag,
_p_upper,
_coef,
_ptRows,
_indCols, // column_numb
_indVals, // column_numb
_num_null,
_permute,
_permute_ginv,
_diag_blocks,
diag_blocks_high,
_upper_blocks,
_lower_blocks,
high_blocks
#ifdef STORE_WHOLE_FACTORIZED
,
_factorized_whole
#endif
);
for (list::iterator it = high_blocks.begin(); it != high_blocks.end();
++it) {
diag_blocks_high[(*it)].free();
}
delete [] diag_blocks_high;
high_blocks.clear();
}
template<>
void TridiagBlockMatrix::
TridiagNumericFact(double *pivot,
const double eps_pivot,
const int dim_aug_kern,
ColumnMatrix *diag_block_save,
vector &num_null_aug,
double *nopd, const bool higher_precision)
{
ColumnMatrix* diag_blocks_high;
list high_blocks;
diag_blocks_high = new ColumnMatrix[_nfront];
TridiagNumericFact_(pivot,
eps_pivot,
dim_aug_kern,
diag_block_save,
num_null_aug,
nopd,
higher_precision,
_verbose,
_fp,
_dim,
_isSymmetric,
_nfront,
_p_front,
_p_diag,
_p_upper,
_coef,
_ptRows,
_indCols, // column_numb
_indVals, // column_numb
_num_null,
_permute,
_permute_ginv,
_diag_blocks,
diag_blocks_high,
_upper_blocks,
_lower_blocks,
high_blocks
#ifdef STORE_WHOLE_FACTORIZED
,
_factorized_whole
#endif
);
for (list::iterator it = high_blocks.begin(); it != high_blocks.end();
++it) {
// fprintf(stderr, "%s %d : %d\n", __FILE__, __LINE__, (*it));
diag_blocks_high[(*it)].free();
}
delete [] diag_blocks_high;
high_blocks.clear();
#if 0
fprintf(stderr, "%s %d : _num_null[%d]= ", __FILE__, __LINE__, _nfront);
for (int i = 0; i < _nfront; i++) {
fprintf(stderr, "%d ", _num_null[i]);
}
fprintf(stderr, "\n");
#endif
}
template<>
void TridiagBlockMatrix, float>::
TridiagNumericFact(double *pivot,
const double eps_pivot,
const int dim_aug_kern,
ColumnMatrix > *diag_block_save,
vector &num_null_aug,
double *nopd, const bool higher_precision)
{
ColumnMatrix >* diag_blocks_high;
list high_blocks;
diag_blocks_high = new ColumnMatrix >[_nfront];
TridiagNumericFact_, float,
complex, double>(pivot,
eps_pivot,
dim_aug_kern,
diag_block_save,
num_null_aug,
nopd,
higher_precision,
_verbose,
_fp,
_dim,
_isSymmetric,
_nfront,
_p_front,
_p_diag,
_p_upper,
_coef,
_ptRows,
_indCols, // column_numb
_indVals, // column_numb
_num_null,
_permute,
_permute_ginv,
_diag_blocks,
diag_blocks_high,
_upper_blocks,
_lower_blocks,
high_blocks
#ifdef STORE_WHOLE_FACTORIZED
,
_factorized_whole
#endif
);
for (list::iterator it = high_blocks.begin(); it != high_blocks.end();
++it) {
diag_blocks_high[(*it)].free();
}
delete [] diag_blocks_high;
high_blocks.clear();
}
template
void TridiagBlockMatrix::NumericFact(const T* coef,
const double eps_pivot,
double *pivot,
const bool kernel_detection,
const bool higher_precision,
const int dim_aug_kern,
const U eps_machine,
double *nopd)
{
int nsing, n0, nn0;
ColumnMatrix a12, a21, a22, s22, b12, b21, b22;
double eps_piv, pivot_val;
bool flag_repeat_piv;
vector perm;
ColumnMatrix* diag_block_save;
vector num_null_aug;
vector list_sing;
const double machine_eps = todouble(eps_machine);
_coef = coef;
diag_block_save = new ColumnMatrix[_nfront];
TridiagNumericFact(pivot, eps_pivot,
dim_aug_kern,
diag_block_save,
num_null_aug, nopd, higher_precision);
nsing = 0;
for (int n = 0; n < _nfront; n++) {
nsing += _num_null[n];
}
if (nsing == 0) {
_nscol = 0;
_detected = true; // without using kernel detection
_n0 = 0;
_list_elim.clear();
_list_schur.clear();
_a12.free();
_a21.free();
_s22.free();
}
else { // if (nsing > 0)
_detected = false; // initialization : detection is not activated
diss_printf(_verbose, _fp, "%s %d nsing = %d nfront=%d : ",
__FILE__, __LINE__, nsing, _nfront);
for (int n = 0; n < _nfront; n++) {
diss_printf(_verbose, _fp, "%d/%d ",
_num_null[n], (_p_front[n + 1] - _p_front[n]));
if (num_null_aug[n] > 0) {
diss_printf(_verbose, _fp, "(+%d)", num_null_aug[n]);
}
}
diss_printf(_verbose, _fp, "\n");
s22.init(nsing, nsing);
perm.resize(nsing);
list_sing.resize(nsing);
nsing = 0;
for (int n = 0; n < _nfront; n++) {
const int offset = _p_front[n];
const int dim1 = _p_front[n + 1] - offset;
const int num_nonsing = dim1 - _num_null[n];
for (int i = num_nonsing; i < dim1; i++, nsing++) { // singular entries
list_sing[nsing] = _permute[i + offset] + offset;
}
}
a12.init(_dim, nsing);
a21.init(_dim, nsing);
a22.init(nsing, nsing);
ComputeSchurComplement(nsing, list_sing, a12, a21, a22);
if (higher_precision) {
eps_piv = machine_epsilon();
}
else {
eps_piv = eps_pivot; // initial is given by user and then repeated
}
flag_repeat_piv = true;
int count_repeat = 0;
while (flag_repeat_piv) {
s22.copy(a22);
// find maximum diagonal from s22[]
pivot_val = 0.0;
for (int i = 0; i < nsing; i++) {
// pivot value in double
const double tmp = blas_abs(s22(i, i));
pivot_val = tmp > pivot_val ? tmp : pivot_val;
}
n0 = 0; // by assuming the matrix is invertible
if (_isSymmetric) {
double fop;
full_ldlt_permute(&nn0, n0, nsing, s22.addrCoefs(), nsing,
&pivot_val, &perm[0], eps_piv, &fop);
}
else {
double fop;
full_ldu_permute(&nn0, n0, nsing, s22.addrCoefs(), nsing,
&pivot_val, &perm[0], eps_piv, &fop);
}
diss_printf(_verbose, _fp,
"%s %d factorize dim = %d eps_piv = %g -> sing = %d\n",
__FILE__, __LINE__, nsing, eps_piv, nn0);
//
if (((nsing - dim_aug_kern) >= nn0) || (eps_piv < TOL_PIVOT)) {
flag_repeat_piv = false;
break;
}
else {
eps_piv /= 10.0;
count_repeat++;
}
} // while (flag_repeat_piv)
n0 = nn0;
diss_printf(_verbose, _fp,
"%s %d : n0 = %d count_repeat = %d\n", __FILE__, __LINE__,
n0, count_repeat);
if ((n0 == 0) && (count_repeat == 0)) {
_n0 = 0;
_detected = true; // without kernel check
}
else if (!kernel_detection) {
_n0 = (-1); // for safety
_detected = false;
}
else { // if ((n0 == 0) && (count_repeat == 0)) && kernel_detection
int nsing1, nsing2;
if (n0 == 0) {
nsing1 = nsing;
b22.init(nsing1, nsing1);
b22.copy(a22); //
}
else { // if (n0 > 0) { // compute Schur complement again
nsing1 = n0 + dim_aug_kern;
nsing2 = nsing - nsing1;
if (nsing2 > 0) {
b21.init(nsing2, nsing1);
b12.init(nsing2, nsing1);
b22.init(nsing1, nsing1);
if (_isSymmetric) {
for (int j = 0; j < nsing1; j++) {
const int jj = perm[nsing2 + j];
for (int i = 0; i <= j; i++) {
const int ii = perm[nsing2 + i];
b22(i, j) = a22(ii, jj);
}
}
// symmetrize
for (int j = 0 ; j < nsing1; j++) {
for (int i = 0; i < j; i++) {
b22(j, i) = b22(i, j);
}
}
for (int i = 0; i < nsing2; i++) {
const int ii = perm[i];
for (int j = 0; j < nsing1; j++) {
const int jj = perm[nsing2 + j];
b12(i, j) = a22(ii, jj);
}
}
// alpha = 1
blas_trsm(CblasLeft, CblasLower, CblasNoTrans, CblasUnit,
nsing2, nsing1, _one, s22.addrCoefs(), nsing,
b12.addrCoefs(), nsing2);
for (int i = 0; i < nsing2; i++) {
for (int j = 0; j < nsing1; j++) {
b21(i, j) = b12(i, j) * s22(i, i);
}
}
} // _isSymmetric
else {
for (int j = 0; j < nsing1; j++) {
const int jj = perm[nsing2 + j];
for (int i = 0; i < nsing1; i++) {
const int ii = perm[nsing2 + i];
b22(i, j) = a22(ii, jj);
}
}
for (int i = 0; i < nsing2; i++) {
const int ii = perm[i];
for (int j = 0; j < nsing1; j++) {
const int jj = perm[nsing2 + j];
b12(i, j) = a22(ii, jj);
b21(i, j) = a22(jj, ii);
}
}
// alpha = T(1)
blas_trsm(CblasLeft, CblasLower, CblasNoTrans, CblasUnit,
nsing2, nsing1, _one, s22.addrCoefs(), nsing,
b12.addrCoefs(), nsing2);
blas_trsm(CblasLeft, CblasLower, CblasTrans, CblasUnit,
nsing2, nsing1, _one, s22.addrCoefs(), nsing,
b21.addrCoefs(), nsing2);
for (int i = 0; i < nsing2; i++) {
for (int j = 0; j < nsing1; j++) {
b12(i, j) *= s22(i, i);
}
}
} // _isSymmetric
// alpha = -1
// beta = 1
blas_gemm(CblasTrans, CblasNoTrans, nsing1, nsing1, nsing2, _none,
b21.addrCoefs(), nsing2, b12.addrCoefs(), nsing2, _one,
b22.addrCoefs(), nsing1);
b21.free();
b12.free();
} // if (nsing2 > 0)
else {
b22.init(nsing1, nsing1);
b22.copy(a22); //
}
} // if (n0 > 0)
bool flag, flag_2x2;
flag = ComputeDimKernel(&nn0, &flag_2x2, b22.addrCoefs(),
nsing1, _isSymmetric,
dim_aug_kern, eps_machine, eps_piv,
_verbose, _fp);
if (!flag) {
_n0 = (-1); // for safety
_detected = false;
}
else {
_n0 = nn0;
_detected = true;
}
} // if ((n0 == 0) && (count_repeat == 0)) && kernel_detection
for (int n = 0; n < _nfront; n++) {
if (diag_block_save[n].size() > 0) {
_diag_blocks[n].copy(diag_block_save[n]);
_num_null[n] -= num_null_aug[n];
num_null_aug[n] = 0; // for safety
}
diag_block_save[n].free(); // release memory
}
// recounting "nsing" after determinated singularity with restoring aug_dim
nsing = 0;
for (int n = 0; n < _nfront; n++) {
const int offset = _p_front[n];
const int dim1 = _p_front[n + 1] - offset;
const int num_nonsing = dim1 - _num_null[n];
for (int i = num_nonsing; i < dim1; i++, nsing++) {
list_sing[nsing] = _permute[i + offset] + offset;
}
}
a12.free();
a21.free();
a21.free();
s22.free();
if ((nsing == _n0) || (!_detected)) { // nsing >= _n0
_nscol = 0;
_list_schur.clear();
_list_elim.resize(nsing);
for (int i = 0; i < nsing; i++){
_list_elim[i] = list_sing[i];
}
_a12.free();
_a21.free();
_s22.free();
if (!_detected) {
_n0 = nsing;
}
}
else { // if ((nsing < _n0) && _detected)
a12.init(_dim, nsing);
a21.init(_dim, nsing);
a22.init(nsing, nsing);
ComputeSchurComplement(nsing, list_sing, a12, a21, a22);
// debug : end
pivot_val = 0.0;
for (int i = 0; i < nsing; i++) {
// pivot value in double
const double tmp = blas_abs(a22(i, i));
pivot_val = tmp > pivot_val ? tmp : pivot_val;
}
diss_printf(_verbose, _fp, "%s %d : _n0 = %d nsing = %d\n",
__FILE__, __LINE__, _n0, nsing);
// factorize Schur complement by knowing the dimension of the kernle : _n0
if (_isSymmetric) {
double fop;
bool flag;
flag = full_ldlt_permute(&nn0, _n0, nsing, a22.addrCoefs(), nsing,
&pivot_val, &perm[0], machine_eps, &fop);
if (!flag) {
diss_printf(_verbose, _fp,
"%s %d : full_ldlt_permute fails : %d != %d\n",
__FILE__, __LINE__, _n0, nn0);
}
}
else {
double fop;
bool flag;
flag = full_ldu_permute(&nn0, _n0, nsing, a22.addrCoefs(), nsing,
&pivot_val, &perm[0], machine_eps, &fop);
if (!flag) {
diss_printf(_verbose, _fp,
"%s %d : full_ldu_permute fails : %d != %d\n",
__FILE__, __LINE__, _n0, nn0);
}
}
_nscol = nsing - _n0;
diss_printf(_verbose, _fp, "%s %d : detected = %s : _nscol = %d _n0 = %d : perm[] = ",
__FILE__, __LINE__, (_detected ? "true" : "false"),
_nscol, _n0);
for (int i = 0; i < nsing; i++) {
diss_printf(_verbose, _fp, "%d ", perm[i]);
}
diss_printf(_verbose, _fp, "\n");
_list_schur.resize(_nscol);
if (_n0 > 0) {
_list_elim.resize(_n0);
}
for (int i = 0; i < _nscol; i++){
_list_schur[i] = _permute_ginv[list_sing[perm[i]]]; // regular part
}
for (int i = 0; i < _n0; i++){
_list_elim[i] = list_sing[perm[i + _nscol]];
}
diss_printf(_verbose, _fp, "%s %d : _nscol = %d : _list_schur[] = ",
__FILE__, __LINE__, _nscol);
for (int i = 0; i < _nscol; i++) {
diss_printf(_verbose, _fp, "%d ", _list_schur[i]);
}
diss_printf(_verbose, _fp, "\n");
if (_n0 > 0) {
diss_printf(_verbose, _fp, "%s %d : n0 = %d : _list_elim[] = ",
__FILE__, __LINE__, _n0);
for (int i = 0; i < _n0; i++) {
diss_printf(_verbose, _fp, "%d ", _list_elim[i]);
}
diss_printf(_verbose, _fp, "\n");
diss_printf(_verbose, _fp,
"%s %d : n0 = %d : _new2old[_list_elim[]] = ",
__FILE__, __LINE__, _n0);
for (int i = 0; i < _n0; i++) {
diss_printf(_verbose, _fp, "%d ", _new2old[_list_elim[i]]);
}
diss_printf(_verbose, _fp, "\n");
} // if (_n0 > 0)
_a12.init(_dim, _nscol);
for (int j = 0; j < _nscol; j++) {
for (int i = 0; i < _dim; i++) {
_a12(_permute_ginv[i], j) = a12(i, perm[j]);
}
}
if (!_isSymmetric) {
_a21.init(_dim, _nscol);
for (int j = 0; j < _nscol; j++) {
for (int i = 0; i < _dim; i++) {
_a21(_permute_ginv[i], j) = a21(i, perm[j]);
}
}
}
else {
_a21.free();
}
_s22.init(_nscol, _nscol);
// a22 is already permuted by perm[] during LDLt/LDU,
// _s22 is smaller than a22
for (int i = 0; i < _nscol; i++) {
for (int j = 0; j < _nscol; j++) {
_s22(i, j) = a22(i, j);
}
}
a12.free();
a21.free();
a22.free();
}
perm.clear();
} // if (nsing > 0)
delete [] diag_block_save;
}
template
void TridiagBlockMatrix::
NumericFact(const double* coef,
const double eps_pivot,
double *pivot,
const bool kernel_detection,
const bool higher_precision,
const int dim_aug_kern,
const double eps_machine,
double *nopd);
template
void TridiagBlockMatrix::
NumericFact(const quadruple* coef,
const double eps_pivot,
double *pivot,
const bool kernel_detection,
const bool higher_precision,
const int dim_aug_kern,
const quadruple eps_machine,
double *nopd);
template void
TridiagBlockMatrix, double>::
NumericFact(const complex* coef,
const double eps_pivot,
double *pivot,
const bool kernel_detection,
const bool higher_precision,
const int dim_aug_kern,
const double eps_machine,
double *nopd);
template
void TridiagBlockMatrix, quadruple>::
NumericFact(const complex* coef,
const double eps_pivot,
double *pivot,
const bool kernel_detection,
const bool higher_precision,
const int dim_aug_kern,
const quadruple eps_machine,
double *nopd);
template
void TridiagBlockMatrix::
NumericFact(const float* coef,
const double eps_pivot,
double *pivot,
const bool kernel_detection,
const bool higher_precision,
const int dim_aug_kern,
const float eps_machine,
double *nopd);
template void
TridiagBlockMatrix, float>::
NumericFact(const complex* coef,
const double eps_pivot,
double *pivot,
const bool kernel_detection,
const bool higher_precision,
const int dim_aug_kern,
const float eps_machine,
double *nopd);
//
template
void TridiagBlockMatrix::ComputeSchurComplement(const int nsing,
vector &list_sing,
ColumnMatrix& a12,
ColumnMatrix& a21,
ColumnMatrix& a22)
{
// _nscol should be set 0 before calling
a12.ZeroClear();
a21.ZeroClear();
a22.ZeroClear();
for (int j = 0; j < nsing; j++) {
extract_column(list_sing[j], a12.addrCoefs() + (j * _dim));
}
for (int j = 0; j < nsing; j++) {
for (int i = 0; i < nsing; i++) {
a22(i, j) = a12(list_sing[i], j);
}
}
for (int j = 0; j < nsing; j++) {
for (int i = 0; i < nsing; i++) {
a12(list_sing[i], j) = _zero;
}
}
if (_isSymmetric) {
a21.copy(a12);
}
else {
for (int j = 0; j < nsing; j++) {
extract_row(list_sing[j], a21.addrCoefs() + (j * _dim));
}
for (int j = 0; j < nsing; j++) {
for (int i = 0; i < nsing; i++) {
a21(list_sing[i], j) = _zero;
}
}
}
SolveMulti(false, false, nsing, a12, 0);
// alpha = -1
// beta = 1
blas_gemm(CblasTrans, CblasNoTrans, nsing, nsing, _dim, _none,
a21.addrCoefs(), _dim, a12.addrCoefs(), _dim, _one,
a22.addrCoefs(), nsing);
if (_isSymmetric) {
// symmetrize
for (int i = 0; i < nsing; i++) {
for (int j = i + 1; j < nsing; j++) {
a22(j, i) = a22(i, j); // acess lower
}
}
}
else {
SolveMulti(false, true, nsing, a21, 0);
}
}
template
void TridiagBlockMatrix::
ComputeSchurComplement(const int nsing, vector &list_sing,
ColumnMatrix& a12,
ColumnMatrix& a21,
ColumnMatrix& a22);
template
void TridiagBlockMatrix::
ComputeSchurComplement(const int nsing, vector &list_sing,
ColumnMatrix& a12,
ColumnMatrix& a21,
ColumnMatrix& a22);
template void
TridiagBlockMatrix, double>::
ComputeSchurComplement(const int nsing, vector &list_sing,
ColumnMatrix >& a12,
ColumnMatrix >& a21,
ColumnMatrix >& a22);
template
void TridiagBlockMatrix, quadruple>::
ComputeSchurComplement(const int nsing, vector &list_sing,
ColumnMatrix >& a12,
ColumnMatrix >& a21,
ColumnMatrix >& a22);
template
void TridiagBlockMatrix::
ComputeSchurComplement(const int nsing, vector &list_sing,
ColumnMatrix& a12,
ColumnMatrix& a21,
ColumnMatrix& a22);
template void
TridiagBlockMatrix, float>::
ComputeSchurComplement(const int nsing, vector &list_sing,
ColumnMatrix >& a12,
ColumnMatrix >& a21,
ColumnMatrix >& a22);
//
template
void TridiagBlockMatrix::SingularNode(vector &list_sing)
{
if (_n0 >= 0) {
list_sing.resize(_n0);
for (int i = 0; i < _n0; i++) {
list_sing[i] = _new2old[_list_elim[i]];
}
}
else {
diss_printf(_verbose, _fp, "%s %d : _n0 = %d\n", __FILE__, __LINE__, _n0);
}
}
template
void TridiagBlockMatrix::
SingularNode(vector &list_sing);
template
void TridiagBlockMatrix::
SingularNode(vector &list_sing);
template void
TridiagBlockMatrix, double>::
SingularNode(vector &list_sing);
template
void TridiagBlockMatrix, quadruple>::
SingularNode(vector &list_sing);
template
void TridiagBlockMatrix::
SingularNode(vector &list_sing);
template void
TridiagBlockMatrix, float>::
SingularNode(vector &list_sing);
//
//#define DEBUG_FACTORIZATION
template
void TridiagNumericFact_(double *pivot,
const double eps_pivot,
const int dim_aug_kern,
ColumnMatrix *diag_block_save,
vector &num_null_aug,
double *nopd,
const bool higher_precision,
bool &_verbose,
FILE* &_fp,
int &_dim,
bool &_isSymmetric,
int &_nfront,
vector &_p_front,
vector &_p_diag,
vector &_p_upper,
const T* &_coef,
vector &_ptRows,
vector &_indCols, // column_numb
vector &_indVals, // column_numb
vector &_num_null,
vector &_permute,
vector &_permute_ginv,
ColumnMatrix* &_diag_blocks,
ColumnMatrix* &_diag_blocks_high,
ColumnMatrix* &_upper_blocks,
ColumnMatrix* &_lower_blocks,
list &_high_blocks
#ifdef STORE_WHOLE_FACTORIZED
,
ColumnMatrix &_factorized_whole
#endif
)
{
int dim1, dim2, nn0;
const int n0 = 0;
int n0_total = 0;
ColumnMatrix diag_works, diag_origs; // supposing "dim2 <= size_b2"
#ifdef SPARSE_OFFDIAG
ColumnMatrix upper, lower;
ColumnMatrix upper_high, lower_high;
#endif
int upper_size_max;
int diag_size_max;
int *permute_diag, *permute_offdiag, *permute_work;
int *permute_diag_inv, *permute_offdiag_inv;
double fop;
//const double eps_pivot_high = machine_epsilon();
const double eps_pivot_high = machine_epsilon();
const T _zero(0.0);
double pivot_save;
_num_null.resize(_nfront);
// _null_lists_local.resize(_nfront);
_permute.resize(_dim);
*pivot = 1.0;
upper_size_max = 0;
for (int n = 1; n < _nfront; n++) {
const int itmp = ((_p_front[n] - _p_front[n - 1]) *
(_p_front[n + 1] - _p_front[n]));
upper_size_max = itmp > upper_size_max ? itmp : upper_size_max;
}
#ifdef SPARSE_OFFDIAG
VectorArray upper_work(upper_size_max);
VectorArray lower_work(upper_size_max);
if (higher_precision) {
VectorArray upper_high_work(upper_size_max);
VectorArray lower_high_work(upper_size_max);
}
#else
VectorArray permute_vals(upper_size_max);
#endif
bool high_factorization = false;
bool high_schur_complement = false;
diag_size_max = 0;
for (int n = 0; n < _nfront; n++) {
const int itmp = _p_front[n + 1] - _p_front[n];
_diag_blocks[n].init(itmp, itmp);
diag_size_max = itmp > diag_size_max ? itmp : diag_size_max;
}
permute_offdiag = new int[diag_size_max];
permute_offdiag_inv = new int[diag_size_max];
permute_diag_inv = new int[diag_size_max];
permute_work = new int[diag_size_max];
{ // n = 0
dim2 = _p_front[1] - _p_front[0];
ColumnMatrix &diag = _diag_blocks[0];
diag.ZeroClear();
permute_diag = &_permute[0] + _p_front[0];
for (int i = _p_front[0]; i < _p_front[1]; i++) {
for (int k = _p_diag[i]; k < _p_upper[i]; k++) {
const int ii = i - _p_front[0];
const int jj = _indCols[k] - _p_front[0];
diag(ii, jj) = _coef[_indVals[k]];
}
}
if (higher_precision) { // copy to higher precision
ColumnMatrix &diag_high = _diag_blocks_high[0];
diag_high.init(dim2, dim2);
for (int j = 0; j < dim2; j++) {
for (int i = 0; i < dim2; i++) {
diag_high(i, j) = conv_prec(diag(i, j));
}
}
pivot_save = *pivot;
}
if (_isSymmetric) {
bool flag;
flag = full_ldlt_permute(&nn0, n0, dim2,
diag.addrCoefs(), dim2, pivot,
permute_diag, eps_pivot, &fop);
}
else {
bool flag;
flag = full_ldu_permute(&nn0, n0, dim2,
diag.addrCoefs(), dim2, pivot,
permute_diag, eps_pivot, &fop);
}
if (higher_precision) {
high_factorization = false;
if (nn0 > 0) {
// if (true) {
fprintf(stderr, "%s %d : switch to quadruple at %d %d/%d ",
__FILE__, __LINE__, 0, nn0, dim2);
high_factorization = true;
_high_blocks.push_back(0);
ColumnMatrix &diag_high = _diag_blocks_high[0];
*pivot = pivot_save;
if (_isSymmetric) {
bool flag;
flag = full_ldlt_permute(&nn0, n0, dim2,
diag_high.addrCoefs(), dim2, pivot,
permute_diag, eps_pivot_high, &fop);
}
else {
bool flag;
flag = full_ldu_permute(&nn0, n0, dim2,
diag_high.addrCoefs(), dim2, pivot,
permute_diag, eps_pivot_high, &fop);
}
fprintf(stderr, "%d\n", nn0);
#if 0
for (int i = 0; i < dim2; i++) {
fprintf(stderr, "%d %s %s\n", i,
tostring(diag(i,i)).c_str(),
tostring(diag_high(i,i)).c_str());
}
#endif
for (int j = 0; j < dim2; j++) {
for (int i = 0; i < dim2; i++) {
diag(i, j) = conv_prec(diag_high(i, j));
}
}
}
}
{
_num_null[0] = nn0;
*nopd += fop;
for (int i = 0; i < nn0; i++) {
// _null_lists_local[0].push_back(i + (dim2 - nn0));
// nullifying rows and columns
for (int i = (dim2 - nn0); i < dim2; i++) {
for (int j = 0; j < dim2; j++) {
diag(i, j) = _zero;
diag(j, i) = _zero;
}
}
}
n0_total += nn0;
}
} // n = 0
for (int n = 1; n < _nfront; n++) {
ColumnMatrix &diag1 = _diag_blocks[n - 1];
vector i0;
dim1 = dim2;
dim2 = _p_front[n + 1] - _p_front[n];
#ifdef SPARSE_OFFDIAG
upper.init(dim1, dim2, upper_work.addrCoefs(), false);
lower.init(dim1, dim2, lower_work.addrCoefs(), false);
upper_high.init(dim1, dim2, upper_high_work.addrCoefs(), false);
lower_high.init(dim1, dim2, lower_high_work.addrCoefs(), false);
#else
ColumnMatrix &upper = _upper_blocks[n]; // 27 Nov.2015
ColumnMatrix &lower = _lower_blocks[n]; // 27 Nov.2015
upper.init(dim1, dim2);
lower.init(dim1, dim2);
ColumnMatrix upper_high;
ColumnMatrix lower_high;
if (high_factorization) {
upper_high.init(dim1, dim2);
lower_high.init(dim1, dim2);
}
#endif
// ColumnMatrix upper, lower
// permute_diag[] is defined in the previous step n-1
// generate permute_offdiag[] from previous permute_diag[]
GenPermuteOffdiag(_p_front[n - 1], _p_front[n], _p_front[n + 1],
_ptRows, _p_diag, _indCols,
permute_diag, permute_diag_inv,
permute_offdiag, permute_offdiag_inv,
i0);
upper.ZeroClear();
if (_isSymmetric) {
for (int i = _p_front[n]; i < _p_front[n + 1]; i++) {
const int jj = permute_offdiag_inv[i - _p_front[n]];
for (int k = _ptRows[i]; k < _p_diag[i]; k++) {
// access lower block to create upper block
const int ii = permute_diag_inv[_indCols[k] - _p_front[n - 1]];
upper(ii, jj) = _coef[_indVals[k]];
}
}
}
else {
lower.ZeroClear();
for (int i = _p_front[n - 1]; i < _p_front[n]; i++) {
const int ii = permute_diag_inv[i - _p_front[n - 1]];
for (int k = _p_upper[i]; k < _ptRows[i + 1]; k++) {
const int jj = permute_offdiag_inv[_indCols[k] - _p_front[n]];
upper(ii, jj) = _coef[_indVals[k]];
}
}
for (int i = _p_front[n]; i < _p_front[n + 1]; i++) {
const int jj = permute_offdiag_inv[i - _p_front[n]];
for (int k = _ptRows[i]; k < _p_diag[i]; k++) {
// same as upper() of _isSymmetric
const int ii = permute_diag_inv[_indCols[k] - _p_front[n - 1]];
lower(ii, jj) = _coef[_indVals[k]];
}
}
}
ColumnMatrix &diag = _diag_blocks[n];
if (high_factorization) {
ColumnMatrix &diag1_high = _diag_blocks_high[n - 1];
for (int j = 0; j < dim2; j++) {
for (int i = 0; i < dim1; i++) {
upper_high(i, j)=conv_prec(upper(i,j));
lower_high(i, j)=conv_prec(lower(i,j));
}
}
full_fw_multiprofile(false, dim1, _num_null[n - 1], dim2,
diag1_high.addrCoefs(), dim1,
upper_high.addrCoefs(), dim1, i0, &fop);
if (_isSymmetric) { // reduce arithmeic by setting T(0) for 0 <=i< i0[j]
for (int j = 0; j < dim2; j++) {
for (int i = 0; i < dim1; i++) {
lower_high(i, j) = upper_high(i, j) * diag1_high(i, i);
}
}
}
else {
full_fw_multiprofile(true, dim1, _num_null[n - 1], dim2,
diag1_high.addrCoefs(), dim1,
lower_high.addrCoefs(), dim1, i0, &fop);
for (int j = 0; j < dim2; j++) {
for (int i = 0; i < dim1; i++) {
lower_high(i, j) *= diag1_high(i, i);
}
}
}
ColumnMatrix &diag_high = _diag_blocks_high[n];
diag_high.init(dim2, dim2);
// alpha = -1, beta = 0
SparseSchur(_isSymmetric, dim2, dim1, i0, upper_high, lower_high,
diag_high, &fop);
for (int i = _p_front[n]; i < _p_front[n + 1]; i++) {
for (int k = _p_diag[i]; k < _p_upper[i]; k++) {
const int ii = permute_offdiag_inv[i - _p_front[n]];
const int jj = permute_offdiag_inv[_indCols[k] - _p_front[n]];
diag_high(ii, jj) += conv_prec(_coef[_indVals[k]]);
}
}
for (int j = 0; j < dim2; j++) {
for (int i = 0; i < dim2; i++) {
diag(i, j) = conv_prec(diag_high(i, j));
}
}
#ifndef SPARSE_OFFDIAG
for (int j = 0; j < dim2; j++) {
for (int i = 0; i < dim1; i++) {
upper_high(i, j) *= diag1_high(i, i);
}
}
#endif
for (int j = 0; j < dim2; j++) {
for (int i = 0; i < dim1; i++) {
upper(i, j)=conv_prec(upper_high(i,j));
lower(i, j)=conv_prec(lower_high(i,j));
}
}
#ifndef SPARSE_OFFDIAG
upper_high.free();
lower_high.free();
#endif
high_schur_complement = true;
// diag_high.free();
} // if (high_factorization)
else {
full_fw_multiprofile(false, dim1, _num_null[n - 1], dim2,
diag1.addrCoefs(), dim1,
upper.addrCoefs(), dim1, i0, &fop);
if (_isSymmetric) { // reduce arithmeic by setting T(0) for 0 <=i< i0[j]
for (int j = 0; j < dim2; j++) {
for (int i = 0; i < dim1; i++) {
lower(i, j) = upper(i, j) * diag1(i, i);
}
}
}
else {
full_fw_multiprofile(true, dim1, _num_null[n - 1], dim2,
diag1.addrCoefs(), dim1,
lower.addrCoefs(), dim1, i0, &fop);
for (int j = 0; j < dim2; j++) {
for (int i = 0; i < dim1; i++) {
lower(i, j) *= diag1(i, i);
}
}
}
// alpha = -1, beta = 0
SparseSchur(_isSymmetric, dim2, dim1, i0, upper, lower, diag, &fop);
for (int i = _p_front[n]; i < _p_front[n + 1]; i++) {
for (int k = _p_diag[i]; k < _p_upper[i]; k++) {
const int ii = permute_offdiag_inv[i - _p_front[n]];
const int jj = permute_offdiag_inv[_indCols[k] - _p_front[n]];
diag(ii, jj) += _coef[_indVals[k]];
}
}
#ifndef SPARSE_OFFDIAG
for (int j = 0; j < dim2; j++) {
for (int i = 0; i < dim1; i++) {
upper(i, j) *= diag1(i, i);
}
}
#endif
} // if (high_factorization)
if (higher_precision) { // copy to higher precision
ColumnMatrix &diag_high = _diag_blocks_high[n];
if (!high_schur_complement) {
diag_high.init(dim2, dim2);
for (int j = 0; j < dim2; j++) {
for (int i = 0; i < dim2; i++) {
diag_high(i, j) = conv_prec(diag(i, j));
}
}
}
pivot_save = *pivot;
}
if (_isSymmetric) {
bool flag;
flag = full_ldlt_permute(&nn0, n0, dim2, diag.addrCoefs(), dim2,
pivot, permute_work, eps_pivot, &fop);
}
else {
bool flag;
flag = full_ldu_permute(&nn0, n0, dim2, diag.addrCoefs(), dim2,
pivot, permute_work, eps_pivot, &fop);
}
if (higher_precision) {
high_factorization = false;
if (nn0 > 0) {
// if (true) {
fprintf(stderr, "%s %d : switch to quadruple at %d %d/%d : ",
__FILE__, __LINE__, n, nn0, dim2);
high_factorization = true;
_high_blocks.push_back(n);
ColumnMatrix &diag_high = _diag_blocks_high[n];
*pivot = pivot_save;
if (_isSymmetric) {
bool flag;
flag = full_ldlt_permute(&nn0, n0, dim2,
diag_high.addrCoefs(), dim2, pivot,
permute_work, eps_pivot_high, &fop);
}
else {
bool flag;
flag = full_ldu_permute(&nn0, n0, dim2,
diag_high.addrCoefs(), dim2, pivot,
permute_work, eps_pivot_high, &fop);
}
fprintf(stderr, "%d\n", nn0);
#if 0
for (int i = 0; i < dim2; i++) {
fprintf(stderr, "%d %s %s\n", i,
tostring(diag(i,i)).c_str(),
tostring(diag_high(i,i)).c_str());
}
#endif
for (int j = 0; j < dim2; j++) {
for (int i = 0; i < dim2; i++) {
diag(i, j) = conv_prec(diag_high(i, j));
}
}
}
else {
#if 0
fprintf(stderr, "%s %d : not using quadruple at %d %d/%d : ",
__FILE__, __LINE__, n, nn0, dim2);
fprintf(stderr, "%d\n", nn0);
// if (nn0 > 0) {
{
for (int i = 0; i < dim2; i++) {
fprintf(stderr, "%d %s\n", i,
tostring(diag(i,i)).c_str());
}
}
#endif
ColumnMatrix &diag_high = _diag_blocks_high[n];
diag_high.free();
}
high_schur_complement = false;
} // if (higher_precision)
permute_diag = &_permute[0] + _p_front[n];
// generate permute_diag from pemrute_work + perumte_upper
for (int i = 0; i < dim2; i++) {
permute_diag[i] = permute_offdiag[permute_work[i]];
}
#ifndef SPARSE_OFFDIAG
for (int j = 0; j < dim2; j++) {
for (int i = 0; i < dim1; i++) {
permute_vals[i + j * dim1] = upper(i, permute_work[j]);
}
}
for (int j = 0; j < dim2; j++) {
for (int i = 0; i < dim1; i++) {
upper(i, j) = permute_vals[i + j * dim1];
}
}
for (int j = 0; j < dim2; j++) {
for (int i = 0; i < dim1; i++) {
permute_vals[i + j * dim1] = lower(i, permute_work[j]);
}
}
for (int j = 0; j < dim2; j++) {
for (int i = 0; i < dim1; i++) {
lower(i, j) = permute_vals[i + j * dim1];
}
}
#endif
{
_num_null[n] = nn0;
*nopd += fop;
for (int i = 0; i < nn0; i++) {
// nullifying rows and columns
for (int i = (dim2 - nn0); i < dim2; i++) {
for (int j = 0; j < dim2; j++) {
diag(i, j) = _zero;
diag(j, i) = _zero;
}
}
} // loop : i
n0_total += nn0;
}
} // loop : n
if ((n0_total > 0) && (dim_aug_kern > 0)) {
int count = 0;
bool flag = false;
num_null_aug.clear();
num_null_aug.resize(_nfront, 0);
for (int n = (_nfront - 1); n >= 0; n--) {
dim1 = _p_front[n + 1] - _p_front[n];
for (int k = (dim1 - _num_null[n] - 1); k >= 0; k--) {
num_null_aug[n]++;
count++;
if (count >= dim_aug_kern) {
flag = true;
break;
}
}
if (flag) {
break;
}
}
if (flag == false) {
diss_printf(_verbose, _fp, "%s %d : dim_aug_kern = %d > dim = %d?\n",
__FILE__, __LINE__, dim_aug_kern, _dim);
}
for (int n = (_nfront - 1); n >= 0; n--) {
diag_block_save[n].free();
if (num_null_aug[n] > 0) {
dim1 = _p_front[n + 1] - _p_front[n];
diag_block_save[n].init(dim1, dim1);
diag_block_save[n].copy(_diag_blocks[n]);
_num_null[n] += num_null_aug[n]; //
for (int j = 0; j < num_null_aug[n]; j++) {
const int jj = dim1 - _num_null[n] + j;
for (int i = 0; i < dim1; i++) {
_diag_blocks[n](i, jj) = _zero;
_diag_blocks[n](jj, i) = _zero;
}
}
}
} // loop : n
} // if ((n0_total > 0) && kernel_detection) {
_permute_ginv.resize(_dim);
for (int n = 0; n < _nfront; n++) {
const int offset = _p_front[n];
for (int i = offset; i < _p_front[n + 1]; i++) {
_permute_ginv[_permute[i] + offset] = i;
}
}
//#ifdef SPARSE_OFFDIAG
// delete [] upper_work;
// delete [] lower_work;
//#else
// delete [] permute_vals;
//#endif
delete [] permute_offdiag;
delete [] permute_diag_inv;
delete [] permute_offdiag_inv;
delete [] permute_work;
//#ifdef DEBUG_FACTORIZATION
// delete [] diag_orig;
//#endif
}
template void
TridiagNumericFact_(double *pivot,
const double eps_pivot,
const int dim_aug_kern,
ColumnMatrix *diag_block_save,
vector &num_null_aug,
double *nopd,
const bool higher_precision,
bool &_verbose,
FILE* &_fp,
int &_dim,
bool &_isSymmetric,
int &_nfront,
vector &_p_front,
vector &_p_diag,
vector &_p_upper,
const double* &_coef,
vector &_ptRows,
vector &_indCols, // column_numb
vector &_indVals, // column_numb
vector &_num_null,
vector &_permute,
vector &_permute_ginv,
ColumnMatrix* &_diag_blocks,
ColumnMatrix* &_diag_blocks_high,
ColumnMatrix* &_upper_blocks,
ColumnMatrix* &_lower_blocks,
list &_high_blocks
#ifdef STORE_WHOLE_FACTORIZED
,
ColumnMatrix &_factorized_whole
#endif
);
template void
TridiagNumericFact_, double,
complex