ExaDG
Loading...
Searching...
No Matches
linear_algebra_utilities.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_UTILITIES_LINEAR_ALGEBRA_UTILITIES_H_
23#define EXADG_SOLVERS_AND_PRECONDITIONERS_UTILITIES_LINEAR_ALGEBRA_UTILITIES_H_
24
25// deal.II
26#include <deal.II/base/exceptions.h>
27#include <deal.II/lac/la_parallel_vector.h>
28#include <deal.II/lac/petsc_vector.h>
29
30namespace ExaDG
31{
32#ifdef DEAL_II_WITH_PETSC
33/*
34 * This function wraps the copy of a PETSc object (sparse matrix,
35 * preconditioner) with a dealii::LinearAlgebra::distributed::Vector, taking
36 * pre-allocated PETSc vector objects (with struct name `Vec`) for the temporary operations
37 */
38template<typename VectorType>
39void
40apply_petsc_operation(
41 VectorType & dst,
42 VectorType const & src,
43 Vec & petsc_vector_dst,
44 Vec & petsc_vector_src,
45 std::function<void(dealii::PETScWrappers::VectorBase &,
46 dealii::PETScWrappers::VectorBase const &)> petsc_operation)
47{
48 {
49 // copy to PETSc internal vector type because there is currently no such
50 // function in deal.II (and the transition via ReadWriteVector is too
51 // slow/poorly tested)
52 PetscInt begin, end;
53 PetscErrorCode ierr = VecGetOwnershipRange(petsc_vector_src, &begin, &end);
54 AssertThrow(ierr == 0, dealii::ExcPETScError(ierr));
55
56 PetscScalar * ptr;
57 ierr = VecGetArray(petsc_vector_src, &ptr);
58 AssertThrow(ierr == 0, dealii::ExcPETScError(ierr));
59
60 const PetscInt local_size = src.get_partitioner()->locally_owned_size();
61 AssertDimension(local_size, static_cast<unsigned int>(end - begin));
62 for(PetscInt i = 0; i < local_size; ++i)
63 {
64 ptr[i] = src.local_element(i);
65 }
66
67 ierr = VecRestoreArray(petsc_vector_src, &ptr);
68 AssertThrow(ierr == 0, dealii::ExcPETScError(ierr));
69 }
70
71 // wrap `Vec` into VectorBase (without copying data)
72 dealii::PETScWrappers::VectorBase petsc_dst(petsc_vector_dst);
73 dealii::PETScWrappers::VectorBase petsc_src(petsc_vector_src);
74
75 petsc_operation(petsc_dst, petsc_src);
76
77 {
78 PetscInt begin, end;
79 PetscErrorCode ierr = VecGetOwnershipRange(petsc_vector_dst, &begin, &end);
80 AssertThrow(ierr == 0, dealii::ExcPETScError(ierr));
81
82 PetscScalar * ptr;
83 ierr = VecGetArray(petsc_vector_dst, &ptr);
84 AssertThrow(ierr == 0, dealii::ExcPETScError(ierr));
85
86 const PetscInt local_size = dst.get_partitioner()->locally_owned_size();
87 AssertDimension(local_size, static_cast<unsigned int>(end - begin));
88
89 for(PetscInt i = 0; i < local_size; ++i)
90 {
91 dst.local_element(i) = ptr[i];
92 }
93
94 ierr = VecRestoreArray(petsc_vector_dst, &ptr);
95 AssertThrow(ierr == 0, dealii::ExcPETScError(ierr));
96 }
97}
98
99/*
100 * This function wraps the copy of a PETSc object (sparse matrix,
101 * preconditioner) with a dealii::LinearAlgebra::distributed::Vector,
102 * allocating PETSc vectors and then calling the other function
103 */
104template<typename VectorType>
105void
106apply_petsc_operation(
107 VectorType & dst,
108 VectorType const & src,
109 MPI_Comm const & petsc_mpi_communicator,
110 std::function<void(dealii::PETScWrappers::VectorBase &,
111 dealii::PETScWrappers::VectorBase const &)> petsc_operation)
112{
113 Vec petsc_vector_dst, petsc_vector_src;
114 VecCreateMPI(petsc_mpi_communicator,
115 dst.get_partitioner()->locally_owned_size(),
116 PETSC_DETERMINE,
117 &petsc_vector_dst);
118 VecCreateMPI(petsc_mpi_communicator,
119 src.get_partitioner()->locally_owned_size(),
120 PETSC_DETERMINE,
121 &petsc_vector_src);
122
123 apply_petsc_operation(dst, src, petsc_vector_dst, petsc_vector_src, petsc_operation);
124
125 PetscErrorCode ierr = VecDestroy(&petsc_vector_dst);
126 AssertThrow(ierr == 0, dealii::ExcPETScError(ierr));
127 ierr = VecDestroy(&petsc_vector_src);
128 AssertThrow(ierr == 0, dealii::ExcPETScError(ierr));
129}
130#endif
131
132
139template<typename Number>
140void
142 dealii::LinearAlgebra::distributed::Vector<Number> & dst,
143 dealii::LinearAlgebra::distributed::Vector<Number> const & src,
144 std::function<void(dealii::LinearAlgebra::distributed::Vector<double> &,
145 dealii::LinearAlgebra::distributed::Vector<double> const &)> operation)
146{
147 if constexpr(std::is_same_v<Number, double>)
148 {
149 operation(dst, src);
150 }
151 else
152 {
153 // create temporal vectors of type double
154 dealii::LinearAlgebra::distributed::Vector<double> dst_double, src_double;
155 dst_double.reinit(dst, true); // do not zero entries
156 src_double.reinit(src, true); // do not zero entries
157 dst_double.copy_locally_owned_data_from(dst);
158 src_double.copy_locally_owned_data_from(src);
159
160 operation(dst_double, src_double);
161
162 // convert: double -> Number
163 dst.copy_locally_owned_data_from(dst_double);
164 }
165}
166
167} // namespace ExaDG
168
169#endif /* EXADG_SOLVERS_AND_PRECONDITIONERS_UTILITIES_LINEAR_ALGEBRA_UTILITIES_H_ */
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