lc-framework-src 0.1.0+lc.2025

Source files for the LC compression framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
This file is part of the LC framework for synthesizing high-speed parallel lossless and error-bounded lossy data compression and decompression algorithms for CPUs and GPUs.

BSD 3-Clause License

Copyright (c) 2021-2025, Noushin Azami, Alex Fallin, Brandon Burtchell, Andrew Rodriguez, Benila Jerald, Yiqian Liu, Anju Mongandampulath Akathoott, and Martin Burtscher
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
   contributors may be used to endorse or promote products derived from
   this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

URL: The latest version of this code is available at https://github.com/burtscher/LC-framework.

Sponsor: This code is based upon work supported by the U.S. Department of Energy, Office of Science, Office of Advanced Scientific Research (ASCR), under contract DE-SC0022223.
*/


#ifndef GPU_RAZE
#define GPU_RAZE


#include "d_zero_elimination.h"
#include "d_repetition_elimination.h"


template <typename T>
static __device__ inline bool d_RAZE(int& csize, byte in [CS], byte out [CS], byte temp [CS])
{
  const int tid = threadIdx.x;
  const int size = csize / sizeof(T);  // words in chunk (rounded down)
  const int extra = csize % sizeof(T);
  const int bits = 8 * sizeof(T);
  assert(CS == 16384);
  T* const in_t = (T*)in;  // T cast
  T* const out_t = (T*)out;

  // count how many MSBs are zero
  int* const count = (int*)temp;  // bits + 2 elements (66)
  if (tid < bits) count[tid] = 0;
  __syncthreads();

  bool allzeros = true;
  for (int i = tid; i < size; i += TPB) {
    const T val = in_t[i];
    if (val != 0) allzeros = false;
    if constexpr (sizeof(T) == 8) {
      const int keep = (val == 0) ? 0 : (64 - __builtin_clzll((long long)val));
      atomicAdd_block(&count[keep], 1);
    } else {
      const int keep = (val == 0) ? 0 : (32 - __builtin_clz((int)val));
      atomicAdd_block(&count[keep], 1);
    }
  }
  allzeros = __syncthreads_and(allzeros);

  // special case if all values (other than extra) are zero
  if (allzeros) {
    // copy leftover bytes
    if constexpr (sizeof(T) > 1) {
      if (tid < extra) out[tid] = in[csize - extra + tid];
    }

    // output csize and keep
    if (tid == WS) {
      out[extra] = bits + 1;  // output special "keep" value
      out[extra + 1] = csize;  // first byte
      out[extra + 2] = csize >> 8;  // second byte
    }
    csize = extra + 3;
    return true;
  }

  // prefix sum and find best size
  if constexpr (bits <= WS) {
    if (tid < WS) {  // first warp only
      // prefix sum of counts
      const int lane = tid;
      int pfs = count[lane];
      int tmp = __shfl_up(pfs, 1);
      if (lane >= 1) pfs += tmp;
      tmp = __shfl_up(pfs, 2);
      if (lane >= 2) pfs += tmp;
      tmp = __shfl_up(pfs, 4);
      if (lane >= 4) pfs += tmp;
      if constexpr (bits > 8) {
        tmp = __shfl_up(pfs, 8);
        if (lane >= 8) pfs += tmp;
        if constexpr (bits > 16) {
          tmp = __shfl_up(pfs, 16);
          if (lane >= 16) pfs += tmp;
          #if defined(WS) && (WS == 64)
          if constexpr (bits > 32) {
            tmp = __shfl_up(pfs, 32);
            if (lane >= 32) pfs += tmp;
          }
          #endif
        }
      }
      count[lane] = pfs;

      // determine maximum savings
      const int sav = (bits <= lane) ? -1 : ((bits - lane) * pfs);
      int val = sav;
      val = max(val, __shfl_xor(val, 1));
      val = max(val, __shfl_xor(val, 2));
      val = max(val, __shfl_xor(val, 4));
      val = max(val, __shfl_xor(val, 8));
      val = max(val, __shfl_xor(val, 16));
      #if defined(WS) && (WS == 64)
      val = max(val, __shfl_xor(val, 32));
      const long long bal = __ballot(val == sav);
      const int who = __ffsll(bal) - 1;
      #else
      static_assert(WS == 32);
      const int bal = __ballot(val == sav);
      const int who = __ffs(bal) - 1;
      #endif
      if (lane == 0) count[64] = val;  // saved
      if (lane == 0) count[65] = who;  // keep
    }
  } else {
    assert(bits == WS * 2);
    if (tid < WS) {  // first warp only
      const int l0 = tid * 2;
      const int l1 = l0 + 1;

      // prefix sum of counts
      const int lane = tid;
      const int c1 = count[l1];
      int pfs = count[l0] + c1;
      int tmp = __shfl_up(pfs, 1);
      if (lane >= 1) pfs += tmp;
      tmp = __shfl_up(pfs, 2);
      if (lane >= 2) pfs += tmp;
      tmp = __shfl_up(pfs, 4);
      if (lane >= 4) pfs += tmp;
      if constexpr (bits > 8) {
        tmp = __shfl_up(pfs, 8);
        if (lane >= 8) pfs += tmp;
        if constexpr (bits > 16) {
          tmp = __shfl_up(pfs, 16);
          if (lane >= 16) pfs += tmp;
          #if defined(WS) && (WS == 64)
          if constexpr (bits > 32) {
            tmp = __shfl_up(pfs, 32);
            if (lane >= 32) pfs += tmp;
          }
          #endif
        }
      }
      count[l1] = pfs;
      count[l0] = pfs - c1;

      // determine maximum savings
      const int sav1 = (bits - l1) * pfs;
      const int sav0 = (bits - l0) * (pfs - c1);
      int val = max(sav0, sav1);
      val = max(val, __shfl_xor(val, 1));
      val = max(val, __shfl_xor(val, 2));
      val = max(val, __shfl_xor(val, 4));
      val = max(val, __shfl_xor(val, 8));
      val = max(val, __shfl_xor(val, 16));
      #if defined(WS) && (WS == 64)
      val = max(val, __shfl_xor(val, 32));
      const long long bal = __ballot((val == sav0) || (val == sav1));
      const int who = __ffsll(bal) - 1;
      #else
      static_assert(WS == 32);
      const int bal = __ballot((val == sav0) || (val == sav1));
      const int who = __ffs(bal) - 1;
      #endif
      if (lane == who) {
        count[64] = val;  // saved
        count[65] = (val == sav0) ? l0 : l1;  // keep
      }
    }
  }
  __syncthreads();

  const int saved = count[64];
  const int keep = count[65];
  const int countk = count[keep];

  // special case if all bits need to be kept
  if (saved == 0) {
    // output all values without bitmap (if they fit)
    if (csize + 3 >= CS) return false;
    for (int i = tid; i < size; i += TPB) {
      out_t[i] = in_t[i];
    }

    // copy leftover bytes
    if constexpr (sizeof(T) > 1) {
      if (tid < extra) out[csize - extra + tid] = in[csize - extra + tid];
    }

    // output csize and keep
    if (tid == 0) {
      out[csize] = bits;  // output special "keep" value
      out[csize + 1] = csize;  // first byte
      out[csize + 2] = csize >> 8;  // second byte
    }
    csize += 3;
    return true;
  }

  // keep some bits from each value (0 <= keep < bits)

  //zero out for atomic OR
  for (int i = tid + size - countk; i < size - countk + ((countk * keep + bits - 1) / bits); i += TPB) {
    out_t[i] = 0;
  }
  __syncthreads();

  // create bitmap
  byte* const bitmap = (byte*)&count[66];  // num elements

  // initialize
  const T tmask = ~(T)0 << keep;  // 111...00
  const T bmask = ~tmask;  // 000...11

  // determine wpos1
  const int ept = (((size + TPB - 1) / TPB + 7) / 8) * 8;  // elements per thread (multiple of 8)
  int cnt = 0;
  for (int i = tid * ept; i < min((tid + 1) * ept, size); i++) {
    const T val = in_t[i];
    if (0 != (val & tmask)) {
      cnt++;
    }
  }
  int wpos1 = block_prefix_sum(cnt, temp) - cnt;
  int wloc2 = bits * (size - countk) + (tid * ept - wpos1) * keep;
  int wpos2 = wloc2 / bits;

  // encode values and generate bitmap
  T oval = 0;
  byte bmp = 0;
  for (int i = tid * ept; i < min((tid + 1) * ept, size); i++) {
    const T val = in_t[i];
    if (0 != (val & tmask)) {
      bmp |= 1 << (i % 8);
      out_t[wpos1++] = val;  // output all bits
    } else {
      if (keep != 0) {
        // output bottom bits only
        const T bval = val & bmask;
        const int shift = wloc2 % bits;
        const int bms = bits - shift;
        oval |= bval << shift;
        if (bms <= keep) {
          out_t[wpos2++] = oval;  //atomicOr_block(&out_t[wpos2++], oval);
          oval = bval >> bms;
        }
        wloc2 += keep;
      }
    }
    if ((i % 8) == 7) {
      bitmap[i / 8] = bmp;
      bmp = 0;
    }
  }
  if ((tid * ept < size) && ((tid + 1) * ept > size)) {
    bitmap[size / 8] = bmp;
  }

  // zero out rest of bitmap
  for (int i = tid + (size + 7) / 8; i < CS / bits; i += TPB) {
    bitmap[i] = 0;
  }
  __syncthreads();

  // output last partial word
  if ((wloc2 % bits) != 0) {
    if constexpr (bits == 8) {
      atomicOr_block((int*)&out_t[wpos2 & ~3], (int)oval << (8 * (wpos2 & 3)));
    } else if constexpr (bits == 16) {
      atomicOr_block((int*)&out_t[wpos2 & ~1], (int)oval << (16 * (wpos2 & 1)));
    } else if constexpr (bits == 32) {
      atomicOr_block((int*)&out_t[wpos2], (int)oval);
    } else {
      atomicOr_block((unsigned long long*)&out_t[wpos2], (unsigned long long)oval);
    }      
  }

  // iteratively compress bitmaps
  const int avail = CS - 3 - extra;
  int wpos = (bits * (size - countk) + keep * countk + 7) / 8;
  int base = 0 / sizeof(T);
  int range = 2048 / sizeof(T);
  cnt = avail - wpos;
  if (!d_REencode<byte, 2048 / sizeof(T), true>(&bitmap[base], range, &out[wpos], cnt, &bitmap[base + range], (int*)temp)) return false;
  wpos += cnt;
  __syncthreads();

  base = 2048 / sizeof(T);
  range = 256 / sizeof(T);
  cnt = avail - wpos;
  if (!d_REencode<byte, 256 / sizeof(T), true>(&bitmap[base], range, &out[wpos], cnt, &bitmap[base + range], (int*)temp)) return false;
  wpos += cnt;
  __syncthreads();

  base = (2048 + 256) / sizeof(T);
  range = 32 / sizeof(T);
  if constexpr (sizeof(T) < 8) {
    cnt = avail - wpos;
    if (!d_REencode<byte, 32 / sizeof(T), true>(&bitmap[base], range, &out[wpos], cnt, &bitmap[base + range], (int*)temp)) return false;
    wpos += cnt;

    base = (2048 + 256 + 32) / sizeof(T);
    range = 4 / sizeof(T);
  }

  // output last level of bitmap
  if (wpos >= avail - range) return false;
  if (tid < range) {  // 4 / sizeof(T)
    out[wpos + tid] = bitmap[base + tid];
  }
  wpos += range;

  // copy leftover bytes
  if constexpr (sizeof(T) > 1) {
    if (tid < extra) out[wpos + tid] = in[csize - extra + tid];
  }

  // output old csize and update csize
  const int new_size = wpos + 3 + extra;
  if (tid == 0) {
    out[new_size - 3] = keep;  // output "keep" value
    out[new_size - 2] = csize;  // bottom byte
    out[new_size - 1] = csize >> 8;  // second byte
  }
  csize = new_size;
  return true;
}


/*
  bits*(size-count[keep]): full values
  keep*count[keep]: bottoms
  ?: compressed bitmap
  possible padding
  (csize%4)*8: extra
  8: keep
  16: csize
*/


template <typename T>
static __device__ inline void d_iRAZE(int& csize, byte in [CS], byte out [CS], byte temp [CS])
{
  // read in original csize and keep
  const int oldsize = in[csize - 2] + ((int)in[csize - 1] << 8);
  const int keep = in[csize - 3];

  const int tid = threadIdx.x;
  const int bits = 8 * sizeof(T);  // bits per word
  const int size = oldsize / sizeof(T);  // words in chunk (rounded down)
  const int extra = oldsize % sizeof(T);  // leftover bytes at end
  T* const in_t = (T*)in;  // T cast
  T* const out_t = (T*)out;
  assert(TPB >= 256);

  if (keep == bits + 1) {  // special case
    // all values (other than extra) are zero
    for (int i = tid; i < size; i += TPB) {
      out_t[i] = 0;
    }
  } else if (keep == bits) {  // keep all bits
    for (int i = tid; i < size; i += TPB) {
      out_t[i] = in_t[i];
    }
  } else {  // keep some bits from each value (0 <= keep < bits)
    assert(CS == 16384);
    int rpos = csize - 3 - extra;
    int* const temp_w = (int*)temp;
    byte* const bitmap = (byte*)&temp_w[WS];

    // iteratively decompress bitmaps
    int base, range;
    if constexpr (sizeof(T) == 8) {
      base = (2048 + 256) / sizeof(T);
      range = 32 / sizeof(T);
      // read in last level of bitmap
      rpos -= range;
      if (tid < range) bitmap[base + tid] = in[rpos + tid];
    } else {
      base = (2048 + 256 + 32) / sizeof(T);
      range = 4 / sizeof(T);
      // read in last level of bitmap
      rpos -= range;
      if (tid < range) bitmap[base + tid] = in[rpos + tid];

      rpos -= __syncthreads_count((tid < range * 8) && ((in[rpos + tid / 8] >> (tid % 8)) & 1));
      base = (2048 + 256) / sizeof(T);
      range = 32 / sizeof(T);
      d_REdecode<byte, 32 / sizeof(T)>(range, &in[rpos], &bitmap[base + range], &bitmap[base], temp_w);
    }
    __syncthreads();

    rpos -= __syncthreads_count((tid < range * 8) && ((bitmap[base + tid / 8] >> (tid % 8)) & 1));
    base = 2048 / sizeof(T);
    range = 256 / sizeof(T);
    d_REdecode<byte, 256 / sizeof(T)>(range, &in[rpos], &bitmap[base + range], &bitmap[base], temp_w);
    __syncthreads();

    if constexpr (sizeof(T) >= 4) {
      rpos -= __syncthreads_count((tid < range * 8) && ((bitmap[base + tid / 8] >> (tid % 8)) & 1));
    }
    if constexpr (sizeof(T) == 2) {
      int sum = __syncthreads_count((tid < range * 8) && ((bitmap[base + tid / 8] >> (tid % 8)) & 1));
      sum += __syncthreads_count((tid + TPB < range * 8) && ((bitmap[base + (tid + TPB) / 8] >> (tid % 8)) & 1));
      rpos -= sum;
    }
    if constexpr (sizeof(T) == 1) {
      int sum = 0;
      for (int i = 0; i < TPB * 4; i += TPB) {
        sum += __syncthreads_count((tid + i < range * 8) && ((bitmap[base + (tid + i) / 8] >> (tid % 8)) & 1));
      }
      rpos -= sum;
    }
    base = 0 / sizeof(T);
    range = 2048 / sizeof(T);
    d_REdecode<byte, 2048 / sizeof(T)>(range, &in[rpos], &bitmap[base + range], &bitmap[base], temp_w);
    __syncthreads();

    // determine rpos1 etc.
    const int ept = (((size + TPB - 1) / TPB + 7) / 8) * 8;  // elements per thread (multiple of 8)
    int cnt = 0;
    for (int i = tid * ept; i < min((tid + 1) * ept, size); i += 8) {
      cnt += __builtin_popcount((int)bitmap[i / 8]);
    }
    int rpos1 = block_prefix_sum(cnt, temp_w) - cnt;
    const int count = temp_w[TPB / WS - 1];
    int rloc2 = bits * count + (tid * ept - rpos1) * keep;
    int rpos2 = rloc2 / bits;

    // decode values
    const T tmask = ~(T)0 << keep;  // 111...00
    const T bmask = ~tmask;  // 000...11
    T ival = in_t[rpos2++];
    byte bmp;
    for (int i = tid * ept; i < min((tid + 1) * ept, size); i++) {
      T val = 0;
      if ((i % 8) == 0) bmp = bitmap[i / 8];
      if ((bmp >> (i % 8)) & 1) {
        val = in_t[rpos1++];  // read all bits
      } else {
        if (keep != 0) {
          // read only bottom bits
          const int shift = rloc2 % bits;
          const int bms = bits - shift;
          T res = ival >> shift;
          if (bms <= keep) {
            ival = in_t[rpos2++];
            res |= ival << bms;
          }
          rloc2 += keep;
          val = res & bmask;
        }
      }
      out_t[i] = val;
    }
  }

  // copy leftover bytes
  if constexpr (sizeof(T) > 1) {
    if (tid < extra) {
      out[oldsize - extra + tid] = in[csize - 3 - extra + tid];
    }
  }

  csize = oldsize;
}


#endif