| 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 |
// Enabling has no effect if the CPU does not support it. |
| 172 |
func WithSSSE3(enabled bool) Option { |
| 173 |
return func(o *options) { |
| •174 |
o.useSSSE3 = enabled && defaultOptions.useSSSE3 |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
// WithAVX2 allows to enable/disable AVX2 instructions. |
| 179 |
// If not set, AVX will be turned on or off automatically based on CPU ID information. |
| 180 |
// This will also disable AVX GFNI instructions. |
| •181 |
// Enabling has no effect if the CPU does not support it. |
| 182 |
func WithAVX2(enabled bool) Option { |
| 183 |
return func(o *options) { |
| •184 |
o.useAVX2 = enabled && defaultOptions.useAVX2 |
| 185 |
if o.useAvxGNFI { |
| 186 |
o.useAvxGNFI = enabled |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
// WithSSE2 allows to enable/disable SSE2 instructions. |
| 192 |
// If not set, SSE2 will be turned on or off automatically based on CPU ID information. |
| •193 |
// Enabling has no effect if the CPU does not support it. |
| 194 |
func WithSSE2(enabled bool) Option { |
| 195 |
return func(o *options) { |
| •196 |
o.useSSE2 = enabled && defaultOptions.useSSE2 |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
// WithAVX512 allows to enable/disable AVX512 (and GFNI) instructions. |
| •201 |
// Enabling has no effect if the CPU does not support it. |
| 202 |
// AVX512 and GFNI are separate CPU features, so this will only enable |
| 203 |
// GFNI if the CPU supports it as well. |
| 204 |
func WithAVX512(enabled bool) Option { |
| 205 |
return func(o *options) { |
| •206 |
o.useAVX512 = enabled && defaultOptions.useAVX512 |
| 207 |
o.useAvx512GFNI = enabled && defaultOptions.useAvx512GFNI |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
// WithGFNI allows to enable/disable AVX512+GFNI instructions. |
| 212 |
// If not set, GFNI will be turned on or off automatically based on CPU ID information. |
| •213 |
// Enabling has no effect if the CPU does not support it. |
| 214 |
func WithGFNI(enabled bool) Option { |
| 215 |
return func(o *options) { |
| •216 |
o.useAvx512GFNI = enabled && defaultOptions.useAvx512GFNI |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
// WithAVXGFNI allows to enable/disable GFNI with AVX instructions. |
| 221 |
// If not set, GFNI will be turned on or off automatically based on CPU ID information. |
| •222 |
// Enabling has no effect if the CPU does not support it. |
| 223 |
func WithAVXGFNI(enabled bool) Option { |
| 224 |
return func(o *options) { |
| •225 |
o.useAvxGNFI = enabled && defaultOptions.useAvxGNFI |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
// WithNEON allows to enable/disable NEON instructions. |
| 230 |
// If not set, NEON will be turned on or off automatically based on CPU ID information. |
| 231 |
// This will also disable SVE instructions. |
| 232 |
// Enabling has no effect if the CPU does not support it. |
| 233 |
func WithNEON(enabled bool) Option { |
| 234 |
return func(o *options) { |
| 235 |
o.useNEON = enabled && defaultOptions.useNEON |
| 236 |
if !o.useNEON { |
| 237 |
o.useSVE = false |
| 238 |
o.vectorLength = 32 |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
// WithSVE allows to enable/disable SVE instructions. |
| 244 |
// If not set, SVE will be turned on or off automatically based on CPU ID information. |
| 245 |
// Enabling has no effect if the CPU does not support it. |
| 246 |
func WithSVE(enabled bool) Option { |
| 247 |
return func(o *options) { |
| 248 |
o.useSVE = enabled && defaultOptions.useSVE |
| 249 |
// SVE uses the hardware vector length, everything else wants 32 byte tables. |
| 250 |
if o.useSVE { |
| 251 |
o.vectorLength = defaultOptions.vectorLength |
| 252 |
} else { |
| 253 |
o.vectorLength = 32 |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
// WithJerasureMatrix causes the encoder to build the Reed-Solomon-Vandermonde |
| 259 |
// matrix in the same way as done by the Jerasure library. |
| 260 |
// The first row and column of the coding matrix only contains 1's in this method |
| 261 |
// so the first parity chunk is always equal to XOR of all data chunks. |
| 262 |
func WithJerasureMatrix() Option { |
| 263 |
return func(o *options) { |
| 264 |
o.useJerasureMatrix = true |
| 265 |
o.usePAR1Matrix = false |
| 266 |
o.useCauchy = false |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
// WithPAR1Matrix causes the encoder to build the matrix how PARv1 |
| 271 |
// does. Note that the method they use is buggy, and may lead to cases |
| 272 |
// where recovery is impossible, even if there are enough parity |
| 273 |
// shards. |
| 274 |
func WithPAR1Matrix() Option { |
| 275 |
return func(o *options) { |
| 276 |
o.useJerasureMatrix = false |
| 277 |
o.usePAR1Matrix = true |
| 278 |
o.useCauchy = false |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
// WithCauchyMatrix will make the encoder build a Cauchy style matrix. |
| 283 |
// The output of this is not compatible with the standard output. |
| 284 |
// A Cauchy matrix is faster to generate. This does not affect data throughput, |
| 285 |
// but will result in slightly faster start-up time. |
| 286 |
func WithCauchyMatrix() Option { |
| 287 |
return func(o *options) { |
| 288 |
o.useJerasureMatrix = false |
| 289 |
o.usePAR1Matrix = false |
| 290 |
o.useCauchy = true |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
// WithFastOneParityMatrix will switch the matrix to a simple xor |
| 295 |
// if there is only one parity shard. |
| 296 |
// The PAR1 matrix already has this property so it has little effect there. |
| 297 |
func WithFastOneParityMatrix() Option { |
| 298 |
return func(o *options) { |
| 299 |
o.fastOneParity = true |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
// WithCustomMatrix causes the encoder to use the manually specified matrix. |
| 304 |
// customMatrix represents only the parity chunks. |
| 305 |
// customMatrix must have at least ParityShards rows and DataShards columns. |
| 306 |
// It can be used for interoperability with libraries which generate |
| 307 |
// the matrix differently or to implement more complex coding schemes like LRC |
| 308 |
// (locally reconstructible codes). |
| 309 |
func WithCustomMatrix(customMatrix [][]byte) Option { |
| 310 |
return func(o *options) { |
| 311 |
o.customMatrix = customMatrix |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
// WithLeopardGF16 will always use leopard GF16 for encoding, |
| 316 |
// even when there is less than 256 shards. |
| 317 |
// This will likely improve reconstruction time for some setups. |
| 318 |
// This is not compatible with Leopard output for <= 256 shards. |
| 319 |
// Note that Leopard places certain restrictions on use see other documentation. |
| 320 |
func WithLeopardGF16(enabled bool) Option { |
| 321 |
return func(o *options) { |
| 322 |
if enabled { |
| 323 |
o.withLeopard = leopardGF16 |
| 324 |
} else { |
| 325 |
o.withLeopard = leopardAsNeeded |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
// WithLeopardGF will use leopard GF for encoding, even when there are fewer than |
| 331 |
// 256 shards. |
| 332 |
// This will likely improve reconstruction time for some setups. |
| 333 |
// Note that Leopard places certain restrictions on use see other documentation. |
| 334 |
func WithLeopardGF(enabled bool) Option { |
| 335 |
return func(o *options) { |
| 336 |
if enabled { |
| 337 |
o.withLeopard = leopardAlways |
| 338 |
} else { |
| 339 |
o.withLeopard = leopardAsNeeded |
| 340 |
} |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
// WithWorkAllocator sets an external allocator for the leopard encoder's |
| 345 |
// temporary work buffers. When provided, the encoder calls [WorkAllocator.Get] |
| 346 |
// and [WorkAllocator.Put] instead of using its internal sync.Pool. |
| 347 |
// |
| 348 |
// This has no effect on non-leopard encoders. |
| 349 |
func WithWorkAllocator(alloc WorkAllocator) Option { |
| 350 |
return func(o *options) { |
| 351 |
o.workAlloc = alloc |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
func (o *options) cpuOptions() string { |
| 356 |
var res []string |
| 357 |
if o.useSSE2 { |
| 358 |
res = append(res, "SSE2") |
| 359 |
} |
| 360 |
if o.useAVX2 { |
| 361 |
res = append(res, "AVX2") |
| 362 |
} |
| 363 |
if o.useSSSE3 { |
| 364 |
res = append(res, "SSSE3") |
| 365 |
} |
| 366 |
if o.useAVX512 { |
| 367 |
res = append(res, "AVX512") |
| 368 |
} |
| 369 |
if o.useAvx512GFNI { |
| 370 |
res = append(res, "AVX512+GFNI") |
| 371 |
} |
| 372 |
if o.useAvxGNFI { |
| 373 |
res = append(res, "AVX+GFNI") |
| 374 |
} |
| 375 |
if o.useSVE { |
| 376 |
res = append(res, "ARM+SVE") |
| 377 |
} else if o.useNEON { |
| 378 |
res = append(res, "ARM+NEON") |
| 379 |
} |
| 380 |
if len(res) == 0 { |
| 381 |
return "pure Go" |
| 382 |
} |
| 383 |
return strings.Join(res, ",") |
| 384 |
} |