Debian Codegraph @

options.go

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 package reedsolomon
2
3 import (
4 "runtime"
5 "strings"
6
7 "github.com/klauspost/cpuid/v2"
8 )
9
10 // Option allows to override processing parameters.
11 type Option func(*options)
12
13 type options struct {
14 maxGoroutines int
15 minSplitSize int
16 shardSize int
17 perRound int
18
19 useAvxGNFI,
20 useAvx512GFNI,
21 useAVX512,
22 useAVX2,
23 useSSSE3,
24 useSSE2,
25 useNEON,
26 useSVE bool
27 vectorLength int
28 skip2B bool
29
30 useJerasureMatrix bool
31 usePAR1Matrix bool
32 useCauchy bool
33 fastOneParity bool
34 inversionCache bool
35 forcedInversionCache bool
36 customMatrix [][]byte
37 withLeopard leopardMode
38 workAlloc WorkAllocator
39
40 // stream options
41 concReads bool
42 concWrites bool
43 streamBS int
44 }
45
46 var defaultOptions = options{
47 maxGoroutines: 384,
48 minSplitSize: -1,
49 fastOneParity: false,
50 inversionCache: true,
51
52 // Detect CPU capabilities.
53 useSSSE3: cpuid.CPU.Supports(cpuid.SSSE3),
54 useSSE2: cpuid.CPU.Supports(cpuid.SSE2),
55 useAVX2: cpuid.CPU.Supports(cpuid.AVX2),
56 useAVX512: cpuid.CPU.Supports(cpuid.AVX512F, cpuid.AVX512BW, cpuid.AVX512VL),
57 useAvx512GFNI: cpuid.CPU.Supports(cpuid.AVX512F, cpuid.GFNI, cpuid.AVX512DQ),
58 useAvxGNFI: cpuid.CPU.Supports(cpuid.AVX, cpuid.GFNI),
59 useNEON: cpuid.CPU.Supports(cpuid.ASIMD),
60 useSVE: cpuid.CPU.Supports(cpuid.SVE),
61 vectorLength: 32, // default vector length is 32 bytes (256 bits) for AVX2 code gen
62 }
63
64 // leopardMode controls the use of leopard GF in encoding and decoding.
65 type leopardMode int
66
67 const (
68 // leopardAsNeeded only switches to leopard 16-bit when there are more than
69 // 256 shards.
70 leopardAsNeeded leopardMode = iota
71 // leopardGF16 uses leopard in 16-bit mode for all shard counts.
72 leopardGF16
73 // leopardAlways uses 8-bit leopard for shards less than or equal to 256,
74 // 16-bit leopard otherwise.
75 leopardAlways
76 )
77
78 func init() {
79 if runtime.GOMAXPROCS(0) <= 1 {
80 defaultOptions.maxGoroutines = 1
81 }
82 }
83
84 // WithMaxGoroutines is the maximum number of goroutines number for encoding & decoding.
85 // Jobs will be split into this many parts, unless each goroutine would have to process
86 // less than minSplitSize bytes (set with WithMinSplitSize).
87 // For the best speed, keep this well above the GOMAXPROCS number for more fine grained
88 // scheduling.
89 // If n <= 0, it is ignored.
90 func WithMaxGoroutines(n int) Option {
91 return func(o *options) {
92 if n > 0 {
93 o.maxGoroutines = n
94 }
95 }
96 }
97
98 // WithAutoGoroutines will adjust the number of goroutines for optimal speed with a
99 // specific shard size.
100 // Send in the shard size you expect to send. Other shard sizes will work, but may not
101 // run at the optimal speed.
102 // Overwrites WithMaxGoroutines.
103 // If shardSize <= 0, it is ignored.
104 func WithAutoGoroutines(shardSize int) Option {
105 return func(o *options) {
106 o.shardSize = shardSize
107 }
108 }
109
110 // WithMinSplitSize is the minimum encoding size in bytes per goroutine.
111 // By default this parameter is determined by CPU cache characteristics.
112 // See WithMaxGoroutines on how jobs are split.
113 // If n <= 0, it is ignored.
114 func WithMinSplitSize(n int) Option {
115 return func(o *options) {
116 if n > 0 {
117 o.minSplitSize = n
118 }
119 }
120 }
121
122 // WithConcurrentStreams will enable concurrent reads and writes on the streams.
123 // Default: Disabled, meaning only one stream will be read/written at the time.
124 // Ignored if not used on a stream input.
125 func WithConcurrentStreams(enabled bool) Option {
126 return func(o *options) {
127 o.concReads, o.concWrites = enabled, enabled
128 }
129 }
130
131 // WithConcurrentStreamReads will enable concurrent reads from the input streams.
132 // Default: Disabled, meaning only one stream will be read at the time.
133 // Ignored if not used on a stream input.
134 func WithConcurrentStreamReads(enabled bool) Option {
135 return func(o *options) {
136 o.concReads = enabled
137 }
138 }
139
140 // WithConcurrentStreamWrites will enable concurrent writes to the the output streams.
141 // Default: Disabled, meaning only one stream will be written at the time.
142 // Ignored if not used on a stream input.
143 func WithConcurrentStreamWrites(enabled bool) Option {
144 return func(o *options) {
145 o.concWrites = enabled
146 }
147 }
148
149 // WithInversionCache allows to control the inversion cache.
150 // This will cache reconstruction matrices so they can be reused.
151 // Enabled by default, or <= 64 shards for Leopard encoding.
152 func WithInversionCache(enabled bool) Option {
153 return func(o *options) {
154 o.inversionCache = enabled
155 o.forcedInversionCache = true
156 }
157 }
158
159 // WithStreamBlockSize allows to set a custom block size per round of reads/writes.
160 // If not set, any shard size set with WithAutoGoroutines will be used.
161 // If WithAutoGoroutines is also unset, 4MB will be used.
162 // Ignored if not used on stream.
163 func WithStreamBlockSize(n int) Option {
164 return func(o *options) {
165 o.streamBS = n
166 }
167 }
168
169 // WithSSSE3 allows to enable/disable SSSE3 instructions.
170 // If not set, SSSE3 will be turned on or off automatically based on CPU ID information.
171 func WithSSSE3(enabled bool) Option {
172 return func(o *options) {
173 o.useSSSE3 = enabled
174 }
175 }
176
177 // WithAVX2 allows to enable/disable AVX2 instructions.
178 // If not set, AVX will be turned on or off automatically based on CPU ID information.
179 // This will also disable AVX GFNI instructions.
180 func WithAVX2(enabled bool) Option {
181 return func(o *options) {
182 o.useAVX2 = enabled
183 if o.useAvxGNFI {
184 o.useAvxGNFI = enabled
185 }
186 }
187 }
188
189 // WithSSE2 allows to enable/disable SSE2 instructions.
190 // If not set, SSE2 will be turned on or off automatically based on CPU ID information.
191 func WithSSE2(enabled bool) Option {
192 return func(o *options) {
193 o.useSSE2 = enabled
194 }
195 }
196
197 // WithAVX512 allows to enable/disable AVX512 (and GFNI) instructions.
198 func WithAVX512(enabled bool) Option {
199 return func(o *options) {
200 o.useAVX512 = enabled
201 o.useAvx512GFNI = enabled
202 }
203 }
204
205 // WithGFNI allows to enable/disable AVX512+GFNI instructions.
206 // If not set, GFNI will be turned on or off automatically based on CPU ID information.
207 func WithGFNI(enabled bool) Option {
208 return func(o *options) {
209 o.useAvx512GFNI = enabled
210 }
211 }
212
213 // WithAVXGFNI allows to enable/disable GFNI with AVX instructions.
214 // If not set, GFNI will be turned on or off automatically based on CPU ID information.
215 func WithAVXGFNI(enabled bool) Option {
216 return func(o *options) {
217 o.useAvxGNFI = enabled
218 }
219 }
220
221 // WithJerasureMatrix causes the encoder to build the Reed-Solomon-Vandermonde
222 // matrix in the same way as done by the Jerasure library.
223 // The first row and column of the coding matrix only contains 1's in this method
224 // so the first parity chunk is always equal to XOR of all data chunks.
225 func WithJerasureMatrix() Option {
226 return func(o *options) {
227 o.useJerasureMatrix = true
228 o.usePAR1Matrix = false
229 o.useCauchy = false
230 }
231 }
232
233 // WithPAR1Matrix causes the encoder to build the matrix how PARv1
234 // does. Note that the method they use is buggy, and may lead to cases
235 // where recovery is impossible, even if there are enough parity
236 // shards.
237 func WithPAR1Matrix() Option {
238 return func(o *options) {
239 o.useJerasureMatrix = false
240 o.usePAR1Matrix = true
241 o.useCauchy = false
242 }
243 }
244
245 // WithCauchyMatrix will make the encoder build a Cauchy style matrix.
246 // The output of this is not compatible with the standard output.
247 // A Cauchy matrix is faster to generate. This does not affect data throughput,
248 // but will result in slightly faster start-up time.
249 func WithCauchyMatrix() Option {
250 return func(o *options) {
251 o.useJerasureMatrix = false
252 o.usePAR1Matrix = false
253 o.useCauchy = true
254 }
255 }
256
257 // WithFastOneParityMatrix will switch the matrix to a simple xor
258 // if there is only one parity shard.
259 // The PAR1 matrix already has this property so it has little effect there.
260 func WithFastOneParityMatrix() Option {
261 return func(o *options) {
262 o.fastOneParity = true
263 }
264 }
265
266 // WithCustomMatrix causes the encoder to use the manually specified matrix.
267 // customMatrix represents only the parity chunks.
268 // customMatrix must have at least ParityShards rows and DataShards columns.
269 // It can be used for interoperability with libraries which generate
270 // the matrix differently or to implement more complex coding schemes like LRC
271 // (locally reconstructible codes).
272 func WithCustomMatrix(customMatrix [][]byte) Option {
273 return func(o *options) {
274 o.customMatrix = customMatrix
275 }
276 }
277
278 // WithLeopardGF16 will always use leopard GF16 for encoding,
279 // even when there is less than 256 shards.
280 // This will likely improve reconstruction time for some setups.
281 // This is not compatible with Leopard output for <= 256 shards.
282 // Note that Leopard places certain restrictions on use see other documentation.
283 func WithLeopardGF16(enabled bool) Option {
284 return func(o *options) {
285 if enabled {
286 o.withLeopard = leopardGF16
287 } else {
288 o.withLeopard = leopardAsNeeded
289 }
290 }
291 }
292
293 // WithLeopardGF will use leopard GF for encoding, even when there are fewer than
294 // 256 shards.
295 // This will likely improve reconstruction time for some setups.
296 // Note that Leopard places certain restrictions on use see other documentation.
297 func WithLeopardGF(enabled bool) Option {
298 return func(o *options) {
299 if enabled {
300 o.withLeopard = leopardAlways
301 } else {
302 o.withLeopard = leopardAsNeeded
303 }
304 }
305 }
306
307 // WithWorkAllocator sets an external allocator for the leopard encoder's
308 // temporary work buffers. When provided, the encoder calls [WorkAllocator.Get]
309 // and [WorkAllocator.Put] instead of using its internal sync.Pool.
310 //
311 // This has no effect on non-leopard encoders.
312 func WithWorkAllocator(alloc WorkAllocator) Option {
313 return func(o *options) {
314 o.workAlloc = alloc
315 }
316 }
317
318 func (o *options) cpuOptions() string {
319 var res []string
320 if o.useSSE2 {
321 res = append(res, "SSE2")
322 }
323 if o.useAVX2 {
324 res = append(res, "AVX2")
325 }
326 if o.useSSSE3 {
327 res = append(res, "SSSE3")
328 }
329 if o.useAVX512 {
330 res = append(res, "AVX512")
331 }
332 if o.useAvx512GFNI {
333 res = append(res, "AVX512+GFNI")
334 }
335 if o.useAvxGNFI {
336 res = append(res, "AVX+GFNI")
337 }
338 if o.useSVE {
339 res = append(res, "ARM+SVE")
340 } else if o.useNEON {
341 res = append(res, "ARM+NEON")
342 }
343 if len(res) == 0 {
344 return "pure Go"
345 }
346 return strings.Join(res, ",")
347 }