Note: this is documentation for an old release. View the latest documentation at docs.fenicsproject.org/dolfinx/v0.9.0/cpp/doxygen/dd/dd6/la_2petsc_8h_source.html
DOLFINx 0.8.0
DOLFINx C++ interface
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 "utils.h"
14#include <boost/lexical_cast.hpp>
15#include <functional>
16#include <petscksp.h>
17#include <petscmat.h>
18#include <petscoptions.h>
19#include <petscvec.h>
20#include <span>
21#include <string>
22#include <vector>
23
24namespace dolfinx::common
25{
26class IndexMap;
27} // namespace dolfinx::common
28
29namespace dolfinx::la
30{
31class SparsityPattern;
32
34namespace petsc
35{
37void error(int error_code, std::string filename, std::string petsc_function);
38
46std::vector<Vec>
47create_vectors(MPI_Comm comm,
48 const std::vector<std::span<const PetscScalar>>& x);
49
55Vec create_vector(const common::IndexMap& map, int bs);
56
65Vec create_vector(MPI_Comm comm, std::array<std::int64_t, 2> range,
66 std::span<const std::int64_t> ghosts, int bs);
67
77Vec create_vector_wrap(const common::IndexMap& map, int bs,
78 std::span<const PetscScalar> x);
79
83template <class V>
85{
86 assert(x.index_map());
87 return create_vector_wrap(*x.index_map(), x.bs(), x.array());
88}
89
101std::vector<IS> create_index_sets(
102 const std::vector<
103 std::pair<std::reference_wrapper<const common::IndexMap>, int>>& maps);
104
106std::vector<std::vector<PetscScalar>> get_local_vectors(
107 const Vec x,
108 const std::vector<
109 std::pair<std::reference_wrapper<const common::IndexMap>, int>>& maps);
110
113 Vec x, const std::vector<std::span<const PetscScalar>>& x_b,
114 const std::vector<
115 std::pair<std::reference_wrapper<const common::IndexMap>, int>>& maps);
116
119Mat create_matrix(MPI_Comm comm, const SparsityPattern& sp,
120 std::string type = std::string());
121
127MatNullSpace create_nullspace(MPI_Comm comm, std::span<const Vec> basis);
128
134namespace options
135{
137void set(std::string option);
138
140template <typename T>
141void set(std::string option, const T value)
142{
143 if (option[0] != '-')
144 option = '-' + option;
145
146 PetscErrorCode ierr;
147 ierr = PetscOptionsSetValue(nullptr, option.c_str(),
148 boost::lexical_cast<std::string>(value).c_str());
149 if (ierr != 0)
150 petsc::error(ierr, __FILE__, "PetscOptionsSetValue");
151}
152
154void clear(std::string option);
155
157void clear();
158} // namespace options
159
166{
167public:
172 Vector(const common::IndexMap& map, int bs);
173
174 // Delete copy constructor to avoid accidental copying of 'heavy' data
175 Vector(const Vector& x) = delete;
176
178 Vector(Vector&& x);
179
191 Vector(Vec x, bool inc_ref_count);
192
194 virtual ~Vector();
195
196 // Assignment operator (disabled)
197 Vector& operator=(const Vector& x) = delete;
198
200 Vector& operator=(Vector&& x);
201
204 Vector copy() const;
205
207 std::int64_t size() const;
208
210 std::int32_t local_size() const;
211
213 std::array<std::int64_t, 2> local_range() const;
214
216 MPI_Comm comm() const;
217
219 void set_options_prefix(std::string options_prefix);
220
223 std::string get_options_prefix() const;
224
226 void set_from_options();
227
229 Vec vec() const;
230
231private:
232 // PETSc Vec pointer
233 Vec _x;
234};
235
239{
240public:
242 Operator(Mat A, bool inc_ref_count);
243
244 // Copy constructor (deleted)
245 Operator(const Operator& A) = delete;
246
248 Operator(Operator&& A);
249
251 virtual ~Operator();
252
254 Operator& operator=(const Operator& A) = delete;
255
258
261 std::array<std::int64_t, 2> size() const;
262
268 Vec create_vector(std::size_t dim) const;
269
271 Mat mat() const;
272
273protected:
274 // PETSc Mat pointer
275 Mat _matA;
276};
277
283class Matrix : public Operator
284{
285public:
290 static auto set_fn(Mat A, InsertMode mode)
291 {
292 return [A, mode, cache = std::vector<PetscInt>()](
293 std::span<const std::int32_t> rows,
294 std::span<const std::int32_t> cols,
295 std::span<const PetscScalar> vals) mutable -> int
296 {
297 PetscErrorCode ierr;
298#ifdef PETSC_USE_64BIT_INDICES
299 cache.resize(rows.size() + cols.size());
300 std::copy(rows.begin(), rows.end(), cache.begin());
301 std::copy(cols.begin(), cols.end(),
302 std::next(cache.begin(), rows.size()));
303 const PetscInt* _rows = cache.data();
304 const PetscInt* _cols = cache.data() + rows.size();
305 ierr = MatSetValuesLocal(A, rows.size(), _rows, cols.size(), _cols,
306 vals.data(), mode);
307#else
308 ierr = MatSetValuesLocal(A, rows.size(), rows.data(), cols.size(),
309 cols.data(), vals.data(), mode);
310#endif
311
312#ifndef NDEBUG
313 if (ierr != 0)
314 petsc::error(ierr, __FILE__, "MatSetValuesLocal");
315#endif
316 return ierr;
317 };
318 }
319
325 static auto set_block_fn(Mat A, InsertMode mode)
326 {
327 return [A, mode, cache = std::vector<PetscInt>()](
328 std::span<const std::int32_t> rows,
329 std::span<const std::int32_t> cols,
330 std::span<const PetscScalar> vals) mutable -> int
331 {
332 PetscErrorCode ierr;
333#ifdef PETSC_USE_64BIT_INDICES
334 cache.resize(rows.size() + cols.size());
335 std::copy(rows.begin(), rows.end(), cache.begin());
336 std::copy(cols.begin(), cols.end(),
337 std::next(cache.begin(), rows.size()));
338 const PetscInt* _rows = cache.data();
339 const PetscInt* _cols = cache.data() + rows.size();
340 ierr = MatSetValuesBlockedLocal(A, rows.size(), _rows, cols.size(), _cols,
341 vals.data(), mode);
342#else
343 ierr = MatSetValuesBlockedLocal(A, rows.size(), rows.data(), cols.size(),
344 cols.data(), vals.data(), mode);
345#endif
346
347#ifndef NDEBUG
348 if (ierr != 0)
349 petsc::error(ierr, __FILE__, "MatSetValuesBlockedLocal");
350#endif
351 return ierr;
352 };
353 }
354
363 static auto set_block_expand_fn(Mat A, int bs0, int bs1, InsertMode mode)
364 {
365 return [A, bs0, bs1, mode, cache0 = std::vector<PetscInt>(),
366 cache1 = std::vector<PetscInt>()](
367 std::span<const std::int32_t> rows,
368 std::span<const std::int32_t> cols,
369 std::span<const PetscScalar> vals) mutable -> int
370 {
371 PetscErrorCode ierr;
372 cache0.resize(bs0 * rows.size());
373 cache1.resize(bs1 * cols.size());
374 for (std::size_t i = 0; i < rows.size(); ++i)
375 for (int k = 0; k < bs0; ++k)
376 cache0[bs0 * i + k] = bs0 * rows[i] + k;
377
378 for (std::size_t i = 0; i < cols.size(); ++i)
379 for (int k = 0; k < bs1; ++k)
380 cache1[bs1 * i + k] = bs1 * cols[i] + k;
381
382 ierr = MatSetValuesLocal(A, cache0.size(), cache0.data(), cache1.size(),
383 cache1.data(), vals.data(), mode);
384#ifndef NDEBUG
385 if (ierr != 0)
386 petsc::error(ierr, __FILE__, "MatSetValuesLocal");
387#endif
388 return ierr;
389 };
390 }
391
393 Matrix(MPI_Comm comm, const SparsityPattern& sp,
394 std::string type = std::string());
395
400 Matrix(Mat A, bool inc_ref_count);
401
402 // Copy constructor (deleted)
403 Matrix(const Matrix& A) = delete;
404
406 Matrix(Matrix&& A) = default;
407
409 ~Matrix() = default;
410
412 Matrix& operator=(const Matrix& A) = delete;
413
415 Matrix& operator=(Matrix&& A) = default;
416
420 enum class AssemblyType : std::int32_t
421 {
422 FINAL,
423 FLUSH
424 };
425
431 void apply(AssemblyType type);
432
434 double norm(Norm norm_type) const;
435
436 //--- Special PETSc Functions ---
437
440 void set_options_prefix(std::string options_prefix);
441
444 std::string get_options_prefix() const;
445
447 void set_from_options();
448};
449
453{
454public:
457 explicit KrylovSolver(MPI_Comm comm);
458
462 KrylovSolver(KSP ksp, bool inc_ref_count);
463
464 // Copy constructor (deleted)
465 KrylovSolver(const KrylovSolver& solver) = delete;
466
468 KrylovSolver(KrylovSolver&& solver);
469
472
473 // Assignment operator (deleted)
474 KrylovSolver& operator=(const KrylovSolver&) = delete;
475
477 KrylovSolver& operator=(KrylovSolver&& solver);
478
480 void set_operator(const Mat A);
481
483 void set_operators(const Mat A, const Mat P);
484
487 int solve(Vec x, const Vec b, bool transpose = false) const;
488
491 void set_options_prefix(std::string options_prefix);
492
495 std::string get_options_prefix() const;
496
498 void set_from_options() const;
499
501 KSP ksp() const;
502
503private:
504 // PETSc solver pointer
505 KSP _ksp;
506};
507} // namespace petsc
508} // namespace dolfinx::la
509
510#endif
Definition IndexMap.h:94
Definition SparsityPattern.h:26
Definition Vector.h:31
constexpr int bs() const
Get block size.
Definition Vector.h:186
std::span< const value_type > array() const
Get local part of the vector (const version)
Definition Vector.h:189
std::shared_ptr< const common::IndexMap > index_map() const
Get IndexMap.
Definition Vector.h:183
Definition petsc.h:453
void set_operator(const Mat A)
Set operator (Mat)
Definition petsc.cpp:675
KSP ksp() const
Return PETSc KSP pointer.
Definition petsc.cpp:791
std::string get_options_prefix() const
Definition petsc.cpp:773
void set_operators(const Mat A, const Mat P)
Set operator and preconditioner matrix (Mat)
Definition petsc.cpp:677
KrylovSolver(MPI_Comm comm)
Definition petsc.cpp:638
void set_from_options() const
Set options from PETSc options database.
Definition petsc.cpp:783
~KrylovSolver()
Destructor.
Definition petsc.cpp:663
int solve(Vec x, const Vec b, bool transpose=false) const
Definition petsc.cpp:686
void set_options_prefix(std::string options_prefix)
Definition petsc.cpp:764
Definition petsc.h:284
static auto set_block_fn(Mat A, InsertMode mode)
Definition petsc.h:325
void set_from_options()
Call PETSc function MatSetFromOptions on the PETSc Mat object.
Definition petsc.cpp:631
double norm(Norm norm_type) const
Return norm of matrix.
Definition petsc.cpp:575
static auto set_fn(Mat A, InsertMode mode)
Definition petsc.h:290
Matrix(MPI_Comm comm, const SparsityPattern &sp, std::string type=std::string())
Create holder for a PETSc Mat object from a sparsity pattern.
Definition petsc.cpp:563
Matrix(Matrix &&A)=default
Move constructor (falls through to base class move constructor)
std::string get_options_prefix() const
Definition petsc.cpp:623
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:363
~Matrix()=default
Destructor.
Matrix & operator=(const Matrix &A)=delete
Assignment operator (deleted)
AssemblyType
Definition petsc.h:421
void apply(AssemblyType type)
Definition petsc.cpp:600
void set_options_prefix(std::string options_prefix)
Definition petsc.cpp:617
Definition petsc.h:239
std::array< std::int64_t, 2 > size() const
Definition petsc.cpp:522
Operator & operator=(const Operator &A)=delete
Assignment operator (deleted)
Operator(Mat A, bool inc_ref_count)
Constructor.
Definition petsc.cpp:497
Vec create_vector(std::size_t dim) const
Definition petsc.cpp:532
Mat mat() const
Return PETSc Mat pointer.
Definition petsc.cpp:560
virtual ~Operator()
Destructor.
Definition petsc.cpp:508
Definition petsc.h:166
void set_from_options()
Call PETSc function VecSetFromOptions on the underlying Vec object.
Definition petsc.cpp:487
std::int64_t size() const
Return global size of the vector.
Definition petsc.cpp:434
std::string get_options_prefix() const
Definition petsc.cpp:478
Vector copy() const
Definition petsc.cpp:424
virtual ~Vector()
Destructor.
Definition petsc.cpp:412
Vector(const common::IndexMap &map, int bs)
Definition petsc.cpp:397
MPI_Comm comm() const
Return MPI communicator.
Definition petsc.cpp:462
std::array< std::int64_t, 2 > local_range() const
Return ownership range for calling rank.
Definition petsc.cpp:452
std::int32_t local_size() const
Return local size of vector (belonging to the call rank)
Definition petsc.cpp:443
Vec vec() const
Return pointer to PETSc Vec object.
Definition petsc.cpp:494
void set_options_prefix(std::string options_prefix)
Sets the prefix used by PETSc when searching the options database.
Definition petsc.cpp:471
Miscellaneous classes, functions and types.
Definition dolfinx_common.h:8
void clear()
Clear PETSc global options database.
Definition petsc.cpp:389
void set(std::string option)
Set PETSc option that takes no value.
Definition petsc.cpp:373
Vec create_vector_wrap(const common::IndexMap &map, int bs, std::span< const PetscScalar > x)
Definition petsc.cpp:101
MatNullSpace create_nullspace(MPI_Comm comm, std::span< const Vec > basis)
Definition petsc.cpp:361
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:191
void error(int error_code, std::string filename, std::string petsc_function)
Print error message for PETSc calls that return an error.
Definition petsc.cpp:33
std::vector< IS > create_index_sets(const std::vector< std::pair< std::reference_wrapper< const common::IndexMap >, int > > &maps)
Definition petsc.cpp:128
std::vector< Vec > create_vectors(MPI_Comm comm, const std::vector< std::span< const PetscScalar > > &x)
Definition petsc.cpp:51
Mat create_matrix(MPI_Comm comm, const SparsityPattern &sp, std::string type=std::string())
Definition petsc.cpp:234
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 vectors.
Definition petsc.cpp:148
Vec create_vector(const common::IndexMap &map, int bs)
Definition petsc.cpp:67
Linear algebra interface.
Definition sparsitybuild.h:15
Norm
Norm types.
Definition utils.h:17