|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +#include "config.cuh" |
| 5 | +#include <cuda.h> |
| 6 | +#include <cuda_runtime.h> |
| 7 | +#include <stdint.h> |
| 8 | + |
| 9 | +// FSST decompression. A thread decodes one string at a time. |
| 10 | +// |
| 11 | +// Per-thread `Scratch` holds 24 bytes across three u64 lanes (`low`, `mid`, |
| 12 | +// `high`) plus a `cursor` byte counter. Byte i lives at bit (8 * (i mod 8)) |
| 13 | +// of: |
| 14 | +// low for i in 0..8 |
| 15 | +// mid for i in 8..16 |
| 16 | +// high for i in 16..24 |
| 17 | +// |
| 18 | +// lsb msb |
| 19 | +// low: [ b0 | b1 | b2 | b3 | b4 | b5 | b6 | b7 ] |
| 20 | +// mid: [ b8 | b9 |b10 |b11 |b12 |b13 |b14 |b15 ] |
| 21 | +// high: [b16 |b17 |b18 |b19 |b20 |b21 |b22 |b23 ] |
| 22 | +// |
| 23 | +// `Scratch::drain` picks the largest aligned store the gates allow |
| 24 | +// (alignment of out_pos, cursor, remaining out_end room). Bytes leave from |
| 25 | +// the low end (`low` byte 0); the kept bytes slide N positions toward that |
| 26 | +// low end across all three lanes i.e. each u64 right-shifts by N*8 and |
| 27 | +// pulls the next lane's low bits up to fill the vacated high bits. |
| 28 | +// `Scratch::push` inserts a length-`len` masked symbol at byte offset |
| 29 | +// `cursor`, spanning at most two of the three lanes. |
| 30 | +// |
| 31 | +// width gate ptx |
| 32 | +// ------ ------------------------------------------ ---------------- |
| 33 | +// 16 B out_pos % 16 == 0, cursor ≥ 16, room ≥ 16 st.global.v2.u64 |
| 34 | +// 8 B out_pos % 8 == 0, cursor ≥ 8, room ≥ 8 st.global.u64 |
| 35 | +// 4 B out_pos % 4 == 0, cursor ≥ 4, room ≥ 4 st.global.u32 |
| 36 | +// 2 B out_pos % 2 == 0, cursor ≥ 2, room ≥ 2 st.global.u16 |
| 37 | +// 1 B (always) st.global.u8 |
| 38 | +// |
| 39 | +// The narrow widths cover the prologue alignment-up (out_pos not yet |
| 40 | +// 16-aligned) and the epilogue tail (< 16 bytes left, no room for u128). |
| 41 | +// In steady state out_pos stays 16-aligned and u128 fires repeatedly. |
| 42 | +// |
| 43 | +// The 256-entry symbol table (≤ 2 KB) is read directly from global memory. |
| 44 | +// Staging it into shared memory measured ~3% slower at 10M rows and ~15% |
| 45 | +// slower at 1M rows (benchmarked on clickbench URLs). The hypothesis is that L1 |
| 46 | +// already holds the table after a few iterations and the explicit shared copy |
| 47 | +// adds bank-conflict latency on the warp-divergent `symbols[code]` reads; the |
| 48 | +// gap is wider at 1M because the kernel is less bandwidth-bound there, so |
| 49 | +// per-load latency shows up more. |
| 50 | +// |
| 51 | +// Decoded symbols are masked to their valid byte length so the table's high |
| 52 | +// bits never leak. The main loop drains to `scratch.cursor ≤ 16`, keeping |
| 53 | +// the next add (≤ 8 bytes) within the 24-byte capacity. |
| 54 | +// |
| 55 | +// `codes_offsets` is templated over the four unsigned integer widths |
| 56 | +// (u8/u16/u32/u64). `output_offsets` is always uint32_t because of the |
| 57 | +// MAX_BUFFER_LEN output limit. |
| 58 | + |
| 59 | +// 24-byte scratch buffer split across three u64 lanes. `cursor` is the |
| 60 | +// number of bytes currently buffered and the next-push offset. |
| 61 | +struct Scratch { |
| 62 | + uint64_t low = 0; |
| 63 | + uint64_t mid = 0; |
| 64 | + uint64_t high = 0; |
| 65 | + uint32_t cursor = 0; |
| 66 | + |
| 67 | + // Insert a length-`len` masked symbol at byte offset `cursor`. The |
| 68 | + // symbol spans at most two of the three lanes. Caller must ensure |
| 69 | + // cursor + len ≤ 24. |
| 70 | + __device__ inline void push(uint64_t sym, uint32_t len) { |
| 71 | + if (cursor < 8) { |
| 72 | + low |= sym << (8u * cursor); |
| 73 | + if (cursor + len > 8) { |
| 74 | + mid |= sym >> (8u * (8u - cursor)); |
| 75 | + } |
| 76 | + } else { |
| 77 | + mid |= sym << (8u * (cursor - 8u)); |
| 78 | + if (cursor + len > 16) { |
| 79 | + high |= sym >> (8u * (16u - cursor)); |
| 80 | + } |
| 81 | + } |
| 82 | + cursor += len; |
| 83 | + } |
| 84 | + |
| 85 | + // Emit one variable-width aligned store from the low end and slide the |
| 86 | + // kept bytes toward the low end across all three lanes. |
| 87 | + __device__ inline void drain(uint8_t *__restrict out, uint32_t &out_pos, uint32_t out_end) { |
| 88 | + if (cursor >= 16 && (out_pos & 15u) == 0 && out_pos + 16 <= out_end) { |
| 89 | + *reinterpret_cast<ulonglong2 *>(out + out_pos) = make_ulonglong2(low, mid); |
| 90 | + low = high; |
| 91 | + mid = 0; |
| 92 | + high = 0; |
| 93 | + out_pos += 16; |
| 94 | + cursor -= 16; |
| 95 | + } else if (cursor >= 8 && (out_pos & 7u) == 0 && out_pos + 8 <= out_end) { |
| 96 | + *reinterpret_cast<uint64_t *>(out + out_pos) = low; |
| 97 | + low = mid; |
| 98 | + mid = high; |
| 99 | + high = 0; |
| 100 | + out_pos += 8; |
| 101 | + cursor -= 8; |
| 102 | + } else if (cursor >= 4 && (out_pos & 3u) == 0 && out_pos + 4 <= out_end) { |
| 103 | + *reinterpret_cast<uint32_t *>(out + out_pos) = (uint32_t)low; |
| 104 | + low = (low >> 32) | (mid << 32); |
| 105 | + mid = (mid >> 32) | (high << 32); |
| 106 | + high >>= 32; |
| 107 | + out_pos += 4; |
| 108 | + cursor -= 4; |
| 109 | + } else if (cursor >= 2 && (out_pos & 1u) == 0 && out_pos + 2 <= out_end) { |
| 110 | + *reinterpret_cast<uint16_t *>(out + out_pos) = (uint16_t)low; |
| 111 | + low = (low >> 16) | (mid << 48); |
| 112 | + mid = (mid >> 16) | (high << 48); |
| 113 | + high >>= 16; |
| 114 | + out_pos += 2; |
| 115 | + cursor -= 2; |
| 116 | + } else { |
| 117 | + out[out_pos] = (uint8_t)low; |
| 118 | + low = (low >> 8) | (mid << 56); |
| 119 | + mid = (mid >> 8) | (high << 56); |
| 120 | + high >>= 8; |
| 121 | + out_pos += 1; |
| 122 | + cursor -= 1; |
| 123 | + } |
| 124 | + } |
| 125 | +}; |
| 126 | + |
| 127 | +template <typename OffT> |
| 128 | +__device__ inline void fsst_decode_string(const uint8_t *__restrict codes_bytes, |
| 129 | + const OffT *__restrict codes_offsets, |
| 130 | + const uint64_t *__restrict symbols, |
| 131 | + const uint8_t *__restrict symbol_lengths, |
| 132 | + const uint32_t *__restrict output_offsets, |
| 133 | + const uint8_t *__restrict validity_bits, |
| 134 | + uint8_t *__restrict output_bytes, |
| 135 | + uint64_t sid) { |
| 136 | + if (((validity_bits[sid >> 3] >> (sid & 7u)) & 1u) == 0u) { |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + OffT in_pos = codes_offsets[sid]; |
| 141 | + const OffT in_end = codes_offsets[sid + 1]; |
| 142 | + uint32_t out_pos = output_offsets[sid]; |
| 143 | + const uint32_t out_end = output_offsets[sid + 1]; |
| 144 | + |
| 145 | + Scratch scratch; |
| 146 | + |
| 147 | + while (in_pos < in_end) { |
| 148 | + // Drain to scratch.cursor ≤ 16 so the next ≤8-byte symbol fits in 24. |
| 149 | + while (scratch.cursor > 16) { |
| 150 | + scratch.drain(output_bytes, out_pos, out_end); |
| 151 | + } |
| 152 | + |
| 153 | + // Decode next code. 255 is the escape for raw literal bytes. |
| 154 | + const uint8_t code = codes_bytes[in_pos]; |
| 155 | + uint64_t sym; |
| 156 | + uint32_t len, consumed; |
| 157 | + if (code == 255) { |
| 158 | + sym = (uint64_t)codes_bytes[in_pos + 1]; |
| 159 | + len = 1; |
| 160 | + consumed = 2; |
| 161 | + } else { |
| 162 | + sym = symbols[code]; |
| 163 | + len = symbol_lengths[code]; |
| 164 | + consumed = 1; |
| 165 | + } |
| 166 | + |
| 167 | + // Zero out the symbol's high bytes beyond its valid length. |
| 168 | + const uint64_t mask = (len == 8) ? ~0ULL : ((1ULL << (8u * len)) - 1ULL); |
| 169 | + sym &= mask; |
| 170 | + |
| 171 | + scratch.push(sym, len); |
| 172 | + in_pos += (OffT)consumed; |
| 173 | + } |
| 174 | + |
| 175 | + // Epilogue: drain everything that's left. |
| 176 | + while (scratch.cursor > 0) { |
| 177 | + scratch.drain(output_bytes, out_pos, out_end); |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +#define GENERATE_FSST_KERNEL(suffix, OffT) \ |
| 182 | + extern "C" __global__ void fsst_##suffix(const uint8_t *__restrict codes_bytes, \ |
| 183 | + const OffT *__restrict codes_offsets, \ |
| 184 | + const uint64_t *__restrict symbols, \ |
| 185 | + const uint8_t *__restrict symbol_lengths, \ |
| 186 | + const uint32_t *__restrict output_offsets, \ |
| 187 | + const uint8_t *__restrict validity_bits, \ |
| 188 | + uint8_t *__restrict output_bytes, \ |
| 189 | + uint64_t num_strings) { \ |
| 190 | + const uint64_t elements_per_block = (uint64_t)blockDim.x * ELEMENTS_PER_THREAD; \ |
| 191 | + const uint64_t block_start = (uint64_t)blockIdx.x * elements_per_block; \ |
| 192 | + const uint64_t block_end = (block_start + elements_per_block < num_strings) \ |
| 193 | + ? (block_start + elements_per_block) \ |
| 194 | + : num_strings; \ |
| 195 | + \ |
| 196 | + for (uint64_t sid = block_start + threadIdx.x; sid < block_end; sid += blockDim.x) { \ |
| 197 | + fsst_decode_string<OffT>(codes_bytes, \ |
| 198 | + codes_offsets, \ |
| 199 | + symbols, \ |
| 200 | + symbol_lengths, \ |
| 201 | + output_offsets, \ |
| 202 | + validity_bits, \ |
| 203 | + output_bytes, \ |
| 204 | + sid); \ |
| 205 | + } \ |
| 206 | + } |
| 207 | + |
| 208 | +GENERATE_FSST_KERNEL(u8, uint8_t) |
| 209 | +GENERATE_FSST_KERNEL(u16, uint16_t) |
| 210 | +GENERATE_FSST_KERNEL(u32, uint32_t) |
| 211 | +GENERATE_FSST_KERNEL(u64, uint64_t) |
0 commit comments