Debian Codegraph @

include/deal.II/lac/vector_memory.templates.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2007 - 2025 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // Part of the source code is dual licensed under Apache-2.0 WITH
9 // LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10 // governing the source code and code contributions can be found in
11 // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12 //
13 // ------------------------------------------------------------------------
14
15 #ifndef dealii_vector_memory_templates_h
16 #define dealii_vector_memory_templates_h
17
18
19 #include <deal.II/base/config.h>
20
21 #include <deal.II/base/kokkos.h>
22 #include <deal.II/base/logstream.h>
23 #include <deal.II/base/memory_consumption.h>
24
25 #include <deal.II/lac/vector_memory.h>
26
27 #include <Kokkos_Core.hpp>
28
29 #include <memory>
30
31 DEAL_II_NAMESPACE_OPEN
32
33
34 template <typename VectorType>
35 typename GrowingVectorMemory<VectorType>::Pool &
36 GrowingVectorMemory<VectorType>::get_pool()
37 {
38 // Kokkos needs to be initialized before constructing the static Pool for
39 // vector types that use Kokkos.
40 // If Kokkos is initialized by deal.II, this make sure that it is finalized
41 // after the Pool has been destroyed.
42 // If Kokkos is not initialized by deal.II, we assume that Kokkos is not
43 // finalized past program end together with static variables and we need to
44 // make sure to empty the Pool when finalizing Kokkos so that the destruction
45 // of the Pool doesn't call Kokkos functions.
46 static auto pool = []() {
47 internal::ensure_kokkos_initialized();
48 if (!internal::dealii_initialized_kokkos)
49 Kokkos::push_finalize_hook(
50 GrowingVectorMemory<VectorType>::release_unused_memory);
51 return GrowingVectorMemory<VectorType>::Pool{};
52 }();
53 return pool;
54 }
55
56
57
58 template <typename VectorType>
59 Threads::Mutex GrowingVectorMemory<VectorType>::mutex;
60
61
62
63 template <typename VectorType>
64 inline GrowingVectorMemory<VectorType>::Pool::Pool()
65 : data(nullptr)
66 {}
67
68
69
70 template <typename VectorType>
71 inline GrowingVectorMemory<VectorType>::Pool::~Pool()
72 {
73 // Nothing to do if memory was unused.
74 if (data == nullptr)
75 return;
76
77 // delete the 'data' object. this also releases all vectors
78 // that are pointed to by the std::unique_ptrs
79 data->clear();
80 delete data;
81 }
82
83
84 template <typename VectorType>
85 inline void
86 GrowingVectorMemory<VectorType>::Pool::initialize(const size_type size)
87 {
88 if (data == nullptr)
89 {
90 data = new std::vector<entry_type>(size);
91
92 for (typename std::vector<entry_type>::iterator i = data->begin();
93 i != data->end();
94 ++i)
95 {
96 i->first = false;
97 i->second = std::make_unique<VectorType>();
98 }
99 }
100 }
101
102
103 template <typename VectorType>
104 inline GrowingVectorMemory<VectorType>::GrowingVectorMemory(
105 const size_type initial_size,
106 const bool log_statistics)
107
108 : total_alloc(0)
109 , current_alloc(0)
110 , log_statistics(log_statistics)
111 {
112 // Constructors generally cannot be called in parallel and so it is not
113 // necessary to guard access to member variables with a mutex -- unless
114 // the member variable being accessed is 'static', which is the case
115 // for the things get_pool() returns, and in that case the mutex itself
116 // must also be 'static' as it is here.
117 std::lock_guard<std::mutex> lock(mutex);
118 get_pool().initialize(initial_size);
119 }
120
121
122 template <typename VectorType>
123 inline GrowingVectorMemory<VectorType>::~GrowingVectorMemory()
124 {
125 AssertNothrow(current_alloc == 0,
126 StandardExceptions::ExcMemoryLeak(current_alloc));
127 if (log_statistics)
128 {
129 deallog << "GrowingVectorMemory:Overall allocated vectors: "
130 << total_alloc << std::endl;
131 deallog << "GrowingVectorMemory:Maximum allocated vectors: "
132 << get_pool().data->size() << std::endl;
133 }
134 }
135
136
137
138 template <typename VectorType>
139 inline VectorType *
140 GrowingVectorMemory<VectorType>::alloc()
141 {
142 std::lock_guard<std::mutex> lock(mutex);
143
144 ++total_alloc;
145 ++current_alloc;
146
147 // See if there is a currently unused vector available in our list
148 for (entry_type &i : *get_pool().data)
149 {
150 if (i.first == false)
151 {
152 i.first = true;
153 return i.second.get();
154 }
155 }
156
157 // No currently unused vector found, so let's just allocate a new one
158 // and return it:
159 const auto &new_entry =
160 get_pool().data->emplace_back(true, std::make_unique<VectorType>());
161
162 return new_entry.second.get();
163 }
164
165
166
167 template <typename VectorType>
168 inline void
169 GrowingVectorMemory<VectorType>::free(const VectorType *const v)
170 {
171 std::lock_guard<std::mutex> lock(mutex);
172
173 // Find the vector to be de-allocated and mark it as now unused:
174 for (entry_type &i : *get_pool().data)
175 {
176 if (v == i.second.get())
177 {
178 i.first = false;
179 --current_alloc;
180 return;
181 }
182 }
183
184 // If we got here, someone is trying to free a vector that has not
185 // been allocated!
186 Assert(false, typename VectorMemory<VectorType>::ExcNotAllocatedHere());
187 }
188
189
190
191 template <typename VectorType>
192 inline void
193 GrowingVectorMemory<VectorType>::release_unused_memory()
194 {
195 std::lock_guard<std::mutex> lock(mutex);
196
197 if (get_pool().data != nullptr)
198 get_pool().data->clear();
199 }
200
201
202
203 template <typename VectorType>
204 inline std::size_t
205 GrowingVectorMemory<VectorType>::memory_consumption() const
206 {
207 std::lock_guard<std::mutex> lock(mutex);
208
209 std::size_t result = sizeof(*this);
210 for (const auto &[_, ptr] : *get_pool().data)
211 result += sizeof(ptr) + (ptr ? MemoryConsumption::memory_consumption(*ptr) :
212 MemoryConsumption::memory_consumption(ptr));
213
214 return result;
215 }
216
217
218 DEAL_II_NAMESPACE_CLOSE
219
220 #endif