-
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 operators
- Loading branch information
Showing
8 changed files
with
95 additions
and
5 deletions.
There are no files selected for viewing
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(in resultVector, in 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(in result, in Unsafe.Add(ref sourceRef, index)); | ||
|
||
return TOperator.ResultSelector(in result, in resultVector); | ||
} | ||
} | ||
} |
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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