Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
vulpes::la::Vector Class Reference

A wrapper around PETSc Vec for distributed vectors. More...

#include <vector.hpp>

Classes

struct  GhostTag
 Tag selecting the ghost-aware constructor overload (see createGhosted()). More...

Public Member Functions

template<typename DofHandler>
 Vector (const DofHandler &dof_handler, PetscInt block_size=1)
 Construct a Vector from a DofHandler.
 Vector (const std::pair< PetscInt, PetscInt > &sizes)
 Construct a Vector given local and global sizes.
template<typename DofHandler>
 Vector (GhostTag, const DofHandler &dof_handler)
 Construct a ghost-aware Vector from a DofHandler: the local form additionally carries a padding segment for this rank's ghost dofs (DofHandler::getGhostDofs()), refreshed on demand by updateGhosts() and readable via toLocalGhostedEigenVector(). Prefer the createGhosted() factory over calling this directly.
Vector & operator= (const Vector &other)=delete
 Vector (const Vector &other)
 Copy constructor.
template<typename DofHandler>
void init (const DofHandler &dof_handler, PetscInt block_size)
 Initialize the vector based on a DofHandler.
template<typename DofHandler>
void initGhosted (const DofHandler &dof_handler)
 Initialize a ghost-aware vector from a DofHandler (see the GhostTag constructor).
void copy (const Vector &other)
 Copy the contents of another vector.
void compress ()
 Compress the vector (do intracommunication).
void print () const
 Print the vector to standard output.
void save (const std::string &filename, const std::string &vecname="", PetscViewerFormat format=PETSC_VIEWER_ASCII_MATLAB) const
 Save the vector to a file.
void saveBinary (const std::string &filename, const std::string &vecname="") const
 Save the vector to a binary file.
void insertValues (const PetscScalar *values, const PetscInt *indices, PetscInt n_values, InsertMode mode=ADD_VALUES)
 Insert values into the vector at specified indices.
void insertValuesBlocked (const PetscScalar *values, const PetscInt *indices, PetscInt n_values, InsertMode mode=ADD_VALUES)
 Insert blocked values into the vector at specified indices.
void insertValues (const Eigen::Ref< const Eigen::MatrixXd > &loc_vec, const datastructure::CrsMap< PetscInt, PetscInt >::RowView< false > &idxs, InsertMode mode=ADD_VALUES)
 Insert values from an Eigen matrix into the vector at specified indices.
void insertValuesBlocked (const Eigen::Ref< const Eigen::MatrixXd > &loc_vec, const datastructure::CrsMap< PetscInt, PetscInt >::RowView< false > &idxs, InsertMode mode=ADD_VALUES)
 Insert blocked values from an Eigen matrix into the vector at specified indices.
void setValue (PetscInt idx, PetscScalar value, InsertMode mode=INSERT_VALUES)
 Set a single value in the vector.
PetscScalar getValue (PetscInt idx) const
 Get a single value from the vector.
void getLocalValues (std::vector< PetscScalar > &values) const
 Get all local values of the vector.
std::array< PetscInt, 2 > getOwnershipRange () const
 Get the ownership range of the vector.
PetscInt getBlockSize () const
 Get the block size of the vector.
Eigen::Map< const Eigen::VectorXd > toEigenMap () const
 Convert the locally-owned vector entries to an Eigen map (same as toLocalEigenMap()).
Eigen::Map< const Eigen::VectorXd > toLocalEigenMap () const
 Convert the locally-owned vector entries to an Eigen map.
void updateGhosts ()
 Refresh the ghost padding of a ghost-aware Vector (see createGhosted()) from each ghost dof's owning rank. Collective; call after writing new values, before reading any ghost value via toLocalGhostedEigenVector(). Only valid on a Vector built via createGhosted().
void updateGhostsBegin ()
 updateGhosts() split in two, so that the exchanges of several vectors overlap: begin all of them, then end all of them.
void updateGhostsEnd ()
void accumulateGhostsBegin ()
 Add the ghost entries of the local form (see getLocalGhostedView()) into the entries of their owning ranks, the reverse of updateGhosts(). Collective; split as updateGhostsBegin/End. The ghost entries keep their values: zero them before the next sum.
void accumulateGhostsEnd ()
Eigen::Map< Eigen::VectorXd > getLocalGhostedView ()
 Writable view of the local form of a ghost-aware Vector (owned dofs, then ghost dofs, indexed by DofHandler::localGhostedIndex()). Valid until restoreLocalGhostedView(), which must be called before any other use of the Vector.
void restoreLocalGhostedView ()
Eigen::VectorXd toLocalGhostedEigenVector () const
 Local-form snapshot of a ghost-aware Vector (see createGhosted()): this rank's owned dofs first ([0, n_owned)), then its ghost dofs in DofHandler::getGhostDofs() order ([n_owned, n_owned + n_ghost)). Use DofHandler::localGhostedIndex(global_dof_id) to index into it rather than hand-rolling ownership-range arithmetic against a global dof id – doing that by hand for a ghost dof is exactly the bug this class exists to remove (a global dof id belonging to another rank is not a valid index into toEigenMap()/toLocalEigenMap()). The ghost segment is only current immediately after updateGhosts(); nothing invalidates it automatically on a subsequent write. Only valid on a Vector built via createGhosted().
std::pair< PetscInt, PetscInt > getLocAndGlobSize () const
 Get local and global sizes of the vector.
const Vec & getPetscVector () const
 Get the underlying PETSc vector (const version).
Vec & getPetscVector ()
 Get the underlying PETSc vector (non-const version).
void zero ()
 Zero out the vector.
void set (double value)
 Set all values in the vector to a constant.
void scale (double value)
 Scale the vector by a constant.
PetscScalar norm (NormType norm_type=NORM_2) const
 Compute the norm of the vector.
void add (const Vector &x)
 Add a vector in place (this += x).
void add (PetscScalar alpha, const Vector &x)
 Add a vector in place (this += alpha x).
void addScaled (PetscScalar alpha, const Vector &x)
 Add a vector in place (this = x + alpha this).
void addGeneric (PetscScalar alpha, PetscScalar beta, const Vector &x)
 Add a vector in place (this = alpha x + beta this).
void addCouple (PetscScalar alpha, const Vector &x, const Vector &y)
 Add a vector in place (this = alpha x + y).
void addTriplet (PetscScalar alpha, PetscScalar beta, PetscScalar gamma, const Vector &x, const Vector &y)
 Add a vector in place (this = alpha x + beta y + gamma this).
void pow (PetscScalar power)
 Replaces each component of a vector by its p-th power.
void abs ()
 Replaces each component of a vector by its absolute value.
PetscInt getSize () const
 Get the size of the vector.
PetscInt getLocalSize () const
 Get the local size of the vector.
PetscScalar sum () const
 Compute the sum of all elements in the vector.
PetscScalar norm2 () const
 Compute the Euclidean norm of the vector.
 ~Vector ()

Static Public Member Functions

template<typename DofHandler>
static Vector createGhosted (const DofHandler &dof_handler)
 Construct a ghost-aware Vector from a DofHandler. See the GhostTag constructor above for what this enables.
static bool compareVectors (const Vector &vec1, const Vector &vec2, double tolerance=1e-12)
 Static method to compare two vectors with tolerance.

Protected Member Functions

void createVector ()
void setVectorSizes (const PetscInt n_loc, const PetscInt n_glob)
void setupVector ()

Protected Attributes

Vec m_petsc_vector
bool m_is_ghosted = false
Vec m_local_form = nullptr
PetscScalar * m_local_array = nullptr

Detailed Description

A wrapper around PETSc Vec for distributed vectors.

Constructor & Destructor Documentation

◆ Vector() [1/4]

template<typename DofHandler>
vulpes::la::Vector::Vector ( const DofHandler & dof_handler,
PetscInt block_size = 1 )
inlineexplicit

Construct a Vector from a DofHandler.

◆ Vector() [2/4]

vulpes::la::Vector::Vector ( const std::pair< PetscInt, PetscInt > & sizes)
inlineexplicit

Construct a Vector given local and global sizes.

◆ Vector() [3/4]

template<typename DofHandler>
vulpes::la::Vector::Vector ( GhostTag ,
const DofHandler & dof_handler )
inline

Construct a ghost-aware Vector from a DofHandler: the local form additionally carries a padding segment for this rank's ghost dofs (DofHandler::getGhostDofs()), refreshed on demand by updateGhosts() and readable via toLocalGhostedEigenVector(). Prefer the createGhosted() factory over calling this directly.

Note
Unlike the plain DofHandler constructor above, this does not take a block_size – only unblocked (block_size == 1) ghost vectors are supported for now.

◆ Vector() [4/4]

vulpes::la::Vector::Vector ( const Vector & other)

Copy constructor.

◆ ~Vector()

vulpes::la::Vector::~Vector ( )

Member Function Documentation

◆ abs()

void vulpes::la::Vector::abs ( )

Replaces each component of a vector by its absolute value.

◆ accumulateGhostsBegin()

void vulpes::la::Vector::accumulateGhostsBegin ( )

Add the ghost entries of the local form (see getLocalGhostedView()) into the entries of their owning ranks, the reverse of updateGhosts(). Collective; split as updateGhostsBegin/End. The ghost entries keep their values: zero them before the next sum.

◆ accumulateGhostsEnd()

void vulpes::la::Vector::accumulateGhostsEnd ( )

◆ add() [1/2]

void vulpes::la::Vector::add ( const Vector & x)
inline

Add a vector in place (this += x).

◆ add() [2/2]

void vulpes::la::Vector::add ( PetscScalar alpha,
const Vector & x )

Add a vector in place (this += alpha x).

◆ addCouple()

void vulpes::la::Vector::addCouple ( PetscScalar alpha,
const Vector & x,
const Vector & y )

Add a vector in place (this = alpha x + y).

◆ addGeneric()

void vulpes::la::Vector::addGeneric ( PetscScalar alpha,
PetscScalar beta,
const Vector & x )

Add a vector in place (this = alpha x + beta this).

◆ addScaled()

void vulpes::la::Vector::addScaled ( PetscScalar alpha,
const Vector & x )

Add a vector in place (this = x + alpha this).

◆ addTriplet()

void vulpes::la::Vector::addTriplet ( PetscScalar alpha,
PetscScalar beta,
PetscScalar gamma,
const Vector & x,
const Vector & y )

Add a vector in place (this = alpha x + beta y + gamma this).

◆ compareVectors()

bool vulpes::la::Vector::compareVectors ( const Vector & vec1,
const Vector & vec2,
double tolerance = 1e-12 )
static

Static method to compare two vectors with tolerance.

◆ compress()

void vulpes::la::Vector::compress ( )

Compress the vector (do intracommunication).

◆ copy()

void vulpes::la::Vector::copy ( const Vector & other)

Copy the contents of another vector.

◆ createGhosted()

template<typename DofHandler>
Vector vulpes::la::Vector::createGhosted ( const DofHandler & dof_handler)
inlinestatic

Construct a ghost-aware Vector from a DofHandler. See the GhostTag constructor above for what this enables.

◆ createVector()

void vulpes::la::Vector::createVector ( )
protected

◆ getBlockSize()

PetscInt vulpes::la::Vector::getBlockSize ( ) const

Get the block size of the vector.

◆ getLocalGhostedView()

Eigen::Map< Eigen::VectorXd > vulpes::la::Vector::getLocalGhostedView ( )

Writable view of the local form of a ghost-aware Vector (owned dofs, then ghost dofs, indexed by DofHandler::localGhostedIndex()). Valid until restoreLocalGhostedView(), which must be called before any other use of the Vector.

◆ getLocalSize()

PetscInt vulpes::la::Vector::getLocalSize ( ) const

Get the local size of the vector.

◆ getLocalValues()

void vulpes::la::Vector::getLocalValues ( std::vector< PetscScalar > & values) const

Get all local values of the vector.

◆ getLocAndGlobSize()

std::pair< PetscInt, PetscInt > vulpes::la::Vector::getLocAndGlobSize ( ) const

Get local and global sizes of the vector.

◆ getOwnershipRange()

std::array< PetscInt, 2 > vulpes::la::Vector::getOwnershipRange ( ) const

Get the ownership range of the vector.

◆ getPetscVector() [1/2]

Vec & vulpes::la::Vector::getPetscVector ( )
inline

Get the underlying PETSc vector (non-const version).

◆ getPetscVector() [2/2]

const Vec & vulpes::la::Vector::getPetscVector ( ) const
inline

Get the underlying PETSc vector (const version).

◆ getSize()

PetscInt vulpes::la::Vector::getSize ( ) const

Get the size of the vector.

◆ getValue()

PetscScalar vulpes::la::Vector::getValue ( PetscInt idx) const

Get a single value from the vector.

◆ init()

template<typename DofHandler>
void vulpes::la::Vector::init ( const DofHandler & dof_handler,
PetscInt block_size )
inline

Initialize the vector based on a DofHandler.

◆ initGhosted()

template<typename DofHandler>
void vulpes::la::Vector::initGhosted ( const DofHandler & dof_handler)
inline

Initialize a ghost-aware vector from a DofHandler (see the GhostTag constructor).

◆ insertValues() [1/2]

void vulpes::la::Vector::insertValues ( const Eigen::Ref< const Eigen::MatrixXd > & loc_vec,
const datastructure::CrsMap< PetscInt, PetscInt >::RowView< false > & idxs,
InsertMode mode = ADD_VALUES )

Insert values from an Eigen matrix into the vector at specified indices.

Parameters
loc_vecEigen reference to the local vector values
idxsIndices where the values should be inserted
modeInsertion mode (ADD_VALUES or INSERT_VALUES)

◆ insertValues() [2/2]

void vulpes::la::Vector::insertValues ( const PetscScalar * values,
const PetscInt * indices,
PetscInt n_values,
InsertMode mode = ADD_VALUES )

Insert values into the vector at specified indices.

Parameters
valuesPointer to the array of values to insert
indicesPointer to the array of indices where values should be inserted
n_valuesNumber of values to insert
modeInsertion mode (ADD_VALUES or INSERT_VALUES)

◆ insertValuesBlocked() [1/2]

void vulpes::la::Vector::insertValuesBlocked ( const Eigen::Ref< const Eigen::MatrixXd > & loc_vec,
const datastructure::CrsMap< PetscInt, PetscInt >::RowView< false > & idxs,
InsertMode mode = ADD_VALUES )

Insert blocked values from an Eigen matrix into the vector at specified indices.

Parameters
loc_vecEigen reference to the local blocked vector values (should be of size block_size times the size of idxs)
idxsIndices where the blocked values should be inserted
modeInsertion mode (ADD_VALUES or INSERT_VALUES)

◆ insertValuesBlocked() [2/2]

void vulpes::la::Vector::insertValuesBlocked ( const PetscScalar * values,
const PetscInt * indices,
PetscInt n_values,
InsertMode mode = ADD_VALUES )

Insert blocked values into the vector at specified indices.

Parameters
valuesPointer to the array of blocked values to insert (of size block_size * n_values)
indicesPointer to the array of indices where blocked values should be inserted
n_valuesNumber of blocked values to insert
modeInsertion mode (ADD_VALUES or INSERT_VALUES)

◆ norm()

PetscScalar vulpes::la::Vector::norm ( NormType norm_type = NORM_2) const

Compute the norm of the vector.

◆ norm2()

PetscScalar vulpes::la::Vector::norm2 ( ) const

Compute the Euclidean norm of the vector.

◆ operator=()

Vector & vulpes::la::Vector::operator= ( const Vector & other)
delete

◆ pow()

void vulpes::la::Vector::pow ( PetscScalar power)

Replaces each component of a vector by its p-th power.

◆ print()

void vulpes::la::Vector::print ( ) const

Print the vector to standard output.

◆ restoreLocalGhostedView()

void vulpes::la::Vector::restoreLocalGhostedView ( )

◆ save()

void vulpes::la::Vector::save ( const std::string & filename,
const std::string & vecname = "",
PetscViewerFormat format = PETSC_VIEWER_ASCII_MATLAB ) const

Save the vector to a file.

◆ saveBinary()

void vulpes::la::Vector::saveBinary ( const std::string & filename,
const std::string & vecname = "" ) const

Save the vector to a binary file.

◆ scale()

void vulpes::la::Vector::scale ( double value)

Scale the vector by a constant.

◆ set()

void vulpes::la::Vector::set ( double value)

Set all values in the vector to a constant.

◆ setupVector()

void vulpes::la::Vector::setupVector ( )
protected

◆ setValue()

void vulpes::la::Vector::setValue ( PetscInt idx,
PetscScalar value,
InsertMode mode = INSERT_VALUES )

Set a single value in the vector.

◆ setVectorSizes()

void vulpes::la::Vector::setVectorSizes ( const PetscInt n_loc,
const PetscInt n_glob )
protected

◆ sum()

PetscScalar vulpes::la::Vector::sum ( ) const

Compute the sum of all elements in the vector.

◆ toEigenMap()

Eigen::Map< const Eigen::VectorXd > vulpes::la::Vector::toEigenMap ( ) const

Convert the locally-owned vector entries to an Eigen map (same as toLocalEigenMap()).

◆ toLocalEigenMap()

Eigen::Map< const Eigen::VectorXd > vulpes::la::Vector::toLocalEigenMap ( ) const

Convert the locally-owned vector entries to an Eigen map.

◆ toLocalGhostedEigenVector()

Eigen::VectorXd vulpes::la::Vector::toLocalGhostedEigenVector ( ) const

Local-form snapshot of a ghost-aware Vector (see createGhosted()): this rank's owned dofs first ([0, n_owned)), then its ghost dofs in DofHandler::getGhostDofs() order ([n_owned, n_owned + n_ghost)). Use DofHandler::localGhostedIndex(global_dof_id) to index into it rather than hand-rolling ownership-range arithmetic against a global dof id – doing that by hand for a ghost dof is exactly the bug this class exists to remove (a global dof id belonging to another rank is not a valid index into toEigenMap()/toLocalEigenMap()). The ghost segment is only current immediately after updateGhosts(); nothing invalidates it automatically on a subsequent write. Only valid on a Vector built via createGhosted().

Note
Unlike toEigenMap()/toLocalEigenMap(), this returns an owned copy, not a zero-copy view: VecGhostGetLocalForm() hands out a separate PETSc object that must be properly restored, so a view tied to it can't be handed back the same way. See vector.cpp.

◆ updateGhosts()

void vulpes::la::Vector::updateGhosts ( )

Refresh the ghost padding of a ghost-aware Vector (see createGhosted()) from each ghost dof's owning rank. Collective; call after writing new values, before reading any ghost value via toLocalGhostedEigenVector(). Only valid on a Vector built via createGhosted().

◆ updateGhostsBegin()

void vulpes::la::Vector::updateGhostsBegin ( )

updateGhosts() split in two, so that the exchanges of several vectors overlap: begin all of them, then end all of them.

◆ updateGhostsEnd()

void vulpes::la::Vector::updateGhostsEnd ( )

◆ zero()

void vulpes::la::Vector::zero ( )

Zero out the vector.

Member Data Documentation

◆ m_is_ghosted

bool vulpes::la::Vector::m_is_ghosted = false
protected

◆ m_local_array

PetscScalar* vulpes::la::Vector::m_local_array = nullptr
protected

◆ m_local_form

Vec vulpes::la::Vector::m_local_form = nullptr
protected

◆ m_petsc_vector

Vec vulpes::la::Vector::m_petsc_vector
protected

The documentation for this class was generated from the following files: