OGS
SimpleMatrixVectorProvider.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
5
6#include <cassert>
7
8#include "BaseLib/Error.h"
9#include "BaseLib/Logging.h"
12
13namespace LinAlg = MathLib::LinAlg;
14
15namespace NumLib
16{
17template <typename MatVec, typename... Args>
18std::pair<MatVec*, bool> SimpleMatrixVectorProvider::get_(
19 std::size_t& id, std::map<MatVec*, std::size_t>& used_map, Args&&... args)
20{
21 id = _next_id++;
22 auto res =
24 std::forward<Args>(args)...)
25 .release(),
26 id);
27 return {res.first->first, true};
28}
29
30template <typename... Args>
31std::pair<GlobalMatrix*, bool> SimpleMatrixVectorProvider::getMatrix_(
32 std::size_t& id, Args&&... args)
33{
34 return get_(id, _used_matrices, std::forward<Args>(args)...);
35}
36
38{
39 return *getMatrix_(id).first;
40}
41
43 MathLib::MatrixSpecifications const& ms, std::size_t& id)
44{
45 return *getMatrix_(id, ms).first;
46 // TODO assert that the returned object always is of the right size
47}
48
50{
51 auto it = _used_matrices.find(const_cast<GlobalMatrix*>(&A));
52 if (it == _used_matrices.end())
53 {
55 "The given matrix has not been found. Cannot release it. "
56 "Aborting.");
57 }
58 else
59 {
60 delete it->first;
61 _used_matrices.erase(it);
62 }
63}
64
65template <typename... Args>
66std::pair<GlobalVector*, bool> SimpleMatrixVectorProvider::getVector_(
67 std::size_t& id, Args&&... args)
68{
69 return get_(id, _used_vectors, std::forward<Args>(args)...);
70}
71
73{
74 return *getVector_(id).first;
75}
76
79{
80 std::size_t id = 0u;
81 return *getVector_(id, ms).first;
82 // TODO assert that the returned object always is of the right size
83}
84
86 MathLib::MatrixSpecifications const& ms, std::size_t& id)
87{
88 return *getVector_(id, ms).first;
89 // TODO assert that the returned object always is of the right size
90}
91
93{
94 std::size_t id = 0u;
95 auto const& res = getVector_(id, x);
96 if (!res.second)
97 { // no new object has been created
98 LinAlg::copy(x, *res.first);
99 }
100 return *res.first;
101}
102
104 std::size_t& id)
105{
106 auto const& res = getVector_(id, x);
107 if (!res.second)
108 { // no new object has been created
109 LinAlg::copy(x, *res.first);
110 }
111 return *res.first;
112}
113
115{
116 auto it = _used_vectors.find(const_cast<GlobalVector*>(&x));
117 if (it == _used_vectors.end())
118 {
119 OGS_FATAL(
120 "The given vector has not been found. Cannot release it. "
121 "Aborting.");
122 }
123 else
124 {
125 delete it->first;
126 _used_vectors.erase(it);
127 }
128}
129
131{
132 if (!_used_matrices.empty())
133 {
134 WARN(
135 "There are still {:d} global matrices in use."
136 " This might be an indicator of a possible waste of memory.",
137 _used_matrices.size());
138 }
139 if (!_used_vectors.empty())
140 {
141 WARN(
142 "There are still {:d} global vectors in use."
143 " This might be an indicator of a possible waste of memory.",
144 _used_vectors.size());
145 }
146
147 for (auto const& ptr_id : _used_matrices)
148 {
149 delete ptr_id.first;
150 }
151 _used_matrices.clear();
152
153 for (auto const& ptr_id : _used_vectors)
154 {
155 delete ptr_id.first;
156 }
157 _used_vectors.clear();
158}
159
164
165} // namespace NumLib
#define OGS_FATAL(...)
Definition Error.h:19
MathLib::EigenMatrix GlobalMatrix
MathLib::EigenVector GlobalVector
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:34
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:30