Loading...
Searching...
No Matches
system_solvers_large.hpp
1/*---------------------------------------------------------------------------*\
2 *
3 * bitpit
4 *
5 * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6 *
7 * -------------------------------------------------------------------------
8 * License
9 * This file is part of bitpit.
10 *
11 * bitpit is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License v3 (LGPL)
13 * as published by the Free Software Foundation.
14 *
15 * bitpit is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with bitpit. If not, see <http://www.gnu.org/licenses/>.
22 *
23\*---------------------------------------------------------------------------*/
24
25#ifndef __BITPIT_SYSTEM_SOLVERS_LARGE_HPP__
26#define __BITPIT_SYSTEM_SOLVERS_LARGE_HPP__
27
28#include <unordered_map>
29#include <unordered_set>
30#include <vector>
31
32#include <petscksp.h>
33
34#include "bitpit_IO.hpp"
35
36#include "system_matrix.hpp"
37
38namespace bitpit {
39
40struct KSPOptions {
41 PetscInt overlap;
42 PetscInt levels;
43
44 PetscBool initial_non_zero;
45 PetscInt restart;
46 PetscInt maxits;
47 PetscScalar rtol;
48 PetscScalar atol;
49
50 PetscInt sublevels;
51 PetscScalar subrtol;
52
54 : overlap(PETSC_DEFAULT), levels(PETSC_DEFAULT),
55 initial_non_zero(PETSC_TRUE), restart(PETSC_DEFAULT),
56 maxits(PETSC_DEFAULT), rtol(PETSC_DEFAULT), atol(PETSC_DEFAULT),
57 sublevels(PETSC_DEFAULT), subrtol(PETSC_DEFAULT)
58 {
59 }
60};
61
62struct KSPStatus {
63 PetscErrorCode error;
64 PetscInt its;
65 KSPConvergedReason convergence;
66
67 KSPStatus()
68 : error(0), its(-1), convergence(KSP_DIVERGED_BREAKDOWN)
69 {
70 }
71};
72
74{
75
76public:
77 virtual ~SystemMatrixOrdering() = default;
78
89 virtual long getRowPermutationRank(long row) const = 0;
90
101 virtual long getColPermutationRank(long col) const = 0;
102
103protected:
104 SystemMatrixOrdering() = default;
105
106};
107
109{
110
111public:
112 NaturalSystemMatrixOrdering() = default;
113
114 long getRowPermutationRank(long row) const override;
115 long getColPermutationRank(long col) const override;
116
117};
118
119template<typename RowRankStorage, typename ColRankStorage>
121{
122
123public:
124 ProxySystemMatrixOrdering(const RowRankStorage *rowRankStorage, const ColRankStorage *colRankStorage);
125
126 long getRowPermutationRank(long row) const override;
127 long getColPermutationRank(long col) const override;
128
129private:
130 const RowRankStorage *m_rowRankStorage;
131 const ColRankStorage *m_colRankStorage;
132
133};
134
136
137public:
139 bool full;
140 bool sorted;
141 };
142
143 virtual ~SystemMatrixAssembler() = default;
144
145#if BITPIT_ENABLE_MPI==1
146 virtual bool isPartitioned() const = 0;
147 virtual const MPI_Comm & getCommunicator() const = 0;
148#endif
149
150 virtual AssemblyOptions getOptions() const = 0;
151
152 virtual int getBlockSize() const = 0;
153
154 virtual long getRowCount() const = 0;
155 virtual long getColCount() const = 0;
156
157 virtual long getRowElementCount() const = 0;
158 virtual long getColElementCount() const = 0;
159
160#if BITPIT_ENABLE_MPI==1
161 virtual long getRowGlobalCount() const = 0;
162 virtual long getColGlobalCount() const = 0;
163
164 virtual long getRowGlobalElementCount() const = 0;
165 virtual long getColGlobalElementCount() const = 0;
166
167 virtual long getRowGlobalOffset() const = 0;
168 virtual long getColGlobalOffset() const = 0;
169
170 virtual long getRowGlobalElementOffset() const = 0;
171 virtual long getColGlobalElementOffset() const = 0;
172#endif
173
174 virtual long getRowNZCount(long rowIndex) const = 0;
175 virtual long getMaxRowNZCount() const = 0;
176
177 virtual void getRowPattern(long rowIndex, ConstProxyVector<long> *pattern) const = 0;
178 virtual void getRowValues(long rowIndex, ConstProxyVector<double> *values) const = 0;
179 virtual void getRowData(long rowIndex, ConstProxyVector<long> *pattern, ConstProxyVector<double> *values) const = 0;
180
181protected:
182 SystemMatrixAssembler() = default;
183
184};
185
187
188public:
190
191#if BITPIT_ENABLE_MPI==1
192 bool isPartitioned() const override;
193 const MPI_Comm & getCommunicator() const override;
194#endif
195
196 AssemblyOptions getOptions() const override;
197
198 int getBlockSize() const override;
199
200 long getRowCount() const override;
201 long getColCount() const override;
202
203 long getRowElementCount() const override;
204 long getColElementCount() const override;
205
206#if BITPIT_ENABLE_MPI==1
207 long getRowGlobalCount() const override;
208 long getColGlobalCount() const override;
209
210 long getRowGlobalElementCount() const override;
211 long getColGlobalElementCount() const override;
212
213 long getRowGlobalOffset() const override;
214 long getColGlobalOffset() const override;
215
216 long getRowGlobalElementOffset() const override;
217 long getColGlobalElementOffset() const override;
218#endif
219
220 long getRowNZCount(long rowIndex) const override;
221 long getMaxRowNZCount() const override;
222
223 void getRowPattern(long rowIndex, ConstProxyVector<long> *pattern) const override;
224 void getRowValues(long rowIndex, ConstProxyVector<double> *values) const override;
225 void getRowData(long rowIndex, ConstProxyVector<long> *pattern, ConstProxyVector<double> *values) const override;
226
227protected:
228 const SparseMatrix *m_matrix;
229
230};
231
233
234public:
235 PetscManager();
236
237 virtual ~PetscManager();
238
239 bool areOptionsEditable() const;
240
241 bool initialize(bool debug);
242 bool finalize();
243
244 void addInitOption(const std::string &option);
245 void addInitOptions(int argc, char **args);
246 void addInitOptions(const std::vector<std::string> &options);
247 void clearInitOptions();
248
249private:
250 static PetscErrorCode displayLogView();
251
252 bool m_externalMPIInitialization;
253 bool m_externalPETScInitialization;
254
255 bool m_optionsEditable;
256 std::vector<std::string> m_options;
257
258 bool m_logViewEnabled;
259
260 void enableLogView();
261
262};
263
265
266public:
268
269 enum FileFormat {
270 FILE_BINARY,
271 FILE_ASCII
272 };
273
274 SystemSolver(bool debug = false);
275 SystemSolver(bool transpose, bool debug);
276 SystemSolver(bool flatten, bool transpose, bool debug);
277 SystemSolver(const std::string &prefix, bool debug = false);
278 SystemSolver(const std::string &prefix, bool transpose, bool debug);
279 SystemSolver(const std::string &prefix, bool flatten, bool transpose, bool debug);
280
281 virtual ~SystemSolver();
282
283 virtual void clear();
284 virtual void clearWorkspace();
285
286 bool isAssembled() const;
287
288 void assembly(const SparseMatrix &matrix);
289 void assembly(const SparseMatrix &matrix, const SystemMatrixOrdering &reordering);
290 void assembly(const Assembler &assembler);
291 void assembly(const Assembler &assembler, const SystemMatrixOrdering &reordering);
292
293 void update(const SparseMatrix &elements);
294 void update(long nRows, const long *rows, const SparseMatrix &elements);
295 void update(const Assembler &assembler);
296 void update(long nRows, const long *rows, const Assembler &assembler);
297
298 BITPIT_DEPRECATED(void setUp());
299 BITPIT_DEPRECATED(bool isSetUp() const);
300
301 bool getTranspose() const;
302 void setTranspose(bool transpose);
303
304 virtual int getBlockSize() const;
305
306 long getRowCount() const;
307 long getColCount() const;
308 long getRowElementCount() const;
309 long getColElementCount() const;
310#if BITPIT_ENABLE_MPI==1
311 long getRowGlobalCount() const;
312 long getColGlobalCount() const;
313 long getRowGlobalElementCount() const;
314 long getColGlobalElementCount() const;
315
316 bool isPartitioned() const;
317#endif
318
319 void solve();
320 void solve(const std::vector<double> &rhs, std::vector<double> *solution);
321
322 void dumpSystem(const std::string &header, const std::string &directory, const std::string &prefix = "") const;
323#if BITPIT_ENABLE_MPI==1
324 void restoreSystem(MPI_Comm communicator, bool redistribute, const std::string &directory,
325 const std::string &prefix = "");
326#else
327 void restoreSystem(const std::string &directory, const std::string &prefix = "");
328#endif
329
330 virtual void setNullSpace();
331 void unsetNullSpace();
332
334 const KSPOptions & getKSPOptions() const;
335
336 const KSPStatus & getKSPStatus() const;
337
338 virtual void exportMatrix(const std::string &filePath, FileFormat exportFormat = FILE_BINARY) const;
339 virtual void importMatrix(const std::string &filePath);
340
341 double * getRHSRawPtr();
342 const double * getRHSRawPtr() const;
343 const double * getRHSRawReadPtr() const;
344 void restoreRHSRawPtr(double *raw_rhs);
345 void restoreRHSRawReadPtr(const double *raw_rhs) const;
346 virtual void exportRHS(const std::string &filePath, FileFormat exportFormat = FILE_BINARY) const;
347 virtual void importRHS(const std::string &filePath);
348
349 double * getSolutionRawPtr();
350 const double * getSolutionRawPtr() const;
351 const double * getSolutionRawReadPtr() const;
352 void restoreSolutionRawPtr(double *raw_solution);
353 void restoreSolutionRawReadPtr(const double *raw_solution) const;
354 virtual void exportSolution(const std::string &filePath, FileFormat exportFormat = FILE_BINARY) const;
355 virtual void importSolution(const std::string &filePath);
356
357 bool isForceConsistencyEnabled() const;
358 void enableForceConsistency(bool enable);
359
360protected:
361 enum VectorSide {
362 VECTOR_SIDE_RIGHT, // Vector that the matrix can be multiplied against
363 VECTOR_SIDE_LEFT, // Vector that the matrix vector product can be stored in
364 };
365
366 bool m_flatten;
367 bool m_transpose;
368
369 Mat m_A;
370 Vec m_rhs;
371 Vec m_solution;
372
373 IS m_rowReordering;
374 IS m_colReordering;
375
376 bool m_convergenceMonitorEnabled;
377
378 KSP m_KSP;
379 bool m_KSPDirty;
380 KSPOptions m_KSPOptions;
381 KSPStatus m_KSPStatus;
382
383 virtual int getDumpVersion() const;
384
385 void matrixAssembly(const Assembler &assembler);
386 void matrixUpdate(long nRows, const long *rows, const Assembler &assembler);
387 virtual void matrixFill(const std::string &filePath);
388 virtual void matrixDump(std::ostream &systemStream, const std::string &directory, const std::string &prefix) const;
389#if BITPIT_ENABLE_MPI==1
390 virtual void matrixRestore(std::istream &systemStream, const std::string &directory, const std::string &prefix, bool redistribute);
391#else
392 virtual void matrixRestore(std::istream &systemStream, const std::string &directory, const std::string &prefix);
393#endif
394 virtual void matrixDestroy();
395
396 virtual void vectorsCreate();
397 virtual void vectorsFill(const std::vector<double> &rhs, const std::vector<double> &solution);
398 virtual void vectorsFill(const std::string &rhsFilePath, const std::string &solutionFilePath);
399 virtual void vectorsReorder(bool invert);
400 virtual void vectorsDump(std::ostream &systemStream, const std::string &directory, const std::string &prefix) const;
401 virtual void vectorsRestore(std::istream &systemStream, const std::string &directory, const std::string &prefix);
402 virtual void vectorsDestroy();
403
404 void clearReordering();
405 void setReordering(long nRows, long nCols, const SystemMatrixOrdering &reordering);
406
407 template<typename DerivedSystemSolver>
408 void assembly(const typename DerivedSystemSolver::Assembler &assembler, const SystemMatrixOrdering &reordering);
409
410 template<typename DerivedSystemSolver>
411 void update(long nRows, const long *rows, const typename DerivedSystemSolver::Assembler &assembler);
412
413 virtual void prepareKSP();
414 virtual void finalizeKSP();
415 virtual void createKSP();
416 virtual void destroyKSP();
417
418 virtual void setupPreconditioner();
419 virtual void setupPreconditioner(PC pc, const KSPOptions &options) const;
420 virtual void prePreconditionerSetupActions();
421 virtual void postPreconditionerSetupActions();
422
423 virtual void setupKrylov();
424 virtual void setupKrylov(KSP ksp, const KSPOptions &options) const;
425 virtual void preKrylovSetupActions();
426 virtual void postKrylovSetupActions();
427
428 virtual void solveKSP();
429 virtual void preKSPSolveActions();
430 virtual void postKSPSolveActions();
431
432 virtual void initializeKSPOptions();
433 virtual void resetKSPOptions(KSPOptions *options) const;
434 virtual void destroyKSPOptions();
435
436 virtual void initializeKSPStatus();
437 virtual void resetKSPStatus();
438 virtual void resetKSPStatus(KSPStatus *status) const;
439 virtual void fillKSPStatus();
440 virtual void fillKSPStatus(KSP ksp, KSPStatus *status) const;
441 virtual void destroyKSPStatus();
442
443 virtual void dumpInfo(std::ostream &stream) const;
444 virtual void restoreInfo(std::istream &stream);
445
446 void createMatrix(int rowBlockSize, int colBlockSize, Mat *matrix) const;
447 void createMatrix(int rowBlockSize, int colBlockSize, int nNestRows, int nNestCols, Mat *subMatrices, Mat *matrix) const;
448 void fillMatrix(Mat matrix, const std::string &filePath) const;
449 void dumpMatrix(Mat matrix, const std::string &directory, const std::string &name) const;
450#if BITPIT_ENABLE_MPI==1
451 void restoreMatrix(const std::string &directory, const std::string &name, bool redistribute, Mat *matrix) const;
452#else
453 void restoreMatrix(const std::string &directory, const std::string &name, Mat *matrix) const;
454#endif
455 void exportMatrix(Mat matrix, const std::string &filePath, FileFormat fileFormat) const;
456 void destroyMatrix(Mat *matrix) const;
457
458 void createVector(int blockSize, Vec *vector) const;
459 void createVector(int blockSize, int nestSize, Vec *subVectors, Vec *vector) const;
460 void fillVector(Vec vector, const std::string &filePath) const;
461 void fillVector(Vec vector, const std::vector<double> &data) const;
462 void reorderVector(Vec vector, IS permutations, bool invert) const;
463 void dumpVector(Vec vector, const std::string &directory, const std::string &name) const;
464 void dumpVector(Vec vector, VectorSide side, const std::string &directory, const std::string &name) const;
465 void restoreVector(const std::string &directory, const std::string &name, Mat matrix, VectorSide side, Vec *vector) const;
466#if BITPIT_ENABLE_MPI==1
467 void restoreVector(const std::string &directory, const std::string &name, bool redistribute, Vec *vector) const;
468#else
469 void restoreVector(const std::string &directory, const std::string &name, Vec *vector) const;
470#endif
471 void restoreVector(const std::string &directory, const std::string &name, std::size_t localSize, std::size_t globalSize, Vec *vector) const;
472 void exportVector(Vec vector, const std::string &filePath, FileFormat fileFormat) const;
473 void exportVector(Vec vector, std::vector<double> *data) const;
474 void destroyVector(Vec *vector) const;
475
476 std::string getInfoFilePath(const std::string &directory, const std::string &name) const;
477 std::string getDataFilePath(const std::string &directory, const std::string &name) const;
478 std::string getFilePath(const std::string &directory, const std::string &name) const;
479
480#if BITPIT_ENABLE_MPI==1
481 const MPI_Comm & getCommunicator() const;
482#endif
483
484private:
485 static PetscManager m_petscManager;
486
487 static int m_nInstances;
488
489 std::string m_prefix;
490
491 bool m_assembled;
492
493#if BITPIT_ENABLE_MPI==1
494 MPI_Comm m_communicator;
495
496 bool m_partitioned;
497#endif
498
499 bool m_forceConsistency;
500
501#if BITPIT_ENABLE_MPI==1
502 void setCommunicator(MPI_Comm communicator);
503 void freeCommunicator();
504#endif
505
506 void resetPermutations();
507
508 void removeNullSpaceFromRHS();
509
510 void openBinaryArchive(const std::string &directory, const std::string &prefix,
511 int block, IBinaryArchive *archive) const;
512 void openBinaryArchive(const std::string &header, const std::string &directory,
513 const std::string &prefix, int block, OBinaryArchive *archive) const;
514
515 void closeBinaryArchive(BinaryArchive *archive) const;
516
517 int getBinaryArchiveBlock() const;
518};
519
520}
521
522// Include template implementations
523#include "system_solvers_large.tpp"
524
525#endif
Base class for binary archives.
Input binary archive.
The NaturalSystemMatrixOrdering class defines allows to use a matrix natural ordering.
long getColPermutationRank(long col) const override
long getRowPermutationRank(long row) const override
Output binary archive.
The PetscManager class handles the interaction with PETSc library.
void addInitOptions(int argc, char **args)
void addInitOption(const std::string &option)
The ProxySystemMatrixOrdering class defines allows to use a matrix ordering defined in some external ...
long getRowPermutationRank(long row) const override
long getColPermutationRank(long col) const override
ProxySystemMatrixOrdering(const RowRankStorage *rowRankStorage, const ColRankStorage *colRankStorage)
Metafunction for generating a list of elements that can be either stored in an external vectror or,...
The SystemMatrixAssembler class provides an interface for defining system matrix assemblers.
The SystemMatrixOrdering class provides an interface for defining classes that allows to reorder the ...
virtual long getColPermutationRank(long col) const =0
virtual long getRowPermutationRank(long row) const =0
The SystemSolver class provides methods for building and solving large linear systems.
void dumpMatrix(Mat matrix, const std::string &directory, const std::string &name) const
void matrixUpdate(long nRows, const long *rows, const Assembler &assembler)
virtual int getDumpVersion() const
virtual void exportMatrix(const std::string &filePath, FileFormat exportFormat=FILE_BINARY) const
virtual void matrixFill(const std::string &filePath)
virtual void prePreconditionerSetupActions()
void restoreSolutionRawPtr(double *raw_solution)
virtual void matrixRestore(std::istream &systemStream, const std::string &directory, const std::string &prefix, bool redistribute)
void fillMatrix(Mat matrix, const std::string &filePath) const
void restoreRHSRawReadPtr(const double *raw_rhs) const
void dumpVector(Vec vector, const std::string &directory, const std::string &name) const
virtual void postPreconditionerSetupActions()
virtual void importMatrix(const std::string &filePath)
void reorderVector(Vec vector, IS permutations, bool invert) const
std::string getInfoFilePath(const std::string &directory, const std::string &name) const
virtual void vectorsDump(std::ostream &systemStream, const std::string &directory, const std::string &prefix) const
virtual void resetKSPOptions(KSPOptions *options) const
void exportVector(Vec vector, const std::string &filePath, FileFormat fileFormat) const
void dumpSystem(const std::string &header, const std::string &directory, const std::string &prefix="") const
void setReordering(long nRows, long nCols, const SystemMatrixOrdering &reordering)
virtual void restoreInfo(std::istream &stream)
void restoreSystem(MPI_Comm communicator, bool redistribute, const std::string &directory, const std::string &prefix="")
const double * getSolutionRawReadPtr() const
virtual void exportRHS(const std::string &filePath, FileFormat exportFormat=FILE_BINARY) const
virtual int getBlockSize() const
void restoreRHSRawPtr(double *raw_rhs)
void setTranspose(bool transpose)
const KSPStatus & getKSPStatus() const
void restoreMatrix(const std::string &directory, const std::string &name, bool redistribute, Mat *matrix) const
void matrixAssembly(const Assembler &assembler)
void createVector(int blockSize, Vec *vector) const
void destroyMatrix(Mat *matrix) const
std::string getFilePath(const std::string &directory, const std::string &name) const
virtual void matrixDump(std::ostream &systemStream, const std::string &directory, const std::string &prefix) const
virtual void dumpInfo(std::ostream &stream) const
void assembly(const SparseMatrix &matrix)
const MPI_Comm & getCommunicator() const
std::string getDataFilePath(const std::string &directory, const std::string &name) const
const double * getRHSRawReadPtr() const
virtual void importSolution(const std::string &filePath)
virtual void importRHS(const std::string &filePath)
void destroyVector(Vec *vector) const
void createMatrix(int rowBlockSize, int colBlockSize, Mat *matrix) const
void fillVector(Vec vector, const std::string &filePath) const
virtual void vectorsFill(const std::vector< double > &rhs, const std::vector< double > &solution)
void enableForceConsistency(bool enable)
void restoreSolutionRawReadPtr(const double *raw_solution) const
virtual void vectorsReorder(bool invert)
virtual void vectorsRestore(std::istream &systemStream, const std::string &directory, const std::string &prefix)
void restoreVector(const std::string &directory, const std::string &name, Mat matrix, VectorSide side, Vec *vector) const
virtual void exportSolution(const std::string &filePath, FileFormat exportFormat=FILE_BINARY) const
The SystemSparseMatrixAssembler class defines an assembler for building the system matrix form a spar...
void getRowValues(long rowIndex, ConstProxyVector< double > *values) const override
const MPI_Comm & getCommunicator() const override
void getRowData(long rowIndex, ConstProxyVector< long > *pattern, ConstProxyVector< double > *values) const override
long getRowNZCount(long rowIndex) const override
AssemblyOptions getOptions() const override
void getRowPattern(long rowIndex, ConstProxyVector< long > *pattern) const override
SystemSparseMatrixAssembler(const SparseMatrix *matrix)
#define BITPIT_DEPRECATED(func)
Definition compiler.hpp:87
PetscScalar subrtol
Deprecated, ASM ILU levels should be set using the "levels" member.
PetscInt levels
Overlap between a pair of subdomains for the partitioned preconditioner.
PetscInt restart
Tells the iterative solver that the initial guess is nonzero.
PetscInt maxits
Number of iterations at which the GMRES method restarts.
PetscScalar atol
Relative convergence tolerance, relative decrease in the preconditioned residual norm.
PetscInt sublevels
Absolute convergence tolerance, absolute size of the preconditioned residual norm.
PetscScalar rtol
Maximum number of iterations.
PetscBool initial_non_zero
Number of levels of fill used by the preconditioner.
KSPOptions()
Deprecated, it has never had any effect on the solution of the system.
bool full
Controls if the assembler is providing all the non-zero values of a row.
--- layout: doxygen_footer ---