FZGPUModules 2.0
GPU-accelerated modular compression pipelines
Loading...
Searching...
No Matches
tupl_stage.h
Go to the documentation of this file.
1#pragma once
2
21#include "stage/stage.h"
22#include "fzm_format.h"
23#include "backend/types.h"
24#include <cstdint>
25#include <cstring>
26#include <stdexcept>
27#include <string>
28#include <unordered_map>
29#include <vector>
30
31namespace fz {
32
47class TUPLStage : public Stage {
48public:
49 TUPLStage()
50 : is_inverse_(false)
51 , block_size_(16384)
52 , word_size_(1)
53 , dim_(2)
54 , actual_output_size_(0)
55 {}
56
57 // ── Stage control ──────────────────────────────────────────────────────
58 void setInverse(bool inv) override { is_inverse_ = inv; }
59 bool isInverse() const override { return is_inverse_; }
60
61 void setBlockSize(size_t bytes) { block_size_ = static_cast<uint32_t>(bytes); }
62 void setWordSize(size_t bytes) { word_size_ = static_cast<uint8_t>(bytes); }
63 void setDim(size_t dim) { dim_ = static_cast<uint8_t>(dim); }
64
65 size_t getBlockSize() const { return block_size_; }
66 size_t getRequiredInputAlignment() const override { return block_size_; }
67 size_t getWordSize() const { return word_size_; }
68 size_t getDim() const { return dim_; }
69
70 // ── Execution ──────────────────────────────────────────────────────────
71 void execute(
72 fz::stream_t stream,
73 MemoryPool* pool,
74 const std::vector<void*>& inputs,
75 const std::vector<void*>& outputs,
76 const std::vector<size_t>& sizes
77 ) override;
78
79 // ── Metadata ───────────────────────────────────────────────────────────
80 std::string getName() const override { return "TUPL"; }
81 size_t getNumInputs() const override { return 1; }
82 size_t getNumOutputs() const override { return 1; }
83
84 std::vector<size_t> estimateOutputSizes(
85 const std::vector<size_t>& input_sizes
86 ) const override {
87 // Size-preserving transform.
88 return {input_sizes[0]};
89 }
90
91 std::unordered_map<std::string, size_t>
92 getActualOutputSizesByName() const override {
93 return {{"output", actual_output_size_}};
94 }
95 size_t getActualOutputSize(int index) const override {
96 return (index == 0) ? actual_output_size_ : 0;
97 }
98
99 uint16_t getStageTypeId() const override {
100 return static_cast<uint16_t>(StageType::TUPL);
101 }
102
103 uint8_t getOutputDataType(size_t) const override {
104 // Raw byte stream — report as UINT8.
105 return static_cast<uint8_t>(DataType::UINT8);
106 }
107
108 // ── Serialization ──────────────────────────────────────────────────────
109 // Header: [0..3] block_size (uint32_t LE), [4] word_size (uint8_t), [5] dim (uint8_t)
111 size_t output_index, uint8_t* buf, size_t max_size
112 ) const override {
113 (void)output_index;
114 if (max_size < 6) return 0;
115 std::memcpy(buf, &block_size_, sizeof(uint32_t));
116 buf[4] = word_size_;
117 buf[5] = dim_;
118 return 6;
119 }
120
121 void deserializeHeader(const uint8_t* buf, size_t size) override {
122 if (size >= 4) std::memcpy(&block_size_, buf, sizeof(uint32_t));
123 if (size >= 5) word_size_ = buf[4];
124 if (size >= 6) dim_ = buf[5];
125 }
126
127 size_t getMaxHeaderSize(size_t) const override { return 6; }
128
129 void saveState() override {
130 saved_block_size_ = block_size_;
131 saved_word_size_ = word_size_;
132 saved_dim_ = dim_;
133 saved_actual_output_size_ = actual_output_size_;
134 }
135
136 void restoreState() override {
137 block_size_ = saved_block_size_;
138 word_size_ = saved_word_size_;
139 dim_ = saved_dim_;
140 actual_output_size_ = saved_actual_output_size_;
141 }
142
143private:
144 bool is_inverse_;
145 uint32_t block_size_;
146 uint32_t saved_block_size_ = 0;
147 uint8_t word_size_;
148 uint8_t saved_word_size_ = 0;
149 uint8_t dim_;
150 uint8_t saved_dim_ = 0;
151 size_t actual_output_size_ = 0;
152 size_t saved_actual_output_size_ = 0;
153
154 // Validate config. Unlike LC (fixed 16 KB chunk shared across all
155 // (dim, word_size) combos, so a chunk frequently doesn't divide evenly
156 // into whole tuples), block_size here is caller-chosen -- we only require
157 // it to be a whole number of words so per-block byte offsets stay
158 // word-aligned; leftover bytes that don't form a whole tuple within a
159 // block are still handled generically (see tupl_stage.cu), not banned.
160 void validateConfig() const {
161 if (word_size_ != 1 && word_size_ != 2 && word_size_ != 4 && word_size_ != 8)
162 throw std::invalid_argument(
163 "TUPLStage: word_size must be 1, 2, 4, or 8");
164 if (dim_ < 2)
165 throw std::invalid_argument("TUPLStage: dim must be >= 2");
166 if (block_size_ == 0 || block_size_ % word_size_ != 0)
167 throw std::invalid_argument(
168 "TUPLStage: block_size must be a positive multiple of word_size");
169 }
170};
171
172} // namespace fz
Definition mempool.h:82
Definition stage.h:30
Definition tupl_stage.h:47
void deserializeHeader(const uint8_t *buf, size_t size) override
Definition tupl_stage.h:121
void setInverse(bool inv) override
Definition tupl_stage.h:58
std::string getName() const override
Definition tupl_stage.h:80
void execute(fz::stream_t stream, MemoryPool *pool, const std::vector< void * > &inputs, const std::vector< void * > &outputs, const std::vector< size_t > &sizes) override
std::vector< size_t > estimateOutputSizes(const std::vector< size_t > &input_sizes) const override
Definition tupl_stage.h:84
size_t serializeHeader(size_t output_index, uint8_t *buf, size_t max_size) const override
Definition tupl_stage.h:110
uint8_t getOutputDataType(size_t) const override
Definition tupl_stage.h:103
void saveState() override
Definition tupl_stage.h:129
uint16_t getStageTypeId() const override
Definition tupl_stage.h:99
size_t getMaxHeaderSize(size_t) const override
Definition tupl_stage.h:127
std::unordered_map< std::string, size_t > getActualOutputSizesByName() const override
Definition tupl_stage.h:92
size_t getRequiredInputAlignment() const override
Definition tupl_stage.h:66
size_t getActualOutputSize(int index) const override
Definition tupl_stage.h:95
FZM binary file format definitions — structs, enums, and helpers.
Definition algorithms.h:48
@ TUPL
Tuple deinterleave (AoS -> SoA) transpose (LC framework lossless component)
Base class interface for all compression stages.
Backend-neutral GPU type aliases.