Skip to content

Commit

Permalink
Add LINQ to sum benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmada committed Jan 21, 2024
1 parent 62219ce commit a0cf219
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 55 deletions.
58 changes: 58 additions & 0 deletions src/NetFabric.Numerics.Tensors.Benchmarks/Baseline.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Numerics;

namespace NetFabric.Numerics.Tensors.Benchmarks;

public static class Baseline
{
public static void Negate<T>(ReadOnlySpan<T> source, Span<T> result)
where T : struct, IUnaryNegationOperators<T, T>
{
if (source.Length > result.Length)
Throw.ArgumentException(nameof(source), "result spans is too small.");

for(var index = 0; index < source.Length; index++)
result[index] = -source[index];
}

public static void Add<T>(ReadOnlySpan<T> source, ReadOnlySpan<T> other, Span<T> result)
where T : struct, IAdditionOperators<T, T, T>
{
if (source.Length != other.Length)
Throw.ArgumentException(nameof(source), "source and other spans must have the same length.");
if (source.Length > result.Length)
Throw.ArgumentException(nameof(source), "result spans is too small.");

for(var index = 0; index < source.Length; index++)
result[index] = source[index] + other[index];
}

public static void Add<T>(ReadOnlySpan<T> source, T value, Span<T> result)
where T : struct, IAdditionOperators<T, T, T>
{
for(var index = 0; index < source.Length; index++)
result[index] = source[index] + value;
}

public static void AddMultiply<T>(ReadOnlySpan<T> source, ReadOnlySpan<T> other, ReadOnlySpan<T> another, Span<T> result)
where T : struct, IAdditionOperators<T, T, T>, IMultiplyOperators<T, T, T>
{
if (source.Length != other.Length)
Throw.ArgumentException(nameof(source), "source and other spans must have the same length.");
if (source.Length != another.Length)
Throw.ArgumentException(nameof(source), "source and another spans must have the same length.");
if (source.Length > result.Length)
Throw.ArgumentException(nameof(source), "result spans is too small.");

for(var index = 0; index < source.Length; index++)
result[index] = (source[index] + other[index]) * another[index];
}

public static T Sum<T>(ReadOnlySpan<T> source)
where T : struct, IAdditiveIdentity<T, T>, IAdditionOperators<T, T, T>
{
var sum = T.AdditiveIdentity;
foreach (var item in source)
sum += item;
return sum;
}
}
67 changes: 12 additions & 55 deletions src/NetFabric.Numerics.Tensors.Benchmarks/MyVectors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ public readonly record struct MyVector2<T>(T X, T Y)
: IAdditiveIdentity<MyVector2<T>, MyVector2<T>>, IAdditionOperators<MyVector2<T>, MyVector2<T>, MyVector2<T>>
where T : struct, IAdditiveIdentity<T, T>, IAdditionOperators<T, T, T>
{
public MyVector2(ValueTuple<T, T> tuple)
: this(tuple.Item1, tuple.Item2)
{ }

public static MyVector2<T> AdditiveIdentity
=> new(T.AdditiveIdentity, T.AdditiveIdentity);

Expand All @@ -17,6 +21,10 @@ public readonly record struct MyVector3<T>(T X, T Y, T Z)
: IAdditiveIdentity<MyVector3<T>, MyVector3<T>>, IAdditionOperators<MyVector3<T>, MyVector3<T>, MyVector3<T>>
where T : struct, IAdditiveIdentity<T, T>, IAdditionOperators<T, T, T>
{
public MyVector3(ValueTuple<T, T, T> tuple)
: this(tuple.Item1, tuple.Item2, tuple.Item3)
{ }

public static MyVector3<T> AdditiveIdentity
=> new(T.AdditiveIdentity, T.AdditiveIdentity, T.AdditiveIdentity);

Expand All @@ -28,64 +36,13 @@ public readonly record struct MyVector4<T>(T X, T Y, T Z, T W)
: IAdditiveIdentity<MyVector4<T>, MyVector4<T>>, IAdditionOperators<MyVector4<T>, MyVector4<T>, MyVector4<T>>
where T : struct, IAdditiveIdentity<T, T>, IAdditionOperators<T, T, T>
{
public MyVector4(ValueTuple<T, T, T, T> tuple)
: this(tuple.Item1, tuple.Item2, tuple.Item3, tuple.Item4)
{ }

public static MyVector4<T> AdditiveIdentity
=> new(T.AdditiveIdentity, T.AdditiveIdentity, T.AdditiveIdentity, T.AdditiveIdentity);

public static MyVector4<T> operator +(MyVector4<T> left, MyVector4<T> right)
=> new(left.X + right.X, left.Y + right.Y, left.Z + right.Z, left.W + right.W);
}

public static class Baseline
{
public static void Negate<T>(ReadOnlySpan<T> source, Span<T> result)
where T : struct, IUnaryNegationOperators<T, T>
{
if (source.Length > result.Length)
Throw.ArgumentException(nameof(source), "result spans is too small.");

for(var index = 0; index < source.Length; index++)
result[index] = -source[index];
}

public static void Add<T>(ReadOnlySpan<T> source, ReadOnlySpan<T> other, Span<T> result)
where T : struct, IAdditionOperators<T, T, T>
{
if (source.Length != other.Length)
Throw.ArgumentException(nameof(source), "source and other spans must have the same length.");
if (source.Length > result.Length)
Throw.ArgumentException(nameof(source), "result spans is too small.");

for(var index = 0; index < source.Length; index++)
result[index] = source[index] + other[index];
}

public static void Add<T>(ReadOnlySpan<T> source, T value, Span<T> result)
where T : struct, IAdditionOperators<T, T, T>
{
for(var index = 0; index < source.Length; index++)
result[index] = source[index] + value;
}

public static void AddMultiply<T>(ReadOnlySpan<T> source, ReadOnlySpan<T> other, ReadOnlySpan<T> another, Span<T> result)
where T : struct, IAdditionOperators<T, T, T>, IMultiplyOperators<T, T, T>
{
if (source.Length != other.Length)
Throw.ArgumentException(nameof(source), "source and other spans must have the same length.");
if (source.Length != another.Length)
Throw.ArgumentException(nameof(source), "source and another spans must have the same length.");
if (source.Length > result.Length)
Throw.ArgumentException(nameof(source), "result spans is too small.");

for(var index = 0; index < source.Length; index++)
result[index] = (source[index] + other[index]) * another[index];
}

public static T Sum<T>(ReadOnlySpan<T> source)
where T : struct, IAdditiveIdentity<T, T>, IAdditionOperators<T, T, T>
{
var sum = T.AdditiveIdentity;
foreach (var item in source)
sum += item;
return sum;
}
}
20 changes: 20 additions & 0 deletions src/NetFabric.Numerics.Tensors.Benchmarks/Sum2DBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public MyVector2<short> Tensor_Short()
public MyVector2<int> Baseline_Int()
=> Baseline.Sum<MyVector2<int>>(arrayInt!);

[BenchmarkCategory("Int")]
[Benchmark]
public MyVector2<int> LINQ_Int()
=> Enumerable.Aggregate(arrayInt!, MyVector2<int>.AdditiveIdentity, (sum, item) => sum + item);

[BenchmarkCategory("Int")]
[Benchmark]
public MyVector2<int> Tensor_Int()
Expand All @@ -71,6 +76,11 @@ public MyVector2<int> Tensor_Int()
public MyVector2<long> Baseline_Long()
=> Baseline.Sum<MyVector2<long>>(arrayLong!);

[BenchmarkCategory("Long")]
[Benchmark]
public MyVector2<long> LINQ_Long()
=> Enumerable.Aggregate(arrayLong!, MyVector2<long>.AdditiveIdentity, (sum, item) => sum + item);

[BenchmarkCategory("Long")]
[Benchmark]
public MyVector2<long> Tensor_Long()
Expand All @@ -97,6 +107,11 @@ public MyVector2<Half> Tensor_Half()
public MyVector2<float> Baseline_Float()
=> Baseline.Sum<MyVector2<float>>(arrayFloat!);

[BenchmarkCategory("Float")]
[Benchmark]
public MyVector2<float> LINQ_Float()
=> Enumerable.Aggregate(arrayFloat!, MyVector2<float>.AdditiveIdentity, (sum, item) => sum + item);

[BenchmarkCategory("Float")]
[Benchmark]
public MyVector2<float> Tensor_Float()
Expand All @@ -110,6 +125,11 @@ public MyVector2<float> Tensor_Float()
public MyVector2<double> Baseline_Double()
=> Baseline.Sum<MyVector2<double>>(arrayDouble!);

[BenchmarkCategory("Double")]
[Benchmark]
public MyVector2<double> LINQ_Double()
=> Enumerable.Aggregate(arrayDouble!, MyVector2<double>.AdditiveIdentity, (sum, item) => sum + item);

[BenchmarkCategory("Double")]
[Benchmark]
public MyVector2<double> Tensor_Double()
Expand Down
20 changes: 20 additions & 0 deletions src/NetFabric.Numerics.Tensors.Benchmarks/Sum3DBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public MyVector3<short> Tensor_Short()
public MyVector3<int> Baseline_Int()
=> Baseline.Sum<MyVector3<int>>(arrayInt!);

[BenchmarkCategory("Int")]
[Benchmark]
public MyVector3<int> LINQ_Int()
=> Enumerable.Aggregate(arrayInt!, MyVector3<int>.AdditiveIdentity, (sum, item) => sum + item);

[BenchmarkCategory("Int")]
[Benchmark]
public MyVector3<int> Tensor_Int()
Expand All @@ -71,6 +76,11 @@ public MyVector3<int> Tensor_Int()
public MyVector3<long> Baseline_Long()
=> Baseline.Sum<MyVector3<long>>(arrayLong!);

[BenchmarkCategory("Long")]
[Benchmark]
public MyVector3<long> LINQ_Long()
=> Enumerable.Aggregate(arrayLong!, MyVector3<long>.AdditiveIdentity, (sum, item) => sum + item);

[BenchmarkCategory("Long")]
[Benchmark]
public MyVector3<long> Tensor_Long()
Expand All @@ -97,6 +107,11 @@ public MyVector3<Half> Tensor_Half()
public MyVector3<float> Baseline_Float()
=> Baseline.Sum<MyVector3<float>>(arrayFloat!);

[BenchmarkCategory("Float")]
[Benchmark]
public MyVector3<float> LINQ_Float()
=> Enumerable.Aggregate(arrayFloat!, MyVector3<float>.AdditiveIdentity, (sum, item) => sum + item);

[BenchmarkCategory("Float")]
[Benchmark]
public MyVector3<float> Tensor_Float()
Expand All @@ -109,6 +124,11 @@ public MyVector3<float> Tensor_Float()
[Benchmark(Baseline = true)]
public MyVector3<double> Baseline_Double()
=> Baseline.Sum<MyVector3<double>>(arrayDouble!);

[BenchmarkCategory("Double")]
[Benchmark]
public MyVector3<double> LINQ_Double()
=> Enumerable.Aggregate(arrayDouble!, MyVector3<double>.AdditiveIdentity, (sum, item) => sum + item);

[BenchmarkCategory("Double")]
[Benchmark]
Expand Down
20 changes: 20 additions & 0 deletions src/NetFabric.Numerics.Tensors.Benchmarks/Sum4DBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public MyVector4<short> Tensor_Short()
public MyVector4<int> Baseline_Int()
=> Baseline.Sum<MyVector4<int>>(arrayInt!);

[BenchmarkCategory("Int")]
[Benchmark]
public MyVector4<int> LINQ_Int()
=> Enumerable.Aggregate(arrayInt!, MyVector4<int>.AdditiveIdentity, (sum, item) => sum + item);

[BenchmarkCategory("Int")]
[Benchmark]
public MyVector4<int> Tensor_Int()
Expand All @@ -71,6 +76,11 @@ public MyVector4<int> Tensor_Int()
public MyVector4<long> Baseline_Long()
=> Baseline.Sum<MyVector4<long>>(arrayLong!);

[BenchmarkCategory("Long")]
[Benchmark]
public MyVector4<long> LINQ_Long()
=> Enumerable.Aggregate(arrayLong!, MyVector4<long>.AdditiveIdentity, (sum, item) => sum + item);

[BenchmarkCategory("Long")]
[Benchmark]
public MyVector4<long> Tensor_Long()
Expand All @@ -97,6 +107,11 @@ public MyVector4<Half> Tensor_Half()
public MyVector4<float> Baseline_Float()
=> Baseline.Sum<MyVector4<float>>(arrayFloat!);

[BenchmarkCategory("Float")]
[Benchmark]
public MyVector4<float> LINQ_Float()
=> Enumerable.Aggregate(arrayFloat!, MyVector4<float>.AdditiveIdentity, (sum, item) => sum + item);

[BenchmarkCategory("Float")]
[Benchmark]
public MyVector4<float> Tensor_Float()
Expand All @@ -109,6 +124,11 @@ public MyVector4<float> Tensor_Float()
[Benchmark(Baseline = true)]
public MyVector4<double> Baseline_Double()
=> Baseline.Sum<MyVector4<double>>(arrayDouble!);

[BenchmarkCategory("Double")]
[Benchmark]
public MyVector4<double> LINQ_Double()
=> Enumerable.Aggregate(arrayDouble!, MyVector4<double>.AdditiveIdentity, (sum, item) => sum + item);

[BenchmarkCategory("Double")]
[Benchmark]
Expand Down
21 changes: 21 additions & 0 deletions src/NetFabric.Numerics.Tensors.Benchmarks/SumBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using System.Runtime.InteropServices;
using System.Linq;

namespace NetFabric.Numerics.Tensors.Benchmarks;

Expand Down Expand Up @@ -55,6 +56,11 @@ public short Tensor_Short()
public int Baseline_Int()
=> Baseline.Sum<int>(arrayInt!);

[BenchmarkCategory("Int")]
[Benchmark]
public int LINQ_Int()
=> Enumerable.Sum(arrayInt!);

[BenchmarkCategory("Int")]
[Benchmark]
public int Tensor_Int()
Expand All @@ -65,6 +71,11 @@ public int Tensor_Int()
public long Baseline_Long()
=> Baseline.Sum<long>(arrayLong!);

[BenchmarkCategory("Long")]
[Benchmark]
public long LINQ_Long()
=> Enumerable.Sum(arrayLong!);

[BenchmarkCategory("Long")]
[Benchmark]
public long Tensor_Long()
Expand All @@ -85,6 +96,11 @@ public Half Tensor_Half()
public float Baseline_Float()
=> Baseline.Sum<float>(arrayFloat!);

[BenchmarkCategory("Float")]
[Benchmark]
public float LINQ_Float()
=> Enumerable.Sum(arrayFloat!);

[BenchmarkCategory("Float")]
[Benchmark]
public float Tensor_Float()
Expand All @@ -95,6 +111,11 @@ public float Tensor_Float()
public double Baseline_Double()
=> Baseline.Sum<double>(arrayDouble!);

[BenchmarkCategory("Double")]
[Benchmark]
public double LINQ_Double()
=> Enumerable.Sum(arrayDouble!);

[BenchmarkCategory("Double")]
[Benchmark]
public double Tensor_Double()
Expand Down

0 comments on commit a0cf219

Please sign in to comment.