FZGPUModules 2.0
GPU-accelerated modular compression pipelines
Loading...
Searching...
No Matches
golomb_rice_stage.h
Go to the documentation of this file.
1#pragma once
2
99
100#include "stage/stage.h"
101#include "fzm_format.h"
102#include "backend/types.h"
104#include <cstdint>
105#include <cstring>
106#include <memory>
107#include <stdexcept>
108#include <string>
109#include <type_traits>
110#include <unordered_map>
111#include <vector>
112
113namespace fz {
114
115template<typename T>
116class GolombRiceStage : public Stage {
117 static_assert(std::is_integral<T>::value && std::is_signed<T>::value,
118 "GolombRiceStage requires a signed integer type");
119public:
120 GolombRiceStage() = default;
121 ~GolombRiceStage() override;
122
123 // ── Stage control ──────────────────────────────────────────────────────
124 void setInverse(bool inv) override { is_inverse_ = inv; }
125 bool isInverse() const override { return is_inverse_; }
126
127 void setChunkSize(size_t bytes) { chunk_size_ = static_cast<uint32_t>(bytes); }
128 size_t getChunkSize() const { return chunk_size_; }
129 size_t getRequiredInputAlignment() const override { return chunk_size_; }
130 uint32_t getCachedOrigBytes() const { return cached_orig_bytes_; }
131
139 FusionSpec getFusionSpec() const override {
140 if (is_inverse_ || chunk_size_ != 16384u || !std::is_same<T, int32_t>::value)
141 return {};
142 return FusionSpec{FusionAccess::SegmentCodec, chunk_size_};
143 }
144
146 FusedOpDecl getFusedOp() const override {
147 if (!getFusionSpec().fusable()) return {};
148 return FusedOpDecl{FusionStrategy::ChunkCooperative, "GolombRiceCoder",
149 "fused/chunk_fusion/chunk_fusion.cuh", {}};
150 }
152 void setFusedArchiveResult(size_t archive_bytes, size_t orig_bytes) override {
153 setFusedResult(archive_bytes, orig_bytes);
154 }
160 void setFusedResult(size_t archive_bytes, size_t orig_bytes) {
161 actual_output_size_ = archive_bytes;
162 cached_orig_bytes_ = static_cast<uint32_t>(orig_bytes);
163 tail_readback_pending_ = false;
164 }
165
166 // ── Execution ──────────────────────────────────────────────────────────
167 void execute(
168 fz::stream_t stream,
169 MemoryPool* pool,
170 const std::vector<void*>& inputs,
171 const std::vector<void*>& outputs,
172 const std::vector<size_t>& sizes
173 ) override;
174 void postStreamSync(fz::stream_t stream) override;
175
176 // ── Metadata ───────────────────────────────────────────────────────────
177 std::string getName() const override { return "GolombRice"; }
178 size_t getNumInputs() const override { return 1; }
179 size_t getNumOutputs() const override { return 1; }
180
181 std::vector<size_t> estimateOutputSizes(
182 const std::vector<size_t>& input_sizes
183 ) const override {
184 if (is_inverse_) {
185 if (cached_orig_bytes_ > 0)
186 return {static_cast<size_t>(cached_orig_bytes_)};
187 return {input_sizes.empty() ? 0 : input_sizes[0]};
188 }
189 // Forward worst case: every chunk falls back to raw storage (original
190 // bytes + 1 flagged size word each) plus the stream header, plus the
191 // tail safety pad postStreamSync may need to zero (see execute()).
192 const size_t n_bytes = input_sizes.empty() ? 0 : input_sizes[0];
193 const size_t n_chunks = (n_bytes + chunk_size_ - 1) / chunk_size_;
194 const size_t hdr = 4 + 4 + 4 * n_chunks;
195 const size_t worst = n_bytes + hdr + kTailPad;
196 return {(worst + 3) & ~size_t(3)};
197 }
198
199 std::unordered_map<std::string, size_t>
200 getActualOutputSizesByName() const override {
201 return {{"output", actual_output_size_}};
202 }
203 size_t getActualOutputSize(int index) const override {
204 return index == 0 ? actual_output_size_ : 0;
205 }
206
218 size_t estimateScratchBytes(
219 const std::vector<size_t>& input_sizes
220 ) const override {
221 if (is_inverse_ || input_sizes.empty()) return 0;
222 const size_t in_bytes = input_sizes[0];
223 const size_t n_chunks = (in_bytes + chunk_size_ - 1) / chunk_size_;
224 return n_chunks * (chunkScratchStride() + 3 * sizeof(uint32_t));
225 }
226
227 uint16_t getStageTypeId() const override {
228 return static_cast<uint16_t>(StageType::GOLOMB_RICE);
229 }
230
231 uint8_t getOutputDataType(size_t) const override {
232 return static_cast<uint8_t>(DataType::UINT8);
233 }
234
235 // ── Serialization ──────────────────────────────────────────────────────
236 static DataType getElementDataType() { return fused::dataTypeOf<T>(); }
237
238 // [0..3] chunk_size, [4] element DataType, [5..8] original byte count. The
239 // original size lets a standalone decompress size the inverse output before the
240 // stream header is read (as RZE does); 5-byte headers from older archives still load.
241 size_t serializeHeader(size_t, uint8_t* buf, size_t max_size) const override {
242 if (max_size < 9) return 0;
243 std::memcpy(buf, &chunk_size_, sizeof(uint32_t));
244 buf[4] = static_cast<uint8_t>(getElementDataType());
245 std::memcpy(buf + 5, &cached_orig_bytes_, sizeof(uint32_t));
246 return 9;
247 }
248 void deserializeHeader(const uint8_t* buf, size_t size) override {
249 if (size >= 4) std::memcpy(&chunk_size_, buf, sizeof(uint32_t));
250 if (size >= 9) std::memcpy(&cached_orig_bytes_, buf + 5, sizeof(uint32_t));
251 }
252 size_t getMaxHeaderSize(size_t) const override { return 9; }
253
254 void saveState() override { saved_chunk_size_ = chunk_size_; }
255 void restoreState() override { chunk_size_ = saved_chunk_size_; }
256
258 uint32_t elemsPerChunk() const { return chunk_size_ / static_cast<uint32_t>(sizeof(T)); }
259
263 static constexpr uint32_t kEscapeQ = 24;
264 static constexpr uint32_t bitWidth() { return 8u * sizeof(T); }
266 static constexpr uint32_t kMaxCandidate() { return bitWidth() > 24 ? 24u : bitWidth() - 1; }
267
286 static constexpr uint32_t kIntervalsPerChunk = 16;
287
298 size_t chunkScratchStride() const {
299 const size_t worst_bits = static_cast<size_t>(elemsPerChunk()) * (kEscapeQ + bitWidth());
300 const size_t worst_bytes = (4 + 4 * kIntervalsPerChunk) /*k + offset table*/
301 + kIntervalsPerChunk /*per-interval rounding slack*/
302 + (worst_bits + 7) / 8;
303 return ((worst_bytes + 3) & ~size_t(3)) + kTailPad;
304 }
305 static constexpr size_t kTailPad = 16;
306
307private:
308 bool is_inverse_ = false;
309 uint32_t chunk_size_ = 16384;
310 uint32_t saved_chunk_size_ = 0;
311 size_t actual_output_size_ = 0;
312 uint32_t cached_orig_bytes_ = 0;
313 uint32_t saved_cached_orig_bytes_ = 0;
314
315 // ── Persistent forward scratch buffers ───────────────────────────────────
316 uint8_t* d_scratch_ = nullptr;
317 uint32_t* d_sizes_dev_ = nullptr;
318 uint32_t* d_dst_off_dev_ = nullptr;
319 mutable bool tail_readback_pending_ = false;
320 mutable uint32_t tail_last_index_ = 0;
321 mutable uint32_t tail_header_size_ = 0;
322 mutable uint8_t* tail_output_ptr_ = nullptr;
323 size_t scratch_capacity_ = 0;
324 MemoryPool* scratch_pool_owner_ = nullptr;
325 bool scratch_from_pool_ = false;
326 std::weak_ptr<const void> scratch_alive_;
327};
328
329} // namespace fz
Definition stage.h:31
virtual std::string getName() const =0
virtual void saveState()
Definition stage.h:179
virtual FusedOpDecl getFusedOp() const
Definition stage.h:300
virtual uint16_t getStageTypeId() const =0
virtual size_t getRequiredInputAlignment() const
Definition stage.h:88
virtual std::unordered_map< std::string, size_t > getActualOutputSizesByName() const =0
virtual FusionSpec getFusionSpec() const
Definition stage.h:289
Compile-time C++ type -> DataType enum mapping, shared by the fused stages that dispatch on multiple ...
FZM binary file format definitions — structs, enums, and helpers.
Definition dag.h:24
@ GOLOMB_RICE
Chunk-local Golomb-Rice entropy coder (exact per-chunk k, escape-bounded).
Definition fzm_format.h:131
DataType
Element data type identifiers used in buffer and stage descriptors.
Definition fzm_format.h:142
Base class interface for all compression stages.
Backend-neutral GPU type aliases.