-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
174 additions
and
9 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/NetFabric.Numerics.Tensors.Benchmarks/SumPairsBenchmarks.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace NetFabric.Numerics.Tensors.Benchmarks; | ||
|
||
public class SumPairsBenchmarks | ||
{ | ||
ValueTuple<short, short>[]? arrayShort; | ||
ValueTuple<int, int>[]? arrayInt; | ||
ValueTuple<long, long>[]? arrayLong; | ||
ValueTuple<Half, Half>[]? arrayHalf; | ||
ValueTuple<float, float>[]? arrayFloat; | ||
ValueTuple<double, double>[]? arrayDouble; | ||
|
||
[Params(10_000)] | ||
public int Count { get; set; } | ||
|
||
[GlobalSetup] | ||
public void GlobalSetup() | ||
{ | ||
var range = Enumerable.Range(0, Count); | ||
arrayShort = range | ||
.Select(value => ((short)value, (short)(value + 1))) | ||
.ToArray(); | ||
arrayInt = range | ||
.Select(value => ((int)value, (int)(value + 1))) | ||
.ToArray(); | ||
arrayLong = range | ||
.Select(value => ((long)value, (long)(value + 1))) | ||
.ToArray(); | ||
arrayHalf = range | ||
.Select(value => ((Half)value, (Half)(value + 1))) | ||
.ToArray(); | ||
arrayFloat = range | ||
.Select(value => ((float)value, (float)(value + 1))) | ||
.ToArray(); | ||
arrayDouble = range | ||
.Select(value => ((double)value, (double)(value + 1))) | ||
.ToArray(); | ||
} | ||
|
||
[Benchmark] | ||
public ValueTuple<short, short> Sum_Short() | ||
=> Tensor.SumPairs<short>(MemoryMarshal.Cast<ValueTuple<short, short>, short>(arrayShort!)); | ||
|
||
[Benchmark] | ||
public ValueTuple<int, int> Sum_Int() | ||
=> Tensor.SumPairs<int>(MemoryMarshal.Cast<ValueTuple<int, int>, int>(arrayInt!)); | ||
|
||
[Benchmark] | ||
public ValueTuple<long, long> Sum_Long() | ||
=> Tensor.SumPairs<long>(MemoryMarshal.Cast<ValueTuple<long, long>, long>(arrayLong!)); | ||
|
||
[Benchmark] | ||
public ValueTuple<Half, Half> Sum_Half() | ||
=> Tensor.SumPairs<Half>(MemoryMarshal.Cast<ValueTuple<Half, Half>, Half>(arrayHalf!)); | ||
|
||
[Benchmark] | ||
public ValueTuple<float, float> Sum_Float() | ||
=> Tensor.SumPairs<float>(MemoryMarshal.Cast<ValueTuple<float, float>, float>(arrayFloat!)); | ||
|
||
[Benchmark] | ||
public ValueTuple<double, double> Sum_Double() | ||
=> Tensor.SumPairs<double>(MemoryMarshal.Cast<ValueTuple<double, double>, double>(arrayDouble!)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System.Linq; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace NetFabric.Numerics.Tensors.UnitTests; | ||
|
||
public class SumPairsTests | ||
{ | ||
public static TheoryData<int> SumData | ||
=> new() { 0, 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 }; | ||
|
||
[Theory] | ||
[MemberData(nameof(SumData))] | ||
public void SumPairs_Short_Should_Succeed(int count) | ||
{ | ||
// arrange | ||
var source = Enumerable.Range(0, count).Select(value => ((short)value, (short)(value + 1))).ToArray(); | ||
var expected = source.Aggregate((0, 0), (sum, value) => (sum.Item1 + value.Item1, sum.Item2 + value.Item2)); | ||
|
||
// act | ||
var result = Tensor.SumPairs<short>(MemoryMarshal.Cast<ValueTuple<short, short>, short>(source)); | ||
|
||
// assert | ||
result.Item1.Should().Be((short)expected.Item1); | ||
result.Item2.Should().Be((short)expected.Item2); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(SumData))] | ||
public void SumPairs_Int_Should_Succeed(int count) | ||
{ | ||
// arrange | ||
var source = Enumerable.Range(0, count).Select(value => (value, value + 1)).ToArray(); | ||
var expected = source.Aggregate((0, 0), (sum, value) => (sum.Item1 + value.Item1, sum.Item2 + value.Item2)); | ||
|
||
// act | ||
var result = Tensor.SumPairs<int>(MemoryMarshal.Cast<ValueTuple<int, int>, int>(source)); | ||
|
||
// assert | ||
result.Item1.Should().Be(expected.Item1); | ||
result.Item2.Should().Be(expected.Item2); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(SumData))] | ||
public void SumPairs_Long_Should_Succeed(int count) | ||
{ | ||
// arrange | ||
var source = Enumerable.Range(0, count).Select(value => ((long)value, (long)(value + 1))).ToArray(); | ||
var expected = source.Aggregate((0l, 0l), (sum, value) => (sum.Item1 + value.Item1, sum.Item2 + value.Item2)); | ||
|
||
// act | ||
var result = Tensor.SumPairs<long>(MemoryMarshal.Cast<ValueTuple<long, long>, long>(source)); | ||
|
||
// assert | ||
result.Item1.Should().Be(expected.Item1); | ||
result.Item2.Should().Be(expected.Item2); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(SumData))] | ||
public void SumPairs_Float_Should_Succeed(int count) | ||
{ | ||
// arrange | ||
var source = Enumerable.Range(0, count).Select(value => ((float)value, (float)(value + 1))).ToArray(); | ||
var expected = source.Aggregate((0.0f, 0.0f), (sum, value) => (sum.Item1 + value.Item1, sum.Item2 + value.Item2)); | ||
|
||
// act | ||
var result = Tensor.SumPairs<float>(MemoryMarshal.Cast<ValueTuple<float, float>, float>(source)); | ||
|
||
// assert | ||
result.Item1.Should().Be(expected.Item1); | ||
result.Item2.Should().Be(expected.Item2); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(SumData))] | ||
public void SumPairs_Double_Should_Succeed(int count) | ||
{ | ||
// arrange | ||
var source = Enumerable.Range(0, count).Select(value => ((double)value, (double)(value + 1))).ToArray(); | ||
var expected = source.Aggregate((0.0, 0.0), (sum, value) => (sum.Item1 + value.Item1, sum.Item2 + value.Item2)); | ||
|
||
// act | ||
var result = Tensor.SumPairs<double>(MemoryMarshal.Cast<ValueTuple<double, double>, double>(source)); | ||
|
||
// assert | ||
result.Item1.Should().Be(expected.Item1); | ||
result.Item2.Should().Be(expected.Item2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters