FZGPUModules 2.0
GPU-accelerated modular compression pipelines
Loading...
Searching...
No Matches
rare_stage.h
Go to the documentation of this file.
1#pragma once
2
34#include "stage/stage.h"
35#include "fzm_format.h"
36#include "backend/types.h"
37#include <cstdint>
38#include <cstring>
39#include <stdexcept>
40#include <string>
41#include <unordered_map>
42#include <vector>
43
44namespace fz {
45
63class RAREStage : public Stage {
64public:
65 RAREStage()
66 : is_inverse_(false)
67 , chunk_size_(16384)
68 , word_size_(1)
69 , actual_output_size_(0)
70 , cached_orig_bytes_(0)
71 , d_scratch_(nullptr)
72 , d_sizes_dev_(nullptr)
73 , d_clean_dev_(nullptr)
74 , d_dst_off_dev_(nullptr)
75 , scratch_capacity_(0)
76 {}
77
78 ~RAREStage() override;
79
80 // ── Stage control ──────────────────────────────────────────────────────
81 void setInverse(bool inv) override { is_inverse_ = inv; }
82 bool isInverse() const override { return is_inverse_; }
83
87 bool isGraphCompatible() const override { return !is_inverse_; }
88
89 void setChunkSize(size_t bytes) { chunk_size_ = static_cast<uint32_t>(bytes); }
90 void setWordSize(size_t bytes) { word_size_ = static_cast<uint8_t>(bytes); }
91
92 size_t getChunkSize() const { return chunk_size_; }
93 size_t getRequiredInputAlignment() const override { return chunk_size_; }
94 int getWordSize() const { return static_cast<int>(word_size_); }
95 uint32_t getCachedOrigBytes() const { return cached_orig_bytes_; }
96
97 // ── Execution ──────────────────────────────────────────────────────────
98 void execute(
99 cudaStream_t stream,
100 MemoryPool* pool,
101 const std::vector<void*>& inputs,
102 const std::vector<void*>& outputs,
103 const std::vector<size_t>& sizes
104 ) override;
105 void postStreamSync(cudaStream_t stream) override;
106
107 // ── Metadata ───────────────────────────────────────────────────────────
108 std::string getName() const override { return "RARE"; }
109 size_t getNumInputs() const override { return 1; }
110 size_t getNumOutputs() const override { return 1; }
111
112 std::vector<size_t> estimateOutputSizes(
113 const std::vector<size_t>& input_sizes
114 ) const override {
115 if (is_inverse_) {
116 if (cached_orig_bytes_ > 0)
117 return {static_cast<size_t>(cached_orig_bytes_)};
118 return {input_sizes.empty() ? 0 : input_sizes[0]};
119 }
120 // Forward: worst case = original data + stream header.
121 const size_t n_bytes = input_sizes.empty() ? 0 : input_sizes[0];
122 const size_t n_chunks = (n_bytes + chunk_size_ - 1) / chunk_size_;
123 const size_t hdr = 4 + 4 + 4 * n_chunks;
124 // postStreamSync()/getActualOutputSizesByName() always round the final
125 // size up to a 4-byte boundary and zero-fill the pad, even when the
126 // real total isn't already aligned (e.g. a partial final chunk stored
127 // raw at a byte count that isn't a multiple of 4) -- reserve that pad
128 // here too, or the caller's allocation is up to 3 bytes short and the
129 // memset in postStreamSync writes out of bounds.
130 const size_t worst = n_bytes + hdr;
131 return {(worst + 3) & ~size_t(3)};
132 }
133
134 std::unordered_map<std::string, size_t>
136 size_t getActualOutputSize(int index) const override;
137
147 const std::vector<size_t>& input_sizes
148 ) const override {
149 if (is_inverse_ || input_sizes.empty()) return 0;
150 const size_t in_bytes = input_sizes[0];
151 const size_t n_chunks = (in_bytes + chunk_size_ - 1) / chunk_size_;
152 return n_chunks * (static_cast<size_t>(chunk_size_) + 3 * sizeof(uint32_t));
153 }
154
155 uint16_t getStageTypeId() const override {
156 return static_cast<uint16_t>(StageType::RARE);
157 }
158
159 uint8_t getOutputDataType(size_t) const override {
160 return static_cast<uint8_t>(DataType::UINT8);
161 }
162
163 // ── Serialization ──────────────────────────────────────────────────────
165 size_t output_index, uint8_t* buf, size_t max_size
166 ) const override {
167 (void)output_index;
168 if (max_size < 9) return 0;
169 std::memcpy(buf, &chunk_size_, sizeof(uint32_t));
170 buf[4] = word_size_;
171 std::memcpy(buf + 5, &cached_orig_bytes_, sizeof(uint32_t));
172 return 9;
173 }
174
175 void deserializeHeader(const uint8_t* buf, size_t size) override {
176 if (size >= 4) std::memcpy(&chunk_size_, buf, sizeof(uint32_t));
177 if (size >= 5) word_size_ = buf[4];
178 if (size >= 9) std::memcpy(&cached_orig_bytes_, buf + 5, sizeof(uint32_t));
179 }
180
181 size_t getMaxHeaderSize(size_t) const override { return 9; }
182
183 void saveState() override {
184 saved_chunk_size_ = chunk_size_;
185 saved_word_size_ = word_size_;
186 saved_cached_orig_bytes_ = cached_orig_bytes_;
187 }
188
189 void restoreState() override {
190 chunk_size_ = saved_chunk_size_;
191 word_size_ = saved_word_size_;
192 cached_orig_bytes_ = saved_cached_orig_bytes_;
193 }
194
195private:
196 bool is_inverse_;
197 uint32_t chunk_size_;
198 uint32_t saved_chunk_size_ = 0;
199 uint8_t word_size_;
200 uint8_t saved_word_size_ = 0;
201 size_t actual_output_size_;
202 uint32_t cached_orig_bytes_ = 0;
203 uint32_t saved_cached_orig_bytes_ = 0;
204
205 // ── Persistent forward scratch buffers ───────────────────────────────────
206 uint8_t* d_scratch_;
207 uint32_t* d_sizes_dev_;
208 uint32_t* d_clean_dev_;
209 uint32_t* d_dst_off_dev_;
210 mutable bool tail_readback_pending_ = false;
211 mutable cudaStream_t tail_readback_stream_ = nullptr;
212 mutable uint32_t tail_last_index_ = 0;
213 mutable uint8_t* tail_output_ptr_ = nullptr;
214 size_t scratch_capacity_;
215 MemoryPool* scratch_pool_owner_ = nullptr;
216 bool scratch_from_pool_ = false;
217};
218
219} // namespace fz
Definition mempool.h:82
Definition rare_stage.h:63
void setInverse(bool inv) override
Definition rare_stage.h:81
size_t getMaxHeaderSize(size_t) const override
Definition rare_stage.h:181
void saveState() override
Definition rare_stage.h:183
bool isGraphCompatible() const override
Definition rare_stage.h:87
void deserializeHeader(const uint8_t *buf, size_t size) override
Definition rare_stage.h:175
void postStreamSync(cudaStream_t stream) override
std::unordered_map< std::string, size_t > getActualOutputSizesByName() const override
uint8_t getOutputDataType(size_t) const override
Definition rare_stage.h:159
void execute(cudaStream_t stream, MemoryPool *pool, const std::vector< void * > &inputs, const std::vector< void * > &outputs, const std::vector< size_t > &sizes) override
size_t getRequiredInputAlignment() const override
Definition rare_stage.h:93
size_t serializeHeader(size_t output_index, uint8_t *buf, size_t max_size) const override
Definition rare_stage.h:164
std::vector< size_t > estimateOutputSizes(const std::vector< size_t > &input_sizes) const override
Definition rare_stage.h:112
size_t estimateScratchBytes(const std::vector< size_t > &input_sizes) const override
Definition rare_stage.h:146
uint16_t getStageTypeId() const override
Definition rare_stage.h:155
size_t getActualOutputSize(int index) const override
std::string getName() const override
Definition rare_stage.h:108
Definition stage.h:30
FZM binary file format definitions — structs, enums, and helpers.
Definition algorithms.h:48
@ RARE
Repetition-Adaptive Reduction Encoding (LC framework, auto-k generalization of RRE)
Base class interface for all compression stages.
Backend-neutral GPU type aliases.