| 1 |
/********************************************************************* |
| 2 |
Blosc - Blocked Shuffling and Compression Library |
| 3 |
|
| 4 |
Copyright (c) 2021 Blosc Development Team <[email protected]> |
| 5 |
https://blosc.org |
| 6 |
License: BSD 3-Clause (see LICENSE.txt) |
| 7 |
|
| 8 |
See LICENSE.txt for details about copyright and rights to use. |
| 9 |
**********************************************************************/ |
| 10 |
|
| 11 |
#include "stune.h" |
| 12 |
|
| 13 |
#include <stdbool.h> |
| 14 |
#include <stdio.h> |
| 15 |
|
| 16 |
|
| 17 |
/* Whether a codec is meant for High Compression Ratios |
| 18 |
Includes LZ4 + BITSHUFFLE here, but not BloscLZ + BITSHUFFLE because, |
| 19 |
for some reason, the latter does not work too well */ |
| 20 |
static bool is_HCR(blosc2_context * context) { |
| 21 |
switch (context->compcode) { |
| 22 |
case BLOSC_BLOSCLZ : |
| 23 |
return false; |
| 24 |
case BLOSC_LZ4 : |
| 25 |
// return (context->filter_flags & BLOSC_DOBITSHUFFLE) ? true : false; |
| 26 |
// Do not treat LZ4 differently than BloscLZ here |
| 27 |
return false; |
| 28 |
case BLOSC_LZ4HC : |
| 29 |
case BLOSC_ZLIB : |
| 30 |
case BLOSC_ZSTD : |
| 31 |
return true; |
| 32 |
default : |
| 33 |
return false; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
int blosc_stune_init(void * config, blosc2_context* cctx, blosc2_context* dctx) { |
| 38 |
BLOSC_UNUSED_PARAM(config); |
| 39 |
BLOSC_UNUSED_PARAM(cctx); |
| 40 |
BLOSC_UNUSED_PARAM(dctx); |
| 41 |
|
| 42 |
return BLOSC2_ERROR_SUCCESS; |
| 43 |
} |
| 44 |
|
| 45 |
// Set the automatic blocksize 0 to its real value |
| 46 |
int blosc_stune_next_blocksize(blosc2_context *context) { |
| 47 |
int32_t clevel = context->clevel; |
| 48 |
int32_t typesize = context->typesize; |
| 49 |
int32_t nbytes = context->sourcesize; |
| 50 |
int32_t user_blocksize = context->blocksize; |
| 51 |
int32_t blocksize = nbytes; |
| 52 |
|
| 53 |
// Protection against very small buffers |
| 54 |
if (nbytes < typesize) { |
| 55 |
context->blocksize = 1; |
| 56 |
return BLOSC2_ERROR_SUCCESS; |
| 57 |
} |
| 58 |
|
| 59 |
int splitmode = split_block(context, typesize, blocksize); |
| 60 |
if (user_blocksize) { |
| 61 |
blocksize = user_blocksize; |
| 62 |
goto last; |
| 63 |
} |
| 64 |
|
| 65 |
if (nbytes >= L1) { |
| 66 |
blocksize = L1; |
| 67 |
|
| 68 |
/* For HCR codecs, increase the block sizes by a factor of 2 because they |
| 69 |
are meant for compressing large blocks (i.e. they show a big overhead |
| 70 |
when compressing small ones). */ |
| 71 |
if (is_HCR(context)) { |
| 72 |
blocksize *= 2; |
| 73 |
} |
| 74 |
|
| 75 |
// Choose a different blocksize depending on the compression level |
| 76 |
switch (clevel) { |
| 77 |
case 0: |
| 78 |
// Case of plain copy |
| 79 |
blocksize /= 4; |
| 80 |
break; |
| 81 |
case 1: |
| 82 |
blocksize /= 2; |
| 83 |
break; |
| 84 |
case 2: |
| 85 |
blocksize *= 1; |
| 86 |
break; |
| 87 |
case 3: |
| 88 |
blocksize *= 2; |
| 89 |
break; |
| 90 |
case 4: |
| 91 |
case 5: |
| 92 |
blocksize *= 4; |
| 93 |
break; |
| 94 |
case 6: |
| 95 |
case 7: |
| 96 |
case 8: |
| 97 |
blocksize *= 8; |
| 98 |
break; |
| 99 |
case 9: |
| 100 |
// Do not exceed 256 KB for non HCR codecs |
| 101 |
blocksize *= 8; |
| 102 |
if (is_HCR(context)) { |
| 103 |
blocksize *= 2; |
| 104 |
} |
| 105 |
break; |
| 106 |
default: |
| 107 |
break; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
/* Now the blocksize for splittable codecs */ |
| 112 |
if (clevel > 0 && splitmode) { |
| 113 |
// For performance reasons, do not exceed 256 KB (it must fit in L2 cache) |
| 114 |
switch (clevel) { |
| 115 |
case 1: |
| 116 |
case 2: |
| 117 |
case 3: |
| 118 |
blocksize = 32 * 1024; |
| 119 |
break; |
| 120 |
case 4: |
| 121 |
case 5: |
| 122 |
case 6: |
| 123 |
blocksize = 64 * 1024; |
| 124 |
break; |
| 125 |
case 7: |
| 126 |
blocksize = 128 * 1024; |
| 127 |
break; |
| 128 |
case 8: |
| 129 |
blocksize = 256 * 1024; |
| 130 |
break; |
| 131 |
case 9: |
| 132 |
default: |
| 133 |
blocksize = 512 * 1024; |
| 134 |
break; |
| 135 |
} |
| 136 |
// Multiply by typesize to get proper split sizes |
| 137 |
blocksize *= typesize; |
| 138 |
// But do not exceed 4 MB per thread (having this capacity in L3 is normal in modern CPUs) |
| 139 |
if (blocksize > 4 * 1024 * 1024) { |
| 140 |
blocksize = 4 * 1024 * 1024; |
| 141 |
} |
| 142 |
if (blocksize < 32 * 1024) { |
| 143 |
/* Do not use a too small blocksize (< 32 KB) when typesize is small */ |
| 144 |
blocksize = 32 * 1024; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
last: |
| 149 |
/* Check that blocksize is not too large */ |
| 150 |
if (blocksize > nbytes) { |
| 151 |
blocksize = nbytes; |
| 152 |
} |
| 153 |
|
| 154 |
// blocksize *must absolutely* be a multiple of the typesize |
| 155 |
if (blocksize > typesize) { |
| 156 |
blocksize = blocksize / typesize * typesize; |
| 157 |
} |
| 158 |
|
| 159 |
context->blocksize = blocksize; |
| 160 |
BLOSC_INFO("compcode: %d, clevel: %d, blocksize: %d, splitmode: %d, typesize: %d", |
| 161 |
context->compcode, context->clevel, blocksize, splitmode, typesize); |
| 162 |
|
| 163 |
return BLOSC2_ERROR_SUCCESS; |
| 164 |
} |
| 165 |
|
| 166 |
int blosc_stune_next_cparams(blosc2_context * context) { |
| 167 |
BLOSC_UNUSED_PARAM(context); |
| 168 |
|
| 169 |
return BLOSC2_ERROR_SUCCESS; |
| 170 |
} |
| 171 |
|
| 172 |
int blosc_stune_update(blosc2_context * context, double ctime) { |
| 173 |
BLOSC_UNUSED_PARAM(context); |
| 174 |
BLOSC_UNUSED_PARAM(ctime); |
| 175 |
|
| 176 |
return BLOSC2_ERROR_SUCCESS; |
| 177 |
} |
| 178 |
|
| 179 |
int blosc_stune_free(blosc2_context * context) { |
| 180 |
BLOSC_UNUSED_PARAM(context); |
| 181 |
|
| 182 |
return BLOSC2_ERROR_SUCCESS; |
| 183 |
} |
| 184 |
|
| 185 |
int split_block(blosc2_context *context, int32_t typesize, int32_t blocksize) { |
| 186 |
switch (context->splitmode) { |
| 187 |
case BLOSC_ALWAYS_SPLIT: |
| 188 |
return 1; |
| 189 |
case BLOSC_NEVER_SPLIT: |
| 190 |
return 0; |
| 191 |
case BLOSC_FORWARD_COMPAT_SPLIT: |
| 192 |
case BLOSC_AUTO_SPLIT: |
| 193 |
// These cases will be handled later |
| 194 |
break; |
| 195 |
default: |
| 196 |
BLOSC_TRACE_WARNING("Unrecognized split mode. Default to BLOSC_FORWARD_COMPAT_SPLIT"); |
| 197 |
} |
| 198 |
|
| 199 |
int compcode = context->compcode; |
| 200 |
return ( |
| 201 |
// Fast codecs like blosclz, lz4 seems to prefer to split |
| 202 |
((compcode == BLOSC_BLOSCLZ) || (compcode == BLOSC_LZ4) |
| 203 |
// and low levels of zstd too |
| 204 |
|| ((compcode == BLOSC_ZSTD) && (context->clevel <= 5)) |
| 205 |
) && |
| 206 |
// ...but split seems to harm cratio too much when not using shuffle |
| 207 |
(context->filter_flags & BLOSC_DOSHUFFLE) && |
| 208 |
(typesize <= MAX_STREAMS) && |
| 209 |
(blocksize / typesize) >= BLOSC_MIN_BUFFERSIZE); |
| 210 |
} |