DOLFINx 0.12.0.0
DOLFINx C++
Loading...
Searching...
No Matches
petsc.h
1// Copyright (C) 2004-2018 Johan Hoffman, Johan Jansson, Anders Logg and
2// Garth N. Wells
3//
4// This file is part of DOLFINx (https://www.fenicsproject.org)
5//
6// SPDX-License-Identifier: LGPL-3.0-or-later
7
8#pragma once
9
10#ifdef HAS_PETSC
11
12#include "Vector.h"
13#include <boost/lexical_cast.hpp>
14#include <functional>
15#include <optional>
16#include <petscksp.h>
17#include <petscmat.h>
18#include <petscoptions.h>
19#include <petscvec.h>
20#include <span>
21#include <string>
22#include <string_view>
23#include <vector>
24
25namespace dolfinx::common
26{
27class IndexMap;
28} // namespace dolfinx::common
29
30namespace dolfinx::la
31{
32class SparsityPattern;
33enum class Norm : std::int8_t;
34
36namespace petsc
37{
39void error(PetscErrorCode error_code, std::string_view filename,
40 std::string_view petsc_function);
41
49std::vector<Vec>
50create_vectors(MPI_Comm comm,
51 const std::vector<std::span<const PetscScalar>>& x);
52
58Vec create_vector(const common::IndexMap& map, int bs);
59
68Vec create_vector(MPI_Comm comm, std::array<std::int64_t, 2> range,
69 std::span<const std::int64_t> ghosts, int bs);
70
80Vec create_vector_wrap(const common::IndexMap& map, int bs,
81 std::span<const PetscScalar> x);
82
86template <class V>
88{
89 assert(x.index_map());
90 return create_vector_wrap(*x.index_map(), x.bs(), x.array());
91}
92
105std::vector<IS> create_index_sets(
106 const std::vector<
107 std::pair<std::reference_wrapper<const common::IndexMap>, int>>& maps);
108
110std::vector<std::vector<PetscScalar>> get_local_vectors(
111 const Vec x,
112 const std::vector<
113 std::pair<std::reference_wrapper<const common::IndexMap>, int>>& maps);
114
117 Vec x, const std::vector<std::span<const PetscScalar>>& x_b,
118 const std::vector<
119 std::pair<std::reference_wrapper<const common::IndexMap>, int>>& maps);
120
123Mat create_matrix(MPI_Comm comm, const SparsityPattern& sp,
124 std::optional<std::string_view> type = std::nullopt);
125
131MatNullSpace create_nullspace(MPI_Comm comm, std::span<const Vec> basis);
132
138namespace options
139{
141void set(std::string option);
142
144template <typename T>
145void set(std::string option, const T& value)
146{
147 if (option[0] != '-')
148 option = '-' + option;
149
150 PetscErrorCode ierr;
151 ierr = PetscOptionsSetValue(nullptr, option.c_str(),
152 boost::lexical_cast<std::string>(value).c_str());
153 if (ierr != 0)
154 petsc::error(ierr, __FILE__, "PetscOptionsSetValue");
155}
156
158void clear(std::string option);
159
161void clear();
162} // namespace options
163
170{
171public:
176 Vector(const common::IndexMap& map, int bs);
177
178 // Delete copy constructor to avoid accidental copying of 'heavy' data
179 Vector(const Vector& x) = delete;
180
182 Vector(Vector&& x) noexcept;
183
195 Vector(Vec x, bool inc_ref_count);
196
198 virtual ~Vector();
199
200 // Assignment operator (disabled)
201 Vector& operator=(const Vector& x) = delete;
202
204 Vector& operator=(Vector&& x) noexcept;
205
208 Vector copy() const;
209
211 std::int64_t size() const;
212
214 std::int32_t local_size() const;
215
217 std::array<std::int64_t, 2> local_range() const;
218
220 MPI_Comm comm() const;
221
223 void set_options_prefix(std::string_view options_prefix);
224
227 std::string get_options_prefix() const;
228
230 void set_from_options();
231
233 Vec vec() const;
234
235private:
236 // PETSc Vec pointer
237 Vec _x;
238};
239
243{
244public:
246 Operator(Mat A, bool inc_ref_count);
247
248 // Copy constructor (deleted)
249 Operator(const Operator& A) = delete;
250
252 Operator(Operator&& A) noexcept;
253
255 virtual ~Operator();
256
258 Operator& operator=(const Operator& A) = delete;
259
261 Operator& operator=(Operator&& A) noexcept;
262
265 std::array<std::int64_t, 2> size() const;
266
272 Vec create_vector(std::size_t dim) const;
273
275 Mat mat() const;
276
277protected:
278 // PETSc Mat pointer
279 Mat _matA;
280};
281
287class Matrix : public Operator
288{
289public:
294 static auto set_fn(Mat A, InsertMode mode)
295 {
296 return [A, mode, cache = std::vector<PetscInt>()](
297 std::span<const std::int32_t> rows,
298 std::span<const std::int32_t> cols,
299 std::span<const PetscScalar> vals) mutable -> int
300 {
301 PetscErrorCode ierr;
302#ifdef PETSC_USE_64BIT_INDICES
303 cache.resize(rows.size() + cols.size());
304 std::ranges::copy(rows, cache.begin());
305 std::ranges::copy(cols, std::next(cache.begin(), rows.size()));
306 const PetscInt* _rows = cache.data();
307 const PetscInt* _cols = cache.data() + rows.size();
308 ierr = MatSetValuesLocal(A, rows.size(), _rows, cols.size(), _cols,
309 vals.data(), mode);
310#else
311 ierr = MatSetValuesLocal(A, rows.size(), rows.data(), cols.size(),
312 cols.data(), vals.data(), mode);
313#endif
314
315#ifndef NDEBUG
316 if (ierr != 0)
317 petsc::error(ierr, __FILE__, "MatSetValuesLocal");
318#endif
319 return ierr;
320 };
321 }
322
328 static auto set_block_fn(Mat A, InsertMode mode)
329 {
330 return [A, mode, cache = std::vector<PetscInt>()](
331 std::span<const std::int32_t> rows,
332 std::span<const std::int32_t> cols,
333 std::span<const PetscScalar> vals) mutable -> int
334 {
335 PetscErrorCode ierr;
336#ifdef PETSC_USE_64BIT_INDICES
337 cache.resize(rows.size() + cols.size());
338 std::ranges::copy(rows, cache.begin());
339 std::ranges::copy(cols, std::next(cache.begin(), rows.size()));
340 const PetscInt* _rows = cache.data();
341 const PetscInt* _cols = cache.data() + rows.size();
342 ierr = MatSetValuesBlockedLocal(A, rows.size(), _rows, cols.size(), _cols,
343 vals.data(), mode);
344#else
345 ierr = MatSetValuesBlockedLocal(A, rows.size(), rows.data(), cols.size(),
346 cols.data(), vals.data(), mode);
347#endif
348
349#ifndef NDEBUG
350 if (ierr != 0)
351 petsc::error(ierr, __FILE__, "MatSetValuesBlockedLocal");
352#endif
353 return ierr;
354 };
355 }
356
365 static auto set_block_expand_fn(Mat A, int bs0, int bs1, InsertMode mode)
366 {
367 return [A, bs0, bs1, mode, cache0 = std::vector<PetscInt>(),
368 cache1 = std::vector<PetscInt>()](
369 std::span<const std::int32_t> rows,
370 std::span<const std::int32_t> cols,
371 std::span<const PetscScalar> vals) mutable -> int
372 {
373 PetscErrorCode ierr;
374 cache0.resize(bs0 * rows.size());
375 cache1.resize(bs1 * cols.size());
376 for (std::size_t i = 0; i < rows.size(); ++i)
377 for (int k = 0; k < bs0; ++k)
378 cache0[bs0 * i + k] = bs0 * rows[i] + k;
379
380 for (std::size_t i = 0; i < cols.size(); ++i)
381 for (int k = 0; k < bs1; ++k)
382 cache1[bs1 * i + k] = bs1 * cols[i] + k;
383
384 ierr = MatSetValuesLocal(A, cache0.size(), cache0.data(), cache1.size(),
385 cache1.data(), vals.data(), mode);
386#ifndef NDEBUG
387 if (ierr != 0)
388 petsc::error(ierr, __FILE__, "MatSetValuesLocal");
389#endif
390 return ierr;
391 };
392 }
393
395 Matrix(MPI_Comm comm, const SparsityPattern& sp,
396 std::optional<std::string_view> type = std::nullopt);
397
402 Matrix(Mat A, bool inc_ref_count);
403
404 // Copy constructor (deleted)
405 Matrix(const Matrix& A) = delete;
406
408 Matrix(Matrix&& A) = default;
409
411 ~Matrix() = default;
412
414 Matrix& operator=(const Matrix& A) = delete;
415
417 Matrix& operator=(Matrix&& A) = default;
418
422 enum class AssemblyType : std::int8_t
423 {
424 FINAL,
425 FLUSH
426 };
427
433 void apply(AssemblyType type);
434
436 double norm(Norm norm_type) const;
437
438 //--- Special PETSc Functions ---
439
442 void set_options_prefix(std::string_view options_prefix);
443
446 std::string get_options_prefix() const;
447
449 void set_from_options();
450};
451
455{
456public:
459 explicit KrylovSolver(MPI_Comm comm);
460
464 KrylovSolver(KSP ksp, bool inc_ref_count);
465
466 // Copy constructor (deleted)
467 KrylovSolver(const KrylovSolver& solver) = delete;
468
470 KrylovSolver(KrylovSolver&& solver) noexcept;
471
474
475 // Assignment operator (deleted)
476 KrylovSolver& operator=(const KrylovSolver&) = delete;
477
479 KrylovSolver& operator=(KrylovSolver&& solver) noexcept;
480
482 void set_operator(const Mat A);
483
485 void set_operators(const Mat A, const Mat P);
486
489 int solve(Vec x, const Vec b, bool transpose = false) const;
490
493 void set_options_prefix(std::string_view options_prefix);
494
497 std::string get_options_prefix() const;
498
500 void set_from_options() const;
501
503 KSP ksp() const;
504
505private:
506 // PETSc solver pointer
507 KSP _ksp;
508};
509} // namespace petsc
510} // namespace dolfinx::la
511
512#endif
Definition IndexMap.h:96
Definition SparsityPattern.h:26
A vector that can be distributed across processes.
Definition Vector.h:50
container_type & array()
Get the process-local part of the vector.
Definition Vector.h:390
constexpr int bs() const
Get block size.
Definition Vector.h:385
std::shared_ptr< const common::IndexMap > index_map() const
Get IndexMap.
Definition Vector.h:382
void set_operator(const Mat A)
Set operator (Mat).
Definition petsc.cpp:680
KSP ksp() const
Return PETSc KSP pointer.
Definition petsc.cpp:797
std::string get_options_prefix() const
Definition petsc.cpp:779
void set_options_prefix(std::string_view options_prefix)
Definition petsc.cpp:769
void set_operators(const Mat A, const Mat P)
Set operator and preconditioner matrix (Mat).
Definition petsc.cpp:682
KrylovSolver(MPI_Comm comm)
Definition petsc.cpp:642
void set_from_options() const
Set options from PETSc options database.
Definition petsc.cpp:789
~KrylovSolver()
Destructor.
Definition petsc.cpp:667
int solve(Vec x, const Vec b, bool transpose=false) const
Definition petsc.cpp:691
Definition petsc.h:288
static auto set_block_fn(Mat A, InsertMode mode)
Definition petsc.h:328
void set_from_options()
Call PETSc function MatSetFromOptions on the PETSc Mat object.
Definition petsc.cpp:635
double norm(Norm norm_type) const
Return norm of matrix.
Definition petsc.cpp:579
static auto set_fn(Mat A, InsertMode mode)
Definition petsc.h:294
Matrix(Matrix &&A)=default
Move constructor (falls through to base class move constructor).
std::string get_options_prefix() const
Definition petsc.cpp:627
Matrix(MPI_Comm comm, const SparsityPattern &sp, std::optional< std::string_view > type=std::nullopt)
Create holder for a PETSc Mat object from a sparsity pattern.
Definition petsc.cpp:567
Matrix & operator=(Matrix &&A)=default
Move assignment operator.
static auto set_block_expand_fn(Mat A, int bs0, int bs1, InsertMode mode)
Definition petsc.h:365
~Matrix()=default
Destructor.
void set_options_prefix(std::string_view options_prefix)
Definition petsc.cpp:621
AssemblyType
Definition petsc.h:423
Matrix & operator=(const Matrix &A)=delete
Assignment operator (deleted).
void apply(AssemblyType type)
Definition petsc.cpp:604
std::array< std::int64_t, 2 > size() const
Definition petsc.cpp:525
Operator & operator=(const Operator &A)=delete
Assignment operator (deleted).
Operator(Mat A, bool inc_ref_count)
Constructor.
Definition petsc.cpp:499
Vec create_vector(std::size_t dim) const
Definition petsc.cpp:535
Mat mat() const
Return PETSc Mat pointer.
Definition petsc.cpp:564
virtual ~Operator()
Destructor.
Definition petsc.cpp:511
void set_from_options()
Call PETSc function VecSetFromOptions on the underlying Vec object.
Definition petsc.cpp:489
std::int64_t size() const
Return global size of the vector.
Definition petsc.cpp:435
std::string get_options_prefix() const
Definition petsc.cpp:480
Vector copy() const
Definition petsc.cpp:425
void set_options_prefix(std::string_view options_prefix)
Sets the prefix used by PETSc when searching the options database.
Definition petsc.cpp:472
virtual ~Vector()
Destructor.
Definition petsc.cpp:413
Vector(const common::IndexMap &map, int bs)
Definition petsc.cpp:398
MPI_Comm comm() const
Return MPI communicator.
Definition petsc.cpp:463
std::array< std::int64_t, 2 > local_range() const
Return ownership range for calling rank.
Definition petsc.cpp:453
std::int32_t local_size() const
Return local size of vector (belonging to the call rank).
Definition petsc.cpp:444
Vec vec() const
Return pointer to PETSc Vec object.
Definition petsc.cpp:496
Miscellaneous classes, functions and types.
Definition dolfinx_common.h:8
Definition petsc.h:139
void clear()
Clear PETSc global options database.
Definition petsc.cpp:390
void set(std::string option)
Set PETSc option that takes no value.
Definition petsc.cpp:374
PETSc linear algebra functions.
Definition petsc.h:37
Vec create_vector_wrap(const common::IndexMap &map, int bs, std::span< const PetscScalar > x)
Definition petsc.cpp:104
MatNullSpace create_nullspace(MPI_Comm comm, std::span< const Vec > basis)
Definition petsc.cpp:362
void scatter_local_vectors(Vec x, const std::vector< std::span< const PetscScalar > > &x_b, const std::vector< std::pair< std::reference_wrapper< const common::IndexMap >, int > > &maps)
Scatter local vectors to Vec.
Definition petsc.cpp:194
std::vector< IS > create_index_sets(const std::vector< std::pair< std::reference_wrapper< const common::IndexMap >, int > > &maps)
Compute PETSc IndexSets (IS) for a stack of index maps.
Definition petsc.cpp:131
std::vector< Vec > create_vectors(MPI_Comm comm, const std::vector< std::span< const PetscScalar > > &x)
Definition petsc.cpp:54
Mat create_matrix(MPI_Comm comm, const SparsityPattern &sp, std::optional< std::string_view > type=std::nullopt)
Definition petsc.cpp:235
std::vector< std::vector< PetscScalar > > get_local_vectors(const Vec x, const std::vector< std::pair< std::reference_wrapper< const common::IndexMap >, int > > &maps)
Copy blocks from Vec into local arrays.
Definition petsc.cpp:151
void error(PetscErrorCode error_code, std::string_view filename, std::string_view petsc_function)
Print error message for PETSc calls that return an error.
Definition petsc.cpp:36
Vec create_vector(const common::IndexMap &map, int bs)
Definition petsc.cpp:70
Linear algebra interface.
Definition dolfinx_la.h:7
dolfinx::la::MatrixCSR< T > transpose(const dolfinx::la::MatrixCSR< T > &A)
Compute the distributed transpose of a MatrixCSR.
Definition mattrans.h:128
Norm
Norm types.
Definition utils.h:17