| 1 |
/*! \file TridiagBlockMatrix.hpp |
| 2 |
\brief tridiagonal factorization algorithm with Cuthill-McKee |
| 3 |
\author François-Xavier Roux, ONERA, Laboratoire Jacques-Louis Lions |
| 4 |
\author Atsushi Suzuki, Laboratoire Jacques-Louis Lions |
| 5 |
\date Jul. 12th 2015 |
| 6 |
\date Nov. 30th 2016 |
| 7 |
*/ |
| 8 |
|
| 9 |
// This file is part of Dissection |
| 10 |
// |
| 11 |
// Dissection is free software: you can redistribute it and/or modify |
| 12 |
// it under the terms of the GNU General Public License as published by |
| 13 |
// the Free Software Foundation, either version 3 of the License, or |
| 14 |
// (at your option) any later version. |
| 15 |
// |
| 16 |
// Linking Dissection statically or dynamically with other modules is making |
| 17 |
// a combined work based on Disssection. Thus, the terms and conditions of |
| 18 |
// the GNU General Public License cover the whole combination. |
| 19 |
// |
| 20 |
// As a special exception, the copyright holders of Dissection give you |
| 21 |
// permission to combine Dissection program with free software programs or |
| 22 |
// libraries that are released under the GNU LGPL and with independent modules |
| 23 |
// that communicate with Dissection solely through the Dissection-fortran |
| 24 |
// interface. You may copy and distribute such a system following the terms of |
| 25 |
// the GNU GPL for Dissection and the licenses of the other code concerned, |
| 26 |
// provided that you include the source code of that other code when and as |
| 27 |
// the GNU GPL requires distribution of source code and provided that you do |
| 28 |
// not modify the Dissection-fortran interface. |
| 29 |
// |
| 30 |
// Note that people who make modified versions of Dissection are not obligated |
| 31 |
// to grant this special exception for their modified versions; it is their |
| 32 |
// choice whether to do so. The GNU General Public License gives permission to |
| 33 |
// release a modified version without this exception; this exception also makes |
| 34 |
// it possible to release a modified version which carries forward this |
| 35 |
// exception. If you modify the Dissection-fortran interface, this exception |
| 36 |
// does not apply to your modified version of Dissection, and you must remove |
| 37 |
// this exception when you distribute your modified version. |
| 38 |
// |
| 39 |
// This exception is an additional permission under section 7 of the GNU |
| 40 |
// General Public License, version 3 ("GPLv3") |
| 41 |
// |
| 42 |
// Dissection is distributed in the hope that it will be useful, |
| 43 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 44 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 45 |
// GNU General Public License for more details. |
| 46 |
// |
| 47 |
// You should have received a copy of the GNU General Public License |
| 48 |
// along with Dissection. If not, see <http://www.gnu.org/licenses/>. |
| 49 |
// |
| 50 |
|
| 51 |
#ifndef _ALGEBRA_TRIDIAGBLOCKMATRIX_HPP_ |
| 52 |
#define _ALGEBRA_TRIDIAGBLOCKMATRIX_HPP_ |
| 53 |
|
| 54 |
#include <vector> |
| 55 |
#include <list> |
| 56 |
#include "Compiler/blas.hpp" |
| 57 |
#include "Compiler/elapsed_time.hpp" |
| 58 |
#include "Algebra/ColumnMatrix.hpp" |
| 59 |
#include "Algebra/SquareBlockMatrix.hpp" |
| 60 |
#include "Algebra/CSR_matrix.hpp" |
| 61 |
|
| 62 |
// setting for SX-ACE to reduce small tridiagonal blocks during DTRSM |
| 63 |
// #define SPARSE_OFFDIAG // CSR data is used in offdiag blocks of tridiag |
| 64 |
// #define STORE_WHOLE_FACTORIZED |
| 65 |
// setting for superscalar CPUs |
| 66 |
#define PERMUTE_UPPER |
| 67 |
|
| 68 |
using std::vector; |
| 69 |
using std::list; |
| 70 |
|
| 71 |
template<typename T, typename U = T> |
| 72 |
class TridiagBlockMatrix |
| 73 |
{ |
| 74 |
public: |
| 75 |
TridiagBlockMatrix(int dim, int size_b1, bool isSymmetric, |
| 76 |
const int nb, const bool verbose, FILE *fp) : |
| 77 |
_diag_block_alloc_status(false), _diag_block_alloc_status_high(false), |
| 78 |
_nb(nb), _verbose(verbose), _fp(fp) { |
| 79 |
init(dim, size_b1, isSymmetric); |
| 80 |
} |
| 81 |
|
| 82 |
void init(int dim, int size_b1, bool isSymmetric) { |
| 83 |
_dim = dim; |
| 84 |
_size_b1 = size_b1; |
| 85 |
_isSymmetric = isSymmetric; |
| 86 |
} |
| 87 |
|
| 88 |
~TridiagBlockMatrix() { |
| 89 |
free(); |
| 90 |
if(_diag_block_alloc_status) { |
| 91 |
delete [] _diag_blocks; |
| 92 |
#ifndef SPARSE_OFFDIAG |
| 93 |
delete [] _upper_blocks; // 27 Nov.2015 |
| 94 |
delete [] _lower_blocks; // 27 Nov.2015 |
| 95 |
#endif |
| 96 |
#ifdef STORE_WHOLE_FACTORIZED |
| 97 |
_factorized_whole.free(); |
| 98 |
#endif |
| 99 |
_diag_block_alloc_status = false; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
void free() { |
| 104 |
for (int n = 0; n < _nfront; n++) { |
| 105 |
_diag_blocks[n].free(); |
| 106 |
#ifndef SPARSE_OFFDIAG |
| 107 |
_upper_blocks[n].free(); // 27 Nov.2015 |
| 108 |
_lower_blocks[n].free(); // 27 Nov.2015 |
| 109 |
#endif |
| 110 |
} |
| 111 |
_a21.free(); |
| 112 |
_a12.free(); |
| 113 |
_s22.free(); |
| 114 |
} |
| 115 |
|
| 116 |
bool isSym() const { |
| 117 |
return _isSymmetric; |
| 118 |
} |
| 119 |
void SymbolicFact(const int color, |
| 120 |
const int color_max, |
| 121 |
int *color_mask, |
| 122 |
const int dim_, |
| 123 |
const int nnz, |
| 124 |
const int *prow_, |
| 125 |
const int *indcols_, |
| 126 |
const int *indvals_); |
| 127 |
|
| 128 |
void NumericFact(const T* coef, |
| 129 |
const double eps_pivot, |
| 130 |
double *pivot, |
| 131 |
const bool kernel_detection, |
| 132 |
const bool higher_precision, |
| 133 |
const int dim_aug_kern, |
| 134 |
const U eps_machine, |
| 135 |
double *nopd); |
| 136 |
|
| 137 |
void ComputeSchurComplement(const int nsing, |
| 138 |
vector<int> &list_sing, |
| 139 |
ColumnMatrix<T>& a12, |
| 140 |
ColumnMatrix<T>& a21, |
| 141 |
ColumnMatrix<T>& a22); |
| 142 |
|
| 143 |
void TridiagNumericFact(double *pivot, |
| 144 |
const double eps_pivot, |
| 145 |
const int dim_aug_kern, |
| 146 |
ColumnMatrix<T> *diag_block_save, |
| 147 |
vector<int> &num_null_aug, |
| 148 |
double *nopd, |
| 149 |
const bool higher_precision = false); |
| 150 |
|
| 151 |
void ComputeSchur(const int dim_, |
| 152 |
int *color_mask, |
| 153 |
const int ncol, |
| 154 |
const int *ptrow1, const int *indcols1, |
| 155 |
const int *indvals1, const int *indvals2, |
| 156 |
const T *coef, |
| 157 |
const int size_b1, |
| 158 |
SquareBlockMatrix<T> & local_s, |
| 159 |
double *nopd, |
| 160 |
elapsed_t *tt); |
| 161 |
|
| 162 |
void SolveMulti(const bool flag_new2old, const bool isTrans, |
| 163 |
const int nhrs, ColumnMatrix<T>& x, const int nscol_ = (-1)); |
| 164 |
|
| 165 |
void SolveSingle(const bool flag_new2old, |
| 166 |
const bool isTrans, T* x, const int nscol_ = (-1)); |
| 167 |
|
| 168 |
void ForwardUpper(bool isTransposed, |
| 169 |
int ncol, ColumnMatrix<T> &b, |
| 170 |
vector<int>& i0, |
| 171 |
vector<T> &dscale); |
| 172 |
|
| 173 |
void extract_column(const int jcol, T *dcol); |
| 174 |
|
| 175 |
void extract_row(const int irow, T *drow); |
| 176 |
|
| 177 |
void SingularNode(vector<int> &list_sing); |
| 178 |
|
| 179 |
int NumNegativeDiags(); |
| 180 |
|
| 181 |
// void KernelIndex(vector<int> &list); |
| 182 |
|
| 183 |
void KernelBasis(const bool isTrans, ColumnMatrix<T> &a12); |
| 184 |
|
| 185 |
int dimension() const {return _dim; } |
| 186 |
int block_size() const {return _size_b1; } |
| 187 |
int nrow() const { return _dim; } |
| 188 |
int rank() const { return (_dim - _n0); } |
| 189 |
int nsing() const { return _n0; } |
| 190 |
bool detected() const { return _detected; } |
| 191 |
double nop() const {return _nop; } |
| 192 |
bool diag_block_alloc_status() const {return _diag_block_alloc_status; } |
| 193 |
bool diag_block_alloc_status_high() const { |
| 194 |
return _diag_block_alloc_status_high; } |
| 195 |
int maxdim() const { return _maxdim; } |
| 196 |
int nnz() const { return _nnz; } |
| 197 |
int nscol() const {return _nscol; } |
| 198 |
int Nfront() const {return _nfront; } |
| 199 |
|
| 200 |
vector<int> &getPtRows() { return _ptRows; } |
| 201 |
vector<int> &getIndCols() { return _indCols; } |
| 202 |
vector<int> &getIndVals() { return _indVals; } |
| 203 |
vector<int> &getNew2old() { return _new2old; } |
| 204 |
vector<int> &getP_front() { return _p_front; } |
| 205 |
vector<int> &getPermute() { return _permute; } |
| 206 |
vector<int> &getPermute_ginv() { return _permute_ginv; } |
| 207 |
vector<int> &getP_diag() { return _p_diag; } |
| 208 |
vector<int> &getP_upper() { return _p_upper; } |
| 209 |
vector<int> &getList_schur() { return _list_schur; } |
| 210 |
vector<int> &getList_elim() { return _list_elim; } |
| 211 |
vector<int> &getNum_null() { return _num_null; } |
| 212 |
void setCoef(const T *coef) { _coef = coef; } |
| 213 |
ColumnMatrix<T> &getA12() { return _a12; } |
| 214 |
ColumnMatrix<T> &getA21() { return _a21; } |
| 215 |
ColumnMatrix<T> &getS22() { return _s22; } |
| 216 |
|
| 217 |
void setNfront(int nfront) { _nfront = nfront; } |
| 218 |
void setMaxdim(int maxdim) { _maxdim = maxdim; } |
| 219 |
void setNnz(int nnz) { _nnz = nnz; } |
| 220 |
void setNop(double nop) { _nop = nop; } |
| 221 |
void setDiag_block_alloc_status(int diag_block_alloc_status) { |
| 222 |
_diag_block_alloc_status = diag_block_alloc_status; |
| 223 |
} |
| 224 |
void setDiag_block_alloc_status_high(int diag_block_alloc_status) { |
| 225 |
_diag_block_alloc_status_high = diag_block_alloc_status; |
| 226 |
} |
| 227 |
void setNscol(int nscol) { _nscol = nscol; } |
| 228 |
void setNsing(int n0) { _n0 = n0; } |
| 229 |
void setDetected(bool detected) { _detected = detected; } |
| 230 |
|
| 231 |
// void setNb(int nb) { _nb = nb; } // for debug |
| 232 |
int getNb() { return _nb; } // for debug |
| 233 |
ColumnMatrix<T>*& getaddrDiagMatrix() { return _diag_blocks; } |
| 234 |
#ifndef SPARSE_OFFDIAG |
| 235 |
ColumnMatrix<T>*& getaddrLowerMatrix() { return _lower_blocks; } |
| 236 |
ColumnMatrix<T>*& getaddrUpperMatrix() { return _upper_blocks; } |
| 237 |
#endif |
| 238 |
private: |
| 239 |
int _nb; |
| 240 |
bool _verbose; |
| 241 |
FILE *_fp; |
| 242 |
bool _isSymmetric; |
| 243 |
const T* _coef; |
| 244 |
int _color; |
| 245 |
int _color_max; |
| 246 |
int _dim; |
| 247 |
int _size_b1; |
| 248 |
int _nfront; |
| 249 |
int _maxdim; |
| 250 |
int _nnz; |
| 251 |
double _nop; |
| 252 |
vector<int> _ptRows; // prow |
| 253 |
vector<int> _indCols; // column_numb |
| 254 |
vector<int> _indVals; // column_numb |
| 255 |
vector<int> _new2old; |
| 256 |
vector<int> _p_front; |
| 257 |
vector<int> _permute; |
| 258 |
vector<int> _permute_ginv; |
| 259 |
vector<int> _p_diag; |
| 260 |
vector<int> _p_upper; |
| 261 |
ColumnMatrix<T>* _diag_blocks; |
| 262 |
#ifndef SPARSE_OFFDIAG |
| 263 |
ColumnMatrix<T>* _upper_blocks; // 27 Nov.2015 |
| 264 |
ColumnMatrix<T>* _lower_blocks; // 27 Nov.2015 |
| 265 |
#endif |
| 266 |
#ifdef STORE_WHOLE_FACTORIZED |
| 267 |
ColumnMatrix<T> _factorized_whole; |
| 268 |
#endif |
| 269 |
bool _diag_block_alloc_status; |
| 270 |
bool _diag_block_alloc_status_high; |
| 271 |
int _nscol; // number of postponed entries |
| 272 |
int _n0; // kernel dimension |
| 273 |
bool _detected; |
| 274 |
ColumnMatrix<T> _a12; |
| 275 |
ColumnMatrix<T> _a21; |
| 276 |
ColumnMatrix<T> _s22; |
| 277 |
vector<int> _list_schur; |
| 278 |
vector<int> _list_elim; // ? |
| 279 |
vector<int> _num_null; |
| 280 |
|
| 281 |
static const T _one; // (1.0); |
| 282 |
static const T _zero; // (0.0); |
| 283 |
static const T _none; // (-1.0); |
| 284 |
}; |
| 285 |
|
| 286 |
void RenumberCSR(const bool shrink_flag, |
| 287 |
const int dim, |
| 288 |
const int nnz, |
| 289 |
vector<int> &b2a, |
| 290 |
vector<int> &a2b, |
| 291 |
const int *aptrows, |
| 292 |
const int *aindcols, |
| 293 |
const int *aindvals, |
| 294 |
vector<int> &bptrows, |
| 295 |
vector<int> &bindcols, vector<int> &bindvals, |
| 296 |
const bool verbose, |
| 297 |
FILE *fp); |
| 298 |
|
| 299 |
void RenumberCSR(const int dim, |
| 300 |
vector<int> &b2a, |
| 301 |
vector<int> &a2b, |
| 302 |
const int *aptrows, |
| 303 |
const int *aindcols, |
| 304 |
vector<int> &bptrows, |
| 305 |
vector<int> &bindcols, |
| 306 |
const bool verbose, |
| 307 |
FILE *fp); |
| 308 |
|
| 309 |
void TridiagStruct(vector<int> &ptrow, vector<int> &indcols, |
| 310 |
const int nfront, |
| 311 |
vector<int> &pfront, vector<int> &p_diag, |
| 312 |
vector<int> &p_upp); |
| 313 |
|
| 314 |
void GenPermuteOffdiag(const int pfront0, const int pfront1, const int pfront2, |
| 315 |
vector<int> &ptrow, vector<int> &ptdiag, |
| 316 |
vector<int> &indcols, |
| 317 |
const int *permute_diag, int *permute_diag_inv, |
| 318 |
int *permute_offdiag, int *permute_offdiag_inv, |
| 319 |
vector<int> &i0); |
| 320 |
|
| 321 |
void GenPermuteUpper(const int nrow, |
| 322 |
vector<int> &remap_eqn, |
| 323 |
const int ncol, |
| 324 |
const int *ptrow, const int *indcols, |
| 325 |
const int n_front, |
| 326 |
vector<int> &p_fronts, |
| 327 |
vector<int> &old2new, |
| 328 |
vector<int> &permute_diag, |
| 329 |
vector<int> &permute_diag_inv, |
| 330 |
vector<int> &permute_upper, |
| 331 |
vector<int> &permute_upper_inv, |
| 332 |
vector<int> &i0, |
| 333 |
const bool verbose, |
| 334 |
FILE *fp); |
| 335 |
|
| 336 |
template<typename T> |
| 337 |
void FillBlockSparse(const T *coef, |
| 338 |
const int dim, |
| 339 |
vector<int>& map_eqn, |
| 340 |
const int *ptrow, |
| 341 |
const int *indcols, const int *indvals, |
| 342 |
vector<int> &new2old_j, |
| 343 |
vector<int> &old2new_j, |
| 344 |
ColumnMatrix<T> &b); |
| 345 |
|
| 346 |
template<typename T, typename U, typename W, typename Z> |
| 347 |
void TridiagNumericFact_(double *pivot, |
| 348 |
const double eps_pivot, |
| 349 |
const int dim_aug_kern, |
| 350 |
ColumnMatrix<T> *diag_block_save, |
| 351 |
vector<int> &num_null_aug, |
| 352 |
double *nopd, const bool higher_precision, |
| 353 |
bool &_verbose, |
| 354 |
FILE* &_fp, |
| 355 |
int &_dim, |
| 356 |
bool &_isSymmetric, |
| 357 |
int &_nfront, |
| 358 |
vector<int> &_p_front, |
| 359 |
vector<int> &_p_diag, |
| 360 |
vector<int> &_p_upper, |
| 361 |
const T* &_coef, |
| 362 |
vector<int> &_ptRows, |
| 363 |
vector<int> &_indCols, // column_numb |
| 364 |
vector<int> &_indVals, // column_numb |
| 365 |
vector<int> &_num_null, |
| 366 |
vector<int> &_permute, |
| 367 |
vector<int> &_permute_ginv, |
| 368 |
ColumnMatrix<T>* &_diag_blocks, |
| 369 |
ColumnMatrix<W>* &_diag_blocks_high, |
| 370 |
ColumnMatrix<T>* &_upper_blocks, |
| 371 |
ColumnMatrix<T>* &_lower_blocks, |
| 372 |
list<int> &_high_blocks |
| 373 |
#ifdef STORE_WHOLE_FACTORIZED |
| 374 |
, |
| 375 |
ColumnMatrix<T> &_factorized_whole |
| 376 |
#endif |
| 377 |
); |
| 378 |
|
| 379 |
#endif |