-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimdReductionsBenchmark.cs
More file actions
103 lines (92 loc) · 3.72 KB
/
Copy pathSimdReductionsBenchmark.cs
File metadata and controls
103 lines (92 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System.Numerics.Tensors;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using Celerity.Primitives;
/// <summary>
/// Head-to-head throughput of <see cref="SimdReductions"/>'s fused/specialized reductions against the BCL
/// composition they replace (issue #197):
/// <list type="bullet">
/// <item><description>
/// <c>MinMax</c> — the single-pass <see cref="SimdReductions.MinMax(System.ReadOnlySpan{int})"/> vs the
/// two-pass <c>TensorPrimitives.Min</c> + <c>TensorPrimitives.Max</c> (the BCL composition) and a naive scalar
/// loop. The fused candidate touches the span once instead of twice, so the win grows as the data spills out
/// of cache.
/// </description></item>
/// <item><description>
/// <c>CheckedSum</c> — the overflow-checked <see cref="SimdReductions.CheckedSum(System.ReadOnlySpan{int})"/> vs
/// a scalar <c>checked</c> loop (the only safe BCL way — <c>TensorPrimitives.Sum</c> wraps silently), with the
/// silently-wrapping <c>TensorPrimitives.Sum</c> shown for reference as the unchecked speed ceiling.
/// </description></item>
/// </list>
/// </summary>
/// <remarks>
/// The <c>TensorPrimitives.*</c> arms are the BCL baselines; the <see cref="SimdReductions"/> arms are the
/// candidates, so each ratio reads as "the fused/checked helper relative to the BCL composition". This is an
/// isolated microbenchmark, so it lives in the <strong>extended</strong> suite (not the per-PR core regression
/// gate). The span lengths span an in-cache size and an out-of-cache size so the single-pass memory-traffic win
/// is visible.
/// </remarks>
[MemoryDiagnoser(false)]
[CategoriesColumn]
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
public class SimdReductionsBenchmark
{
[Params(1024, 1_000_000)]
public int Length;
private int[] data = null!;
private int[] smallMagnitude = null!;
[GlobalSetup]
public void Setup()
{
var rng = new Random(0x5197);
data = new int[Length];
smallMagnitude = new int[Length];
for (int i = 0; i < Length; i++)
{
data[i] = rng.Next(int.MinValue, int.MaxValue);
// Bounded so the checked sum never overflows on the hot benchmark path.
smallMagnitude[i] = rng.Next(-1000, 1000);
}
}
// ---- MinMax: fused single pass vs two-pass BCL composition ----
[Benchmark(Baseline = true)]
[BenchmarkCategory("MinMax")]
public (int, int) MinMax_TensorPrimitives_TwoPass()
=> (TensorPrimitives.Min<int>(data), TensorPrimitives.Max<int>(data));
[Benchmark]
[BenchmarkCategory("MinMax")]
public (int, int) MinMax_NaiveScalarLoop()
{
int[] a = data;
int min = a[0], max = a[0];
for (int i = 1; i < a.Length; i++)
{
if (a[i] < min) min = a[i];
if (a[i] > max) max = a[i];
}
return (min, max);
}
[Benchmark]
[BenchmarkCategory("MinMax")]
public (int Min, int Max) MinMax_SimdReductions()
=> SimdReductions.MinMax(data);
// ---- CheckedSum: vectorized overflow-checked sum vs scalar checked loop ----
[Benchmark(Baseline = true)]
[BenchmarkCategory("CheckedSum")]
public int CheckedSum_ScalarCheckedLoop()
{
int[] a = smallMagnitude;
int sum = 0;
for (int i = 0; i < a.Length; i++)
sum = checked(sum + a[i]);
return sum;
}
[Benchmark]
[BenchmarkCategory("CheckedSum")]
public int CheckedSum_TensorPrimitivesUnchecked()
=> TensorPrimitives.Sum<int>(smallMagnitude); // wraps silently — the unchecked speed ceiling, for reference
[Benchmark]
[BenchmarkCategory("CheckedSum")]
public int CheckedSum_SimdReductions()
=> SimdReductions.CheckedSum(smallMagnitude);
}