-
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.
Use interface static methods to define tensor operators
- Loading branch information
Showing
24 changed files
with
373 additions
and
686 deletions.
There are no files selected for viewing
64 changes: 0 additions & 64 deletions
64
src/NetFabric.Numerics.Benchmarks/SpanVector2SumBenchmarks.cs
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
using System; | ||
using System.Numerics; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace NetFabric.Numerics | ||
{ | ||
public static partial class Tensor | ||
{ | ||
/// <summary> | ||
/// Aggregates the elements in the specified <see cref="ReadOnlySpan{T}"/> using the specified <see cref="IAggregationOperator{T}"/>. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the elements in the span.</typeparam> | ||
/// <typeparam name="TOperator">The type of the aggregation operator.</typeparam> | ||
/// <param name="source">The source span.</param> | ||
/// <returns>The aggregated value.</returns> | ||
public static T Aggregate<T, TOperator>(ReadOnlySpan<T> source) | ||
where T : struct | ||
where TOperator : struct, IAggregationOperator<T> | ||
{ | ||
var result = TOperator.Seed; | ||
var resultVector = new Vector<T>(TOperator.Seed); | ||
nint index = 0; | ||
|
||
if (Vector.IsHardwareAccelerated && | ||
Vector<T>.IsSupported && | ||
source.Length >= Vector<T>.Count) | ||
{ | ||
var sourceVectors = MemoryMarshal.Cast<T, Vector<T>>(source); | ||
|
||
ref var sourceVectorsRef = ref MemoryMarshal.GetReference(sourceVectors); | ||
for (nint indexVector = 0; indexVector < sourceVectors.Length; indexVector++) | ||
resultVector = TOperator.Invoke(resultVector, Unsafe.Add(ref sourceVectorsRef, indexVector)); | ||
|
||
index = source.Length - source.Length % Vector<T>.Count; | ||
} | ||
|
||
ref var sourceRef = ref MemoryMarshal.GetReference(source); | ||
for (; index < source.Length; index++) | ||
result = TOperator.Invoke(result, Unsafe.Add(ref sourceRef, index)); | ||
|
||
return TOperator.ResultSelector(result, resultVector); | ||
} | ||
} | ||
} |
Oops, something went wrong.