2017-08-11 21:24:48 -04:00
|
|
|
package reedsolomon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/klauspost/cpuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Option allows to override processing parameters.
|
|
|
|
type Option func(*options)
|
|
|
|
|
|
|
|
type options struct {
|
2019-02-12 07:02:27 -05:00
|
|
|
maxGoroutines int
|
|
|
|
minSplitSize int
|
|
|
|
useAVX512, useAVX2, useSSSE3, useSSE2 bool
|
|
|
|
usePAR1Matrix bool
|
|
|
|
useCauchy bool
|
|
|
|
shardSize int
|
2017-08-11 21:24:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var defaultOptions = options{
|
2017-11-24 17:23:20 -05:00
|
|
|
maxGoroutines: 384,
|
2018-01-03 16:47:22 -05:00
|
|
|
minSplitSize: 1024,
|
2017-08-11 21:24:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if runtime.GOMAXPROCS(0) <= 1 {
|
|
|
|
defaultOptions.maxGoroutines = 1
|
|
|
|
}
|
|
|
|
// Detect CPU capabilities.
|
|
|
|
defaultOptions.useSSSE3 = cpuid.CPU.SSSE3()
|
2017-08-30 12:49:00 -04:00
|
|
|
defaultOptions.useSSE2 = cpuid.CPU.SSE2()
|
2019-02-12 07:02:27 -05:00
|
|
|
defaultOptions.useAVX2 = cpuid.CPU.AVX2()
|
|
|
|
defaultOptions.useAVX512 = cpuid.CPU.AVX512F() && cpuid.CPU.AVX512BW()
|
2017-08-11 21:24:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithMaxGoroutines is the maximum number of goroutines number for encoding & decoding.
|
|
|
|
// Jobs will be split into this many parts, unless each goroutine would have to process
|
|
|
|
// less than minSplitSize bytes (set with WithMinSplitSize).
|
|
|
|
// For the best speed, keep this well above the GOMAXPROCS number for more fine grained
|
|
|
|
// scheduling.
|
|
|
|
// If n <= 0, it is ignored.
|
|
|
|
func WithMaxGoroutines(n int) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
if n > 0 {
|
|
|
|
o.maxGoroutines = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-03 16:47:22 -05:00
|
|
|
// WithAutoGoroutines will adjust the number of goroutines for optimal speed with a
|
|
|
|
// specific shard size.
|
|
|
|
// Send in the shard size you expect to send. Other shard sizes will work, but may not
|
|
|
|
// run at the optimal speed.
|
|
|
|
// Overwrites WithMaxGoroutines.
|
|
|
|
// If shardSize <= 0, it is ignored.
|
|
|
|
func WithAutoGoroutines(shardSize int) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.shardSize = shardSize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-11 21:24:48 -04:00
|
|
|
// WithMinSplitSize is the minimum encoding size in bytes per goroutine.
|
|
|
|
// See WithMaxGoroutines on how jobs are split.
|
|
|
|
// If n <= 0, it is ignored.
|
|
|
|
func WithMinSplitSize(n int) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
if n > 0 {
|
|
|
|
o.minSplitSize = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func withSSE3(enabled bool) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.useSSSE3 = enabled
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func withAVX2(enabled bool) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.useAVX2 = enabled
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-30 12:49:00 -04:00
|
|
|
func withSSE2(enabled bool) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.useSSE2 = enabled
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 07:02:27 -05:00
|
|
|
func withAVX512(enabled bool) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.useAVX512 = enabled
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-11 21:24:48 -04:00
|
|
|
// WithPAR1Matrix causes the encoder to build the matrix how PARv1
|
|
|
|
// does. Note that the method they use is buggy, and may lead to cases
|
|
|
|
// where recovery is impossible, even if there are enough parity
|
|
|
|
// shards.
|
|
|
|
func WithPAR1Matrix() Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.usePAR1Matrix = true
|
2017-11-24 17:23:20 -05:00
|
|
|
o.useCauchy = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithCauchyMatrix will make the encoder build a Cauchy style matrix.
|
|
|
|
// The output of this is not compatible with the standard output.
|
|
|
|
// A Cauchy matrix is faster to generate. This does not affect data throughput,
|
|
|
|
// but will result in slightly faster start-up time.
|
|
|
|
func WithCauchyMatrix() Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.useCauchy = true
|
|
|
|
o.usePAR1Matrix = false
|
2017-08-11 21:24:48 -04:00
|
|
|
}
|
|
|
|
}
|