vdsp-blas-reduce

command
v0.6.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: MIT Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

vdsp-blas-reduce

Zero-cgo (CGO_ENABLED=0) Apple Accelerate vDSP / BLAS / vImage elementwise vector reduction benchmark and parity demo (Sum, Min, Max) for float32 and float16, matching x/jaccl/reduce.go semantics on Apple Silicon.

What It Tests

  • float32 elementwise reductions: ScalarReduceFloat32 (exact x/jaccl/reduce.go NaN and </> semantics) vs vDSP_vadd, vDSP_vmin, vDSP_vmax, and cblas_saxpy (alpha = 1.0).
  • float32 horizontal sum: vDSP_sve (1-vector scan, also usable as a fast NaN presence check).
  • float16 elementwise reductions: ScalarReduceFloat16 (exact x/jaccl/reduce.go halfToFloat32/float32ToHalf + halfSumNaN) vs an L2-tiled vImageConvert_Planar16FtoPlanarF -> vDSP -> vImageConvert_PlanarFtoPlanar16F pipeline (note: vDSP_vflt16 converts signed int16, not IEEE 754 binary16).
  • Zero-allocation dispatch: Reducer holds a reusable [7]uintptr argument slot, vImage_Buffer headers, and two 32,768-element (256 KiB total) float32 tile buffers, invoking all-GPR vDSP_* and vImageConvert_* symbols through purego.SyscallN at strictly 0 allocs/op.

Usage

CGO_ENABLED=0 go vet ./examples/accelerate/vdsp-blas-reduce
CGO_ENABLED=0 go test -v -count=1 -timeout 60s ./examples/accelerate/vdsp-blas-reduce
CGO_ENABLED=0 go test -run=^$ -bench=BenchmarkSmoke -benchmem ./examples/accelerate/vdsp-blas-reduce
CGO_ENABLED=0 go run ./examples/accelerate/vdsp-blas-reduce

Measured Results (Apple M4 Max, macOS 26.6.2, Go 1.27.0)

Effective GB/s counts 3 * payload (read dst + read src + write dst) for in-place binary reductions and 1 * payload for vDSP_sve.

go test -bench=BenchmarkSmokeReduce1MiB -benchmem (1 MiB payload)
Benchmark ns/op Throughput B/op allocs/op
float32/Sum/Scalar 6,937,830 453.42 MB/s 0 0
float32/Sum/vDSP 77,466 40,608.02 MB/s 0 0
float32/Sum/BLAS (cblas_saxpy) 15,605 201,578.97 MB/s 176 5
float16/Sum/Scalar 18,491,136 170.12 MB/s 0 0
float16/Sum/vImage+vDSP 1,376,306 2,285.63 MB/s 2 0
float32 Sum Ladder (Scalar Go vs vDSP_vadd vs cblas_saxpy)
Payload Scalar (µs) Scalar GB/s vDSP_vadd (µs) vDSP GB/s vDSP Speedup cblas_saxpy (µs) BLAS GB/s BLAS Speedup
4 KiB 15.27 0.80 0.96 12.76 15.86x 6.70 1.84 2.28x
16 KiB 77.96 0.63 2.45 20.05 31.81x 4.36 11.28 17.90x
64 KiB 220.70 0.89 5.22 37.69 42.30x 6.93 28.37 31.85x
256 KiB 1,102.69 0.71 7.60 103.45 145.05x 29.58 26.59 37.28x
1 MiB 12,564.30 0.25 46.81 67.20 268.40x 104.52 30.10 120.21x
4 MiB 65,706.67 0.19 404.12 31.14 162.59x 309.41 40.67 212.36x
16 MiB 53,171.59 0.95 1,835.02 27.43 28.98x 5,112.92 9.84 10.40x
64 MiB 200,696.42 1.00 6,535.62 30.80 30.71x 4,641.70 43.37 43.24x
float16 Sum Ladder (Scalar Go vs vImage+vDSP, tile = 32,768 elems)
Payload Scalar (µs) Scalar GB/s vImage+vDSP (µs) vImage+vDSP GB/s Speedup
4 KiB 78.00 0.16 3.06 4.02 25.53x
16 KiB 200.68 0.24 9.84 5.00 20.40x
64 KiB 1,388.60 0.14 172.16 1.14 8.07x
256 KiB 3,954.74 0.20 268.94 2.92 14.70x
1 MiB 39,674.52 0.08 481.04 6.54 82.48x
4 MiB 106,027.92 0.12 2,917.53 4.31 36.34x
16 MiB 215,914.62 0.23 39,660.24 1.27 5.44x
64 MiB 576,139.38 0.35 32,195.92 6.25 17.89x
float32 Horizontal Sum (vDSP_sve)
Payload vDSP_sve (µs) Read GB/s
4 KiB 0.85 4.83
16 KiB 1.10 14.89
64 KiB 3.02 21.72
256 KiB 19.03 13.78
1 MiB 110.36 9.50
4 MiB 274.94 15.26
16 MiB 937.84 17.89
64 MiB 3,139.01 21.38

Takeaways for x/jaccl/reduce.go

  1. Use purego.SyscallN + reusable [7]uintptr args for vDSP / vImage: The generated accelerate.VDSP_* and accelerate.Cblas_saxpy bindings go through purego.RegisterFunc (reflect.MakeFunc), which incurs 5 allocs/op. Because vDSP_vadd, vDSP_vmin, vDSP_vmax, vDSP_sve, and vImageConvert_Planar* take only pointer/integer arguments (all GPRs), purego.SyscallN with a preallocated arg array achieves 0 allocs/op.
  2. NaN & signed-zero parity on Min / Max:
    • For Sum, vDSP_vadd and cblas_saxpy match x/jaccl/reduce.go on (qNaN, 1.0), (1.0, qNaN), and (-0.0, +0.0).
    • For Min and Max, x/jaccl/reduce.go evaluates if a < b { return a } return b, which returns 1.0 for Min(qNaN, 1.0) and NaN for Min(1.0, qNaN). Apple Silicon vDSP_vmin and vDSP_vmax propagate NaN symmetrically in both operand positions and return -0.0 for Min(-0.0, +0.0) / +0.0 for Max(+0.0, -0.0). If strict x/jaccl tie-breaking is required when NaNs are present, gate with a NaN check or fall back to scalar.
  3. Tiled float16 via vImageConvert + vDSP: Converting 32,768-element tiles (256 KiB total float32 scratch) keeps the working set L2-resident and delivers 8x-82x speedups over scalar Go fp16 conversion loops at 0 allocs/op.

Documentation

Rendered for darwin/amd64

Overview

Command vdsp-blas-reduce benchmarks and verifies Apple Accelerate vDSP and BLAS elementwise vector reductions (Sum, Min, Max) for float32 and float16 against scalar Go loops matching x/jaccl/reduce.go semantics.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL