Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ keep NaN bits exactly; for the others a signalling NaN may come back quieted):
on 10⁵ float64 on one core of cfarm420 they went from 243–464 µs to 70–81 µs
([docs/perf.md](docs/perf.md), *Since the table*).

Float64 `Maximum` and `Minimum` run on the go-simd/floats (v0.26.0) kernels,
the builtin `max` and `min` bit for bit for numbers and zeros (a NaN gives a
NaN; which one is target-dependent): on 10⁵ float64 on one core of cfarm420
they went from 139–151 µs to 76–82 µs.

**Behaviour change in the next release (a MINOR): `Ravel` and `Real` return
views, as NumPy does.** `Ravel` of an array whose elements are one row-major
run (a contiguous array, or a contiguous slice of one) shares its storage
Expand Down
2 changes: 1 addition & 1 deletion bench/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/go-blas/blas v0.2.1 // indirect
github.com/go-fft/fft v0.29.0 // indirect
github.com/go-fleettools/fleettools v0.81.0 // indirect
github.com/go-simd/floats v0.23.0 // indirect
github.com/go-simd/floats v0.26.0 // indirect
golang.org/x/sys v0.49.0 // indirect
)

Expand Down
4 changes: 2 additions & 2 deletions bench/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ github.com/go-fleettools/fleettools v0.81.0 h1:UnMflmNw5b82Z+7Ums7UVaW5RrGuACCVm
github.com/go-fleettools/fleettools v0.81.0/go.mod h1:y/o3p2HZAvto9J5G5gtPxhN45zgvaYFAXCY9RCK/lQw=
github.com/go-scikit/refbench v0.3.0 h1:ng9b3FRwOWyarkoHHLar1bs43TWi9TydZeO29Yr9iSw=
github.com/go-scikit/refbench v0.3.0/go.mod h1:PhJxvLwXiQeKLP2f5ndcb9k5k4PTvU1rSFJ/MKRCrU0=
github.com/go-simd/floats v0.23.0 h1:aHEbbiVmhLmoR8FdUWDCHMRctFajm11wKnfbbs9FOxk=
github.com/go-simd/floats v0.23.0/go.mod h1:Aq5a6oY5pUqm9Hpi+iV2nrNmaaUb0rwHvUHIerXWSvA=
github.com/go-simd/floats v0.26.0 h1:IovUqTYY7U+CPDh/4JJMoCGBzuYK6zpSOzuWg/tn4Ks=
github.com/go-simd/floats v0.26.0/go.mod h1:Aq5a6oY5pUqm9Hpi+iV2nrNmaaUb0rwHvUHIerXWSvA=
golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA=
golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
36 changes: 36 additions & 0 deletions benchmarks/maxmin_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package benchmarks

import (
"testing"

nd "github.com/go-ndarray/ndarray"
)

// BenchmarkMaxMin times the float64 pairwise Maximum and Minimum on 10^5
// elements, the size of the single-core table (#118), including the result
// allocation.
func BenchmarkMaxMin(b *testing.B) {
const n = 100000
x := vec(n)
d := make([]float64, n)
for i := range d {
d[i] = float64((i*31)%89) * 0.5 // differs from vec(n), so both operands win in turn
}
y, _ := nd.FromData(d, n)
ops := []struct {
name string
do func()
}{
{"Maximum", func() { _, _ = x.Maximum(y) }},
{"Minimum", func() { _, _ = x.Minimum(y) }},
}
for _, op := range ops {
b.Run(op.name, func(b *testing.B) {
b.SetBytes(2 * n * 8)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
op.do()
}
})
}
}
12 changes: 12 additions & 0 deletions docs/perf.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,18 @@ rounds:
ns and `Real` from 84–91 µs to 101–104 ns, 803 kB to 128 B per call. The
new table measures them against NumPy at 63 ns and 97 ns against 174 ns and
168 ns (ratios 0.36 and 0.58). This is a behaviour change (see the README).
- Float64 `Maximum` and `Minimum` run on the go-simd/floats v0.26.0 kernels
instead of the scalar builtin loop, in every path that calls them (same
shape, scalar and repeated operands, broadcast). The results are bit for bit
those of the builtin `max` and `min` for numbers and zeros; a NaN gives a NaN,
as before, and which NaN is target-dependent
(`TestMaximumMinimumFloats`). On 10⁵ float64 including the result
allocation, cfarm420, one pinned core, a test binary of main against one of
this change, four interleaved rounds of 1 s at load 3.6 to 4.0
(`benchmarks/maxmin_bench_test.go`): `Maximum` went from 139–150 µs to
76–82 µs and `Minimum` from 141–151 µs to 76–81 µs. The allocation is
unchanged, 2 per call and 803 kB. No NumPy ratio is claimed for these
numbers: the new table has not been run on this change.

What remains of the allocation cause is Go's own cost. A CPU profile of a bare
`make([]float64, 100000)` shows about 60% garbage-collector marking (assists
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.27.2
require (
github.com/go-blas/blas v0.2.1
github.com/go-fft/fft v0.29.0
github.com/go-simd/floats v0.23.0
github.com/go-simd/floats v0.26.0
)

require golang.org/x/sys v0.49.0 // indirect
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ github.com/go-blas/blas v0.2.1 h1:5uZfmvXVZn9CjE+I3DalALqWHdKz+rbBs8VffH3dAZo=
github.com/go-blas/blas v0.2.1/go.mod h1:WjhlqzQ+RA8tS473f18QtcJl5Q0GonPs3RJIxT5G7Bk=
github.com/go-fft/fft v0.29.0 h1:WtJb5K3dmbLWREvvZ50od0fxtjIBiXrg7mVybz/nPoA=
github.com/go-fft/fft v0.29.0/go.mod h1:GN5V4TWCjV05ECqH1ONfjFM6Z1m2shqCdssxKGqQ41s=
github.com/go-simd/floats v0.23.0 h1:aHEbbiVmhLmoR8FdUWDCHMRctFajm11wKnfbbs9FOxk=
github.com/go-simd/floats v0.23.0/go.mod h1:Aq5a6oY5pUqm9Hpi+iV2nrNmaaUb0rwHvUHIerXWSvA=
github.com/go-simd/floats v0.26.0 h1:IovUqTYY7U+CPDh/4JJMoCGBzuYK6zpSOzuWg/tn4Ks=
github.com/go-simd/floats v0.26.0/go.mod h1:Aq5a6oY5pUqm9Hpi+iV2nrNmaaUb0rwHvUHIerXWSvA=
golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA=
golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
20 changes: 10 additions & 10 deletions internal/kernels/kernels.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,21 @@ func sqrtScalar(dst, src []float64) {
}

// Maximum writes the pairwise maximum of a[i] and b[i] into dst[i]. It is
// NaN-propagating like numpy.maximum: it uses the builtin max, not math.Max,
// because math.Max returns +Inf for max(NaN, +Inf) where numpy returns NaN.
// NaN-propagating like numpy.maximum and bit-identical to the builtin max for
// numbers and zeros (+0 above -0), not math.Max, because math.Max returns +Inf
// for max(NaN, +Inf) where numpy returns NaN. It is go-simd/floats' Maximum: a
// vector kernel where there is one, the builtin loop elsewhere. A NaN result is
// a NaN; which NaN is target-dependent. len(a), len(b) >= len(dst) is required
// and dst may alias a or b.
func Maximum(dst, a, b []float64) {
for i := range dst {
dst[i] = max(a[i], b[i])
}
floats.Maximum(dst, a[:len(dst)], b[:len(dst)])
}

// Minimum writes the pairwise minimum of a[i] and b[i] into dst[i],
// NaN-propagating like numpy.minimum (builtin min; math.Min returns -Inf for
// min(NaN, -Inf)).
// NaN-propagating like numpy.minimum (the builtin min; math.Min returns -Inf
// for min(NaN, -Inf)), through go-simd/floats' Minimum as Maximum is.
func Minimum(dst, a, b []float64) {
for i := range dst {
dst[i] = min(a[i], b[i])
}
floats.Minimum(dst, a[:len(dst)], b[:len(dst)])
}

// Sum returns the sum of all elements of a.
Expand Down
122 changes: 122 additions & 0 deletions internal/kernels/maxmin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package kernels

import (
"math"
"math/rand"
"testing"
)

// maxminPool is the value table: zeros of both signs, infinities, subnormals,
// neighbours of 1, and random bit patterns that are not NaN.
func maxminPool() []float64 {
v := []float64{
0, math.Copysign(0, -1), 1, -1, math.Inf(1), math.Inf(-1),
5e-324, -5e-324, 2.2250738585072009e-308, -2.2250738585072014e-308,
math.MaxFloat64, -math.MaxFloat64, math.Nextafter(1, 2), math.Nextafter(-1, -2),
0.5, -0.5, 1e-300, -1e-300,
}
rng := rand.New(rand.NewSource(7))
for len(v) < 600 {
x := math.Float64frombits(rng.Uint64())
if !math.IsNaN(x) {
v = append(v, x)
}
}
return v
}

func maxminCheck(t *testing.T, name string, k func(dst, a, b []float64), ref func(x, y float64) float64) {
t.Helper()
pool := maxminPool()
lengths := make([]int, 0, 36)
for n := 0; n <= 33; n++ {
lengths = append(lengths, n)
}
lengths = append(lengths, ParThreshold-1, ParThreshold+37, 100000)
for _, n := range lengths {
for off := 0; off < 4; off++ {
a, b := make([]float64, n), make([]float64, n)
for i := range a {
a[i] = pool[(off*13+i*7)%len(pool)]
b[i] = pool[(off*29+i*11+3)%len(pool)]
}
if n >= 36 { // every ordered pair of the first 18 values, zeros in both orders
for i := 0; i < 18*18 && i < n; i++ {
a[i], b[i] = pool[i/18], pool[i%18]
}
}
check := func(label string, got []float64) {
for i := range got {
if w := ref(a[i], b[i]); math.Float64bits(got[i]) != math.Float64bits(w) {
t.Fatalf("%s %s n=%d [%d] (%#x, %#x): got %#x want %#x", name, label, n, i,
math.Float64bits(a[i]), math.Float64bits(b[i]),
math.Float64bits(got[i]), math.Float64bits(w))
}
}
}
dst := make([]float64, n)
k(dst, a, b)
check("fresh", dst)

// Aliasing: out = a, then out = b (the reference reads the originals).
a0, b0 := append([]float64(nil), a...), append([]float64(nil), b...)
k(a, a, b)
got := append([]float64(nil), a...)
copy(a, a0)
check("out=a", got)
k(b, a, b)
got = append([]float64(nil), b...)
copy(b, b0)
check("out=b", got)
}
}
}

// nanCheck: a NaN in either operand gives a NaN. Which NaN is target-dependent.
func nanCheck(t *testing.T, name string, k func(dst, a, b []float64)) {
t.Helper()
nan := math.NaN()
snan := math.Float64frombits(0x7FF0000000000001)
vals := []float64{0, math.Copysign(0, -1), 1, math.Inf(1), math.Inf(-1), 5e-324}
for _, n := range []int{1, 2, 3, 4, 7, 8, 9, 16, 17, 33, 100} {
for _, bad := range []float64{nan, snan, -nan} {
for pos := 0; pos < n; pos += 3 {
for _, inA := range []bool{true, false} {
a, b, dst := make([]float64, n), make([]float64, n), make([]float64, n)
for i := range a {
a[i], b[i] = vals[i%len(vals)], vals[(i+2)%len(vals)]
}
if inA {
a[pos] = bad
} else {
b[pos] = bad
}
k(dst, a, b)
for i := range dst {
want := i == pos
if math.IsNaN(dst[i]) != want {
t.Fatalf("%s n=%d pos=%d [%d]: IsNaN = %v, want %v", name, n, pos, i, math.IsNaN(dst[i]), want)
}
}
}
}
}
a, b, dst := make([]float64, n), make([]float64, n), make([]float64, n) // both NaN
for i := range a {
a[i], b[i] = nan, math.Float64frombits(0x7FF0000000000001)
}
k(dst, a, b)
for i := range dst {
if !math.IsNaN(dst[i]) {
t.Fatalf("%s n=%d: NaN, NaN gave %v", name, n, dst[i])
}
}
}
}

func TestMaximumMinimumFloats(t *testing.T) {
maxminCheck(t, "Maximum", Maximum, func(x, y float64) float64 { return max(x, y) })
maxminCheck(t, "Minimum", Minimum, func(x, y float64) float64 { return min(x, y) })
nanCheck(t, "Maximum", Maximum)
nanCheck(t, "Minimum", Minimum)
}
Loading