/*! \file DissectionSolver.cpp
\brief task mangemanet of dissection algorithm
\author Atsushi Suzuki, Laboratoire Jacques-Louis Lions
\date Mar. 30th 2012
\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
#include
#include
#include "Driver/DissectionSolver.hpp"
#include "Driver/C_BlasRoutines.hpp"
#include "Driver/C_KernDetect.hpp"
#include "Driver/CopyMatrix.hpp"
#include "Driver/TridiagBlockMatrix.hpp"
#include "Splitters/MetisSplitter.hpp"
#include "Algebra/SparseRenumbering.hpp"
#include "Algebra/VectorArray.hpp"
#include "Compiler/DissectionIO.hpp"
#define NORMALIZE_KERNEL_BASIS
template
const T DissectionSolver::_one = T(1.0);
template
const T DissectionSolver::_none = T(-1.0);
template
const T DissectionSolver::_zero = T(0.0);
template
void DissectionSolver::
Destroy(void)
{
for (int m = 0; m < _graph_colors;m++) {
if (!_tridiagQueue[m]->tridiagSolver()) {
delete _dissectionQueue[m];
for (typename vector* >::const_iterator it =
_dissectionMatrix[m].begin();
it != _dissectionMatrix[m].end(); ++it) {
delete (*it);
}
}
else {
delete _tridiagMatrix[m];
}
}
delete [] _dissectionQueue;
delete [] _dissectionMatrix;
delete [] _tridiagMatrix;
delete _ptDA;
delete[] _precDiag;
NumericFree();
delete [] _Schur;
delete [] _kernel;
delete [] _singIdx; // added 01 Oct.2013 Atsushi
for (int m = 0; m < _graph_colors;m++) {
if (_with_btree && !_tridiagQueue[m]->tridiagSolver()) {
delete _btree[m]; // added 01 Oct.2013 Atsushi
}
delete _tridiagQueue[m];
}
delete [] _tridiagQueue;
delete [] _btree;
_index_isolated.clear();
}
template
void DissectionSolver::Destroy(void);
template
void DissectionSolver::Destroy(void);
template
void DissectionSolver::Destroy(void);
template
void DissectionSolver::Destroy(void);
template
void DissectionSolver, double>::Destroy(void);
template
void DissectionSolver, quadruple>::Destroy(void);
template
void DissectionSolver, quadruple,
complex, double>::Destroy(void);
template
void DissectionSolver, double,
complex, quadruple>::Destroy(void);
template
void DissectionSolver::Destroy(void);
template
void DissectionSolver, float>::Destroy(void);
//
template
void DissectionSolver::
NumericFree(void)
{
diss_printf(_verbose, _fp,
"%s %d : void DissectionSolver::NumericFree()",
__FILE__, __LINE__ );
if (_status_factorized) {
diss_printf(_verbose, _fp, "start ");
for (int m = 0; m < _graph_colors; m++) {
_Schur[m].free();
_kernel[m].free();
_singIdx[m].clear(); // added 01 Oct.2013 Atsushi
}
diss_printf(_verbose, _fp, "end");
}
_status_factorized = false;
diss_printf(_verbose, _fp, ".\n");
}
template
void DissectionSolver::NumericFree(void);
template
void DissectionSolver::NumericFree(void);
template
void DissectionSolver::NumericFree(void);
template
void DissectionSolver::NumericFree(void);
template
void DissectionSolver, double>::NumericFree(void);
template
void DissectionSolver, quadruple>::NumericFree(void);
//
template
void DissectionSolver, quadruple, complex, double>::NumericFree(void);
template
void DissectionSolver, double, complex, quadruple>::NumericFree(void);
template
void DissectionSolver::NumericFree(void);
template
void DissectionSolver, float>::NumericFree(void);
//
template
void DissectionSolver::
SaveCSRMatrix(const int called, const T *coefs_)
{
diss_printf(true, stderr,
"%s %d : specialized template is not yet defined.\n",
__FILE__, __LINE__);
}
template
void DissectionSolver::SaveCSRMatrix(const int called,
const double *coefs);
template
void DissectionSolver, double>::
SaveCSRMatrix(const int called,
const complex *coefs);
template
void DissectionSolver::
SaveCSRMatrix(const int called,
const quadruple *coefs);
template
void DissectionSolver, quadruple>::
SaveCSRMatrix(const int called,
const complex *coefs);
template
void DissectionSolver::SaveCSRMatrix(const int called,
const float *coefs);
template
void DissectionSolver, float>::
SaveCSRMatrix(const int called,
const complex *coefs);
//
template
void DissectionSolver::
SaveMMMatrix(const int called, const T *coefs_)
{
diss_printf(true, stderr,
"%s %d : specialized template is not yet defined.\n",
__FILE__, __LINE__);
}
template<>
void DissectionSolver::SaveMMMatrix(const int called,
const double *coefs_)
{
SaveMMMatrix_(_dim,
_ptDA->nnz(),
_ptDA->isSymmetric(),
_ptDA->isUpper(),
_ptDA->getRows(),
_ptDA->getIndCols(),
called, coefs_);
}
template<>
void DissectionSolver, double>::
SaveMMMatrix(const int called,
const complex *coefs_)
{
SaveMMMatrix_(_dim,
_ptDA->nnz(),
_ptDA->isWhole() ? false : _ptDA->isSymmetric(),
_ptDA->isUpper(),
_ptDA->getRows(),
_ptDA->getIndCols(),
called, coefs_);
}
template
void DissectionSolver::
SaveMMMatrix(const int called,
const quadruple *coefs_);
template
void DissectionSolver, quadruple>::
SaveMMMatrix(const int called,
const complex *coefs_);
//
void SaveMMMatrix_(const int dim,
const int nnz,
const bool isSymmetric,
const bool isUpper,
const int *ptrows,
const int *indcols,
const int called,
const double *coefs_)
{
// MatrixMarket format : one-based index, lower part stored for symmetric
char fname[256];
int pid = get_process_id();
FILE *fp;
sprintf(fname, "matrix.%04d.%06d.data", called, pid);
if ((fp = fopen(fname, "w")) != NULL) {
fprintf(fp, "%%%%MatrixMarket matrix coordinate real %s\n%d %d %d\n",
isSymmetric? "symmetric" : "general",
dim, dim, nnz);
if (isUpper) {
for (int i = 0; i < dim; i++) {
for (int k = ptrows[i]; k < ptrows[i + 1]; k++) {
const int jj = indcols[k] + 1;
fprintf(fp, "%d %d %.16e\n", jj, (i + 1), coefs_[k]);
}
}
}
else {
for (int i = 0; i < dim; i++) {
for (int k = ptrows[i]; k < ptrows[i + 1]; k++) {
const int jj = indcols[k] + 1;
fprintf(fp, "%d %d %.16e\n", (i + 1), jj, coefs_[k]);
}
}
}
fclose(fp);
}
else {
fprintf(stderr,
"%s %d : fail to open %s\n",
__FILE__, __LINE__, fname);
exit(-1);
}
}
void SaveMMMatrix_(const int dim,
const int nnz,
const bool isSymmetric,
const bool isUpper,
const int *ptrows,
const int *indcols,
const int called,
const complex *coefs_)
{
// MatrixMarket format : one-based index, lower part stored for symmetric
char fname[256];
int pid = get_process_id();
FILE *fp;
sprintf(fname, "matrix.%04d.%06d.data", called, pid);
if ((fp = fopen(fname, "w")) != NULL) {
fprintf(fp, "%%%%MatrixMarket matrix coordinate complex %s\n%d %d %d\n",
isSymmetric? "symmetric" : "general",
dim, dim, nnz);
if (isUpper) {
for (int i = 0; i < dim; i++) {
for (int k = ptrows[i]; k < ptrows[i + 1]; k++) {
const int jj = indcols[k] + 1;
fprintf(fp, "%d %d %.16e %.16e\n", jj, (i + 1),
coefs_[k].real(), coefs_[k].imag());
}
}
}
else {
for (int i = 0; i < dim; i++) {
for (int k = ptrows[i]; k < ptrows[i + 1]; k++) {
const int jj = indcols[k] + 1;
fprintf(fp, "%d %d %.16e %.16e\n", (i + 1), jj,
coefs_[k].real(), coefs_[k].imag());
}
}
}
fclose(fp);
}
else {
fprintf(stderr,
"%s %d : fail to open %s\n",
__FILE__, __LINE__, fname);
exit(-1);
}
}
// copy from higher precision than T and U : quadruple <- T = double
template
void DissectionSolver::
CopyQueueFwBw(DissectionSolver &qdslv)
{
bool isSym = qdslv.ptDA()->isSymmetric();
bool isUpper = qdslv.ptDA()->isUpper();
bool isWhole = qdslv.ptDA()->isWhole();
_dim = qdslv.dimension();
_graph_colors = qdslv.graph_colors();
_scaling = qdslv.scaling();
_verbose = qdslv.verbose();
_num_threads = qdslv.num_threads();
_nsing = qdslv.nsing();
_fp = qdslv.get_filedescriptor();
bool verbose = qdslv.verbose();
// T *coefs;
_dissectionQueue = new DissectionQueue*[_graph_colors];
_tridiagQueue = new TridiagQueue*[_graph_colors];
_dissectionMatrix =
new vector* >[_graph_colors];
_tridiagMatrix = new TridiagBlockMatrix*[_graph_colors];
_Schur = new SchurMatrix[_graph_colors];
_kernel = new KernelMatrix[_graph_colors];
_singIdx = new vector[_graph_colors];
for (int m = 0; m < _graph_colors; m++) {
_Schur[m].getAcol() = new SparseMatrix(); // dummy allocation
_Schur[m].getArow() = new SparseMatrix(); // dummy allocation
}
DissectionQueue** dQ = qdslv.getDissectionQueue();
TridiagQueue** tQ = qdslv.getTridiagQueue();
_precDiag = new U[_dim];
for (int i = 0; i < _dim; i++) {
_precDiag[i] = conv_prec(qdslv.addrPrecDiag()[i]);
}
_ptDA = new SparseMatrix(isSym, isUpper, isWhole);
CopySparseMatrix(_ptDA, qdslv.ptDA());
// coefs = _ptDA->getCoef();
for (int m = 0; m < _graph_colors; m++) {
if (tQ[m]->tridiagSolver()) {
const int dim = tQ[m]->dimension();
_tridiagMatrix[m] = new TridiagBlockMatrix(dim,
SIZE_B1,
isSym,
0, // no other Tridiag
verbose,
_fp);
CopyTridiagBlockMatrix(*_tridiagMatrix[m],
*qdslv.getTridiagBlockMatrix()[m],
_ptDA->getCoef());
_tridiagQueue[m] = new TridiagQueue(true, verbose, _fp);
_tridiagQueue[m]->generate_queue(_tridiagMatrix[m],
tQ[m]->dimension(),
tQ[m]->nnz(),
tQ[m]->isMapped(),
tQ[m]->remap_eqn(),
tQ[m]->ptRows(),
tQ[m]->indCols(),
tQ[m]->indVals(),
_ptDA->getCoef());
}
else { // if (tQ[m]->tridiagSolver())
Dissection::Tree* btree = qdslv.btree()[m];
_tridiagQueue[m] = new TridiagQueue(false, verbose, _fp);
const int nb_doms = btree->NumberOfSubdomains();
_dissectionMatrix[m].resize(nb_doms);
_dissectionQueue[m] = new DissectionQueue(btree,
_dissectionMatrix[m],
_num_threads,
isSym,
verbose,
_fp);
typename vector* >::const_iterator it = qdslv.getDissectionMatrix()[m].begin();
typename vector* >::const_iterator jt = _dissectionMatrix[m].begin();
for ( ; it != qdslv.getDissectionMatrix()[m].end(); ++it, ++jt) {
if ((*it)->islast()) {
int color = (*it)->ColorTridiagBlockMatrix();
for (int i = 0; i < color; i++) {
CopyTridiagBlockMatrix(*(*jt)->addrtridiagBlock()[i],
*(*it)->addrtridiagBlock()[i],
_ptDA->getCoef());
} // loop : i
} // if ((*it)->islast())
else {
CopySquareBlockMatrix((*jt)->diagBlock(), (*it)->diagBlock());
}
SquareBlockMatrix* diag = (*jt)->addrdiagBlock();
RectBlockMatrix* lower = (*jt)->addrlowerBlock();
if (!isSym) {
CopyRectBlockMatrix(*lower, (*it)->lowerBlock());
}
RectBlockMatrix* upper = (*jt)->addrupperBlock();
CopyRectBlockMatrix(*upper, (*it)->upperBlock());
CopyDissectionMatrix((*jt),
(*it),
diag,
lower,
upper);
} // loop : it, jt
_dissectionQueue[m]->generate_queue_fwbw(_dissectionMatrix[m],
dQ[m]->dimension(),
dQ[m]->nnz(), _ptDA->getCoef());
} // if (tQ[m]->tridiagSolver())
CopyKernelMatrix(_kernel[m], qdslv.getKernelMatrix()[m]);
_singIdx[m] = qdslv.getSingVal()[m];
} // loop : m
// qdslv.GetMatrixScaling(_precDiag);
_index_isolated = qdslv.getIndexIsolated();
_status_factorized = true;
_btree = new Dissection::Tree*[_graph_colors]; // dummy allocation
_with_btree = false;
}
template
void DissectionSolver::
CopyQueueFwBw(DissectionSolver &qdslv);
template
void DissectionSolver, double, complex, quadruple>::
CopyQueueFwBw(DissectionSolver, quadruple,
complex, double> &qdslv);
template
void DissectionSolver::
CopyQueueFwBw(DissectionSolver &qdslv);
template
void DissectionSolver, quadruple, complex, double>::
CopyQueueFwBw(DissectionSolver, double, complex, quadruple> &qdslv);
template
void DissectionSolver::
CopyQueueFwBw(DissectionSolver &qdslv);
template
void DissectionSolver, float, complex, double>::
CopyQueueFwBw(DissectionSolver, double, complex, float> &qdslv);
template
void DissectionSolver::
SymbolicFact(const int dim_,
const int *ptRows,
const int *indCols,
const bool isSym,
const bool isUpper,
const bool isWhole,
const int decomposer_,
const int nbLevels_,
const int minNodes)
{
SymbolicFact_(dim_,
ptRows[dim_],
false,
ptRows,
indCols,
(long long int *)NULL, // const long long int *ptRows,
(long long int *)NULL, // const long long int *indCols,
isSym,
isUpper,
isWhole,
decomposer_,
nbLevels_,
minNodes);
}
template
void DissectionSolver::SymbolicFact(const int dim_,
const int *ptRows,
const int *indCols,
const bool isSym,
const bool isUpper,
const bool isWhole,
const int decomposer,
const int nbLevels_,
const int minNodes);
template
void DissectionSolver::SymbolicFact(const int dim_,
const int *ptRows,
const int *indCols,
const bool isSym,
const bool isUpper,
const bool isWhole,
const int decomposer,
const int nbLevels_,
const int minNodes);
template
void DissectionSolver, double>::
SymbolicFact(const int dim_,
const int *ptRows,
const int *indCols,
const bool isSym,
const bool isUpper,
const bool isWhole,
const int decomposer,
const int nbLevels_,
const int minNodes);
template
void DissectionSolver, quadruple>::
SymbolicFact(const int dim_,
const int *ptRows,
const int *indCols,
const bool isSym,
const bool isUpper,
const bool isWhole,
const int decomposer,
const int nbLevels_,
const int minNodes);
template
void DissectionSolver::SymbolicFact(const int dim_,
const int *ptRows,
const int *indCols,
const bool isSym,
const bool isUpper,
const bool isWhole,
const int decomposer,
const int nbLevels_,
const int minNodes);
template
void DissectionSolver::SymbolicFact(const int dim_,
const int *ptRows,
const int *indCols,
const bool isSym,
const bool isUpper,
const bool isWhole,
const int decomposer,
const int nbLevels_,
const int minNodes);
template
void DissectionSolver, double, complex, quadruple>::
SymbolicFact(const int dim_,
const int *ptRows,
const int *indCols,
const bool isSym,
const bool isUpper,
const bool isWhole,
const int decomposer,
const int nbLevels_,
const int minNodes);
template
void DissectionSolver, quadruple, complex, double>::
SymbolicFact(const int dim_,
const int *ptRows,
const int *indCols,
const bool isSym,
const bool isUpper,
const bool isWhole,
const int decomposer,
const int nbLevels_,
const int minNodes);
template
void DissectionSolver::SymbolicFact(const int dim_,
const int *ptRows,
const int *indCols,
const bool isSym,
const bool isUpper,
const bool isWhole,
const int decomposer,
const int nbLevels_,
const int minNodes);
template
void DissectionSolver, float>::SymbolicFact(const int dim_,
const int *ptRows,
const int *indCols,
const bool isSym,
const bool isUpper,
const bool isWhole,
const int decomposer,
const int nbLevels_,
const int minNodes);
//
template
void DissectionSolver::
SymbolicFact(const int dim_,
const long long int *ptRows,
const long long int *indCols,
const bool isSym,
const bool isUpper,
const bool isWhole,
const int decomposer_,
const int nbLevels_,
const int minNodes)
{
SymbolicFact_(dim_,
(int)ptRows[dim_],
true,
(int *)NULL, // const int *ptRows,
(int *)NULL, // const int *indCols,
ptRows,
indCols,
isSym,
isUpper,
isWhole,
decomposer_,
nbLevels_,
minNodes);
}
template
void DissectionSolver::SymbolicFact(const int dim_,
const long long int *ptRows,
const long long int *indCols,
const bool isSym,
const bool isUpper,
const bool isWhole,
const int decomposer,
const int nbLevels_,
const int minNodes);
template
void DissectionSolver::SymbolicFact(const int dim_,
const long long int *ptRows,
const long long int *indCols,
const bool isSym,
const bool isUpper,
const bool isWhole,
const int decomposer,
const int nbLevels_,
const int minNodes);
template
void DissectionSolver, double>::
SymbolicFact(const int dim_,
const long long int *ptRows,
const long long int *indCols,
const bool isSym,
const bool isUpper,
const bool isWhole,
const int decomposer,
const int nbLevels_,
const int minNodes);
template
void DissectionSolver, quadruple>::
SymbolicFact(const int dim_,
const long long int *ptRows,
const long long int *indCols,
const bool isSym,
const bool isUpper,
const bool isWhole,
const int decomposer,
const int nbLevels_,
const int minNodes);
template
void DissectionSolver::SymbolicFact(const int dim_,
const long long int *ptRows,
const long long int *indCols,
const bool isSym,
const bool isUpper,
const bool isWhole,
const int decomposer,
const int nbLevels_,
const int minNodes);
template
void DissectionSolver, float>::SymbolicFact(const int dim_,
const long long int *ptRows,
const long long int *indCols,
const bool isSym,
const bool isUpper,
const bool isWhole,
const int decomposer,
const int nbLevels_,
const int minNodes);
//
template
void DissectionSolver::
SymbolicFact_(const int dim_,
const int nz_,
const bool flagint64,
const int *ptRows,
const int *indCols,
const long long int *ptRows64,
const long long int *indCols64,
const bool isSym,
const bool isUpper,
const bool isWhole,
const int decomposer_,
const int nbLevels_,
const int minNodes)
{
// decomposer = 0: SCOTCH, 1 : METIS, 2 : TRIDIAG(Cuthill-McKee)
int dim = dim_;
clock_t t0_cpu, t1_cpu, t2_cpu;
elapsed_t t0_elapsed, t1_elapsed, t2_elapsed;
_dim = dim_;
const int nz = nz_;
int nbLevels, decomposer;
bool flag, berr;
int *map_eqn, *remap_eqn;
int num_threads;
t0_cpu = clock();
get_realtime(&t0_elapsed);
if (flagint64) {
_ptDA = new SparseMatrix(dim, nz, ptRows64, indCols64,
isSym, isUpper, isWhole);
}
else {
_ptDA = new SparseMatrix(dim, nz, ptRows, indCols,
isSym, isUpper, isWhole);
}
diss_printf(_verbose, _fp,
"%s %d : isSym = %s isUpper = %s isWhole = %s decomposer = %d\n",
__FILE__, __LINE__,
isSym ? "ture" : "false",
isUpper ? "ture" : "false",
isWhole ? "ture" : "false",
decomposer_);
switch(nbLevels_) {
case 1 :
diss_printf(_verbose, _fp,
"%s %d : Dissection :: not activated, switch to tridiag\n",
__FILE__, __LINE__);
decomposer = TRIDIAG_DECOMPOSER;
break;
case (-1):
nbLevels = (int)log2((double)dim_ / (double)minNodes);
if (nbLevels < 2) {
decomposer = TRIDIAG_DECOMPOSER;
}
else {
decomposer = decomposer_;
}
break;
default:
nbLevels = nbLevels_;
decomposer = decomposer_;
break;
}
map_eqn = new int[dim]; // delete [] in DissectionSolver::Destroy(void)
remap_eqn = new int[dim]; // delete [] in DissectionSolver::Destroy(void)
for (int i = 0; i < dim; i++) {
map_eqn[i] = i;
remap_eqn[i] = i;
}
CSR_indirect *unsym_csr = new CSR_indirect;
bool unsym_csr_alloc = false;
// int *ptUnsymRows, *indUnsymCols, *indVals, *indSymVals;
if (isSym) {
if (isWhole) {
unsym_csr->n = dim;
unsym_csr->nnz = nz;
unsym_csr->ptRows = _ptDA->getRows(); // (int *)ptRows;
unsym_csr->indCols = _ptDA->getIndCols(); //(int *)indCols;
unsym_csr->indVals = new int[nz];
unsym_csr->indVals_unsym = new int[nz]; // for safe use of delete []
}
else {
const int nnz0 = nz * 2 - dim;
unsym_csr->n = dim;
unsym_csr->nnz = nnz0;
unsym_csr->ptRows = new int[dim + 1];
unsym_csr->indCols = new int[nnz0];
unsym_csr_alloc = true;
unsym_csr->indVals = new int[nnz0];
unsym_csr->indVals_unsym = (int *)NULL; // for safe use of delete []
// only used as extend symmetric symbolic structure to unsymmetric
int nnz1;
nnz1 = CSR_sym2unsym(unsym_csr,
_ptDA->getRows(), _ptDA->getIndCols(),
map_eqn, remap_eqn, //
dim, isUpper);
if (nnz1 != nnz0) {
fprintf(stderr,
"%s %d : symmetric matrix has no diagonal entry %d != %d\n",
__FILE__, __LINE__, nnz0, nnz1);
exit(-1);
}
}
}
else {
unsym_csr->n = dim;
unsym_csr->nnz = nz;
unsym_csr->indVals = new int[nz];
unsym_csr->indVals_unsym = new int[nz];
unsym_csr->ptRows = _ptDA->getRows(); //(int *)ptRows;
unsym_csr->indCols = _ptDA->getIndCols(); //(int *)indCols;
}
int* graph_mask = new int[dim];
{
_graph_colors = getColorMaskCSR(graph_mask, unsym_csr, _verbose, _fp);
int count = 0;
for (int i = 0; i < dim; i++) {
if (graph_mask[i] == 0) {
count++;
}
}
_index_isolated.resize(count);
count = 0;
for (int i = 0; i < dim; i++) {
if (graph_mask[i] == 0) {
_index_isolated[count] = i;
count++;
}
}
}
_btree = new Dissection::Tree*[_graph_colors];
_with_btree = true;
_dissectionMatrix = new vector* >[_graph_colors];
_dissectionQueue = new DissectionQueue*[_graph_colors];
_tridiagMatrix = new TridiagBlockMatrix*[_graph_colors];
_tridiagQueue = new TridiagQueue*[_graph_colors];
// loop over each connected graph
for (int m = 0; m < _graph_colors; m++) {
bool flag_dissection = false;
int i0, i1, i2;
const int color = m + 1;
int count = 0;
int dim1, dim2;
dim1 = 0;
dim2 = 0;
for (int i = 0; i < dim; i++) {
if (graph_mask[i] == color) {
dim1++;
}
else if (graph_mask[i] == (-color)) {
dim2++;
}
}
count = dim1 + dim2;
i0 = 0;
i1 = dim1;
i2 = count;
for (int i = 0; i < dim; i++) {
if (graph_mask[i] == color) {
remap_eqn[i0++] = i; // remap : new(mapped) to old(original) index
}
else if (graph_mask[i] == (-color)) {
remap_eqn[i1++] = i; // remap : new(mapped) to old(original) index
}
else {
remap_eqn[i2++] = i;
}
}
for (int i = 0; i < dim; i++) {
map_eqn[remap_eqn[i]] = i; // map : old(original) to new (mapped) index
}
if (isSym) {
if (isWhole) {
if (m == 0) { //reconnect to newly allocated array
unsym_csr->ptRows = new int[dim + 1];
unsym_csr->indCols = new int[nz];
unsym_csr_alloc = true;
}
CSR_unsym2unsym(unsym_csr,
_ptDA->getRows(), _ptDA->getIndCols(),
map_eqn, remap_eqn, //
dim, _verbose, _fp);
}
else {
CSR_sym2unsym(unsym_csr,
_ptDA->getRows(), _ptDA->getIndCols(),
map_eqn, remap_eqn, //
dim, isUpper, _verbose, _fp);
}
}
else {
if (m == 0) { //reconnect to newly allocated array
unsym_csr->ptRows = new int[dim + 1];
unsym_csr->indCols = new int[nz];
unsym_csr_alloc = true;
}
CSR_unsym2unsym(unsym_csr,
_ptDA->getRows(), _ptDA->getIndCols(),
map_eqn, remap_eqn, //
dim, _verbose, _fp);
}
flag = false;
if (_graph_colors > 1) {
const int level_tmp = (int)log2((double)count / (double)minNodes);
nbLevels = nbLevels < level_tmp ? nbLevels : level_tmp;
nbLevels = nbLevels < 2 ? 2 : nbLevels; //
}
diss_printf(_verbose, _fp,
"%s %d : count = %d decomposer = %d\n", __FILE__, __LINE__,
count, decomposer);
if ((count > SIZE_TRIDIAG) && ((decomposer == SCOTCH_DECOMPOSER) ||
(decomposer == METIS_DECOMPOSER))) {
flag_dissection = true;
diss_printf(_verbose, _fp,
"%s %d : %s applied to color %d neq = %d with %d levels\n",
__FILE__, __LINE__,
(decomposer == METIS_DECOMPOSER) ? "METIS" : "SCOTCH",
color, count,
nbLevels);
while (flag == false) {
_btree[m] =
new Dissection::Tree(_fp,
berr,
(unsigned)count,
unsym_csr,
isSym,
remap_eqn,
nbLevels,
minNodes,
#ifdef NO_METIS
NULL,
#else
((decomposer == METIS_DECOMPOSER) ? MetisSplitter : NULL),
#endif
true, _verbose);
diss_printf(_verbose, _fp,
"%s %d : Dissection::Tree %s decomposition\n",
__FILE__, __LINE__,
(decomposer == METIS_DECOMPOSER) ? "METIS" : "SCOTCH");
flag = true;
for (int d = 1; d <= _btree[m]->NumberOfSubdomains(); d++) {
// if (_btree[m]->sizeOfDomain(d) == 0) {
const int isd = _btree[m]->sizeOfDomain(d);
if (isd <= DIM_AUG_KERN) {
if (d > 1) {
diss_printf(_verbose, _fp,
"%s %d : Dissection:: %d-th too small nrow = %d\n",
__FILE__, __LINE__, d, isd);
}
else {
fprintf(stderr,
"%s %d : too small first bisector %d not yet accepted\n",
__FILE__, __LINE__, isd);
exit(-1);
}
} // if (isd == 0)
} // loop : d
if (flag) {
break;
}
else {
if (berr == true) {
delete _btree[m]; // for retry of graph partitioning
}
}
} // while
// _nbLevels = _btree[m]->NumberOfLevels();
diss_printf(_verbose, _fp,
"%s %d : Tree strategy = 0 : nblevels = %d -> %d\n",
__FILE__, __LINE__,
nbLevels, _btree[m]->NumberOfLevels());
if ((_btree[m]->NumberOfLevels() == 1)
|| ((nbLevels - _btree[m]->NumberOfLevels()) > 3) || berr == false) {
#ifdef NO_METIS
flag_dissection = false;
#else
flag_dissection = true;
diss_printf(_verbose, _fp,
"%s %d :: retry partitioning by METIS : %d\n",
__FILE__, __LINE__, nbLevels);
nbLevels = nbLevels > 4 ? (nbLevels - 3) : 2;
flag = false;
while (flag == false) {
_btree[m] =
new Dissection::Tree(_fp, berr, (unsigned)count,
unsym_csr,
isSym,
remap_eqn,
nbLevels, minNodes,
MetisSplitter,
true, _verbose);
diss_printf(_verbose, _fp,
"%s %d :: Tree METIS decomposition **\n",
__FILE__, __LINE__ );
flag = true;
for (int d = 1; d <= _btree[m]->NumberOfSubdomains(); d++) {
if (_btree[m]->sizeOfDomain(d) == 0) {
flag = false;
}
}
if (flag) {
break;
}
else {
delete _btree[m];
nbLevels--;
}
} // while (flag == flase)
#endif
} // if
} // if ((count > SIZE_TRIDIAG) && ((decomposer == SCOTCH_DECOMPOSER)...
if (flag_dissection) {
bool verify_root = (_btree[m]->sizeOfDomain(1) <= SIZE_B1);
int nb_doms = _btree[m]->NumberOfSubdomains();
_dissectionMatrix[m].resize(nb_doms);
diss_printf(_verbose, _fp,
"%s %d : ", __FILE__, __LINE__);
diss_printf(_verbose, _fp,
"num_threads = %d dim = %d dim_diss. = %d nbLevels = %d\n",
_num_threads, _dim, count, _btree[m]->NumberOfLevels());
for (int d = 1; d <= _btree[m]->NumberOfSubdomains(); d++) {
diss_printf(_verbose, _fp,
"( %d % d) ", d, _btree[m]->sizeOfDomain(d));
}
diss_printf(_verbose, _fp, "\n");
const int level_last = _btree[m]->NumberOfLevels();
const int nb_doms_dense0 = (1U << (level_last - 1));
if (_num_threads > nb_doms_dense0 || verify_root) {
diss_printf(_verbose, _fp,
"%s %d : ", __FILE__, __LINE__);
diss_printf(_verbose, _fp,
"thread number reduced : %d -> %d\n",
_num_threads, 1);
diss_printf(_verbose, _fp,
"which is used as number of threads for computation\n");
// num_threads = nb_doms_dense0;
num_threads = 1;
}
else {
num_threads = _num_threads;
}
_dissectionQueue[m] = new DissectionQueue(_btree[m],
_dissectionMatrix[m],
num_threads,
isSym,
_verbose,
_fp);
// generation of queue for numerical factorization is treated as a part of
// symbolic factorization : _queue_symb, {_queue_static, _queue_dynamic}
_dissectionQueue[m]->generate_queue(_dissectionMatrix[m],
_ptDA->nnz(), _ptDA->getCoef());
_dissectionQueue[m]->generate_queue_fwbw(_dissectionMatrix[m],
_ptDA->dimension(),
_ptDA->nnz(), _ptDA->getCoef());
_tridiagQueue[m] = new TridiagQueue(false, _verbose, _fp);
} // if (flag_dissection)
else {
diss_printf(_verbose, _fp,
"%s %d : TridiagSolver : m = %d count = %d\n",
__FILE__, __LINE__, m, count);
const int nnz = unsym_csr->ptRows[count];
bool isMapped = ((_graph_colors > 1) || (_index_isolated.size() > 0));
_tridiagMatrix[m] = new TridiagBlockMatrix(count, SIZE_B1, isSym,
0, // no other Tridiag
_verbose, _fp);
_tridiagQueue[m] = new TridiagQueue(true, _verbose, _fp);
_tridiagQueue[m]->generate_queue(_tridiagMatrix[m], count, nnz,
isMapped, remap_eqn,
unsym_csr->ptRows,
unsym_csr->indCols,
unsym_csr->indVals,
_ptDA->getCoef());
}
} // loop : m
if (unsym_csr_alloc) {
delete [] unsym_csr->ptRows;
delete [] unsym_csr->indCols;
}
delete [] unsym_csr->indVals;
delete [] unsym_csr->indVals_unsym; // isSym == (int *)NULL
delete unsym_csr;
delete [] map_eqn;
delete [] remap_eqn;
delete [] graph_mask; // added 01 Oct.2013 Atsushi
t1_cpu = clock();
get_realtime(&t1_elapsed);
for (int m = 0; m < _graph_colors; m++) {
if (_tridiagQueue[m]->tridiagSolver()) {
_tridiagQueue[m]->exec_symb_fact();
}
else {
_dissectionQueue[m]->exec_symb_fact();
}
}
_precDiag = new U[_dim]; // preparation for numerical fact.
_Schur = new SchurMatrix[_graph_colors];
_kernel = new KernelMatrix[_graph_colors];
_singIdx = new vector[_graph_colors];
_status_factorized = false;
t2_cpu = clock();
get_realtime(&t2_elapsed);
diss_printf(_verbose, _fp,
"graph paritioner. : cpu time = %.4e elapsed time = %.4e\n",
(double)(t1_cpu - t0_cpu) / (double)CLOCKS_PER_SEC,
convert_time(t1_elapsed, t0_elapsed));
diss_printf(_verbose, _fp,
"queue symb. fact. : cpu time = %.4e elapsed time = %.4e\n",
(double)(t2_cpu - t1_cpu) / (double)CLOCKS_PER_SEC,
convert_time(t2_elapsed, t1_elapsed));
}
template
void DissectionSolver::
NumericFact(const int called,
T *coefs,
const int scaling,
const double eps_pivot,
const bool kernel_detection_all,
const int dim_augkern,
const double machine_eps_,
const bool assume_invertible,
const bool higher_precision)
{
clock_t t0_cpu, t1_cpu, t2_cpu, t3_cpu;
elapsed_t t0_elapsed, t1_elapsed, t2_elapsed, t3_elapsed;
const U eps_machine = machine_eps_ < 0.0 ? machine_epsilon() : U(machine_eps_);
t0_cpu = clock();
_assume_invertible = assume_invertible;
_dim_augkern = dim_augkern;
get_realtime(&t0_elapsed);
diss_printf(_verbose, _fp, "%s %d : eps_machine = %s scaling = %d\n",
__FILE__, __LINE__,
tostring(eps_machine).c_str(), scaling);
_scaling = scaling;
// _ptDA->normalize(_scaling, coefs, _precDiag);
normalize(_scaling, coefs, _ptDA, _precDiag);
t1_cpu = clock();
get_realtime(&t1_elapsed);
for (int m = 0; m < _graph_colors; m++) {
if (_tridiagQueue[m]->tridiagSolver()) {
_tridiagQueue[m]->exec_num_fact(called,
eps_pivot,
true, // matrix will not be decomposed
dim_augkern,
eps_machine,
higher_precision);
}
else {
_dissectionQueue[m]->exec_num_fact(called,
eps_pivot,
kernel_detection_all,
dim_augkern,
eps_machine,
higher_precision);
}
}
t2_cpu = clock();
get_realtime(&t2_elapsed);
// dense {0 [1 2] [3 4 5 6] .... } + sparse {[2^(_nbLevels-1)-1... ]}
int sz_total = 0;
VectorArray work(_dim);
for (int m = 0; m < _graph_colors; m++) {
if (_tridiagQueue[m]->tridiagSolver()) {
const int nrow = _tridiagMatrix[m]->nrow();
const int nn0 = nrow - _tridiagMatrix[m]->rank();
if (nn0 > 0) {
_tridiagMatrix[m]->SingularNode(_kernel[m].getKernListEq());
// _ptDA_kern_list_eq[m]);
_kernel[m].set_dimension(nn0);
_kernel[m].getKernProj().init(nn0);
//_ptDA_kern_proj[m].init(nn0);
_tridiagMatrix[m]->KernelBasis(false, _kernel[m].getKernBasis());
//_ptDA_kern_basis[m]); // isTrans
if (_tridiagQueue[m]->isMapped()) {
int *remap_eqn = _tridiagQueue[m]->remap_eqn();
ColumnMatrix kernel_tmp;
kernel_tmp.init(_dim, nn0);
for (int n = 0; n < nn0; n++) {
for (int i = 0; i < _dim; i++) {
kernel_tmp(i, n) = _zero;
}
for (int i = 0; i < nrow; i++) {
kernel_tmp(remap_eqn[i], n) = _kernel[m].getKernBasis()(i, n);
}
}
_kernel[m].getKernBasis().free();
_kernel[m].getKernBasis().init(_dim, nn0);
for (int n = 0; n < nn0; n++) {
for (int i = 0; i < _dim; i++) {
_kernel[m].getKernBasis()(i, n) = kernel_tmp(i, n);
}
}
kernel_tmp.free();
} // if (_tridiagQueue[m]->isMapped())
T *kern_basis = _kernel[m].getKernBasis().addrCoefs();
//_ptDA_kern_basis[m].addrCoefs();
diss_printf(_verbose, _fp,
"%s %d : residual of kernel vertors : %d\n",
__FILE__, __LINE__, nn0);
for (int i = 0; i < nn0; i++) {
_ptDA->prod((kern_basis + (i * _dim)), work.addrCoefs());
U stmp = blas_l2norm(_dim, work.addrCoefs(), 1);
diss_printf(_verbose, _fp, "%d %s\n", i, tostring(stmp).c_str());
}
if(_scaling) {
for (int j = 0; j < nn0; j++) {
const int jtmp = j * _dim;
for (int i = 0; i < _dim; i++) {
kern_basis[i + jtmp] *= _precDiag[i];
}
}
}
_kernel[m].getTKernBasis().init(_dim, nn0);
// normalize each kernel_basis
for (int j = 0; j < nn0; j++) {
U stmp(0.0);
U one(1.0);
stmp = blas_l2norm(_dim, kern_basis + (j * _dim), 1);
stmp = one / stmp;
blas_scal2(_dim, stmp, (kern_basis + (j * _dim)), 1);
}
for (int i = 0; i < nn0; i++) {
for(int j = i; j < nn0; j++) {
// _ptDA_kern_proj[m](i, j) =
_kernel[m].getKernProj()(i,j) =
blas_dot(_dim,
(kern_basis + (i * _dim)), 1,
(kern_basis + (j * _dim)), 1);
}
// symmetrize
for (int j = (i + 1); j < nn0; j++) {
_kernel[m].getKernProj()(j,i) = _kernel[m].getKernProj()(i,j);
// _ptDA_kern_proj[m](j, i) = _ptDA_kern_proj[m](i, j);
}
}
full_ldlh(nn0, _kernel[m].getKernProj().addrCoefs(), nn0);
if (!_tridiagMatrix[m]->isSym()) {
T *kernt_basis = _kernel[m].getTKernBasis().addrCoefs();
_tridiagMatrix[m]->KernelBasis(true, _kernel[m].getTKernBasis());
if (_tridiagQueue[m]->isMapped()) {
int *remap_eqn = _tridiagQueue[m]->remap_eqn();
ColumnMatrix kernel_tmp;
kernel_tmp.init(_dim, nn0);
for (int n = 0; n < nn0; n++) {
for (int i = 0; i < _dim; i++) {
kernel_tmp(i, n) = _zero;
}
for (int i = 0; i < nrow; i++) {
kernel_tmp(remap_eqn[i], n) = _kernel[m].getTKernBasis()(i, n);
}
}
_kernel[m].getTKernBasis().free();
_kernel[m].getTKernBasis().init(_dim, nn0);
for (int n = 0; n < nn0; n++) {
for (int i = 0; i < _dim; i++) {
_kernel[m].getTKernBasis()(i, n) = kernel_tmp(i, n);
}
}
kernel_tmp.free();
} // if (_tridiagQueue[m]->isMapped())
diss_printf(_verbose, _fp,
"%s %d : residual of transposed kernel vertors : %d\n",
__FILE__, __LINE__, nn0);
for (int i = 0; i < nn0; i++) {
_ptDA->prodt((kernt_basis + (i * _dim)), work.addrCoefs());
U stmp = blas_l2norm(_dim, work.addrCoefs(), 1);
diss_printf(_verbose, _fp, "%d %s\n", i, tostring(stmp).c_str());
}
if(_scaling) {
for (int j = 0; j < nn0; j++) {
const int jtmp = j * _dim;
for (int i = 0; i < _dim; i++) {
kernt_basis[i + jtmp] *= _precDiag[i];
}
}
}
} // if (!_tridiagMatrix[m]->isSym())
} // if (nn0 > 0)
else {
_kernel[m].set_dimension(0);
_kernel[m].getKernProj().init(0);
// _ptDA_kern_proj[m].init(0); // = new SquareMatrix();
_kernel[m].getKernBasis().init(0, 0); // = new ColumnMatrix();
_kernel[m].getKernListEq().clear();
_kernel[m].getKernListEqLeft().clear();
}
_singIdx[m].resize(0);
_Schur[m].getAcol() = new SparseMatrix(); // dummy allocation
_Schur[m].getArow() = new SparseMatrix(); // dummy allocation
// if (_status_factorized) { // for safty of second call
_Schur[m].getSlduList().clear(); //
_Schur[m].getSlduListLeft().clear(); //
_Schur[m].getSldu().free();
} // if (_tridiagQueue[m]->tridiagSolver())
else {
int sz = 0, sz1 = 0;
bool kernel_detected = true;
vector kernel_found;
// int id_dom = 1;
for (int j = 0; j < _dissectionMatrix[m].size(); j++ ) {
vector& singIdx = _dissectionMatrix[m][j]->singIdxPermute();
if (singIdx.size() > 0) {
diss_printf(_verbose, _fp,
"%s %d : ", __FILE__, __LINE__);
diss_printf(_verbose, _fp,
"%d : %s : %d :: %d [ ", (j + 1), // selfIndex() == j
_dissectionMatrix[m][j]->KernelDetected() ? "true" : "false",
_dissectionMatrix[m][j]->nrow(),
(int)singIdx.size());
vector::const_iterator it = singIdx.begin();
for ( ;it != singIdx.end(); ++it) {
diss_printf(_verbose, _fp, "%d ", *it);
}
diss_printf(_verbose, _fp, "]\n");
} // if (singIdx.size() > 0)
if (singIdx.size() > 0) {
// id_dom = j + 1;
kernel_found.push_back(j);
sz += singIdx.size();
if (!_dissectionMatrix[m][j]->KernelDetected()) {
//if (true) { // 29 Oct.2014 debug
sz1 += singIdx.size();
diss_printf(_verbose, _fp,
"%s %d : ", __FILE__, __LINE__);
diss_printf(_verbose, _fp, "domain %d kernel is not detected\n", j);
kernel_detected = false;
}
}
} // loop : j
sz_total += sz;
if (sz > 0) {
if (kernel_detected && (!_assume_invertible)) {
_singIdx[m].resize(sz);
int k = 0;
for (int j = 0; j < _dissectionMatrix[m].size(); j++ ) {
const int *loc2glob = _btree[m]->getDiagLoc2Glob(j + 1);
for (vector::const_iterator it =
_dissectionMatrix[m][j]->singIdxPermute().begin();
it != _dissectionMatrix[m][j]->singIdxPermute().end();
++it, k++) {
_singIdx[m][k] = loc2glob[*it];
} // loop : d
}
#if 0
if (_verbose) {
std::sort(_singIdx[m].begin(), _singIdx[m].end());
char fname[256];
FILE *fp;
int pid = get_process_id();
sprintf(fname, "singularindex.%06d.data", pid);
if ((fp = fopen(fname, "w")) != NULL) {
fprintf(fp, "# %d \n", (int)_singIdx[m].size());
for (int i = 0; i < _singIdx[m].size(); i++) {
fprintf(fp, "%d\n", _singIdx[m][i]);
}
fclose(fp);
}
else {
fprintf(stderr,
"%s %d : fail to open %s\n",
__FILE__, __LINE__, fname);
exit(-1);
}
} // if (_verbose)
BuildKernels(_singIdx[m],
sz,
_Schur[m], _kernel[m]);
#else
vector* > diags; // dummy
vector >augkern_indexes; // dummy
BuildKernelsDetection(sz,
_singIdx[m],
augkern_indexes,
diags,
eps_pivot,
dim_augkern,
_Schur[m],
_kernel[m], false);
#endif
// need to copy kern basis projection matrix from global to m-th array
} // if (kernel_detected)
else {
diss_printf(_verbose, _fp,
"%s %d : candidates of null pivots = %d, to be fixed\n",
__FILE__, __LINE__, sz);
_singIdx[m].resize(sz + dim_augkern); //= vector
int k = 0;
for (int d = 1; d <= _btree[m]->NumberOfSubdomains(); d++) {
const int j = _btree[m]->selfIndex(d);
if (_dissectionMatrix[m][j]->KernelDetected() &&
(_dissectionMatrix[m][j]->diagBlock().dim_kern() > 0)) {
_dissectionMatrix[m][j]->diagBlock().set_KernelDetected(false);
}
const int *loc2glob = _btree[m]->getDiagLoc2Glob(d);
for (vector::const_iterator it =
_dissectionMatrix[m][j]->singIdxPermute().begin();
it != _dissectionMatrix[m][j]->singIdxPermute().end();
++it, k++) {
_singIdx[m][k] = loc2glob[*it];
}
} // loop : d
// selecting dim_augkern indices for comparison in the kernel detection
diss_printf(_verbose, _fp,
"%s %d : size = %d : ", __FILE__, __LINE__,
(int)_singIdx[m].size());
for (int i = 0; i < _singIdx[m].size(); i++) {
diss_printf(_verbose, _fp, "%d ", _singIdx[m][i]);
}
diss_printf(_verbose, _fp, "\n");
vector* > diags;
vector >augkern_indexes;
int nn = 1; // level of dense part with 1 indexing
k = 0;
while (nn <= dim_augkern) {
SquareBlockMatrix* diag =
&_dissectionMatrix[m][_btree[m]->selfIndex(nn)]->diagBlock();
const int *loc2glob = _btree[m]->getDiagLoc2Glob(nn);
vector &permute = diag->getPermute();
vector augkern_index;
// augkern_index.resize(dim_augkern);
int n = diag->dimension() - 1; //diag.dimension() == permute.size()
int k0 = 0;
while ((k < dim_augkern) && (n >= 0)) {
const int itmp = loc2glob[permute[n]];
bool flag = true;
for (int i = 0; i < sz; i++) {
if(_singIdx[m][i] == itmp) {
flag = false;
break;
}
}
if (flag) {
_singIdx[m][sz + k0] = itmp;
augkern_index.push_back(n);
k0++;
k++;
}
n--;
} // while
diags.push_back(diag);
augkern_indexes.push_back(augkern_index);
if (k == dim_augkern) {
break;
}
nn++; //
} // while (nn <= dim_augkern)
diss_printf(_verbose, _fp,
"%s %d : adding local node using %d diag matrices ",
__FILE__, __LINE__, (int)augkern_indexes.size());
for (vector >::const_iterator it = augkern_indexes.begin();
it != augkern_indexes.end(); ++it) {
for (vector::const_iterator jt = (*it).begin();
jt != (*it).end();
++jt) {
diss_printf(_verbose, _fp, "%d ", (*jt));
}
}
diss_printf(_verbose, _fp, "to the last level.\n");
diss_printf(_verbose, _fp,
"%s %d : singIdx = %d : ",
__FILE__, __LINE__, (int)_singIdx[m].size());
for (int i = 0; i < _singIdx[m].size(); i++) {
diss_printf(_verbose, _fp, "%d ", _singIdx[m][i]);
}
diss_printf(_verbose, _fp, "\n");
std::sort(_singIdx[m].begin(), _singIdx[m].end());
diss_printf(_verbose, _fp,
"%s %d : singIdx sorted = %d : ",
__FILE__, __LINE__, (int)_singIdx[m].size());
for (int i = 0; i < _singIdx[m].size(); i++) {
diss_printf(_verbose,_fp, "%d ", _singIdx[m][i]);
}
diss_printf(_verbose, _fp, "\n");
int kern_dim = 0;
BuildKernelsDetection(kern_dim,
_singIdx[m],
augkern_indexes,
diags,
eps_pivot,
dim_augkern,
_Schur[m],
_kernel[m],
true);
if (kern_dim == (-1)) {
int pid = get_process_id();
SaveCSRMatrix(_called, coefs);
fprintf(stderr,
"%s %d : DissectionSolver CSR matrix dumped : %d %d\n",
__FILE__, __LINE__, _called, pid);
exit(-1);
}
} // if (kernel_detected)
} // if (sz > 0)
else {
_singIdx[m].resize(0);
_Schur[m].getAcol() = new SparseMatrix(); // dummy allocation
_Schur[m].getArow() = new SparseMatrix(); // dummy allocation
// _ptDA_arow[m] = new SparseMatrix(true);
_kernel[m].set_dimension(0);
_kernel[m].getKernProj().init(0); // _ptDA_kern_proj[m].init(0); //
_kernel[m].getKernBasis().init(0, 0); //
_kernel[m].getKernListEq().clear();
_kernel[m].getKernListEqLeft().clear();
// if (_status_factorized) { // for safty of second call
_Schur[m].getSlduList().clear(); // _ptDA_sldu_list[m].clear();
_Schur[m].getSlduListLeft().clear(); // _ptDA_sldu_list[m].clear();
_Schur[m].getSldu().free(); // _ptDA_sldu[m].free();
}
} // if (_tridiagQueue[m]->tridiagSolver())
} // loop : _graph_colors
work.free();
{
int itmp = 0;
for (int m = 0; m < _graph_colors; m++) {
itmp += _singIdx[m].size();
}
_nsing = itmp; // keep total dimension of kernel of singluar blocks
}
t3_cpu = clock();
get_realtime(&t3_elapsed);
//clock_gettime(CLOCK_REALTIME, &ts3);
diss_printf(_verbose, _fp,
"queue num. fact. : cpu time = %.4e elapsed time = %.4e\n",
(double)(t2_cpu - t1_cpu) / (double)CLOCKS_PER_SEC,
convert_time(t2_elapsed, t1_elapsed));
diss_printf(_verbose, _fp,
"total num. fact. : cpu time = %.4e elapsed time = %.4e\n",
(double)(t3_cpu - t0_cpu) / (double)CLOCKS_PER_SEC,
convert_time(t2_elapsed, t0_elapsed));
diss_printf(_verbose, _fp,
"scaling matrix : cpu time = %.4e elapsed time = %.4e\n",
(double)(t1_cpu - t0_cpu) / (double)CLOCKS_PER_SEC,
convert_time(t1_elapsed, t0_elapsed));
if (sz_total > 0) {
diss_printf(_verbose, _fp,
"kernel vec. gen : cpu time = %.4e elapsed time = %.4e\n",
(double)(t3_cpu - t2_cpu) / (double)CLOCKS_PER_SEC,
convert_time(t3_elapsed, t2_elapsed));
} // (sz_total > 0) {
// check error
// this should be a function with specialized template
if (_verbose) {
int count = 0;
for (int m = 0; m < _graph_colors; m++) {
diss_printf(_verbose, _fp,
"%s %d :negative diagnol entries : color = %d\n",
__FILE__, __LINE__, m);
if (_tridiagQueue[m]->tridiagSolver()) {
int count0;
count0 = _tridiagMatrix[m]->NumNegativeDiags();
if (count0 > 0) {
diss_printf(_verbose, _fp,
"%d / %d\n", count0, _tridiagMatrix[m]->nrow());
}
count += count0;
}
else {
int level;
level = (_btree[m]->NumberOfLevels() - 1);
const unsigned begdom = 1U << level;
const unsigned enddom = begdom * 2;
for (int d = begdom; d < enddom; d++) {
const int d0 = _btree[m]->selfIndex(d);
// const void *diagsparse = _dissectionMatrix[m][d0]->diagSparse();
TridiagBlockMatrix **tridiag = _dissectionMatrix[m][d0]->addrtridiagBlock();
int colors = _dissectionMatrix[m][d0]->ColorTridiagBlockMatrix();
int count0 = 0;
for (int i = 0; i < colors; i++) {
count0 += tridiag[i]->NumNegativeDiags();
}
if (count0 > 0) {
diss_printf(_verbose, _fp, "%d : %d / %d\n",
d0, count0, _dissectionMatrix[m][d0]->nrow());
}
count += count0;
}
for (int level = (_btree[m]->NumberOfLevels() - 2); level >= 0; level--) {
const unsigned begdom = 1U << level;
const unsigned enddom = begdom * 2;
for (int d = begdom; d < enddom; d++) {
const int d0 = _btree[m]->selfIndex(d);
int count0;
SquareBlockMatrix& diag = _dissectionMatrix[m][d0]->diagBlock();
count0 = count_diag_negative(diag);
if (count0 > 0) {
diss_printf(_verbose, _fp, "%d : %d / %d\n",
d0, count0, _dissectionMatrix[m][d0]->nrow());
}
count += count0;
}
}
} // (_tridiagQueue[m]->tridiagSolver()) {
{
int count0;
SubSquareMatrix& diag = _Schur[m].getSldu();
count0 = count_diag_negative(diag);
if (count0 > 0) {
diss_printf(_verbose, _fp,
"-1 : %d / %d\n", count0, diag.dimension());
}
count += count0;
}
} // loop : m
if (count > 0) {
diss_printf(_verbose, _fp,
"%s %d :negative diagonal entries = %d / %d\n",
__FILE__, __LINE__, count, _dim);
}
} // if (_verbose)
//
bool kernel_flag = false;
for (int m = 0; m < _graph_colors; m++) {
if (_kernel[m].dimension() != 0) {
kernel_flag = true;
break;
}
}
#if 0
if (kernel_flag && (!_ptDA->isSymmetric())) {
ComputeTransposedKernels();
}
#endif
if(_verbose) {
int n1 = 0;
for (int m = 0; m < _graph_colors; m++) {
n1 += _Schur[m].getSlduList().size();
}
VectorArray xx(_dim);
VectorArray yy(_dim);
VectorArray ww(_dim);
for (int i = 0; i < _dim; i++) {
ww[i] = T(2.0 * ((double)rand() / (double)RAND_MAX) - 1.0);
// ww[i] = T((double)(i % 11));
}
VectorArray bb(_dim);
bb.ZeroClear();
diss_printf(_verbose, _fp, "%s %d : compute error in (Ker A)^\\perp\n",
__FILE__, __LINE__);
SpMV(ww.addrCoefs(), xx.addrCoefs(), true); // isScaling = true
if (kernel_flag) {
ProjectionKernelOrthSingle(xx.addrCoefs(), "creating solution", false);
}
SpMV(xx.addrCoefs(), bb.addrCoefs(), true); // isScaling = true
if (kernel_flag) {
ProjectionKernelOrthSingle(bb.addrCoefs(), "RHS", true);
// ProjectionImageSingle(bb.addrCoefs(), "RHS", false);
}
blas_copy(_dim, bb.addrCoefs(), 1, yy.addrCoefs(), 1);
SolveSingle(yy.addrCoefs(), false, false, true); // isScaling = true
if (kernel_flag) {
ProjectionKernelOrthSingle(yy.addrCoefs(), "computed solution", false);
}
SpMV(yy.addrCoefs(), ww.addrCoefs(), true);
U norm0, norm1;
norm0 = blas_l2norm(_dim, xx.addrCoefs(), 1);
blas_axpy(_dim, _none, xx.addrCoefs(), 1, yy.addrCoefs(), 1);
norm1 = blas_l2norm(_dim, yy.addrCoefs(), 1);
diss_printf(_verbose, _fp, "%s %d : error = %s / %s = %s\n",
__FILE__, __LINE__,
tostring(norm1).c_str(), tostring(norm0).c_str(),
tostring(norm1 / norm0).c_str());
if (todouble(norm1 / norm0) > 1.0e-5) {
int pid = get_process_id();
int nnz = _ptDA->nnz();
diss_printf(_verbose, stderr, "%s %d : too large error = %s / %s = %s\n",
__FILE__, __LINE__,
tostring(norm1).c_str(), tostring(norm0).c_str(),
tostring(norm1 / norm0).c_str());
// SaveMMMatrix(_called, coefs);
// _status_factorized = false;
// return;
}
norm0 = blas_l2norm(_dim, bb.addrCoefs(), 1);
blas_axpy(_dim, _none, bb.addrCoefs(), 1, ww.addrCoefs(), 1);
norm1 = blas_l2norm(_dim, ww.addrCoefs(), 1);
diss_printf(_verbose, _fp, "%s %d : residual = %s / %s = %s\n",
__FILE__, __LINE__,
tostring(norm1).c_str(), tostring(norm0).c_str(),
tostring(norm1 / norm0).c_str());
} // if (_verbose)
_status_factorized = true;
}
template
void DissectionSolver::NumericFact(const int called,
double *coefs,
const int scaling,
const double eps_pivot,
const bool kernel_detection_all,
const int dim_augkern,
const double machine_eps_,
const bool assume_invertible,
const bool higher_precision);
template
void DissectionSolver, double>::
NumericFact(const int called,
complex *coefs,
const int scaling,
const double eps_pivot,
const bool kernel_detection_all,
const int dim_augkern,
const double machine_eps_,
const bool assume_invertible,
const bool higher_precision);
template
void DissectionSolver::NumericFact(const int called,
quadruple *coefs,
const int scaling,
const double eps_pivot,
const bool kernel_detection_all,
const int dim_augkern,
const double machine_eps_,
const bool assume_invertible,
const bool higher_precision);
template
void DissectionSolver, quadruple>::
NumericFact(const int called,
complex *coefs,
const int scaling,
const double eps_pivot,
const bool kernel_detection_all,
const int dim_augkern,
const double machine_eps_,
const bool assume_invertible,
const bool higher_precision);
template
void DissectionSolver::
NumericFact(const int called,
double *coefs,
const int scaling,
const double eps_pivot,
const bool kernel_detection_all,
const int dim_augkern,
const double machine_eps_,
const bool assume_invertible,
const bool higher_precision);
template
void DissectionSolver, double, complex, quadruple>::
NumericFact(const int called,
complex *coefs,
const int scaling,
const double eps_pivot,
const bool kernel_detection_all,
const int dim_augkern,
const double machine_eps_,
const bool assume_invertible,
const bool higher_precision);
template
void DissectionSolver::
NumericFact(const int called,
quadruple *coefs,
const int scaling,
const double eps_pivot,
const bool kernel_detection_all,
const int dim_augkern,
const double machine_eps_,
const bool assume_invertible,
const bool higher_precision);
template
void DissectionSolver, quadruple, complex, double>::
NumericFact(const int called,
complex *coefs,
const int scaling,
const double eps_pivot,
const bool kernel_detection_all,
const int dim_augkern,
const double machine_eps_,
const bool assume_invertible,
const bool higher_precision);
template
void DissectionSolver::NumericFact(const int called,
float *coefs,
const int scaling,
const double eps_pivot,
const bool kernel_detection_all,
const int dim_augkern,
const double machine_eps_,
const bool assume_invertible,
const bool higher_precision);
template
void DissectionSolver, float>::
NumericFact(const int called,
complex *coefs,
const int scaling,
const double eps_pivot,
const bool kernel_detection_all,
const int dim_augkern,
const double machine_eps_,
const bool assume_invertible,
const bool higher_precision);
//
template
void DissectionSolver::
GetNullPivotIndices(int *pivots)
{
int i0 = 0;
for (int m = 0; m < _graph_colors; m++) {
const int n0 = _kernel[m].dimension(); //_ptDA_kern_proj[m].dimension();
// should be optimized : replaced by memcopy()?
for (int i = 0; i < n0; i++, i0++) {
pivots[i0] = _kernel[m].getKernListEq()[i]; //_ptDA_kern_list_eq[m][i];
}
} // loop : _graph_colors : m
}
template
void DissectionSolver::GetNullPivotIndices(int *pivots);
template
void DissectionSolver::GetNullPivotIndices(int *pivots);
template
void DissectionSolver, double>::GetNullPivotIndices(int *pivots);
template
void DissectionSolver, quadruple>::GetNullPivotIndices(int *pivots);
template
void DissectionSolver::GetNullPivotIndices(int *pivots);
template
void DissectionSolver::GetNullPivotIndices(int *pivots);
template
void DissectionSolver::GetNullPivotIndices(int *pivots);
template
void DissectionSolver, float>::GetNullPivotIndices(int *pivots);
template
void DissectionSolver::GetNullPivotIndices(int *pivots);
//
template
int DissectionSolver::
GetMaxColors()
{
int nn = 0;
for (int m = 0; m < _graph_colors; m++) {
if (!_tridiagQueue[m]->tridiagSolver()) {
nn++;
}
}
return nn;
}
template
int DissectionSolver::GetMaxColors();
template
int DissectionSolver::GetMaxColors();
template
int DissectionSolver, double>::GetMaxColors();
template
int DissectionSolver, quadruple>::GetMaxColors();
template
int DissectionSolver::GetMaxColors();
template
int DissectionSolver::GetMaxColors();
template
int DissectionSolver::GetMaxColors();
template
int DissectionSolver, float>::GetMaxColors();
//
template
void DissectionSolver::
GetSmallestPivotIndices(const int n, int *pivots)
{
int nn = 0;
for (int m = 0; m < _graph_colors; m++) {
if (!_tridiagQueue[m]->tridiagSolver()) {
SquareBlockMatrix* diag =
&_dissectionMatrix[m][_btree[m]->selfIndex(1)]->diagBlock();
const int *loc2glob = _btree[m]->getDiagLoc2Glob(1);
vector &permute = diag->getPermute();
int ndiag = diag->dimension();
if (n > ndiag) {
diss_printf(_verbose, _fp, "%s %d : GetSmallestPivotIndices %d > %d\n",
__FILE__, __LINE__, n, ndiag);
}
for (int k = (ndiag - 1); k >= (ndiag - n); k--, nn++) {
pivots[nn] = loc2glob[permute[k]];
}
}
} // loop : m
}
template
void DissectionSolver::GetSmallestPivotIndices(const int n,
int *pivots);
template
void DissectionSolver::GetSmallestPivotIndices(const int n, int *pivots);
template
void DissectionSolver, double>::GetSmallestPivotIndices(const int n, int *pivots);
template
void DissectionSolver, quadruple>::GetSmallestPivotIndices(const int n, int *pivots);
template
void DissectionSolver::GetSmallestPivotIndices(const int n, int *pivots);
template
void DissectionSolver::GetSmallestPivotIndices(const int n, int *pivots);
template
void DissectionSolver::GetSmallestPivotIndices(const int n,
int *pivots);
template
void DissectionSolver, float>::GetSmallestPivotIndices(const int n, int *pivots);
//
template
void DissectionSolver::
GetKernelVectors(T *kern_basis)
{
int ii = 0;
for (int m = 0; m < _graph_colors; m++) {
const int n0 = _kernel[m].dimension(); //_ptDA_kern_proj[m].dimension();
// should be optimized : replaced by memcopy()?
for (int j = 0; j < n0; j++) {
for (int i = 0; i < _dim; i++, ii++) {
kern_basis[ii] = _kernel[m].getKernBasis().addrCoefs()[ii];
}
}
} // loop : _graph_colors : m
}
template
void DissectionSolver