ExaDG
Loading...
Searching...
No Matches
preconditioner_amg.h
1/* ______________________________________________________________________
2 *
3 * ExaDG - High-Order Discontinuous Galerkin for the Exa-Scale
4 *
5 * Copyright (C) 2021 by the ExaDG authors
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 * ______________________________________________________________________
20 */
21
22#ifndef EXADG_SOLVERS_AND_PRECONDITIONERS_PRECONDITIONERS_PRECONDITIONER_AMG_H_
23#define EXADG_SOLVERS_AND_PRECONDITIONERS_PRECONDITIONERS_PRECONDITIONER_AMG_H_
24
25// deal.II
26#include <deal.II/lac/la_parallel_vector.h>
27#include <deal.II/lac/petsc_precondition.h>
28#include <deal.II/lac/petsc_solver.h>
29#include <deal.II/lac/petsc_sparse_matrix.h>
30#include <deal.II/lac/trilinos_precondition.h>
31#include <deal.II/lac/trilinos_sparse_matrix.h>
32
33// Trilinos
34#ifdef DEAL_II_WITH_TRILINOS
35# include <ml_MultiLevelPreconditioner.h>
36#endif
37
38// ExaDG
39#include <exadg/solvers_and_preconditioners/multigrid/multigrid_parameters.h>
40#include <exadg/solvers_and_preconditioners/preconditioners/preconditioner_base.h>
41#include <exadg/solvers_and_preconditioners/solvers/iterative_solvers_dealii_wrapper.h>
42#include <exadg/solvers_and_preconditioners/utilities/linear_algebra_utilities.h>
43#include <exadg/utilities/print_functions.h>
44
45namespace ExaDG
46{
47template<int dim, int spacedim>
48std::unique_ptr<MPI_Comm, void (*)(MPI_Comm *)>
49create_subcommunicator(dealii::DoFHandler<dim, spacedim> const & dof_handler)
50{
51 unsigned int n_locally_owned_cells = 0;
52 for(auto const & cell : dof_handler.active_cell_iterators())
53 if(cell->is_locally_owned())
54 ++n_locally_owned_cells;
55
56 MPI_Comm const mpi_comm = dof_handler.get_mpi_communicator();
57
58 // In case some of the MPI ranks do not have cells, we create a
59 // sub-communicator to exclude all those processes from the MPI
60 // communication in the matrix-based operations and hence speed up those
61 // operations. Note that we have to free the communicator again, which is
62 // done by a custom deleter of the unique pointer that is run when it goes
63 // out of scope.
64 if(dealii::Utilities::MPI::min(n_locally_owned_cells, mpi_comm) == 0)
65 {
66 std::unique_ptr<MPI_Comm, void (*)(MPI_Comm *)> subcommunicator(new MPI_Comm,
67 [](MPI_Comm * comm) {
68 MPI_Comm_free(comm);
69 delete comm;
70 });
71 MPI_Comm_split(mpi_comm,
72 n_locally_owned_cells > 0,
73 dealii::Utilities::MPI::this_mpi_process(mpi_comm),
74 subcommunicator.get());
75
76 return subcommunicator;
77 }
78 else
79 {
80 std::unique_ptr<MPI_Comm, void (*)(MPI_Comm *)> communicator(new MPI_Comm, [](MPI_Comm * comm) {
81 delete comm;
82 });
83 *communicator = mpi_comm;
84
85 return communicator;
86 }
87}
88
89#ifdef DEAL_II_WITH_TRILINOS
90inline Teuchos::ParameterList
91get_ML_parameter_list(dealii::TrilinosWrappers::PreconditionAMG::AdditionalData const & ml_data,
92 int const dimension)
93{
94 Teuchos::ParameterList parameter_list;
95
96 // Slightly modified from deal::PreconditionAMG::AdditionalData::set_parameters().
97 if(ml_data.elliptic == true)
98 {
99 ML_Epetra::SetDefaults("SA", parameter_list);
100 }
101 else
102 {
103 ML_Epetra::SetDefaults("NSSA", parameter_list);
104 parameter_list.set("aggregation: block scaling", true);
105 }
106 parameter_list.set("aggregation: type", "Uncoupled");
107 parameter_list.set("smoother: type", ml_data.smoother_type);
108 parameter_list.set("coarse: type", ml_data.coarse_type);
109
110// Force re-initialization of the random seed to make ML deterministic
111// (only supported in trilinos >12.2):
112# if DEAL_II_TRILINOS_VERSION_GTE(12, 4, 0)
113 parameter_list.set("initialize random seed", true);
114# endif
115
116 parameter_list.set("smoother: sweeps", static_cast<int>(ml_data.smoother_sweeps));
117 parameter_list.set("cycle applications", static_cast<int>(ml_data.n_cycles));
118 if(ml_data.w_cycle == true)
119 {
120 parameter_list.set("prec type", "MGW");
121 }
122 else
123 {
124 parameter_list.set("prec type", "MGV");
125 }
126
127 parameter_list.set("smoother: Chebyshev alpha", 10.);
128 parameter_list.set("smoother: ifpack overlap", static_cast<int>(ml_data.smoother_overlap));
129 parameter_list.set("aggregation: threshold", ml_data.aggregation_threshold);
130
131 // Minimum size of the coarse problem, i.e. no coarser problems
132 // smaller than `coarse: max size` are constructed.
133 parameter_list.set("coarse: max size", 2000);
134
135 // This extends the settings in deal::PreconditionAMG::AdditionalData::set_parameters().
136 parameter_list.set("repartition: enable", 1);
137 parameter_list.set("repartition: max min ratio", 1.3);
138 parameter_list.set("repartition: min per proc", 300);
139 parameter_list.set("repartition: partitioner", "Zoltan");
140 parameter_list.set("repartition: Zoltan dimensions", dimension);
141
142 if(ml_data.output_details)
143 {
144 parameter_list.set("ML output", 10);
145 }
146 else
147 {
148 parameter_list.set("ML output", 0);
149 }
150
151 return parameter_list;
152}
153
154template<typename Operator>
155class PreconditionerML : public PreconditionerBase<double>
156{
157private:
158 typedef dealii::LinearAlgebra::distributed::Vector<double> VectorType;
159
160 typedef dealii::TrilinosWrappers::PreconditionAMG::AdditionalData MLData;
161
162public:
163 // distributed sparse system matrix
164 dealii::TrilinosWrappers::SparseMatrix system_matrix;
165
166private:
167 dealii::TrilinosWrappers::PreconditionAMG amg;
168
169public:
170 PreconditionerML(Operator const & op, bool const initialize, MLData ml_data = MLData())
171 : pde_operator(op), ml_data(ml_data)
172 {
173 // initialize system matrix
174 pde_operator.init_system_matrix(system_matrix,
175 op.get_matrix_free().get_dof_handler().get_mpi_communicator());
176
177 if(initialize)
178 {
179 this->update();
180 }
181 }
182
183 void
184 vmult(VectorType & dst, VectorType const & src) const override
185 {
186 amg.vmult(dst, src);
187 }
188
189 void
190 apply_krylov_solver_with_amg_preconditioner(VectorType & dst,
191 VectorType const & src,
192 MultigridCoarseGridSolver const & solver_type,
193 SolverData const & solver_data) const
194 {
195 dealii::ReductionControl solver_control(solver_data.max_iter,
196 solver_data.abs_tol,
197 solver_data.rel_tol);
198
199 if(solver_type == MultigridCoarseGridSolver::CG)
200 {
201 dealii::SolverCG<VectorType> solver(solver_control);
202 solver.solve(system_matrix, dst, src, *this);
203 }
204 else if(solver_type == MultigridCoarseGridSolver::GMRES)
205 {
206 typename dealii::SolverGMRES<VectorType>::AdditionalData gmres_data;
207 gmres_data.max_n_tmp_vectors = solver_data.max_krylov_size;
208 gmres_data.right_preconditioning = true;
209
210 dealii::SolverGMRES<VectorType> solver(solver_control, gmres_data);
211 solver.solve(system_matrix, dst, src, *this);
212 }
213 else
214 {
215 AssertThrow(false, dealii::ExcMessage("Not implemented."));
216 }
217 }
218
219 void
220 update() override
221 {
222 // Clear content of matrix since calculate_system_matrix() adds the result.
223 system_matrix *= 0.0;
224
225 // Re-calculate the system matrix.
226 pde_operator.calculate_system_matrix(system_matrix);
227
228 // Construct AMG preconditioner based on `Teuchos::ParameterList`.
229 unsigned int const dimension = pde_operator.get_matrix_free().dimension;
230 Teuchos::ParameterList parameter_list = get_ML_parameter_list(ml_data, dimension);
231
232 // Add near null space basis vectors to `Teuchos::ParameterList`.
233 // If the `std::vector<std::vector<double>> constant_modes_values`
234 // were filled, use these, otherwise use `std::vector<std::vector<bool>> constant_modes`.
235 std::vector<std::vector<bool>> constant_modes;
236 std::vector<std::vector<double>> constant_modes_values;
237 pde_operator.get_constant_modes(constant_modes, constant_modes_values);
238 MPI_Comm const & mpi_comm = system_matrix.get_mpi_communicator();
239 bool const some_constant_mode_set =
240 dealii::Utilities::MPI::logical_or(constant_modes.size() > 0, mpi_comm);
241 if(not some_constant_mode_set)
242 {
243 bool const some_constant_mode_values_set =
244 dealii::Utilities::MPI::logical_or(constant_modes_values.size() > 0, mpi_comm);
245 AssertThrow(some_constant_mode_values_set > 0,
246 dealii::ExcMessage(
247 "Neither `constant_modes` nor `constant_modes_values` were provided. "
248 "AMG setup requires near null space basis vectors."));
249 ml_data.constant_modes_values = constant_modes_values;
250 }
251 else
252 {
253 ml_data.constant_modes = constant_modes;
254 }
255
256 // Add near null space basis vectors to Teuchos::ParameterList.
257 // `ptr_distributed_modes` must stay alive for amg.initialize()
258 std::unique_ptr<Epetra_MultiVector> ptr_operator_modes;
259 ml_data.set_operator_null_space(parameter_list,
260 ptr_operator_modes,
261 system_matrix.trilinos_matrix());
262
263 // Initialize with the `Teuchos::ParameterList`.
264 amg.initialize(system_matrix, parameter_list);
265
266 this->update_needed = false;
267 }
268
269private:
270 // reference to matrix-free operator
271 Operator const & pde_operator;
272
273 MLData ml_data;
274};
275#endif
276
277#ifdef DEAL_II_WITH_PETSC
278/*
279 * Wrapper class for BoomerAMG from Hypre
280 */
281template<typename Operator, typename Number>
282class PreconditionerBoomerAMG : public PreconditionerBase<Number>
283{
284private:
285 typedef dealii::LinearAlgebra::distributed::Vector<Number> VectorType;
286
287 typedef dealii::PETScWrappers::PreconditionBoomerAMG::AdditionalData BoomerData;
288
289 // subcommunicator; declared before the matrix to ensure that it gets
290 // deleted after the matrix and preconditioner depending on it
291 std::unique_ptr<MPI_Comm, void (*)(MPI_Comm *)> subcommunicator;
292
293public:
294 // distributed sparse system matrix
295 dealii::PETScWrappers::MPI::SparseMatrix system_matrix;
296
297 // amg preconditioner for access by PETSc solver
298 dealii::PETScWrappers::PreconditionBoomerAMG amg;
299
300 PreconditionerBoomerAMG(Operator const & op,
301 bool const initialize,
302 BoomerData boomer_data = BoomerData())
303 : subcommunicator(
304 create_subcommunicator(op.get_matrix_free().get_dof_handler(op.get_dof_index()))),
305 pde_operator(op),
306 boomer_data(boomer_data)
307 {
308 // initialize system matrix
309 pde_operator.init_system_matrix(system_matrix, *subcommunicator);
310
311 if(initialize)
312 {
313 this->update();
314 }
315 }
316
317 ~PreconditionerBoomerAMG()
318 {
319 if(system_matrix.m() > 0)
320 {
321 PetscErrorCode ierr = VecDestroy(&petsc_vector_dst);
322 AssertThrow(ierr == 0, dealii::ExcPETScError(ierr));
323 ierr = VecDestroy(&petsc_vector_src);
324 AssertThrow(ierr == 0, dealii::ExcPETScError(ierr));
325 }
326 }
327
328 void
329 vmult(VectorType & dst, VectorType const & src) const override
330 {
331 if(system_matrix.m() > 0)
332 apply_petsc_operation(dst,
333 src,
334 petsc_vector_dst,
335 petsc_vector_src,
336 [&](dealii::PETScWrappers::VectorBase & petsc_dst,
337 dealii::PETScWrappers::VectorBase const & petsc_src) {
338 amg.vmult(petsc_dst, petsc_src);
339 });
340 }
341
342 void
343 apply_krylov_solver_with_amg_preconditioner(VectorType & dst,
344 VectorType const & src,
345 MultigridCoarseGridSolver const & solver_type,
346 SolverData const & solver_data) const
347 {
348 apply_petsc_operation(dst,
349 src,
350 system_matrix.get_mpi_communicator(),
351 [&](dealii::PETScWrappers::VectorBase & petsc_dst,
352 dealii::PETScWrappers::VectorBase const & petsc_src) {
353 dealii::ReductionControl solver_control(solver_data.max_iter,
354 solver_data.abs_tol,
355 solver_data.rel_tol);
356
357 if(solver_type == MultigridCoarseGridSolver::CG)
358 {
359 dealii::PETScWrappers::SolverCG solver(solver_control);
360 solver.solve(system_matrix, petsc_dst, petsc_src, amg);
361 }
362 else if(solver_type == MultigridCoarseGridSolver::GMRES)
363 {
364 dealii::PETScWrappers::SolverGMRES solver(solver_control);
365 solver.solve(system_matrix, petsc_dst, petsc_src, amg);
366 }
367 else
368 {
369 AssertThrow(false, dealii::ExcMessage("Not implemented."));
370 }
371 });
372 }
373
374 void
375 update() override
376 {
377 // clear content of matrix since the next calculate_system_matrix calls
378 // add their result; since we might run this on a sub-communicator, we
379 // skip the processes that do not participate in the matrix and have size
380 // zero
381 if(system_matrix.m() > 0)
382 system_matrix = 0.0;
383
384 calculate_preconditioner();
385
386 this->update_needed = false;
387 }
388
389private:
390 void
391 calculate_preconditioner()
392 {
393 // calculate_matrix in case the current MPI rank participates in the PETSc communicator
394 if(system_matrix.m() > 0)
395 {
396 pde_operator.calculate_system_matrix(system_matrix);
397
398 amg.initialize(system_matrix, boomer_data);
399
400 // get vector partitioner
401 dealii::LinearAlgebra::distributed::Vector<typename Operator::value_type> vector;
402 pde_operator.initialize_dof_vector(vector);
403 VecCreateMPI(system_matrix.get_mpi_communicator(),
404 vector.get_partitioner()->locally_owned_size(),
405 PETSC_DETERMINE,
406 &petsc_vector_dst);
407 VecCreateMPI(system_matrix.get_mpi_communicator(),
408 vector.get_partitioner()->locally_owned_size(),
409 PETSC_DETERMINE,
410 &petsc_vector_src);
411 }
412 }
413
414 // reference to MultigridOperator
415 Operator const & pde_operator;
416
417 BoomerData boomer_data;
418
419 // PETSc vector objects to avoid re-allocation in every vmult() operation
420 mutable Vec petsc_vector_src;
421 mutable Vec petsc_vector_dst;
422};
423#endif
424
428template<typename Operator, typename Number>
429class PreconditionerAMG : public PreconditionerBase<Number>
430{
431private:
432 typedef typename PreconditionerBase<Number>::VectorType VectorType;
433
434public:
435 PreconditionerAMG(Operator const & pde_operator, bool const initialize, AMGData const & data)
436 {
437 (void)pde_operator;
438 (void)initialize;
439 this->data = data;
440
441 if(data.amg_type == AMGType::BoomerAMG)
442 {
443#ifdef DEAL_II_WITH_PETSC
444 preconditioner_boomer =
445 std::make_shared<PreconditionerBoomerAMG<Operator, Number>>(pde_operator,
446 initialize,
447 data.boomer_data);
448#else
449 AssertThrow(false, dealii::ExcMessage("deal.II is not compiled with PETSc!"));
450#endif
451 }
452 else if(data.amg_type == AMGType::ML)
453 {
454#ifdef DEAL_II_WITH_TRILINOS
455 preconditioner_ml =
456 std::make_shared<PreconditionerML<Operator>>(pde_operator, initialize, data.ml_data);
457#else
458 AssertThrow(false, dealii::ExcMessage("deal.II is not compiled with Trilinos!"));
459#endif
460 }
461 else
462 {
463 AssertThrow(false, dealii::ExcNotImplemented());
464 }
465 }
466
467 void
468 vmult(VectorType & dst, VectorType const & src) const final
469 {
470 if(data.amg_type == AMGType::BoomerAMG)
471 {
472#ifdef DEAL_II_WITH_PETSC
473 preconditioner_boomer->vmult(dst, src);
474#else
475 AssertThrow(false, dealii::ExcMessage("deal.II is not compiled with PETSc!"));
476#endif
477 }
478 else if(data.amg_type == AMGType::ML)
479 {
480#ifdef DEAL_II_WITH_TRILINOS
482 dst,
483 src,
484 [&](dealii::LinearAlgebra::distributed::Vector<double> & dst_double,
485 dealii::LinearAlgebra::distributed::Vector<double> const & src_double) {
486 preconditioner_ml->vmult(dst_double, src_double);
487 });
488#else
489 AssertThrow(false, dealii::ExcMessage("deal.II is not compiled with Trilinos!"));
490#endif
491 }
492 else
493 {
494 AssertThrow(false, dealii::ExcNotImplemented());
495 }
496 }
497
498 void
499 apply_krylov_solver_with_amg_preconditioner(VectorType & dst,
500 VectorType const & src,
501 MultigridCoarseGridSolver const & solver_type,
502 SolverData const & solver_data) const
503 {
504 if(data.amg_type == AMGType::BoomerAMG)
505 {
506#ifdef DEAL_II_WITH_PETSC
507 std::shared_ptr<PreconditionerBoomerAMG<Operator, Number>> preconditioner =
508 std::dynamic_pointer_cast<PreconditionerBoomerAMG<Operator, Number>>(preconditioner_boomer);
509
510 preconditioner->apply_krylov_solver_with_amg_preconditioner(dst,
511 src,
512 solver_type,
513 solver_data);
514#else
515 AssertThrow(false, dealii::ExcMessage("deal.II is not compiled with PETSc!"));
516#endif
517 }
518 else if(data.amg_type == AMGType::ML)
519 {
520#ifdef DEAL_II_WITH_TRILINOS
521 std::shared_ptr<PreconditionerML<Operator>> preconditioner =
522 std::dynamic_pointer_cast<PreconditionerML<Operator>>(preconditioner_ml);
523
525 dst,
526 src,
527 [&](dealii::LinearAlgebra::distributed::Vector<double> & dst_double,
528 dealii::LinearAlgebra::distributed::Vector<double> const & src_double) {
529 preconditioner->apply_krylov_solver_with_amg_preconditioner(dst_double,
530 src_double,
531 solver_type,
532 solver_data);
533 });
534#else
535 AssertThrow(false, dealii::ExcMessage("deal.II is not compiled with Trilinos!"));
536#endif
537 }
538 else
539 {
540 AssertThrow(false, dealii::ExcNotImplemented());
541 }
542 }
543
544 void
545 update() final
546 {
547 if(data.amg_type == AMGType::BoomerAMG)
548 {
549#ifdef DEAL_II_WITH_PETSC
550 preconditioner_boomer->update();
551#else
552 AssertThrow(false, dealii::ExcMessage("deal.II is not compiled with PETSc!"));
553#endif
554 }
555 else if(data.amg_type == AMGType::ML)
556 {
557#ifdef DEAL_II_WITH_TRILINOS
558 preconditioner_ml->update();
559#else
560 AssertThrow(false, dealii::ExcMessage("deal.II is not compiled with Trilinos!"));
561#endif
562 }
563 else
564 {
565 AssertThrow(false, dealii::ExcNotImplemented());
566 }
567
568 this->update_needed = false;
569 }
570
571private:
572 AMGData data;
573
574 std::shared_ptr<PreconditionerBase<Number>> preconditioner_boomer;
575
576 std::shared_ptr<PreconditionerBase<double>> preconditioner_ml;
577};
578
579} // namespace ExaDG
580
581#endif /* EXADG_SOLVERS_AND_PRECONDITIONERS_PRECONDITIONERS_PRECONDITIONER_AMG_H_ */
Definition preconditioner_base.h:35
Definition driver.cpp:33
void apply_function_in_double_precision(dealii::LinearAlgebra::distributed::Vector< Number > &dst, dealii::LinearAlgebra::distributed::Vector< Number > const &src, std::function< void(dealii::LinearAlgebra::distributed::Vector< double > &, dealii::LinearAlgebra::distributed::Vector< double > const &)> operation)
Definition linear_algebra_utilities.h:141
Definition multigrid_parameters.h:98
Definition solver_data.h:34