Loading [MathJax]/extensions/tex2jax.js
Note: this is documentation for an old release. View the latest documentation at docs.fenicsproject.org/v0.1.0/v0.9.0/cpp
DOLFINx  0.1.0
DOLFINx C++ interface
All Classes Namespaces Functions Variables Typedefs Enumerations Pages
PETScVector.h
1 // Copyright (C) 2004-2018 Johan Hoffman, Johan Jansson, Anders Logg and Garth
2 // 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 #include "utils.h"
11 #include <array>
12 #include <cstdint>
13 #include <functional>
14 #include <petscvec.h>
15 #include <vector>
16 #include <xtl/xspan.hpp>
17 
18 namespace dolfinx
19 {
20 namespace common
21 {
22 class IndexMap;
23 }
24 namespace la
25 {
26 
34 Vec create_ghosted_vector(const common::IndexMap& map, int bs,
35  xtl::span<PetscScalar> x);
36 
38 void petsc_error(int error_code, std::string filename,
39  std::string petsc_function);
40 
52 std::vector<IS> create_petsc_index_sets(
53  const std::vector<
54  std::pair<std::reference_wrapper<const common::IndexMap>, int>>& maps);
55 
63 Vec create_petsc_vector(const common::IndexMap& map, int bs);
64 
75 Vec create_petsc_vector(MPI_Comm comm, std::array<std::int64_t, 2> range,
76  const std::vector<std::int64_t>& ghosts, int bs);
77 
79 std::vector<std::vector<PetscScalar>> get_local_vectors(
80  const Vec x,
81  const std::vector<
82  std::pair<std::reference_wrapper<const common::IndexMap>, int>>& maps);
83 
86  Vec x, const std::vector<xtl::span<const PetscScalar>>& x_b,
87  const std::vector<
88  std::pair<std::reference_wrapper<const common::IndexMap>, int>>& maps);
89 
96 {
97 public:
104  PETScVector(const common::IndexMap& map, int bs);
105 
106  // Delete copy constructor to avoid accidental copying of 'heavy' data
107  PETScVector(const PETScVector& x) = delete;
108 
111 
123  PETScVector(Vec x, bool inc_ref_count);
124 
126  virtual ~PETScVector();
127 
128  // Assignment operator (disabled)
129  PETScVector& operator=(const PETScVector& x) = delete;
130 
132  PETScVector& operator=(PETScVector&& x);
133 
137  PETScVector copy() const;
138 
140  std::int64_t size() const;
141 
143  std::int32_t local_size() const;
144 
146  std::array<std::int64_t, 2> local_range() const;
147 
149  MPI_Comm mpi_comm() const;
150 
157  PetscReal norm(la::Norm type) const;
158 
160  void set_options_prefix(std::string options_prefix);
161 
164  std::string get_options_prefix() const;
165 
167  void set_from_options();
168 
170  Vec vec() const;
171 
172 private:
173  // PETSc Vec pointer
174  Vec _x;
175 };
176 } // namespace la
177 } // namespace dolfinx
dolfinx::la::scatter_local_vectors
void scatter_local_vectors(Vec x, const std::vector< xtl::span< const PetscScalar >> &x_b, const std::vector< std::pair< std::reference_wrapper< const common::IndexMap >, int >> &maps)
Scatter local vectors to Vec.
Definition: PETScVector.cpp:144
dolfinx::la::Norm
Norm
Norm types.
Definition: utils.h:15
dolfinx::la::PETScVector::get_options_prefix
std::string get_options_prefix() const
Returns the prefix used by PETSc when searching the options database.
Definition: PETScVector.cpp:295
dolfinx::la::PETScVector::norm
PetscReal norm(la::Norm type) const
Compute norm of vector.
Definition: PETScVector.cpp:262
dolfinx::la::petsc_error
void petsc_error(int error_code, std::string filename, std::string petsc_function)
Print error message for PETSc calls that return an error.
Definition: PETScVector.cpp:26
dolfinx::common::IndexMap
This class represents the distribution index arrays across processes. An index array is a contiguous ...
Definition: IndexMap.h:48
dolfinx::la::PETScVector::set_options_prefix
void set_options_prefix(std::string options_prefix)
Sets the prefix used by PETSc when searching the options database.
Definition: PETScVector.cpp:288
dolfinx::la::PETScVector::size
std::int64_t size() const
Return global size of vector.
Definition: PETScVector.cpp:225
dolfinx::la::PETScVector::set_from_options
void set_from_options()
Call PETSc function VecSetFromOptions on the underlying Vec object.
Definition: PETScVector.cpp:304
dolfinx::la::PETScVector::copy
PETScVector copy() const
Create a copy of the vector.
Definition: PETScVector.cpp:215
dolfinx::la::PETScVector
A simple wrapper for a PETSc vector pointer (Vec). Its main purpose is to assist with memory/lifetime...
Definition: PETScVector.h:95
dolfinx::la::PETScVector::PETScVector
PETScVector(const common::IndexMap &map, int bs)
Create vector.
Definition: PETScVector.cpp:188
dolfinx::la::PETScVector::local_size
std::int32_t local_size() const
Return local size of vector (belonging to this process)
Definition: PETScVector.cpp:234
dolfinx::la::create_petsc_vector
Vec create_petsc_vector(const common::IndexMap &map, int bs)
Create a ghosted PETSc Vec.
Definition: PETScVector.cpp:76
dolfinx::la::create_ghosted_vector
Vec create_ghosted_vector(const common::IndexMap &map, int bs, xtl::span< PetscScalar > x)
Create a PETSc Vec that wraps the data in an array.
Definition: PETScVector.cpp:64
dolfinx::la::PETScVector::mpi_comm
MPI_Comm mpi_comm() const
Return MPI communicator.
Definition: PETScVector.cpp:253
dolfinx::la::PETScVector::local_range
std::array< std::int64_t, 2 > local_range() const
Return ownership range for calling rank.
Definition: PETScVector.cpp:243
dolfinx::la::PETScVector::~PETScVector
virtual ~PETScVector()
Destructor.
Definition: PETScVector.cpp:203
dolfinx::la::get_local_vectors
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: PETScVector.cpp:101
dolfinx::la::create_petsc_index_sets
std::vector< IS > create_petsc_index_sets(const std::vector< std::pair< std::reference_wrapper< const common::IndexMap >, int >> &maps)
Definition: PETScVector.cpp:44
dolfinx::la::PETScVector::vec
Vec vec() const
Return pointer to PETSc Vec object.
Definition: PETScVector.cpp:311