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"
19
20namespace LinAlg = MathLib::LinAlg;
21
22namespace NumLib
23{
24template <typename MatVec, typename... Args>
25std::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
37template <typename... Args>
38std::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 {
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
72template <typename... Args>
73std::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 const& ptr_id : _used_matrices)
155 {
156 delete ptr_id.first;
157 }
158 _used_matrices.clear();
159
160 for (auto const& ptr_id : _used_vectors)
161 {
162 delete ptr_id.first;
163 }
164 _used_vectors.clear();
165}
166
171
172} // namespace NumLib
#define OGS_FATAL(...)
Definition Error.h:26
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
Global vector based on Eigen vector.
Definition EigenVector.h:25
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