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