FZGPUModules 2.0
GPU-accelerated modular compression pipelines
Loading...
Searching...
No Matches
mempool.h
Go to the documentation of this file.
1#pragma once
2
7
8#include "backend/types.h"
9
10#include <cstddef>
11#include <memory>
12#include <string>
13#include <unordered_map>
14#include <vector>
15
16namespace fz {
17
19struct AllocationInfo {
20 void* ptr;
21 size_t size;
22 std::string tag;
23 bool in_use;
24
25 AllocationInfo(void* p = nullptr, size_t s = 0, const std::string& t = "")
26 : ptr(p), size(s), tag(t), in_use(true) {}
27};
28
39 void* ptr;
40 size_t bytes;
41 std::string tag;
42 bool is_pinned;
43};
44
46struct MemoryPoolConfig {
51
53
54 MemoryPoolConfig(
55 size_t input_size = 0,
56 float multiplier = 3.0f,
57 int device = 0,
58 bool reuse = true,
59 bool force_fallback = false)
60 : input_data_size(input_size),
61 pool_size_multiplier(multiplier),
62 device_id(device),
63 enable_reuse(reuse),
65
67 size_t getPoolSize() const {
68 if (input_data_size == 0) return 1024ULL * 1024 * 1024;
69 return static_cast<size_t>(input_data_size * pool_size_multiplier);
70 }
71};
72
82class MemoryPool {
83public:
84 explicit MemoryPool(const MemoryPoolConfig& config = MemoryPoolConfig());
85 ~MemoryPool();
86
87 MemoryPool(const MemoryPool&) = delete;
88 MemoryPool& operator=(const MemoryPool&) = delete;
89
90 // ── Stream-ordered allocation (I/O buffers) ───────────────────────────────
91
102 void* allocate(size_t size, fz::stream_t stream,
103 const std::string& tag = "", bool persistent = false);
104
106 void free(void* ptr, fz::stream_t stream);
107
108 // ── Persistent allocation (stage-internal scratch) ────────────────────────
109
120 void* allocatePersistentDevice(size_t bytes, const std::string& tag = "");
121
132 void* allocatePersistentPinned(size_t bytes, const std::string& tag = "");
133
135 void freePersistentDevice(void* ptr);
136
138 void freePersistentPinned(void* ptr);
139
140 // ── Lifecycle ─────────────────────────────────────────────────────────────
141
143 void reset(fz::stream_t stream);
144
146 void trim();
147
156 void setReleaseThreshold(size_t bytes);
157
159 void synchronize(fz::stream_t stream);
160
161 // ── Stats & debug ─────────────────────────────────────────────────────────
162
164 size_t getPersistentDeviceBytes() const { return persistent_device_bytes_; }
165
167 size_t getPersistentPinnedBytes() const { return persistent_pinned_bytes_; }
168
170 size_t getCurrentUsage() const {
171 if (!mem_pool_) return current_allocated_bytes_;
172 return fz::getPoolUsedMemCurrent(mem_pool_);
173 }
174
176 size_t getPeakUsage() const {
177 if (!mem_pool_) return current_allocated_bytes_;
178 return fz::getPoolUsedMemHigh(mem_pool_);
179 }
180
182 size_t getAllocationCount() const { return allocations_.size() + persistent_allocations_.size(); }
183
184 void printStats() const;
185
190 size_t getConfiguredSize() const { return config_.getPoolSize(); }
191
193 fz::mempool_t getMemPool() const { return mem_pool_; }
194
196 bool isFallbackMode() const { return mem_pool_ == nullptr; }
197
198 int getDeviceId() const { return config_.device_id; }
199
200 // ── Lifetime token ────────────────────────────────────────────────────────
201
217 std::weak_ptr<const void> lifetimeToken() const { return alive_; }
218
219private:
221 std::shared_ptr<const void> alive_ = std::make_shared<const char>('\0');
222
223 MemoryPoolConfig config_;
224 fz::mempool_t mem_pool_;
225
226 std::unordered_map<void*, AllocationInfo> allocations_;
227 std::unordered_map<void*, AllocationInfo> persistent_allocations_;
228
229 // Persistent allocations (device + pinned host) — freed individually or in destructor.
230 std::vector<PersistentAllocInfo> persistent_allocs_;
231 size_t persistent_device_bytes_ = 0;
232 size_t persistent_pinned_bytes_ = 0;
233
234 size_t total_allocations_;
235 size_t total_frees_;
236 // Host-side running total of live bytes — used for overflow detection without
237 // querying a CUDA attribute on every hot-path allocation.
238 size_t current_allocated_bytes_;
239 // Set the first time current_allocated_bytes_ exceeds configured pool size
240 // so the overflow warning fires only once per reset() cycle.
241 bool overflow_warned_;
242 bool initialized_;
243
244 void initializeMemPool();
245};
246
247} // namespace fz
void synchronize(fz::stream_t stream)
size_t getPersistentPinnedBytes() const
Definition mempool.h:167
void freePersistentPinned(void *ptr)
bool isFallbackMode() const
Definition mempool.h:196
std::weak_ptr< const void > lifetimeToken() const
Definition mempool.h:217
fz::mempool_t getMemPool() const
Definition mempool.h:193
size_t getPeakUsage() const
Definition mempool.h:176
void * allocatePersistentPinned(size_t bytes, const std::string &tag="")
void free(void *ptr, fz::stream_t stream)
void reset(fz::stream_t stream)
size_t getCurrentUsage() const
Definition mempool.h:170
void * allocate(size_t size, fz::stream_t stream, const std::string &tag="", bool persistent=false)
size_t getAllocationCount() const
Definition mempool.h:182
void setReleaseThreshold(size_t bytes)
void * allocatePersistentDevice(size_t bytes, const std::string &tag="")
size_t getPersistentDeviceBytes() const
Definition mempool.h:164
size_t getConfiguredSize() const
Definition mempool.h:190
void freePersistentDevice(void *ptr)
Definition dag.h:24
size_t getPoolUsedMemCurrent(mempool_t pool)
Definition types.h:89
size_t getPoolUsedMemHigh(mempool_t pool)
Definition types.h:97
size_t size
Size in bytes.
Definition mempool.h:21
std::string tag
Debug label (e.g. "lorenzo_output").
Definition mempool.h:22
void * ptr
Device pointer.
Definition mempool.h:20
bool in_use
True while allocated.
Definition mempool.h:23
Definition mempool.h:46
int device_id
CUDA device index.
Definition mempool.h:49
bool enable_reuse
Enable opportunistic buffer reuse.
Definition mempool.h:50
size_t getPoolSize() const
Definition mempool.h:67
size_t input_data_size
Input byte count used to size the pool.
Definition mempool.h:47
float pool_size_multiplier
Pool capacity = input_data_size × multiplier.
Definition mempool.h:48
bool force_fallback
Skip pool creation and use cudaMalloc; for vGPU or testing.
Definition mempool.h:52
Definition mempool.h:38
bool is_pinned
true = cudaMallocHost; false = cudaMalloc
Definition mempool.h:42
Backend-neutral GPU type aliases.