Skip to content

Commit d0907b6

Browse files
Merge pull request #346 from jojoasticot/docs/petsc-documentation
Internship documentation
2 parents 42a2d2f + 83708e7 commit d0907b6

File tree

3 files changed

+133
-10
lines changed

3 files changed

+133
-10
lines changed

docs/_sidebar.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- Technical Notes
1616
* [BSR matrix format](matrix/matrix-format-bsr.md)
1717
* [Hypre parameters cheat sheet](solvers/hypre-parameters-cheatsheet.md)
18+
* [PETSc parameters cheat sheet](solvers/petsc-parameters-cheatsheet.md)
1819
* [Imposing Dirichlet conditions](fem/fem-dirichlet-conditions.md)
1920
* [FEM gradient calculation](fem/fem-gradient-calculation.md)
2021
* [Geometric calculation](fem/fem-geometric-quantities.md)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
### ArcaneFEM interface to PETSc cheat sheet
2+
3+
## Axl parameters
4+
5+
Following abbreviations can be useful for reading the table below.
6+
7+
- [D] - Default
8+
- [I] - Integer type
9+
- [R] - Real type
10+
- [S] - String type
11+
12+
| Parameter | Use | Comment |
13+
| --------- | --- | ------- |
14+
| `solver` | Applied Krylov solver. Corresponds to the `-ksp_type` PETSc parameter. | [S] [D] = "cg"<br />**Note**: You can find all possible options on [this page](https://petsc.org/release/manual/ksp/#tab-kspdefaults). |
15+
| `pc-type` | Applied preconditioner. Corresponds to the `-pc_type` PETSc parameter. | [S] [D] = "jacobi"<br />**Note**: You can find all possible options on [this page](https://petsc.org/release/manual/ksp/#tab-pcdefaults). |
16+
| `rtol` | Relative convergence tolerance for the Krylov Solver. | [R] [D] = 1.0e-7 |
17+
| `atol` | Absolute convergence tolerance for the Krylov Solver. | [R] [D] = 0.0<br />set `rtol` to 0 if you want to use `atol` |
18+
| `max-iter` | Maximum Krylov iterations. | [I] [D] = 1000 |
19+
20+
## Matrix and Vector types
21+
22+
The matrix and vector types use internally by PETSc are deducted by default by a number of parameters:
23+
24+
- Is the program running on a single process or on multi processes ?
25+
- Is the program running on GPU(s) (`AcceleratorRuntime` option) ?
26+
27+
**Notes**:
28+
- Before using a certain type of matrix on GPU, check if it is supported on your machine. You can find this information on [this page](https://petsc.org/release/overview/gpu_roadmap).
29+
- All possible options for matrix types can be found [here](https://petsc.org/release/overview/matrix_table).
30+
- All possible options for vector types can be found [here](https://petsc.org/release/overview/vector_table).
31+
32+
## Custom options
33+
34+
Finally, you can specify any PETSc option you want with the `-A,petsc_flags` Arcane option. The flags specified by this option will **overwrite** the default ones (`atol`, `rtol`, `pc\_type`, `mat\_type`...). This can be useful to test some options quickly or to specify ones that are not in the `.axl` ([GAMG options](https://petsc.org/release/manualpages/PC/PCGAMG) for example).
35+
36+
To enable the optimizations that were done for the allocation of the matrix, the user has to put ```m_linear_system.setConstantMatrixSparsity(true);``` and / or ```m_linear_system.setConstantMatrixValues(true);```.
37+
The user cannot have constant values without constant sparsity. Arcane will prevent this behaviour by throwing an error.

femutils/PETScDoFLinearSystem.cc

Lines changed: 95 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ class PETScDoFLinearSystemImpl
8585

8686
void solve() override;
8787

88+
/*!
89+
* \brief Set user parameters to PETSc.
90+
*
91+
* This function will call PetscInitialized() with
92+
* the argc and argv of the -A,petsc_flags option.
93+
* This will set the parameters passed by the user
94+
* to PETSc.
95+
*/
96+
8897
void setSolverCommandLineArguments(const CommandLineArguments& args) override
8998
{
9099
PetscInitialize(args.commandLineArgc(), args.commandLineArgv(), nullptr, nullptr);
@@ -113,6 +122,7 @@ class PETScDoFLinearSystemImpl
113122
Mat m_petsc_matrix;
114123
ISLocalToGlobalMapping m_petsc_map;
115124

125+
//! Indicates if the solve function has already been called
116126
bool m_is_initialized = false;
117127

118128
private:
@@ -140,13 +150,26 @@ class PETScDoFLinearSystemImpl
140150

141151
private:
142152

143-
void _computeMatrixNumeration(MPI_Comm mpi_comm);
153+
void _computeMatrixNumeration();
144154
void _handleParameters(IParallelMng* pm);
145155
void _preallocateMatrix();
146156
void _initSolve();
147157
UniqueArray<PetscInt> _CSRToCOO(const Span<const int32_t> csr_rows, int32_t nb_value);
148158
};
149159

160+
/*!
161+
* \brief Set default parameters to PETSc.
162+
*
163+
* This function will call PetscInitialized() if it still does
164+
* not had been called.
165+
*
166+
* It will then deduct the default mat_type and vec_type.
167+
*
168+
* Finally, the function will set default parameters to PETSc
169+
* only if the user did not already set this parameter via
170+
* the -A,petsc_flags option.
171+
*/
172+
150173
void PETScDoFLinearSystemImpl::_handleParameters(IParallelMng* pm)
151174
{
152175
PetscBool is_initialized;
@@ -199,20 +222,26 @@ void PETScDoFLinearSystemImpl::_handleParameters(IParallelMng* pm)
199222
}
200223

201224
PetscBool set;
202-
PETSC_OPTION(Real, "-ksp_rtol", m_ksp_rtol);
203-
PETSC_OPTION(Real, "-ksp_atol", m_ksp_atol);
204-
PETSC_OPTION(Int, "-ksp_max_it", m_ksp_max_it);
205-
PETSC_OPTION_STRING("-ksp_type", m_ksp_type);
206-
PETSC_OPTION_STRING("-pc_type", m_pc_type);
207-
PETSC_OPTION_STRING("-mat_type", m_mat_type);
208-
PETSC_OPTION_STRING("-vec_type", m_vec_type);
225+
PETSC_OPTION(Real, "-ksp_rtol", m_ksp_rtol)
226+
PETSC_OPTION(Real, "-ksp_atol", m_ksp_atol)
227+
PETSC_OPTION(Int, "-ksp_max_it", m_ksp_max_it)
228+
PETSC_OPTION_STRING("-ksp_type", m_ksp_type)
229+
PETSC_OPTION_STRING("-pc_type", m_pc_type)
230+
PETSC_OPTION_STRING("-mat_type", m_mat_type)
231+
PETSC_OPTION_STRING("-vec_type", m_vec_type)
209232

210233
info() << "[PETSc-Info] Using " << std::string(m_mat_type.c_str()) << " matrix type";
211234
info() << "[PETSc-Info] Using " << std::string(m_vec_type.c_str()) << " vector type";
212235
}
213236

237+
/*!
238+
* \brief Compute global numeration of the matrix.
239+
*
240+
* Each rank owns consecutive rows of the matrix in increasing order.
241+
*/
242+
214243
void PETScDoFLinearSystemImpl::
215-
_computeMatrixNumeration(MPI_Comm mpi_comm)
244+
_computeMatrixNumeration()
216245
{
217246
// TODO use ISLocalToGlobalMappingCreate PETSc struct
218247
IItemFamily* dof_family = dofFamily();
@@ -255,6 +284,10 @@ _computeMatrixNumeration(MPI_Comm mpi_comm)
255284
m_rhs_work_values.resize(m_nb_own_row);
256285
}
257286

287+
/*!
288+
* \brief Convert CSR format rows into COO format rows
289+
*/
290+
258291
UniqueArray<PetscInt> PETScDoFLinearSystemImpl::_CSRToCOO(const Span<const int32_t> csr_rows, int32_t nb_value)
259292
{
260293
UniqueArray<PetscInt> coo_rows;
@@ -273,6 +306,13 @@ UniqueArray<PetscInt> PETScDoFLinearSystemImpl::_CSRToCOO(const Span<const int32
273306
return coo_rows;
274307
}
275308

309+
/*!
310+
* \brief Allocate matrix related PETSc objects
311+
*
312+
* The function allocates the matrix in COO format,
313+
* the KSP solver and the IS map.
314+
*/
315+
276316
void PETScDoFLinearSystemImpl::_preallocateMatrix()
277317
{
278318
IItemFamily* dof_family = dofFamily();
@@ -318,6 +358,31 @@ void PETScDoFLinearSystemImpl::_preallocateMatrix()
318358
PetscCallAbort(mpi_comm, KSPSetFromOptions(m_petsc_solver_context));
319359
}
320360

361+
/*!
362+
* \brief Initialize the solving
363+
*
364+
* The initialization does several things:
365+
* - calls handleParameters()
366+
* - calls computeMatrixNumeration()
367+
* - handle PETSc objects preallocation
368+
*
369+
* For allocation of PETSc objects:
370+
*
371+
* If the matrix has a constant sparsity, it will preallocate
372+
* the matrix in this function and free it at the destruction
373+
* of this object. Otherwise, the matrix will be allocated and
374+
* freed at each call to the solve() function. The same applies
375+
* for the KSP object and the IS map.
376+
*
377+
* If the matrix has constant values, it will set the values
378+
* in the matrix in this function. Otherwise, the matrix values
379+
* will be set at each call to the solve() function.
380+
*
381+
* At the end of the function, we set the m_is_initialized
382+
* attribute to true to indicate that we do not need to
383+
* call this function again.
384+
*/
385+
321386
void PETScDoFLinearSystemImpl::_initSolve()
322387
{
323388
IItemFamily* dof_family = dofFamily();
@@ -331,7 +396,7 @@ void PETScDoFLinearSystemImpl::_initSolve()
331396
ARCANE_THROW(NotSupportedException, "Cannot have constant matrix values and variable matrix sparsity.");
332397

333398
_handleParameters(pm);
334-
_computeMatrixNumeration(mpi_comm);
399+
_computeMatrixNumeration();
335400

336401
if (isMatrixSparsityConstant())
337402
_preallocateMatrix();
@@ -346,6 +411,26 @@ void PETScDoFLinearSystemImpl::_initSolve()
346411
m_is_initialized = true;
347412
}
348413

414+
/*!
415+
* \brief Solve the linear system
416+
*
417+
* The function call _initSolve function on its first
418+
* time been called.
419+
*
420+
* The function calls _preallocateMatrix if necessary,
421+
* and allocates the vectors.
422+
*
423+
* It then calls KSPSolve from the PETSc library to
424+
* solve the linear system, and put the result in
425+
* the m_petsc_solution_vector attribute of the class.
426+
*
427+
*
428+
* Before every allocation / deallocation, the function
429+
* makes sure to check the constant sparsity and
430+
* constant values to avoid a double free error or a
431+
* use after free error.
432+
*/
433+
349434
void PETScDoFLinearSystemImpl::
350435
solve()
351436
{

0 commit comments

Comments
 (0)