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