From c8fc9e30dbc5d853074e829f33c8381ab6ef062a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anta=CC=83o=20Almada?= Date: Sat, 27 Jan 2024 10:52:53 +0000 Subject: [PATCH] Add Vector element iteration benchmarks --- .../VectorSumBenchmarks.cs | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 src/NetFabric.Numerics.Tensors.Benchmarks/VectorSumBenchmarks.cs diff --git a/src/NetFabric.Numerics.Tensors.Benchmarks/VectorSumBenchmarks.cs b/src/NetFabric.Numerics.Tensors.Benchmarks/VectorSumBenchmarks.cs new file mode 100644 index 0000000..d90ea94 --- /dev/null +++ b/src/NetFabric.Numerics.Tensors.Benchmarks/VectorSumBenchmarks.cs @@ -0,0 +1,127 @@ +using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Configs; +using System.Numerics; +using System.Runtime.CompilerServices; + +namespace NetFabric.Numerics.Tensors.Benchmarks; + +[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)] +[CategoriesColumn] +public class VectorSumBenchmarks +{ + readonly Vector vectorShort = new(1); + readonly Vector vectorInt = new(1); + readonly Vector vectorLong = new(1L); + // readonly Vector vectorHalf = new((Half)1); + readonly Vector vectorFloat = new(1.0f); + readonly Vector vectorDouble = new(1.0); + + [BenchmarkCategory("Short")] + [Benchmark(Baseline = true)] + public short Sum_Short() + => Vector.Sum(vectorShort); + + [BenchmarkCategory("Short")] + [Benchmark] + public short Element_Short() + => SumElement(vectorShort); + + [BenchmarkCategory("Short")] + [Benchmark] + public short Unsafe_Short() + => SumUnsafe(vectorShort); + + [BenchmarkCategory("Int")] + [Benchmark(Baseline = true)] + public int Sum_Int() + => Vector.Sum(vectorInt); + + [BenchmarkCategory("Int")] + [Benchmark] + public int Element_Int() + => SumElement(vectorInt); + + [BenchmarkCategory("Int")] + [Benchmark] + public int Unsafe_Int() + => SumUnsafe(vectorInt); + + [BenchmarkCategory("Long")] + [Benchmark(Baseline = true)] + public long Sum_Long() + => Vector.Sum(vectorLong); + + [BenchmarkCategory("Long")] + [Benchmark] + public long Element_Long() + => SumElement(vectorLong); + + [BenchmarkCategory("Long")] + [Benchmark] + public long Unsafe_Long() + => SumUnsafe(vectorLong); + + // [BenchmarkCategory("Half")] + // [Benchmark(Baseline = true)] + // public Half Sum_Half() + // => Vector.Sum(vectorHalf); + + // [BenchmarkCategory("Half")] + // [Benchmark] + // public Half Element_Half() + // => SumElement(vectorHalf); + + // [BenchmarkCategory("Half")] + // [Benchmark] + // public Half Unsafe_Half() + // => SumUnsafe(vectorHalf); + + [BenchmarkCategory("Float")] + [Benchmark(Baseline = true)] + public float Sum_Float() + => Vector.Sum(vectorFloat); + + [BenchmarkCategory("Float")] + [Benchmark] + public float Element_Float() + => SumElement(vectorFloat); + + [BenchmarkCategory("Float")] + [Benchmark] + public float Unsafe_Float() + => SumUnsafe(vectorFloat); + + [BenchmarkCategory("Double")] + [Benchmark(Baseline = true)] + public double Sum_Double() + => Vector.Sum(vectorDouble); + + [BenchmarkCategory("Double")] + [Benchmark] + public double Element_Double() + => SumElement(vectorDouble); + + [BenchmarkCategory("Double")] + [Benchmark] + public double Unsafe_Double() + => SumUnsafe(vectorDouble); + + static T SumElement(Vector vector) + where T : struct, IAdditiveIdentity, IAdditionOperators + { + var sum = T.AdditiveIdentity; + for (var index = 0; index < Vector.Count; index++) + sum += vector[index]; + return sum; + } + + static T SumUnsafe(Vector vector) + where T : struct, IAdditiveIdentity, IAdditionOperators + { + var sum = T.AdditiveIdentity; + ref var address = ref Unsafe.As, T>(ref Unsafe.AsRef(in vector)); + for (var index = 0; index < Vector.Count; index++) + sum += Unsafe.Add(ref address, index); + return sum; + } +} \ No newline at end of file