OGS
NumLib::SimpleMatrixVectorProvider Class Referencefinal

Detailed Description

Manages storage for matrices and vectors.

This is a simple implementation of the MatrixProvider and VectorProvider interfaces.

It is simple insofar it does not reuse released matrices/vectors, but keeps them in memory until they are acquired again by the user.

Definition at line 29 of file SimpleMatrixVectorProvider.h.

#include <SimpleMatrixVectorProvider.h>

Inheritance diagram for NumLib::SimpleMatrixVectorProvider:
[legend]
Collaboration diagram for NumLib::SimpleMatrixVectorProvider:
[legend]

Public Member Functions

 SimpleMatrixVectorProvider ()=default
 
 SimpleMatrixVectorProvider (SimpleMatrixVectorProvider const &)=delete
 
SimpleMatrixVectorProvideroperator= (SimpleMatrixVectorProvider const &)=delete
 
GlobalVectorgetVector (std::size_t &id) override
 Get an uninitialized vector with the given id. More...
 
GlobalVectorgetVector (GlobalVector const &x) override
 Get a copy of x. More...
 
GlobalVectorgetVector (GlobalVector const &x, std::size_t &id) override
 Get a copy of x in the storage of the vector with the given id. More...
 
GlobalVectorgetVector (MathLib::MatrixSpecifications const &ms) override
 Get a vector according to the given specifications. More...
 
GlobalVectorgetVector (MathLib::MatrixSpecifications const &ms, std::size_t &id) override
 
void releaseVector (GlobalVector const &x) override
 
GlobalMatrixgetMatrix (std::size_t &id) override
 Get an uninitialized matrix with the given id. More...
 
GlobalMatrixgetMatrix (MathLib::MatrixSpecifications const &ms, std::size_t &id) override
 
void releaseMatrix (GlobalMatrix const &A) override
 
 ~SimpleMatrixVectorProvider () override
 
- Public Member Functions inherited from NumLib::MatrixProvider
virtual ~MatrixProvider ()=default
 
- Public Member Functions inherited from NumLib::VectorProvider
virtual ~VectorProvider ()=default
 

Private Member Functions

template<typename... Args>
std::pair< GlobalMatrix *, bool > getMatrix_ (std::size_t &id, Args &&... args)
 
template<typename... Args>
std::pair< GlobalVector *, bool > getVector_ (std::size_t &id, Args &&... args)
 
template<typename MatVec , typename... Args>
std::pair< MatVec *, bool > get_ (std::size_t &id, std::map< MatVec *, std::size_t > &used_map, Args &&... args)
 

Private Attributes

std::size_t _next_id = 1
 
std::map< GlobalMatrix *, std::size_t > _used_matrices
 
std::map< GlobalVector *, std::size_t > _used_vectors
 

Constructor & Destructor Documentation

◆ SimpleMatrixVectorProvider() [1/2]

NumLib::SimpleMatrixVectorProvider::SimpleMatrixVectorProvider ( )
default

◆ SimpleMatrixVectorProvider() [2/2]

NumLib::SimpleMatrixVectorProvider::SimpleMatrixVectorProvider ( SimpleMatrixVectorProvider const &  )
delete

◆ ~SimpleMatrixVectorProvider()

NumLib::SimpleMatrixVectorProvider::~SimpleMatrixVectorProvider ( )
override

Definition at line 137 of file SimpleMatrixVectorProvider.cpp.

138 {
139  if (!_used_matrices.empty())
140  {
141  WARN(
142  "There are still {:d} global matrices in use."
143  " This might be an indicator of a possible waste of memory.",
144  _used_matrices.size());
145  }
146  if (!_used_vectors.empty())
147  {
148  WARN(
149  "There are still {:d} global vectors in use."
150  " This might be an indicator of a possible waste of memory.",
151  _used_vectors.size());
152  }
153 
154  for (auto& ptr_id : _used_matrices)
155  {
156  delete ptr_id.first;
157  }
158 
159  for (auto& ptr_id : _used_vectors)
160  {
161  delete ptr_id.first;
162  }
163 }
void WARN(char const *fmt, Args const &... args)
Definition: Logging.h:37
std::map< GlobalMatrix *, std::size_t > _used_matrices
std::map< GlobalVector *, std::size_t > _used_vectors

References _used_matrices, _used_vectors, and WARN().

Member Function Documentation

◆ get_()

template<typename MatVec , typename... Args>
std::pair< MatVec *, bool > NumLib::SimpleMatrixVectorProvider::get_ ( std::size_t &  id,
std::map< MatVec *, std::size_t > &  used_map,
Args &&...  args 
)
private

Definition at line 25 of file SimpleMatrixVectorProvider.cpp.

27 {
28  id = _next_id++;
29  auto res =
31  std::forward<Args>(args)...)
32  .release(),
33  id);
34  return {res.first->first, true};
35 }

References _next_id.

Referenced by getMatrix_(), and getVector_().

◆ getMatrix() [1/2]

GlobalMatrix & NumLib::SimpleMatrixVectorProvider::getMatrix ( MathLib::MatrixSpecifications const &  ms,
std::size_t &  id 
)
overridevirtual

Get a matrix according to the given specifications in the storage of the matrix with the given id.

Implements NumLib::MatrixProvider.

Definition at line 49 of file SimpleMatrixVectorProvider.cpp.

51 {
52  return *getMatrix_(id, ms).first;
53  // TODO assert that the returned object always is of the right size
54 }
std::pair< GlobalMatrix *, bool > getMatrix_(std::size_t &id, Args &&... args)

References getMatrix_().

◆ getMatrix() [2/2]

GlobalMatrix & NumLib::SimpleMatrixVectorProvider::getMatrix ( std::size_t &  id)
overridevirtual

Get an uninitialized matrix with the given id.

Implements NumLib::MatrixProvider.

Definition at line 44 of file SimpleMatrixVectorProvider.cpp.

45 {
46  return *getMatrix_(id).first;
47 }

References getMatrix_().

◆ getMatrix_()

template<typename... Args>
std::pair< GlobalMatrix *, bool > NumLib::SimpleMatrixVectorProvider::getMatrix_ ( std::size_t &  id,
Args &&...  args 
)
private

Definition at line 38 of file SimpleMatrixVectorProvider.cpp.

40 {
41  return get_(id, _used_matrices, std::forward<Args>(args)...);
42 }
std::pair< MatVec *, bool > get_(std::size_t &id, std::map< MatVec *, std::size_t > &used_map, Args &&... args)

References _used_matrices, and get_().

Referenced by getMatrix().

◆ getVector() [1/5]

GlobalVector & NumLib::SimpleMatrixVectorProvider::getVector ( GlobalVector const &  x)
overridevirtual

Get a copy of x.

Implements NumLib::VectorProvider.

Definition at line 99 of file SimpleMatrixVectorProvider.cpp.

100 {
101  std::size_t id = 0u;
102  auto const& res = getVector_(id, x);
103  if (!res.second)
104  { // no new object has been created
105  LinAlg::copy(x, *res.first);
106  }
107  return *res.first;
108 }
std::pair< GlobalVector *, bool > getVector_(std::size_t &id, Args &&... args)
void copy(PETScVector const &x, PETScVector &y)
Definition: LinAlg.cpp:37

References MathLib::LinAlg::copy(), and getVector_().

◆ getVector() [2/5]

GlobalVector & NumLib::SimpleMatrixVectorProvider::getVector ( GlobalVector const &  x,
std::size_t &  id 
)
overridevirtual

Get a copy of x in the storage of the vector with the given id.

Implements NumLib::VectorProvider.

Definition at line 110 of file SimpleMatrixVectorProvider.cpp.

112 {
113  auto const& res = getVector_(id, x);
114  if (!res.second)
115  { // no new object has been created
116  LinAlg::copy(x, *res.first);
117  }
118  return *res.first;
119 }

References MathLib::LinAlg::copy(), and getVector_().

◆ getVector() [3/5]

GlobalVector & NumLib::SimpleMatrixVectorProvider::getVector ( MathLib::MatrixSpecifications const &  ms)
overridevirtual

Get a vector according to the given specifications.

Implements NumLib::VectorProvider.

Definition at line 84 of file SimpleMatrixVectorProvider.cpp.

86 {
87  std::size_t id = 0u;
88  return *getVector_(id, ms).first;
89  // TODO assert that the returned object always is of the right size
90 }

References getVector_().

◆ getVector() [4/5]

GlobalVector & NumLib::SimpleMatrixVectorProvider::getVector ( MathLib::MatrixSpecifications const &  ms,
std::size_t &  id 
)
overridevirtual

Get a vector according to the given specifications in the storage of the vector with the given id.

Implements NumLib::VectorProvider.

Definition at line 92 of file SimpleMatrixVectorProvider.cpp.

94 {
95  return *getVector_(id, ms).first;
96  // TODO assert that the returned object always is of the right size
97 }

References getVector_().

◆ getVector() [5/5]

GlobalVector & NumLib::SimpleMatrixVectorProvider::getVector ( std::size_t &  id)
overridevirtual

Get an uninitialized vector with the given id.

Implements NumLib::VectorProvider.

Definition at line 79 of file SimpleMatrixVectorProvider.cpp.

80 {
81  return *getVector_(id).first;
82 }

References getVector_().

◆ getVector_()

template<typename... Args>
std::pair< GlobalVector *, bool > NumLib::SimpleMatrixVectorProvider::getVector_ ( std::size_t &  id,
Args &&...  args 
)
private

Definition at line 73 of file SimpleMatrixVectorProvider.cpp.

75 {
76  return get_(id, _used_vectors, std::forward<Args>(args)...);
77 }

References _used_vectors, and get_().

Referenced by getVector().

◆ operator=()

SimpleMatrixVectorProvider& NumLib::SimpleMatrixVectorProvider::operator= ( SimpleMatrixVectorProvider const &  )
delete

◆ releaseMatrix()

void NumLib::SimpleMatrixVectorProvider::releaseMatrix ( GlobalMatrix const &  A)
overridevirtual

Release the given matrix.

Precondition
A must have been acquired before, i.e., you must not call this method twice in a row in the same A!

Implements NumLib::MatrixProvider.

Definition at line 56 of file SimpleMatrixVectorProvider.cpp.

57 {
58  auto it = _used_matrices.find(const_cast<GlobalMatrix*>(&A));
59  if (it == _used_matrices.end())
60  {
61  OGS_FATAL(
62  "The given matrix has not been found. Cannot release it. "
63  "Aborting.");
64  }
65  else
66  {
67  delete it->first;
68  _used_matrices.erase(it);
69  }
70 }
#define OGS_FATAL(...)
Definition: Error.h:26

References _used_matrices, and OGS_FATAL.

◆ releaseVector()

void NumLib::SimpleMatrixVectorProvider::releaseVector ( GlobalVector const &  x)
overridevirtual

Release the given vector.

Precondition
x must have been acquired before, i.e., you must not call this method twice in a row in the same x!

Implements NumLib::VectorProvider.

Definition at line 121 of file SimpleMatrixVectorProvider.cpp.

122 {
123  auto it = _used_vectors.find(const_cast<GlobalVector*>(&x));
124  if (it == _used_vectors.end())
125  {
126  OGS_FATAL(
127  "The given vector has not been found. Cannot release it. "
128  "Aborting.");
129  }
130  else
131  {
132  delete it->first;
133  _used_vectors.erase(it);
134  }
135 }
Global vector based on Eigen vector.
Definition: EigenVector.h:26

References _used_vectors, and OGS_FATAL.

Member Data Documentation

◆ _next_id

std::size_t NumLib::SimpleMatrixVectorProvider::_next_id = 1
private

Definition at line 75 of file SimpleMatrixVectorProvider.h.

Referenced by get_().

◆ _used_matrices

std::map<GlobalMatrix*, std::size_t> NumLib::SimpleMatrixVectorProvider::_used_matrices
private

◆ _used_vectors

std::map<GlobalVector*, std::size_t> NumLib::SimpleMatrixVectorProvider::_used_vectors
private

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