chombo-discharge
Loading...
Searching...
No Matches
CD_ParticleMemory.H
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2021-2026 SINTEF Energy Research
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
13#ifndef CD_PARTICLEMEMORY_H
14#define CD_PARTICLEMEMORY_H
15
16// Std includes
17#include <atomic>
18#include <cstddef>
19
20// Chombo includes
21#include <memtrack.H>
22
23// Our includes
24#include <CD_NamespaceHeader.H>
25
55namespace ParticleMemory {
56
60enum class Kind
61{
62 Container = 0,
63 Buffer = 1,
64 NumKinds = 2
65};
66
67#ifdef CH_USE_MEMORY_TRACKING
68
77inline std::atomic<std::size_t> s_bytes[static_cast<int>(Kind::NumKinds)]{};
78
82inline std::atomic<std::size_t> s_peak[static_cast<int>(Kind::NumKinds)]{};
83
87inline std::atomic<std::size_t> s_totalBytes{0};
88
95inline std::atomic<std::size_t> s_totalPeak{0};
96
97#endif
98
107inline void
108raisePeak([[maybe_unused]] std::atomic<std::size_t>& a_mark, [[maybe_unused]] const std::size_t a_value) noexcept
109{
110#ifdef CH_USE_MEMORY_TRACKING
111 std::size_t seen = a_mark.load(std::memory_order_relaxed);
112
113 while (a_value > seen && !a_mark.compare_exchange_weak(seen, a_value, std::memory_order_relaxed)) {
114 }
115#endif
116}
117
123inline void
124addBytes([[maybe_unused]] const Kind a_kind, [[maybe_unused]] const std::size_t a_bytes) noexcept
125{
126#ifdef CH_USE_MEMORY_TRACKING
127 const int k = static_cast<int>(a_kind);
128
129 raisePeak(s_peak[k], s_bytes[k].fetch_add(a_bytes, std::memory_order_relaxed) + a_bytes);
130 raisePeak(s_totalPeak, s_totalBytes.fetch_add(a_bytes, std::memory_order_relaxed) + a_bytes);
131#endif
132}
133
139inline void
140removeBytes([[maybe_unused]] const Kind a_kind, [[maybe_unused]] const std::size_t a_bytes) noexcept
141{
142#ifdef CH_USE_MEMORY_TRACKING
143 s_bytes[static_cast<int>(a_kind)].fetch_sub(a_bytes, std::memory_order_relaxed);
144 s_totalBytes.fetch_sub(a_bytes, std::memory_order_relaxed);
145#endif
146}
147
153inline std::size_t
154getBytes([[maybe_unused]] const Kind a_kind) noexcept
155{
156#ifdef CH_USE_MEMORY_TRACKING
157 return s_bytes[static_cast<int>(a_kind)].load(std::memory_order_relaxed);
158#else
159 return 0;
160#endif
161}
162
168inline std::size_t
169getPeak([[maybe_unused]] const Kind a_kind) noexcept
170{
171#ifdef CH_USE_MEMORY_TRACKING
172 return s_peak[static_cast<int>(a_kind)].load(std::memory_order_relaxed);
173#else
174 return 0;
175#endif
176}
177
182inline std::size_t
184{
185#ifdef CH_USE_MEMORY_TRACKING
186 return s_totalBytes.load(std::memory_order_relaxed);
187#else
188 return 0;
189#endif
190}
191
196inline std::size_t
197getPeakBytes() noexcept
198{
199#ifdef CH_USE_MEMORY_TRACKING
200 return s_totalPeak.load(std::memory_order_relaxed);
201#else
202 return 0;
203#endif
204}
205} // namespace ParticleMemory
206
207#include <CD_NamespaceFooter.H>
208
209#endif
Running totals of the memory held by particle data on this rank.
Definition CD_ParticleMemory.H:55
std::size_t getPeakBytes() noexcept
Get the high-water mark across all kinds.
Definition CD_ParticleMemory.H:197
std::size_t getBytes(const Kind a_kind) noexcept
Get the bytes currently held by one kind.
Definition CD_ParticleMemory.H:154
void removeBytes(const Kind a_kind, const std::size_t a_bytes) noexcept
Record a release.
Definition CD_ParticleMemory.H:140
void raisePeak(std::atomic< std::size_t > &a_mark, const std::size_t a_value) noexcept
Raise a high-water mark to a_value unless it is already at least that.
Definition CD_ParticleMemory.H:108
std::size_t getPeak(const Kind a_kind) noexcept
Get the high-water mark of one kind.
Definition CD_ParticleMemory.H:169
std::size_t getAllocatedBytes() noexcept
Get the bytes currently held by particle data of every kind.
Definition CD_ParticleMemory.H:183
void addBytes(const Kind a_kind, const std::size_t a_bytes) noexcept
Record an allocation.
Definition CD_ParticleMemory.H:124
Kind
The kinds of particle memory tracked here. See the namespace documentation.
Definition CD_ParticleMemory.H:61
@ Container
ParticleSoA arenas: capacity a rank is holding.
@ NumKinds
Number of kinds.
@ Buffer
Flat exchange buffers: payload in flight.