Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start vectorizing TensorPrimitives #91596

Merged
merged 5 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
<Compile Include="System\ThrowHelper.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
<Compile Include="System\Numerics\Tensors\TensorPrimitives.netstandard.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
<Compile Include="System\Numerics\Tensors\TensorPrimitives.netcore.cs" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="System.Numerics.Tensors.Tests" Key="00240000048000009400000006020000002400005253413100040000010001004b86c4cb78549b34bab61a3b1800e23bfeb5b3ec390074041536a7e3cbd97f5f04cf0f857155a8928eaa29ebfd11cfbbad3ba70efea7bda3226c6a8d370a4cd303f714486b6ebc225985a638471e6ef571cc92a4613c00b8fa65d61ccee0cbe5f36330c9a01f4183559f1bef24cc2917c6d913e3a541333a1d05d9bed22b38cb" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,8 @@
namespace System.Numerics.Tensors
{
/// <summary>Performs primitive tensor operations over spans of memory.</summary>
public static class TensorPrimitives
public static partial class TensorPrimitives
{
/// <summary>Computes the element-wise result of: <c><paramref name="x" /> + <paramref name="y" /></c>.</summary>
/// <param name="x">The first tensor, represented as a span.</param>
/// <param name="y">The second tensor, represented as a span.</param>
/// <param name="destination">The destination tensor, represented as a span.</param>
/// <exception cref="ArgumentException">Length of '<paramref name="x" />' must be same as length of '<paramref name="y" />'.</exception>
/// <exception cref="ArgumentException">Destination is too short.</exception>
/// <remarks>This method effectively does <c><paramref name="destination" />[i] = <paramref name="x" />[i] + <paramref name="y" />[i]</c>.</remarks>
public static void Add(ReadOnlySpan<float> x, ReadOnlySpan<float> y, Span<float> destination)
{
if (x.Length != y.Length)
{
ThrowHelper.ThrowArgument_SpansMustHaveSameLength(nameof(x), nameof(y));
}

if (x.Length > destination.Length)
{
ThrowHelper.ThrowArgument_DestinationTooShort();
}

for (int i = 0; i < x.Length; i++)
{
destination[i] = x[i] + y[i];
}
}

/// <summary>Computes the element-wise result of: <c><paramref name="x" /> + <paramref name="y" /></c>.</summary>
/// <param name="x">The first tensor, represented as a span.</param>
/// <param name="y">The second tensor, represented as a scalar.</param>
/// <param name="destination">The destination tensor, represented as a span.</param>
/// <exception cref="ArgumentException">Destination is too short.</exception>
/// <remarks>This method effectively does <c><paramref name="destination" />[i] = <paramref name="x" />[i] + <paramref name="y" /></c>.</remarks>
public static void Add(ReadOnlySpan<float> x, float y, Span<float> destination)
{
if (x.Length > destination.Length)
{
ThrowHelper.ThrowArgument_DestinationTooShort();
}

for (int i = 0; i < x.Length; i++)
{
destination[i] = x[i] + y;
}
}

/// <summary>Computes the element-wise result of: <c><paramref name="x" /> - <paramref name="y" /></c>.</summary>
/// <param name="x">The first tensor, represented as a span.</param>
/// <param name="y">The second tensor, represented as a scalar.</param>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;

namespace System.Numerics.Tensors
{
/// <summary>Performs primitive tensor operations over spans of memory.</summary>
public static partial class TensorPrimitives
{
/// <summary>Computes the element-wise result of: <c><paramref name="x" /> + <paramref name="y" /></c>.</summary>
/// <param name="x">The first tensor, represented as a span.</param>
/// <param name="y">The second tensor, represented as a span.</param>
/// <param name="destination">The destination tensor, represented as a span.</param>
/// <exception cref="ArgumentException">Length of '<paramref name="x" />' must be same as length of '<paramref name="y" />'.</exception>
/// <exception cref="ArgumentException">Destination is too short.</exception>
/// <remarks>This method effectively does <c><paramref name="destination" />[i] = <paramref name="x" />[i] + <paramref name="y" />[i]</c>.</remarks>
public static unsafe void Add(ReadOnlySpan<float> x, ReadOnlySpan<float> y, Span<float> destination)
{
if (x.Length != y.Length)
{
ThrowHelper.ThrowArgument_SpansMustHaveSameLength(nameof(x), nameof(y));
}

if (x.Length > destination.Length)
{
ThrowHelper.ThrowArgument_DestinationTooShort();
}

ref float xRef = ref MemoryMarshal.GetReference(x);
ref float yRef = ref MemoryMarshal.GetReference(y);
ref float dRef = ref MemoryMarshal.GetReference(destination);
int remaining = x.Length;

#if NET8_0_OR_GREATER
if (Vector512.IsHardwareAccelerated && remaining >= Vector512<float>.Count)
{
do
{
Vector512.StoreUnsafe(Vector512.LoadUnsafe(ref xRef) + Vector512.LoadUnsafe(ref yRef), ref dRef);

xRef = ref Unsafe.Add(ref xRef, Vector512<float>.Count);
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
yRef = ref Unsafe.Add(ref yRef, Vector512<float>.Count);
dRef = ref Unsafe.Add(ref dRef, Vector512<float>.Count);
remaining -= Vector512<float>.Count;
}
while (remaining >= Vector512<float>.Count);
}
#endif

if (Vector256.IsHardwareAccelerated && remaining >= Vector256<float>.Count)
{
do
{
Vector256.StoreUnsafe(Vector256.LoadUnsafe(ref xRef) + Vector256.LoadUnsafe(ref yRef), ref dRef);

xRef = ref Unsafe.Add(ref xRef, Vector256<float>.Count);
yRef = ref Unsafe.Add(ref yRef, Vector256<float>.Count);
dRef = ref Unsafe.Add(ref dRef, Vector256<float>.Count);
remaining -= Vector256<float>.Count;
}
while (remaining >= Vector256<float>.Count);
}

if (Vector128.IsHardwareAccelerated && remaining >= Vector128<float>.Count)
{
do
{
Vector128.StoreUnsafe(Vector128.LoadUnsafe(ref xRef) + Vector128.LoadUnsafe(ref yRef), ref dRef);

xRef = ref Unsafe.Add(ref xRef, Vector128<float>.Count);
yRef = ref Unsafe.Add(ref yRef, Vector128<float>.Count);
dRef = ref Unsafe.Add(ref dRef, Vector128<float>.Count);
remaining -= Vector128<float>.Count;
}
while (remaining >= Vector128<float>.Count);
}

while (remaining != 0)
{
dRef = xRef + yRef;

xRef = ref Unsafe.Add(ref xRef, 1);
yRef = ref Unsafe.Add(ref yRef, 1);
dRef = ref Unsafe.Add(ref dRef, 1);
remaining--;
}
}

/// <summary>Computes the element-wise result of: <c><paramref name="x" /> + <paramref name="y" /></c>.</summary>
/// <param name="x">The first tensor, represented as a span.</param>
/// <param name="y">The second tensor, represented as a scalar.</param>
/// <param name="destination">The destination tensor, represented as a span.</param>
/// <exception cref="ArgumentException">Destination is too short.</exception>
/// <remarks>This method effectively does <c><paramref name="destination" />[i] = <paramref name="x" />[i] + <paramref name="y" /></c>.</remarks>
public static void Add(ReadOnlySpan<float> x, float y, Span<float> destination)
{
if (x.Length > destination.Length)
{
ThrowHelper.ThrowArgument_DestinationTooShort();
}

ref float xRef = ref MemoryMarshal.GetReference(x);
ref float dRef = ref MemoryMarshal.GetReference(destination);
int remaining = x.Length;

#if NET8_0_OR_GREATER
tannergooding marked this conversation as resolved.
Show resolved Hide resolved
if (Vector512.IsHardwareAccelerated && remaining >= Vector512<float>.Count)
{
Vector512<float> yVec = Vector512.Create(y);
do
{
Vector512.StoreUnsafe(Vector512.LoadUnsafe(ref xRef) + yVec, ref dRef);

xRef = ref Unsafe.Add(ref xRef, Vector512<float>.Count);
dRef = ref Unsafe.Add(ref dRef, Vector512<float>.Count);
remaining -= Vector512<float>.Count;
}
while (remaining >= Vector512<float>.Count);
}
#endif

if (Vector256.IsHardwareAccelerated && remaining >= Vector256<float>.Count)
{
Vector256<float> yVec = Vector256.Create(y);
do
{
Vector256.StoreUnsafe(Vector256.LoadUnsafe(ref xRef) + yVec, ref dRef);

xRef = ref Unsafe.Add(ref xRef, Vector256<float>.Count);
dRef = ref Unsafe.Add(ref dRef, Vector256<float>.Count);
remaining -= Vector256<float>.Count;
}
while (remaining >= Vector256<float>.Count);
}

if (Vector128.IsHardwareAccelerated && remaining >= Vector128<float>.Count)
{
Vector128<float> yVec = Vector128.Create(y);
do
{
Vector128.StoreUnsafe(Vector128.LoadUnsafe(ref xRef) + yVec, ref dRef);

xRef = ref Unsafe.Add(ref xRef, Vector128<float>.Count);
dRef = ref Unsafe.Add(ref dRef, Vector128<float>.Count);
remaining -= Vector128<float>.Count;
}
while (remaining >= Vector128<float>.Count);
}

while (remaining != 0)
{
dRef = xRef + y;

xRef = ref Unsafe.Add(ref xRef, 1);
dRef = ref Unsafe.Add(ref dRef, 1);
remaining--;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.InteropServices;

namespace System.Numerics.Tensors
{
/// <summary>Performs primitive tensor operations over spans of memory.</summary>
public static unsafe partial class TensorPrimitives
{
/// <summary>Computes the element-wise result of: <c><paramref name="x" /> + <paramref name="y" /></c>.</summary>
/// <param name="x">The first tensor, represented as a span.</param>
/// <param name="y">The second tensor, represented as a span.</param>
/// <param name="destination">The destination tensor, represented as a span.</param>
/// <exception cref="ArgumentException">Length of '<paramref name="x" />' must be same as length of '<paramref name="y" />'.</exception>
/// <exception cref="ArgumentException">Destination is too short.</exception>
/// <remarks>This method effectively does <c><paramref name="destination" />[i] = <paramref name="x" />[i] + <paramref name="y" />[i]</c>.</remarks>
public static void Add(ReadOnlySpan<float> x, ReadOnlySpan<float> y, Span<float> destination)
{
if (x.Length != y.Length)
{
ThrowHelper.ThrowArgument_SpansMustHaveSameLength(nameof(x), nameof(y));
}

if (x.Length > destination.Length)
{
ThrowHelper.ThrowArgument_DestinationTooShort();
}

fixed (float* xPtr = &MemoryMarshal.GetReference(x), yPtr = &MemoryMarshal.GetReference(y), destPtr = &MemoryMarshal.GetReference(destination))
{
float* px = xPtr, py = yPtr, pd = destPtr;
int remaining = x.Length;

if (Vector.IsHardwareAccelerated && remaining >= Vector<float>.Count)
{
do
{
*(Vector<float>*)pd = *(Vector<float>*)px + *(Vector<float>*)py;

px += Vector<float>.Count;
py += Vector<float>.Count;
pd += Vector<float>.Count;
remaining -= Vector<float>.Count;
}
while (remaining >= Vector<float>.Count);
}

while (remaining != 0)
{
*pd = *px + *py;

px++;
py++;
pd++;
remaining--;
}
}
}

/// <summary>Computes the element-wise result of: <c><paramref name="x" /> + <paramref name="y" /></c>.</summary>
/// <param name="x">The first tensor, represented as a span.</param>
/// <param name="y">The second tensor, represented as a scalar.</param>
/// <param name="destination">The destination tensor, represented as a span.</param>
/// <exception cref="ArgumentException">Destination is too short.</exception>
/// <remarks>This method effectively does <c><paramref name="destination" />[i] = <paramref name="x" />[i] + <paramref name="y" /></c>.</remarks>
public static void Add(ReadOnlySpan<float> x, float y, Span<float> destination)
{
if (x.Length > destination.Length)
{
ThrowHelper.ThrowArgument_DestinationTooShort();
}

fixed (float* xPtr = &MemoryMarshal.GetReference(x), destPtr = &MemoryMarshal.GetReference(destination))
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
{
float* px = xPtr, pd = destPtr;
int remaining = x.Length;

if (Vector.IsHardwareAccelerated && remaining >= Vector<float>.Count)
{
Vector<float> yVec = new Vector<float>(y);
do
{
*(Vector<float>*)pd = *(Vector<float>*)px + yVec;

px += Vector<float>.Count;
pd += Vector<float>.Count;
remaining -= Vector<float>.Count;
}
while (remaining >= Vector<float>.Count);
}

while (remaining != 0)
{
*pd = *px + y;

px++;
pd++;
remaining--;
}
}
}
}
}
Loading
Loading