ExaDG
Loading...
Searching...
No Matches
petsc_operation.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 INCLUDE_SOLVERS_AND_PRECONDITIONERS_PETSCOPERATION_H_
23#define INCLUDE_SOLVERS_AND_PRECONDITIONERS_PETSCOPERATION_H_
24
25#include <deal.II/base/exceptions.h>
26#include <deal.II/lac/petsc_vector.h>
27
28namespace ExaDG
29{
30#ifdef DEAL_II_WITH_PETSC
31/*
32 * A typedef to make use of PETSc data structure clearer
33 */
34typedef Vec VectorTypePETSc;
35
36/*
37 * This function wraps the copy of a PETSc object (sparse matrix,
38 * preconditioner) with a dealii::LinearAlgebra::distributed::Vector, taking
39 * pre-allocated PETSc vector objects (with struct name `Vec`, aka
40 * VectorTypePETSc) for the temporary operations
41 */
42template<typename VectorType>
43void
44apply_petsc_operation(
45 VectorType & dst,
46 VectorType const & src,
47 VectorTypePETSc & petsc_vector_dst,
48 VectorTypePETSc & petsc_vector_src,
49 std::function<void(dealii::PETScWrappers::VectorBase &,
50 dealii::PETScWrappers::VectorBase const &)> petsc_operation)
51{
52 {
53 // copy to PETSc internal vector type because there is currently no such
54 // function in deal.II (and the transition via ReadWriteVector is too
55 // slow/poorly tested)
56 PetscInt begin, end;
57 PetscErrorCode ierr = VecGetOwnershipRange(petsc_vector_src, &begin, &end);
58 AssertThrow(ierr == 0, dealii::ExcPETScError(ierr));
59
60 PetscScalar * ptr;
61 ierr = VecGetArray(petsc_vector_src, &ptr);
62 AssertThrow(ierr == 0, dealii::ExcPETScError(ierr));
63
64 const PetscInt local_size = src.get_partitioner()->locally_owned_size();
65 AssertDimension(local_size, static_cast<unsigned int>(end - begin));
66 for(PetscInt i = 0; i < local_size; ++i)
67 {
68 ptr[i] = src.local_element(i);
69 }
70
71 ierr = VecRestoreArray(petsc_vector_src, &ptr);
72 AssertThrow(ierr == 0, dealii::ExcPETScError(ierr));
73 }
74
75 // wrap `Vec` (aka VectorTypePETSc) into VectorBase (without copying data)
76 dealii::PETScWrappers::VectorBase petsc_dst(petsc_vector_dst);
77 dealii::PETScWrappers::VectorBase petsc_src(petsc_vector_src);
78
79 petsc_operation(petsc_dst, petsc_src);
80
81 {
82 PetscInt begin, end;
83 PetscErrorCode ierr = VecGetOwnershipRange(petsc_vector_dst, &begin, &end);
84 AssertThrow(ierr == 0, dealii::ExcPETScError(ierr));
85
86 PetscScalar * ptr;
87 ierr = VecGetArray(petsc_vector_dst, &ptr);
88 AssertThrow(ierr == 0, dealii::ExcPETScError(ierr));
89
90 const PetscInt local_size = dst.get_partitioner()->locally_owned_size();
91 AssertDimension(local_size, static_cast<unsigned int>(end - begin));
92
93 for(PetscInt i = 0; i < local_size; ++i)
94 {
95 dst.local_element(i) = ptr[i];
96 }
97
98 ierr = VecRestoreArray(petsc_vector_dst, &ptr);
99 AssertThrow(ierr == 0, dealii::ExcPETScError(ierr));
100 }
101}
102
103/*
104 * This function wraps the copy of a PETSc object (sparse matrix,
105 * preconditioner) with a dealii::LinearAlgebra::distributed::Vector,
106 * allocating a PETSc vectors and then calling the other function
107 */
108template<typename VectorType>
109void
110apply_petsc_operation(
111 VectorType & dst,
112 VectorType const & src,
113 MPI_Comm const & petsc_mpi_communicator,
114 std::function<void(dealii::PETScWrappers::VectorBase &,
115 dealii::PETScWrappers::VectorBase const &)> petsc_operation)
116{
117 VectorTypePETSc petsc_vector_dst, petsc_vector_src;
118 VecCreateMPI(petsc_mpi_communicator,
119 dst.get_partitioner()->locally_owned_size(),
120 PETSC_DETERMINE,
121 &petsc_vector_dst);
122 VecCreateMPI(petsc_mpi_communicator,
123 src.get_partitioner()->locally_owned_size(),
124 PETSC_DETERMINE,
125 &petsc_vector_src);
126
127 apply_petsc_operation(dst, src, petsc_vector_dst, petsc_vector_src, petsc_operation);
128
129 PetscErrorCode ierr = VecDestroy(&petsc_vector_dst);
130 AssertThrow(ierr == 0, dealii::ExcPETScError(ierr));
131 ierr = VecDestroy(&petsc_vector_src);
132 AssertThrow(ierr == 0, dealii::ExcPETScError(ierr));
133}
134#endif
135
136} // namespace ExaDG
137
138#endif /* INCLUDE_SOLVERS_AND_PRECONDITIONERS_INVERTDIAGONAL_H_ */
Definition driver.cpp:33