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
float32elementwise reductions:ScalarReduceFloat32(exactx/jaccl/reduce.goNaN and</>semantics) vsvDSP_vadd,vDSP_vmin,vDSP_vmax, andcblas_saxpy(alpha = 1.0).float32horizontal sum:vDSP_sve(1-vector scan, also usable as a fast NaN presence check).float16elementwise reductions:ScalarReduceFloat16(exactx/jaccl/reduce.gohalfToFloat32/float32ToHalf+halfSumNaN) vs an L2-tiledvImageConvert_Planar16FtoPlanarF->vDSP->vImageConvert_PlanarFtoPlanar16Fpipeline (note:vDSP_vflt16converts signedint16, not IEEE 754 binary16).- Zero-allocation dispatch:
Reducerholds a reusable[7]uintptrargument slot,vImage_Bufferheaders, and two 32,768-element (256 KiBtotal)float32tile buffers, invoking all-GPRvDSP_*andvImageConvert_*symbols throughpurego.SyscallNat strictly0 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
- Use
purego.SyscallN+ reusable[7]uintptrargs forvDSP/vImage: The generatedaccelerate.VDSP_*andaccelerate.Cblas_saxpybindings go throughpurego.RegisterFunc(reflect.MakeFunc), which incurs 5 allocs/op. BecausevDSP_vadd,vDSP_vmin,vDSP_vmax,vDSP_sve, andvImageConvert_Planar*take only pointer/integer arguments (all GPRs),purego.SyscallNwith a preallocated arg array achieves0 allocs/op. - NaN & signed-zero parity on
Min/Max:- For
Sum,vDSP_vaddandcblas_saxpymatchx/jaccl/reduce.goon(qNaN, 1.0),(1.0, qNaN), and(-0.0, +0.0). - For
MinandMax,x/jaccl/reduce.goevaluatesif a < b { return a } return b, which returns1.0forMin(qNaN, 1.0)andNaNforMin(1.0, qNaN). Apple SiliconvDSP_vminandvDSP_vmaxpropagateNaNsymmetrically in both operand positions and return-0.0forMin(-0.0, +0.0)/+0.0forMax(+0.0, -0.0). If strictx/jaccltie-breaking is required when NaNs are present, gate with a NaN check or fall back to scalar.
- For
- Tiled
float16viavImageConvert+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 at0 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.
Click to show internal directories.
Click to hide internal directories.