FZGPUModules 2.0
GPU-accelerated modular compression pipelines
Loading...
Searching...
No Matches
hf_buf.h
1// Adapted from PHF reference (origin/v1.1.0_dev:modules/codec/huffman/hf_hl.hh)
2// Changes:
3// - Replaced #include "mem/cxx_smart_ptr.h" with local RAII wrappers below.
4// - Replaced #include "err.hh" (not needed in header; hf_buf.cc uses cuda_check.h).
5// - Removed HuffmanCodec<E> class (not used; we use Buf<E> + high_level<E> directly).
6// - Removed timer/io includes (not needed).
7// - Buf<E> refactored to use pool-managed raw pointers instead of unique_ptr members.
8// Constructor takes MemoryPool* and allocates via allocatePersistentDevice/Pinned.
9// Destructor asks the pool to free and untrack them via freePersistentDevice/Pinned.
10// The pool is the sole owner; Buf<E> holds non-owning raw pointers.
11
12#pragma once
13
14#include "backend/types.h"
15#include <cstddef>
16#include <cstdint>
17#include <memory>
18
19#include "hf.h"
20#include "hf_impl.hh"
21#include "mem/mempool.h"
22
23// ── HuffmanHelper (used by hf_kernels.cu) ────────────────────────────────────
24
25struct HuffmanHelper {
26 static const int BLOCK_DIM_ENCODE = 256;
27 static const int BLOCK_DIM_DEFLATE = 256;
28 static const int ENC_SEQUENTIALITY = 4;
29 static const int DEFLATE_CONSTANT = 4;
30};
31
32// ── HF_SPACE / HF_STREAM convenience macros ──────────────────────────────────
33
34#define HF_SPACE phf::Buf<E>
35#define HF_STREAM void*
36
37// ── phf::Buf<E> ──────────────────────────────────────────────────────────────
38
39namespace phf {
40
41template <typename E>
42struct Buf {
43 using H4 = uint32_t;
44 using M = PHF_METADATA;
45
46 struct RC {
47 static const int SCRATCH = 0;
48 static const int FREQ = 1;
49 static const int BK = 2;
50 static const int REVBK = 3;
51 static const int PAR_NBIT = 4;
52 static const int PAR_NCELL = 5;
53 static const int PAR_ENTRY = 6;
54 static const int BITSTREAM = 7;
55 static const int END = 8;
56 };
57
58 struct memcpy_helper {
59 void* const ptr;
60 size_t const nbyte;
61 size_t const dst;
62 };
63
64 using SYM = E;
65 using Header = phf_header;
66
67 // ── Fields ────────────────────────────────────────────────────────────────
68 const size_t len;
69 size_t pardeg;
70 size_t sublen;
71 const size_t bklen;
72 const size_t revbk4_bytes;
73 const size_t bitstream_max_len;
85 const size_t scratch4_len;
86
87 uint16_t rt_bklen;
88 int numSMs;
89
90 size_t total_footprint_d = 0;
91 size_t total_footprint_h = 0;
92
93 // Device scratch — raw pointers, allocated from pool_ on construction
94 H4* d_scratch4;
95 H4* h_scratch4;
96 PHF_BYTE* d_encoded; // alias into d_scratch4 (not a separate allocation)
97 PHF_BYTE* h_encoded; // alias into h_scratch4 (not a separate allocation)
98
99 H4* d_bitstream4;
100 H4* h_bitstream4;
101
102 H4* d_bk4;
103 H4* h_bk4;
104 PHF_BYTE* d_revbk4;
105 PHF_BYTE* h_revbk4;
106
107 // Per-partition metadata
108 M* d_par_nbit;
109 M* h_par_nbit;
110 M* d_par_ncell;
111 M* h_par_ncell;
112 M* d_par_entry;
113 M* h_par_entry;
114
115 // Histogram buffers — pre-allocated for forward execute; size = bklen each
116 uint32_t* d_freq;
117 uint32_t* h_freq;
118
119 // Device-book build status: status, flags, floor shift used, range offender.
120 // Kept persistent because terminal DeviceResident reads it only after the
121 // pipeline completion barrier.
122 uint32_t* d_book_meta;
123 uint32_t* h_book_meta;
124
125 // ── Static helpers ────────────────────────────────────────────────────────
126 static int _revbk4_bytes(int bklen);
127 static int _revbk8_bytes(int bklen);
128
140 static size_t _bitstream4_len(size_t pardeg, size_t sublen);
141
142 // Non-copyable, non-movable
143 Buf(const Buf&) = delete;
144 Buf& operator=(const Buf&) = delete;
145 Buf(Buf&&) = delete;
146 Buf& operator=(Buf&&) = delete;
147
148 // ── Constructor / destructor ──────────────────────────────────────────────
149
155 Buf(size_t inlen, size_t _bklen, fz::MemoryPool* pool, int _pardeg = -1);
156 ~Buf();
157
158 // ── Mutators ──────────────────────────────────────────────────────────────
159 void register_runtime_bklen(int _rt_bklen) { rt_bklen = _rt_bklen; }
160
161 void memcpy_merge(phf_header& header, phf_stream_t stream);
162 void clear_buffer();
163
164private:
165 fz::MemoryPool* pool_; // non-owning; used only in destructor to return allocations
167 std::weak_ptr<const void> pool_alive_;
168};
169
170// ── phf::high_level<E> ───────────────────────────────────────────────────────
171
172template <typename E>
173struct high_level {
174 // Build codebook from host histogram; H2D copies codebook and revbook.
175 static int build_book(Buf<E>* buf, uint32_t* h_hist,
176 uint16_t rt_bklen, HF_STREAM stream);
177
178 // GPU coarse encode: histogram must already be done (histogram D2H happened
179 // outside this function to fill h_hist before build_book was called).
180 // Output lives at buf->d_encoded; *outlen = phf_encoded_bytes(&header).
181 static int encode(Buf<E>* buf, E* in_data, size_t data_len,
182 uint8_t** out_encoded, size_t* encoded_len,
183 phf_header& header, HF_STREAM stream);
184
185 // GPU coarse decode: reads phf_header from in_encoded[0..127],
186 // reconstructs symbols into out_decoded.
187 static int decode(Buf<E>* buf, phf_header& header,
188 PHF_BYTE* in_encoded, E* out_decoded, HF_STREAM stream);
189};
190
191} // namespace phf
Definition mempool.h:82
Stream-ordered CUDA memory pool for pipeline buffer management.
Backend-neutral GPU type aliases.