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