FZGPUModules 2.0
GPU-accelerated modular compression pipelines
Loading...
Searching...
No Matches
dag.h
Go to the documentation of this file.
1
5#pragma once
6
7/*
8 * ADVANCED API — no source-compatibility promise. The types here (CompressionDAG,
9 * DAGNode, BufferInfo, the fusion planner/registry) are pipeline internals exposed
10 * for advanced/experimental use; they may change or be removed in any release.
11 * Most users only need <fzgpumodules.h> / pipeline/compressor.h. See the API tiers
12 * in docs/api_reference.md.
13 */
14
15#include "pipeline/perf.h"
16
17#include "backend/types.h"
18#include <memory>
19#include <string>
20#include <unordered_map>
21#include <unordered_set>
22#include <vector>
23
24namespace fz {
25
26// Forward declarations
27class Stage;
28class MemoryPool;
29struct FusedImpl;
30
32enum class MemoryStrategy {
33 MINIMAL,
35};
36
38struct BufferInfo {
39 size_t size;
40 size_t initial_size;
41 size_t allocated_size;
42 void* d_ptr;
43 std::string tag;
44
45 int remaining_consumers;
46 std::vector<int> consumer_stage_ids;
47 int producer_stage_id;
48 int producer_output_index;
49
50 bool is_allocated;
55 size_t valid_size = 0;
56
58 : size(0), initial_size(0), allocated_size(0), d_ptr(nullptr), tag(""),
59 remaining_consumers(0), producer_stage_id(-1), producer_output_index(0),
60 is_allocated(false), is_persistent(false), is_external(false) {}
61};
62
64struct DAGNode {
65 int id;
66 Stage* stage;
67 std::string name;
68
69 std::vector<int> input_buffer_ids;
70 std::vector<int> output_buffer_ids;
71 std::unordered_map<int, int> output_index_to_buffer_id;
72
73 std::vector<DAGNode*> dependencies;
74 std::vector<DAGNode*> dependents;
75
76 int level;
77 int execution_order;
78 fz::stream_t stream;
79
80 bool is_executed;
81 fz::event_t completion_event;
82 fz::event_t start_event;
83
84 // Pre-sized vectors for execute() — allocated at finalize(), reused every call
85 // to avoid per-call heap allocations of the input/output/sizes arrays.
86 std::vector<void*> exec_inputs;
87 std::vector<void*> exec_outputs;
88 std::vector<size_t> exec_sizes;
89
90 DAGNode(Stage* s = nullptr)
91 : id(-1), stage(s), level(-1), execution_order(-1),
92 stream(nullptr), is_executed(false), completion_event(nullptr), start_event(nullptr) {}
93};
94
105public:
108
109 // ── Construction ──────────────────────────────────────────────────────────
110
112 DAGNode* addStage(Stage* stage, std::string name = "");
113
122 int addDependency(DAGNode* dependent, DAGNode* dependency,
123 size_t buffer_size = 0, int output_index = 0);
124
125 void setInputBuffer(DAGNode* node, size_t size, const std::string& tag = "input");
126 void setOutputBuffer(DAGNode* node, size_t size, const std::string& tag = "output");
127
132 int addUnconnectedOutput(DAGNode* node, size_t size, int output_index, const std::string& tag);
133
138 bool connectExistingOutput(DAGNode* producer, DAGNode* consumer, int output_index);
139
140 void updateBufferTag(int buffer_id, const std::string& tag);
141 void setBufferPersistent(int buffer_id, bool persistent);
142
144 void finalize();
145
147 void configureStreams(int num_streams);
148
149 // ── Execution ─────────────────────────────────────────────────────────────
150
151 void execute(fz::stream_t stream);
152
157 void preallocateBuffers(fz::stream_t stream = 0);
158
160 void reset(fz::stream_t stream = 0);
161
162 // ── Buffer access ─────────────────────────────────────────────────────────
163
164 void* getBuffer(int buffer_id) const;
165
170 void setExternalPointer(int buffer_id, void* external_ptr);
171
172 void updateBufferSize(int buffer_id, size_t new_size);
173
179 void setExternalValidSize(int buffer_id, size_t valid_bytes);
180
186 bool consumedOnlyByUnpaddedFusion(int buffer_id) const;
187
188 // ── Query & debug ─────────────────────────────────────────────────────────
189
191 size_t getTotalBufferSize() const;
192
201 size_t computeTopoPoolSize() const;
202
203 size_t getPeakMemoryUsage() const { return peak_memory_usage_; }
204 size_t getCurrentMemoryUsage() const { return current_memory_usage_; }
205 size_t getBufferSize(int buffer_id) const;
206 const BufferInfo& getBufferInfo(int buffer_id) const;
207 const std::vector<std::vector<DAGNode*>>& getLevels() const { return levels_; }
208 const std::vector<DAGNode*>& getNodes() const { return nodes_; }
209
211 int getMaxParallelism() const;
212
214 size_t getStreamCount() const { return streams_.size(); }
215
222 DAGNode* head = nullptr;
223 DAGNode* tail = nullptr;
224 DAGNode* dispatch = nullptr;
225 std::vector<DAGNode*> members;
226 std::vector<Stage*> stages;
227 const FusedImpl* impl = nullptr;
228 std::string execution_path;
229 bool executed = false;
230 };
231
233 void setFusedGroups(std::vector<FusedGroupExec> groups);
234 size_t getFusedGroupCount() const { return fused_groups_.size(); }
235 const std::vector<FusedGroupExec>& getFusedGroups() const { return fused_groups_; }
236 size_t getFusedInternalBufferCount() const { return fused_internal_buffers_.size(); }
237
238 void printDAG() const;
239 void printBufferLifetimes() const;
240
246 void enableBoundsCheck(bool enable) { bounds_check_enabled_ = enable; }
247 bool isBoundsCheckEnabled() const { return bounds_check_enabled_; }
248
254 void setColoringEnabled(bool enable) { coloring_disabled_ = !enable; }
255 bool isColoringEnabled() const { return coloring_applied_; }
256 size_t getColorRegionCount() const { return color_region_sizes_.size(); }
257
265 void setCaptureMode(bool capture);
266 bool isCaptureMode() const { return capture_mode_; }
267
268 // ── Profiling ─────────────────────────────────────────────────────────────
269
274 void enableProfiling(bool enable);
275 bool isProfilingEnabled() const { return profiling_enabled_; }
276
278 std::vector<StageTimingResult> collectTimings();
279
280private:
281 MemoryPool* mem_pool_;
282 MemoryStrategy strategy_;
283
284 std::vector<DAGNode*> nodes_;
285 std::unordered_map<int, BufferInfo> buffers_;
286
287 int next_buffer_id_;
288 bool is_finalized_;
289
290 std::vector<fz::stream_t> streams_;
291 bool owns_streams_;
292
293 std::vector<std::vector<DAGNode*>> levels_;
294 int max_level_;
295
296 size_t current_memory_usage_;
297 size_t peak_memory_usage_;
298
299 bool profiling_enabled_;
300 bool bounds_check_enabled_;
301 bool capture_mode_;
302
303 // Buffer coloring (PREALLOCATE only). Non-overlapping buffers share a color
304 // and are aliased into one pool region. color_region_ptrs_ owns the allocations.
305 bool coloring_disabled_;
306 bool coloring_applied_;
307 std::unordered_map<int, int> buffer_color_;
308 std::vector<size_t> color_region_sizes_;
309 std::vector<void*> color_region_ptrs_;
310
311 // Fusion: groups whose staged execute()s are replaced by one fused runner.
312 std::vector<FusedGroupExec> fused_groups_;
313 std::unordered_map<DAGNode*, size_t> fused_dispatch_;
314 std::unordered_set<DAGNode*> fused_member_;
317 std::unordered_set<int> fused_internal_buffers_;
318
319 void assignLevels();
320 void assignStreams();
321 void allocateBuffer(int buffer_id, fz::stream_t stream);
322 void freeBuffer(int buffer_id, fz::stream_t stream);
323 void planPreallocation();
324 void colorBuffers();
325
334 bool executeFusedNode(DAGNode* node, fz::stream_t stream);
335};
336
337} // namespace fz
Definition dag.h:104
DAGNode * addStage(Stage *stage, std::string name="")
void setCaptureMode(bool capture)
int addDependency(DAGNode *dependent, DAGNode *dependency, size_t buffer_size=0, int output_index=0)
void setExternalValidSize(int buffer_id, size_t valid_bytes)
void setColoringEnabled(bool enable)
Definition dag.h:254
size_t computeTopoPoolSize() const
size_t getTotalBufferSize() const
void preallocateBuffers(fz::stream_t stream=0)
bool consumedOnlyByUnpaddedFusion(int buffer_id) const
int addUnconnectedOutput(DAGNode *node, size_t size, int output_index, const std::string &tag)
void setExternalPointer(int buffer_id, void *external_ptr)
size_t getStreamCount() const
Definition dag.h:214
void enableBoundsCheck(bool enable)
Definition dag.h:246
void setFusedGroups(std::vector< FusedGroupExec > groups)
void enableProfiling(bool enable)
void reset(fz::stream_t stream=0)
std::vector< StageTimingResult > collectTimings()
void configureStreams(int num_streams)
int getMaxParallelism() const
bool connectExistingOutput(DAGNode *producer, DAGNode *consumer, int output_index)
Definition mempool.h:82
Definition stage.h:31
Definition dag.h:24
MemoryStrategy
Definition dag.h:32
@ MINIMAL
Allocate on-demand, free at last consumer. Lowest peak memory.
@ PREALLOCATE
Allocate everything upfront at finalize(). Required for graph mode.
Pipeline and per-stage profiling result types.
Definition dag.h:38
bool is_external
Definition dag.h:52
bool is_persistent
If true, survives reset() until DAG destruction.
Definition dag.h:51
size_t valid_size
Definition dag.h:55
DAGNode * dispatch
member whose schedule point launches the kernel
Definition dag.h:224
std::vector< DAGNode * > members
all group nodes (skipped individually)
Definition dag.h:225
DAGNode * head
first logical node on the fused main path
Definition dag.h:222
const FusedImpl * impl
matched fused implementation
Definition dag.h:227
std::vector< Stage * > stages
group stages for the runner context
Definition dag.h:226
DAGNode * tail
last node; its output = the archive
Definition dag.h:223
std::string execution_path
last runtime path, when reported by runner
Definition dag.h:228
bool executed
reset between DAG executions
Definition dag.h:229
Definition dag.h:64
fz::event_t start_event
Non-null only when profiling is enabled.
Definition dag.h:82
A registered fused implementation and its matcher.
Definition fusion_registry.h:90
Backend-neutral GPU type aliases.