version GIT relicensing-7336-ga5359f9c0a 2026-12-03 09:40:02+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
PSCToolkitWrappers::SparseMatrix Class Reference

#include <deal.II/lac/psblas_sparse_matrix.h>

Detailed Description

Implementation of a distributed parallel sparse matrix class based on PSBLAS (Parallel Sparse BLAS), which is the computational kernel of the PSCToolkit library.

Rows of the matrix are distributed across MPI processes according to an IndexSet partitioning (just as for the Vector class). The matrix supports element-wise set/add assembly as well as matrix-vector products (vmult(), Tvmult(), and their _add variants).

A typical workflow for using this matrix is as follows:

  1. Create the matrix by providing a SparsityPattern (recommended) or an IndexSet that describes the parallel row partitioning, together with the MPI communicator, either through the constructor or a call to reinit(). At this point the matrix is in the Build state.
  2. Fill the matrix by inserting or adding entries through set() or add().
  3. Call compress() to transition the matrix to the Assembled state, in which matrix-vector products and other algebraic operations become available.

A SparsityPattern can optionally be provided at construction time to pre-allocate the non-zero structure, which is strongly recommended for performance.

Definition at line 65 of file psblas_sparse_matrix.h.

Inheritance diagram for PSCToolkitWrappers::SparseMatrix:
[legend]

Public Types

using size_type = ::types::global_dof_index
 
using value_type = double
 

Public Member Functions

 SparseMatrix ()
 
 ~SparseMatrix ()
 
 SparseMatrix (const SparsityPattern &psblas_sparsity_pattern, const MPI_Comm communicator=MPI_COMM_WORLD)
 
 SparseMatrix (const SparseMatrix &)=delete
 
SparseMatrix & operator= (const SparseMatrix &)=delete
 
SparseMatrix & operator= (const value_type d)=delete
 
void copy_from (const SparseMatrix &other)
 
void reinit (const IndexSet &parallel_partitioning, const MPI_Comm communicator=MPI_COMM_WORLD)
 
void reinit (const SparsityPattern &psblas_sparsity_pattern, const MPI_Comm communicator=MPI_COMM_WORLD)
 
void reinit (const IndexSet &local_rows, const DynamicSparsityPattern &sparsity_pattern, const MPI_Comm communicator=MPI_COMM_WORLD)
 
size_type m () const
 
size_type n () const
 
size_type local_size () const
 
std::pair< size_type, size_type > local_range () const
 
bool in_local_range (const size_type index) const
 
size_type n_nonzero_elements () const
 
value_type el (const size_type i, const size_type j) const
 
value_type diag_element (const size_type i) const
 
value_type operator() (const size_type i, const size_type j) const
 
void set (const size_type i, const size_type j, const value_type value)
 
void set (const std::vector< size_type > &indices, const FullMatrix< double > &matrix)
 
void add (const size_type i, const size_type j, const value_type value)
 
void add (const size_type row, const std::vector< size_type > &col_indices, const std::vector< value_type > &values, const bool=false)
 
void add (const size_type row, const size_type ncols, const std::vector< size_type > &col_indices, const value_type *values, const bool=false, const bool=false)
 
void add (const size_type row, const size_type ncols, const std::vector< size_type > &col_indices, const std::vector< value_type > &values, const bool=false, const bool=false)
 
void add (const size_type row, const size_type n_cols, const size_type *col_indices, const value_type *values, const bool elide_zero_values=true, const bool col_indices_are_sorted=false)
 
void compress ()
 
void vmult (Vector &dst, const Vector &src) const
 
void vmult_add (Vector &dst, const Vector &src) const
 
void Tvmult (Vector &dst, const Vector &src) const
 
void Tvmult_add (Vector &dst, const Vector &src) const
 
value_type l1_norm () const
 
value_type linfty_norm () const
 
value_type frobenius_norm () const
 
value_type trace () const
 
psb_c_dspmat * get_psblas_matrix () const
 
psb_c_descriptor * get_psblas_descriptor () const
 
MPI_Comm get_mpi_communicator () const
 
template<class Archive >
void serialize (Archive &ar, const unsigned int version)
 
Querying the observer pointers an object has.
unsigned int n_subscriptions () const
 
template<typename StreamType >
void list_subscribers (StreamType &stream) const
 
void list_subscribers () const
 

Static Public Member Functions

static ::ExceptionBase & ExcInUse (int arg1, std::string arg2, std::string arg3)
 
static ::ExceptionBase & ExcNoSubscriber (std::string arg1, std::string arg2)
 

Private Member Functions

EnableObserverPointer functionality

Classes derived from EnableObserverPointer provide a facility to subscribe to this object. This is mostly used by the ObserverPointer class.

void subscribe (std::atomic< bool > *const validity, const std::string &identifier="") const
 
void unsubscribe (std::atomic< bool > *const validity, const std::string &identifier="") const
 
void check_no_subscribers () const noexcept
 

Private Attributes

MPI_Comm communicator
 
psb_c_dspmat * psblas_sparse_matrix
 
std::shared_ptr< psb_c_descriptor > psblas_descriptor
 
psb_c_ctxt * psblas_context
 
internal::State state
 
std::atomic< unsigned int > counter
 
std::map< std::string, unsigned int > counter_map
 
std::vector< std::atomic< bool > * > validity_pointers
 
const std::type_info * object_info
 

Static Private Attributes

static std::mutex mutex
 

Friends

class PreconditionAMG
 

Member Typedef Documentation

◆ size_type

Type for container size.

Definition at line 71 of file psblas_sparse_matrix.h.

◆ value_type

Type for container values.

Definition at line 76 of file psblas_sparse_matrix.h.

Constructor & Destructor Documentation

◆ SparseMatrix() [1/3]

Default constructor. Generates an empty (zero-size) matrix.

Definition at line 36 of file psblas_sparse_matrix.cc.

◆ ~SparseMatrix()

SparseMatrix< number >::~SparseMatrix ( )

Destructor. Internally, its frees the PSBLAS sparse matrix.

Definition at line 68 of file psblas_sparse_matrix.cc.

◆ SparseMatrix() [2/3]

SparseMatrix< number >::SparseMatrix ( const SparsityPattern &  psblas_sparsity_pattern,
const MPI_Comm  communicator = MPI_COMM_WORLD 
)

Generate a matrix from a PSBLAS SparsityPattern.

Definition at line 42 of file psblas_sparse_matrix.cc.

◆ SparseMatrix() [3/3]

PSCToolkitWrappers::SparseMatrix::SparseMatrix ( const SparseMatrix &  )
delete

Copy-constructor is deleted.

Member Function Documentation

◆ operator=() [1/2]

SparseMatrix & PSCToolkitWrappers::SparseMatrix::operator= ( const SparseMatrix &  )
delete

Copy assignment is deleted.

◆ operator=() [2/2]

SparseMatrix & PSCToolkitWrappers::SparseMatrix::operator= ( const value_type  d)
delete

Assignment of a scalar to all elements of the matrix is deleted.

◆ copy_from()

void SparseMatrix< number >::copy_from ( const SparseMatrix &  other)

Copy the contents of another SparseMatrix into this one.

Note
Currently not implemented for PSBLAS matrices.

Definition at line 81 of file psblas_sparse_matrix.cc.

◆ reinit() [1/3]

void SparseMatrix< number >::reinit ( const IndexSet &  parallel_partitioning,
const MPI_Comm  communicator = MPI_COMM_WORLD 
)

Initialize using an IndexSet and a MPI communicator to describe the parallel partitioning of the matrix.

Definition at line 91 of file psblas_sparse_matrix.cc.

◆ reinit() [2/3]

void SparseMatrix< number >::reinit ( const SparsityPattern &  psblas_sparsity_pattern,
const MPI_Comm  communicator = MPI_COMM_WORLD 
)

Initialize using an IndexSet and a MPI communicator to describe the parallel partitioning of the matrix.

Definition at line 147 of file psblas_sparse_matrix.cc.

◆ reinit() [3/3]

void SparseMatrix< number >::reinit ( const IndexSet &  local_rows,
const DynamicSparsityPattern &  sparsity_pattern,
const MPI_Comm  communicator = MPI_COMM_WORLD 
)

Initialize a square matrix where the size() of the IndexSet determines the number of rows and columns.

Definition at line 181 of file psblas_sparse_matrix.cc.

◆ m()

SparseMatrix::size_type SparseMatrix< number >::m ( ) const

Return the number of rows in this matrix.

Definition at line 335 of file psblas_sparse_matrix.cc.

◆ n()

SparseMatrix::size_type SparseMatrix< number >::n ( ) const

Return the number of columns in this matrix.

Definition at line 343 of file psblas_sparse_matrix.cc.

◆ local_size()

SparseMatrix::size_type SparseMatrix< number >::local_size ( ) const

Return the local dimension of the matrix, i.e. the number of rows stored on the present MPI process. For sequential matrices, this number is the same as m(), but for parallel matrices it may be smaller. To figure out which elements exactly are stored locally, use local_range().

Definition at line 303 of file psblas_sparse_matrix.cc.

◆ local_range()

std::pair< SparseMatrix::size_type, SparseMatrix::size_type > SparseMatrix< number >::local_range ( ) const

Return a pair of indices indicating which rows of this matrix are stored locally. The first number is the index of the first row stored, the second the index of the one past the last one that is stored locally. If this is a sequential matrix, then the result will be the pair (0,m()), otherwise it will be a pair (i,i+n), where n=local_size().

Definition at line 310 of file psblas_sparse_matrix.cc.

◆ in_local_range()

bool SparseMatrix< number >::in_local_range ( const size_type  index) const

Return whether index is in the local range or not, see also local_range().

Definition at line 326 of file psblas_sparse_matrix.cc.

◆ n_nonzero_elements()

SparseMatrix::size_type SparseMatrix< number >::n_nonzero_elements ( ) const

Return the number of nonzero elements of this matrix.

Definition at line 351 of file psblas_sparse_matrix.cc.

◆ el()

SparseMatrix::value_type SparseMatrix< number >::el ( const size_type  i,
const size_type  j 
) const

Return the value of the matrix entry (i,j).

Definition at line 376 of file psblas_sparse_matrix.cc.

◆ diag_element()

SparseMatrix::value_type SparseMatrix< number >::diag_element ( const size_type  i) const

Return the diagonal element of the matrix at row i.

Definition at line 393 of file psblas_sparse_matrix.cc.

◆ operator()()

SparseMatrix::value_type SparseMatrix< number >::operator() ( const size_type  i,
const size_type  j 
) const

Return the element (i,j) of the matrix. This function is equivalent to the el() function.

Definition at line 402 of file psblas_sparse_matrix.cc.

◆ set() [1/2]

void SparseMatrix< number >::set ( const size_type  i,
const size_type  j,
const value_type  value 
)

Set the element (i,j) to 'value'. If value is not a finite number an exception is thrown.

Definition at line 359 of file psblas_sparse_matrix.cc.

◆ set() [2/2]

void SparseMatrix< number >::set ( const std::vector< size_type > &  indices,
const FullMatrix< double > &  matrix 
)

Set all elements given in a FullMatrix into the sparse matrix locations given by indices. In other words, this function writes the elements in full_matrix into the calling matrix, using the local-to-global indexing specified by indices for both the rows and the columns of the matrix. This function assumes a quadratic sparse matrix and a quadratic full_matrix, the usual situation in FE calculations.

Definition at line 436 of file psblas_sparse_matrix.cc.

◆ add() [1/5]

void SparseMatrix< number >::add ( const size_type  i,
const size_type  j,
const value_type  value 
)

Add value 'value' to the element (i,j). If value is not a finite number an exception is thrown.

Definition at line 482 of file psblas_sparse_matrix.cc.

◆ add() [2/5]

void PSCToolkitWrappers::SparseMatrix::add ( const size_type  row,
const std::vector< size_type > &  col_indices,
const std::vector< value_type > &  values,
const bool  = false 
)

Set several elements in the specified row of the matrix with column indices as given by col_indices to the respective value.

◆ add() [3/5]

void SparseMatrix< number >::add ( const size_type  row,
const size_type  ncols,
const std::vector< size_type > &  col_indices,
const value_type *  values,
const bool  = false,
const bool  = false 
)

Add an array of values given by values in the given global matrix row at columns specified by col_indices in the sparse matrix.

Definition at line 502 of file psblas_sparse_matrix.cc.

◆ add() [4/5]

void SparseMatrix< number >::add ( const size_type  row,
const size_type  ncols,
const std::vector< size_type > &  col_indices,
const std::vector< value_type > &  values,
const bool  = false,
const bool  = false 
)

Same as above, but with a vector of values instead of a pointer to an array. The vector must have the same size as the number of columns given by ncols and the size of col_indices.

Definition at line 518 of file psblas_sparse_matrix.cc.

◆ add() [5/5]

void SparseMatrix< number >::add ( const size_type  row,
const size_type  n_cols,
const size_type *  col_indices,
const value_type *  values,
const bool  elide_zero_values = true,
const bool  col_indices_are_sorted = false 
)

Same as above, but using raw pointers for the column indices and values. The array of column indices must have the same size as ncols, and the array of values must have the same size as ncols as well.

Definition at line 535 of file psblas_sparse_matrix.cc.

◆ compress()

void SparseMatrix< number >::compress ( )

Compress the matrix after all insertions have been done.

Definition at line 562 of file psblas_sparse_matrix.cc.

◆ vmult()

void SparseMatrix< number >::vmult ( Vector &  dst,
const Vector &  src 
) const

Matrix-vector multiplication: let dst = M*src with M being this matrix.

Source and destination must not be the same vector.

Definition at line 586 of file psblas_sparse_matrix.cc.

◆ vmult_add()

void SparseMatrix< number >::vmult_add ( Vector &  dst,
const Vector &  src 
) const

Adding matrix-vector multiplication: Add M*src to dst with M being this matrix.

Source and destination must not be the same vector.

Definition at line 608 of file psblas_sparse_matrix.cc.

◆ Tvmult()

void SparseMatrix< number >::Tvmult ( Vector &  dst,
const Vector &  src 
) const

Matrix-vector multiplication: let dst = MT*src with M being this matrix. This function does the same as vmult() but takes the transposed matrix.

Source and destination must not be the same vector.

Definition at line 630 of file psblas_sparse_matrix.cc.

◆ Tvmult_add()

void SparseMatrix< number >::Tvmult_add ( Vector &  dst,
const Vector &  src 
) const

Adding matrix-vector multiplication: Add M^T*src to dst with M being this matrix.

Source and destination must not be the same vector.

Definition at line 655 of file psblas_sparse_matrix.cc.

◆ l1_norm()

SparseMatrix::value_type SparseMatrix< number >::l1_norm ( ) const

Return the l1-norm of the matrix.

Note
Currently not implemented.

Definition at line 680 of file psblas_sparse_matrix.cc.

◆ linfty_norm()

SparseMatrix::value_type SparseMatrix< number >::linfty_norm ( ) const

Return the linfty-norm of the matrix.

Note
Currently not implemented.

Definition at line 688 of file psblas_sparse_matrix.cc.

◆ frobenius_norm()

SparseMatrix::value_type SparseMatrix< number >::frobenius_norm ( ) const

Return the Frobenius of the matrix.

Note
Currently not implemented.

Definition at line 696 of file psblas_sparse_matrix.cc.

◆ trace()

SparseMatrix::value_type SparseMatrix< number >::trace ( ) const

Returns the trace of the matrix, i.e., the sum of the diagonal elements.

Definition at line 704 of file psblas_sparse_matrix.cc.

◆ get_psblas_matrix()

psb_c_dspmat * SparseMatrix< number >::get_psblas_matrix ( ) const

Get the underlying PSBLAS sparse matrix. Do not use this function unless you know what you are doing.

Definition at line 412 of file psblas_sparse_matrix.cc.

◆ get_psblas_descriptor()

psb_c_descriptor * SparseMatrix< number >::get_psblas_descriptor ( ) const

Get the underlying PSBLAS descriptor. Do not use this function unless you know what you are doing.

Definition at line 420 of file psblas_sparse_matrix.cc.

◆ get_mpi_communicator()

MPI_Comm SparseMatrix< number >::get_mpi_communicator ( ) const

Return the underlying MPI communicator. Do not use this function unless you know what you are doing.

Definition at line 428 of file psblas_sparse_matrix.cc.

◆ n_subscriptions()

unsigned int EnableObserverPointer::n_subscriptions ( ) const
inlineinherited

Return the present number of subscriptions to this object. This allows to use this class for reference counted lifetime determination where the last one to unsubscribe also deletes the object.

Definition at line 318 of file enable_observer_pointer.h.

◆ list_subscribers() [1/2]

template<typename StreamType >
void EnableObserverPointer::list_subscribers ( StreamType &  stream) const
inlineinherited

List the subscribers to the input stream.

Definition at line 335 of file enable_observer_pointer.h.

◆ list_subscribers() [2/2]

void EnableObserverPointer::list_subscribers ( ) const
inherited

List the subscribers to deallog.

Definition at line 200 of file enable_observer_pointer.cc.

◆ serialize()

template<class Archive >
void EnableObserverPointer::serialize ( Archive &  ar,
const unsigned int  version 
)
inlineinherited

Read or write the data of this object to or from a stream for the purpose of serialization using the BOOST serialization library.

This function does not actually serialize any of the member variables of this class. The reason is that what this class stores is only who subscribes to this object, but who does so at the time of storing the contents of this object does not necessarily have anything to do with who subscribes to the object when it is restored. Consequently, we do not want to overwrite the subscribers at the time of restoring, and then there is no reason to write the subscribers out in the first place.

Definition at line 327 of file enable_observer_pointer.h.

◆ subscribe()

void EnableObserverPointer::subscribe ( std::atomic< bool > *const  validity,
const std::string &  identifier = "" 
) const
privateinherited

Subscribes a user of the object by storing the pointer validity. The subscriber may be identified by text supplied as identifier.

Definition at line 136 of file enable_observer_pointer.cc.

◆ unsubscribe()

void EnableObserverPointer::unsubscribe ( std::atomic< bool > *const  validity,
const std::string &  identifier = "" 
) const
privateinherited

Unsubscribes a user from the object.

Note
The identifier and the validity pointer must be the same as the one supplied to subscribe().

Definition at line 154 of file enable_observer_pointer.cc.

◆ check_no_subscribers()

void EnableObserverPointer::check_no_subscribers ( ) const
privatenoexceptinherited

Check that there are no objects subscribing to this object. If this check passes then it is safe to destroy the current object. It this check fails then this function will either abort or print an error message to deallog (by using the AssertNothrow mechanism), but will not throw an exception.

Note
Since this function is just a consistency check it does nothing in release mode.
If this function is called when there is an uncaught exception then, rather than aborting, this function prints an error message to the standard error stream and returns.

Definition at line 58 of file enable_observer_pointer.cc.

Friends And Related Symbol Documentation

◆ PreconditionAMG

friend class PreconditionAMG
friend

Typedef for the AMG preconditioner type.

Definition at line 408 of file psblas_sparse_matrix.h.

Member Data Documentation

◆ communicator

MPI_Comm PSCToolkitWrappers::SparseMatrix::communicator
private

The underlying MPI communicator.

Definition at line 385 of file psblas_sparse_matrix.h.

◆ psblas_sparse_matrix

psb_c_dspmat* PSCToolkitWrappers::SparseMatrix::psblas_sparse_matrix
private

Pointer the underlying PSBLAS sparse matrix.

Definition at line 390 of file psblas_sparse_matrix.h.

◆ psblas_descriptor

std::shared_ptr<psb_c_descriptor> PSCToolkitWrappers::SparseMatrix::psblas_descriptor
private

Shared pointer to the underlying PSBLAS descriptor object.

Definition at line 395 of file psblas_sparse_matrix.h.

◆ psblas_context

psb_c_ctxt* PSCToolkitWrappers::SparseMatrix::psblas_context
private

Pointer to the underlying PSBLAS context object.

Definition at line 400 of file psblas_sparse_matrix.h.

◆ state

internal::State PSCToolkitWrappers::SparseMatrix::state
private

State of the descriptor associated with the vector. Its state can be either default, building or assembled).

Definition at line 406 of file psblas_sparse_matrix.h.

◆ counter

std::atomic<unsigned int> EnableObserverPointer::counter
mutableprivateinherited

Store the number of objects which subscribed to this object. Initially, this number is zero, and upon destruction it shall be zero again (i.e. all objects which subscribed should have unsubscribed again).

The creator (and owner) of an object is counted in the map below if HE manages to supply identification.

We use the mutable keyword in order to allow subscription to constant objects also.

This counter may be read from and written to concurrently in multithreaded code: hence we use the std::atomic class template.

Definition at line 212 of file enable_observer_pointer.h.

◆ counter_map

std::map<std::string, unsigned int> EnableObserverPointer::counter_map
mutableprivateinherited

In this map, we count subscriptions for each different identification string supplied to subscribe().

Definition at line 218 of file enable_observer_pointer.h.

◆ validity_pointers

std::vector<std::atomic<bool> *> EnableObserverPointer::validity_pointers
mutableprivateinherited

In this vector, we store pointers to the validity bool in the ObserverPointer objects that subscribe to this class.

Definition at line 224 of file enable_observer_pointer.h.

◆ object_info

const std::type_info* EnableObserverPointer::object_info
mutableprivateinherited

Pointer to the typeinfo object of this object, from which we can later deduce the class name. Since this information on the derived class is neither available in the destructor, nor in the constructor, we obtain it in between and store it here.

Definition at line 232 of file enable_observer_pointer.h.

◆ mutex

std::mutex EnableObserverPointer::mutex
staticprivateinherited

A mutex used to ensure data consistency when accessing the mutable members of this class. This lock is used in the subscribe() and unsubscribe() functions, as well as in list_subscribers().

Definition at line 239 of file enable_observer_pointer.h.


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