| 1 |
/*! \file DissectionSolver.hpp |
| 2 |
\brief task mangemanet of dissection algorithm |
| 3 |
\author Atsushi Suzuki, Laboratoire Jacques-Louis Lions |
| 4 |
\date Mar. 30th 2012 |
| 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 _DISSECTION_SOLVER_ |
| 52 |
#define _DISSECTION_SOLVER_ |
| 53 |
#include <cassert> |
| 54 |
#include <vector> |
| 55 |
|
| 56 |
#include "Compiler/OptionCompiler.hpp" |
| 57 |
#include "Driver/DissectionVersion.hpp" |
| 58 |
#include "Driver/C_threads_tasks.hpp" |
| 59 |
#include "Driver/DissectionMatrix.hpp" |
| 60 |
#include "Driver/DissectionQueue.hpp" |
| 61 |
#include "Driver/TridiagBlockMatrix.hpp" |
| 62 |
#include "Driver/TridiagQueue.hpp" |
| 63 |
#include "Algebra/SparseMatrix.hpp" |
| 64 |
#include "Driver/DissectionDefault.hpp" |
| 65 |
|
| 66 |
// #include <map> |
| 67 |
#include <vector> |
| 68 |
#include <list> |
| 69 |
#include "Splitters/BisectionTree.hpp" |
| 70 |
|
| 71 |
#ifdef BLAS_MKL |
| 72 |
#include "mkl_lapack.h" |
| 73 |
#include "mkl_types.h" |
| 74 |
#endif |
| 75 |
|
| 76 |
# include <cstdlib> |
| 77 |
#include <time.h> |
| 78 |
|
| 79 |
using std::vector; |
| 80 |
using std::list; |
| 81 |
|
| 82 |
void SaveMMMatrix_(const int dim, |
| 83 |
const int nnz, |
| 84 |
const bool isSymmetric, |
| 85 |
const bool isUpper, |
| 86 |
const int *ptrows, |
| 87 |
const int *indcols, |
| 88 |
const int called, |
| 89 |
const double *coefs_); |
| 90 |
|
| 91 |
void SaveMMMatrix_(const int dim, |
| 92 |
const int nnz, |
| 93 |
const bool isSymmetric, |
| 94 |
const bool isUpper, |
| 95 |
const int *ptrows, |
| 96 |
const int *indcols, |
| 97 |
const int called, |
| 98 |
const complex<double> *coefs_); |
| 99 |
|
| 100 |
// example on usage of template |
| 101 |
// T : complex<double>, U : double, W : complex<quadruple>, Z : quadruple |
| 102 |
// T : complex<quadruple>, U : quadruple, W : complex<duoble>, Z : double |
| 103 |
// T : complex<quadruple>, U : quadruple, W = T, Z = U |
| 104 |
// T : quadruple, U = T, W = T, Z = U |
| 105 |
template<typename T, typename U = T, typename W = T, typename Z = U> |
| 106 |
class DissectionSolver |
| 107 |
{ |
| 108 |
public: |
| 109 |
DissectionSolver(int num_threads, bool verbose, int called, FILE *fp) : |
| 110 |
_precDiag(NULL), _num_threads(num_threads), |
| 111 |
_status_factorized(false), _called(called) |
| 112 |
{ |
| 113 |
if (fp == NULL) { |
| 114 |
_verbose = false; |
| 115 |
} |
| 116 |
else { |
| 117 |
_verbose = verbose; |
| 118 |
_fp = fp; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
~DissectionSolver() |
| 123 |
{ |
| 124 |
Destroy(); |
| 125 |
} |
| 126 |
|
| 127 |
std::string Version(void) { |
| 128 |
string version = (to_string(DISSECTION_VERSION) + "." + |
| 129 |
to_string(DISSECTION_RELEASE) + "." + |
| 130 |
to_string(DISSECTION_PATCHLEVEL)); |
| 131 |
return version; |
| 132 |
} |
| 133 |
|
| 134 |
void SaveCSRMatrix(const int called, |
| 135 |
const T *coefs); |
| 136 |
void SaveMMMatrix(const int called, |
| 137 |
const T *coefs); |
| 138 |
|
| 139 |
void Destroy(void); |
| 140 |
|
| 141 |
void NumericFree(void); |
| 142 |
|
| 143 |
void SymbolicFact_(const int dim_, |
| 144 |
const int nz_, |
| 145 |
const bool flagint64, |
| 146 |
const int *ptRows, |
| 147 |
const int *indCols, |
| 148 |
const long long int *ptRows64, |
| 149 |
const long long int *indCols64, |
| 150 |
const bool sym, |
| 151 |
const bool upper, |
| 152 |
const bool isWhole, |
| 153 |
const int decomposer, |
| 154 |
const int nbLevels, |
| 155 |
const int minNodes); |
| 156 |
|
| 157 |
void SymbolicFact(const int dim, |
| 158 |
const int *ptRows, |
| 159 |
const int *indCols, |
| 160 |
const bool sym, |
| 161 |
const bool upper, |
| 162 |
const bool isWhole = false, |
| 163 |
const int decomposer = SCOTCH_DECOMPOSER, |
| 164 |
const int nbLevels = (-1), |
| 165 |
const int minNodes = MINNODES); |
| 166 |
|
| 167 |
void SymbolicFact(const int dim, |
| 168 |
const long long int *ptRows64, |
| 169 |
const long long int *indCols64, |
| 170 |
const bool sym, |
| 171 |
const bool upper, |
| 172 |
const bool isWhole = false, |
| 173 |
const int decomposer = SCOTCH_DECOMPOSER, |
| 174 |
const int nbLevels = (-1), |
| 175 |
const int minNodes = MINNODES); |
| 176 |
|
| 177 |
void NumericFact(const int called, |
| 178 |
T *coefs, |
| 179 |
const int scaling = KKT_SCALING, //useful for the Stokes eqs. |
| 180 |
const double eps_pivot = EPS_PIVOT, |
| 181 |
const bool kernel_detection_all = false, |
| 182 |
const int dim_augkern = DIM_AUG_KERN, |
| 183 |
const double machine_eps_ = -1.0, |
| 184 |
const bool assume_invertible = false, |
| 185 |
const bool higher_precision = false); |
| 186 |
|
| 187 |
bool getFactorized(void) { return _status_factorized; }; |
| 188 |
|
| 189 |
void CopyQueueFwBw(DissectionSolver<W, Z, T, U> &qdslv); |
| 190 |
int GetMaxColors(); |
| 191 |
void GetNullPivotIndices(int *pivots); |
| 192 |
void GetSmallestPivotIndices(const int n, int *pivots); |
| 193 |
|
| 194 |
void GetKernelVectors(T *kern_basis); |
| 195 |
void GetTransKernelVectors(T *kernt_basis); |
| 196 |
void GetMatrixScaling(Z *weight); |
| 197 |
|
| 198 |
void ProjectionImageSingle(T *x, string name = string("")); |
| 199 |
|
| 200 |
void ProjectionKernelOrthSingle(T *x, string name = string(""), |
| 201 |
bool isTrans = false); |
| 202 |
void ProjectionImageMulti(T *x, int nrhs); |
| 203 |
|
| 204 |
void SpMV(const T *x, T *y, bool scaling_flag = true); |
| 205 |
void SpMtV(const T *x, T *y, bool scaling_flag = true); |
| 206 |
|
| 207 |
void SolveScaled(T *x, int nrhs, bool isTrans); |
| 208 |
void SolveScaledRefinement(T *x, int nrhs, vector<int> &singIdx, |
| 209 |
bool isTrans); |
| 210 |
|
| 211 |
void QueueFwBw(T **x, int *nrhs); |
| 212 |
|
| 213 |
void SolveSingle(T *x, bool projection, bool isTrans, bool isScaling, |
| 214 |
const int nexcls = 0); |
| 215 |
void SolveSingle2(T *x, bool isScaling, bool pseudo = false); |
| 216 |
|
| 217 |
void SolveMulti(T *x, int nrhs, bool projection, bool isTrans, |
| 218 |
bool isScaling, const int nexcls = 0); |
| 219 |
|
| 220 |
void SolveSingleDebug(T *x, T* y); |
| 221 |
|
| 222 |
void BuildSingCoefs(T *DSsingCoefs, |
| 223 |
SparseMatrix<T> *DCsCoefs, |
| 224 |
T *DBsCoefs, |
| 225 |
vector<int> &singIdx, |
| 226 |
const bool isTrans = false); |
| 227 |
|
| 228 |
void BuildKernels(vector<int> &singIdx_, |
| 229 |
int n2, |
| 230 |
SchurMatrix<T> &Schur, |
| 231 |
KernelMatrix<T> &kernel); |
| 232 |
|
| 233 |
void BuildKernelsDetection(int &n0, |
| 234 |
vector<int> &singIdx, |
| 235 |
vector<vector<int> >& augkern_indexes, |
| 236 |
vector<SquareBlockMatrix<T>* >& diags, |
| 237 |
const double eps, |
| 238 |
const int dim_augkern, |
| 239 |
SchurMatrix<T> &Schur, |
| 240 |
KernelMatrix<T> &kernel, |
| 241 |
const bool enableDetection = true); |
| 242 |
Dissection::Tree** btree() { return _btree; } |
| 243 |
SparseMatrix<T>* ptDA() { return _ptDA; } |
| 244 |
int dimension(void) { return _dim; } |
| 245 |
int kern_dimension(void); |
| 246 |
int postponed_pivots(void); |
| 247 |
int ComputeTransposedKernels(void); |
| 248 |
int get_num_threads(void) { return _num_threads; } |
| 249 |
int scaling(void) const { return _scaling; } |
| 250 |
bool verbose(void) const { return _verbose; } |
| 251 |
int num_threads(void) const { return _num_threads; } |
| 252 |
bool isScaled(void) { return (_scaling > 0 ? true : false); } |
| 253 |
int nsing(void) const { return _nsing; } |
| 254 |
void SetNsing(int nsing) { _nsing = nsing; } |
| 255 |
int graph_colors() const { return _graph_colors; } |
| 256 |
|
| 257 |
FILE* get_filedescriptor() { return _fp; } |
| 258 |
U* addrPrecDiag() { return _precDiag; } |
| 259 |
DissectionQueue<T, U>** getDissectionQueue() { return _dissectionQueue; } |
| 260 |
TridiagQueue<T, U>** getTridiagQueue() { return _tridiagQueue; } |
| 261 |
|
| 262 |
vector<DissectionMatrix<T, U>* >* getDissectionMatrix() { |
| 263 |
return _dissectionMatrix; |
| 264 |
} |
| 265 |
TridiagBlockMatrix<T, U>** getTridiagBlockMatrix() { return _tridiagMatrix; } |
| 266 |
|
| 267 |
KernelMatrix<T>* getKernelMatrix() { return _kernel; } |
| 268 |
vector<int>* getSingVal() { return _singIdx; } |
| 269 |
vector<int>& getIndexIsolated() { return _index_isolated; } |
| 270 |
|
| 271 |
DissectionSolver(const DissectionSolver &s) |
| 272 |
{ |
| 273 |
_ptDA = s._ptDA; |
| 274 |
_btree = s._btree; |
| 275 |
_dissectionMatrix = s._dissectionMatrix; |
| 276 |
_dissectionQueue = s._dissectionQueue; |
| 277 |
_kernel = s._kernel; |
| 278 |
_singIdx = s._singIdx; |
| 279 |
_precDiag = s._precDiag; |
| 280 |
_scaling = s._scaling; |
| 281 |
_verbose = s._verbose; |
| 282 |
_num_threads = s._num_threads; |
| 283 |
} |
| 284 |
|
| 285 |
private: |
| 286 |
SparseMatrix<T>* _ptDA; |
| 287 |
Dissection::Tree** _btree; |
| 288 |
vector<DissectionMatrix<T, U>* >* _dissectionMatrix; |
| 289 |
DissectionQueue<T, U>** _dissectionQueue; |
| 290 |
TridiagBlockMatrix<T, U>** _tridiagMatrix; |
| 291 |
TridiagQueue<T, U>** _tridiagQueue; |
| 292 |
SchurMatrix<T>* _Schur; |
| 293 |
KernelMatrix<T>* _kernel; |
| 294 |
vector<int>* _singIdx; |
| 295 |
U* _precDiag; |
| 296 |
vector<int> _index_isolated; |
| 297 |
int _scaling; |
| 298 |
bool _verbose; |
| 299 |
int _num_threads; |
| 300 |
bool _assume_invertible; |
| 301 |
bool _status_factorized; |
| 302 |
int _dim; |
| 303 |
int _graph_colors; |
| 304 |
int _dim_augkern; |
| 305 |
int _nsing; |
| 306 |
int _called; |
| 307 |
FILE *_fp; |
| 308 |
bool _with_btree; |
| 309 |
static const T _one; // (1.0); |
| 310 |
static const T _zero; // (0.0); |
| 311 |
static const T _none; // (-1.0); |
| 312 |
}; // End class DissictionSolver |
| 313 |
|
| 314 |
#endif |