OGS
SimpleMatrixVectorProvider.cpp
Go to the documentation of this file.
1 
12 
13 #include <cassert>
14 
15 #include "BaseLib/Error.h"
16 #include "BaseLib/Logging.h"
17 #include "MathLib/LinAlg/LinAlg.h"
19 
20 namespace LinAlg = MathLib::LinAlg;
21 
22 namespace NumLib
23 {
24 template <typename MatVec, typename... Args>
25 std::pair<MatVec*, bool> SimpleMatrixVectorProvider::get_(
26  std::size_t& id, std::map<MatVec*, std::size_t>& used_map, Args&&... args)
27 {
28  id = _next_id++;
29  auto res =
31  std::forward<Args>(args)...)
32  .release(),
33  id);
34  return {res.first->first, true};
35 }
36 
37 template <typename... Args>
38 std::pair<GlobalMatrix*, bool> SimpleMatrixVectorProvider::getMatrix_(
39  std::size_t& id, Args&&... args)
40 {
41  return get_(id, _used_matrices, std::forward<Args>(args)...);
42 }
43 
45 {
46  return *getMatrix_(id).first;
47 }
48 
50  MathLib::MatrixSpecifications const& ms, std::size_t& id)
51 {
52  return *getMatrix_(id, ms).first;
53  // TODO assert that the returned object always is of the right size
54 }
55 
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 }
71 
72 template <typename... Args>
73 std::pair<GlobalVector*, bool> SimpleMatrixVectorProvider::getVector_(
74  std::size_t& id, Args&&... args)
75 {
76  return get_(id, _used_vectors, std::forward<Args>(args)...);
77 }
78 
80 {
81  return *getVector_(id).first;
82 }
83 
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 }
91 
93  MathLib::MatrixSpecifications const& ms, std::size_t& id)
94 {
95  return *getVector_(id, ms).first;
96  // TODO assert that the returned object always is of the right size
97 }
98 
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 }
109 
111  std::size_t& id)
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 }
120 
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 }
136 
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 }
164 
165 } // namespace NumLib
#define OGS_FATAL(...)
Definition: Error.h:26
void WARN(char const *fmt, Args const &... args)
Definition: Logging.h:37
Global vector based on Eigen vector.
Definition: EigenVector.h:26
std::pair< GlobalVector *, bool > getVector_(std::size_t &id, Args &&... args)
void releaseVector(GlobalVector const &x) override
GlobalVector & getVector(std::size_t &id) override
Get an uninitialized vector with the given id.
std::pair< GlobalMatrix *, bool > getMatrix_(std::size_t &id, Args &&... args)
std::map< GlobalMatrix *, std::size_t > _used_matrices
std::pair< MatVec *, bool > get_(std::size_t &id, std::map< MatVec *, std::size_t > &used_map, Args &&... args)
GlobalMatrix & getMatrix(std::size_t &id) override
Get an uninitialized matrix with the given id.
std::map< GlobalVector *, std::size_t > _used_vectors
void releaseMatrix(GlobalMatrix const &A) override
void copy(PETScVector const &x, PETScVector &y)
Definition: LinAlg.cpp:37