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