Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
104 changes: 104 additions & 0 deletions src/benchmarks/micro/sve/GatherLoad.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using System;
using System.Diagnostics;
using System.Numerics;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.Arm;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Extensions;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Filters;
using MicroBenchmarks;

namespace SveBenchmarks
{
[BenchmarkCategory(Categories.Runtime)]
[OperatingSystemsArchitectureFilter(allowed: true, System.Runtime.InteropServices.Architecture.Arm64)]
[Config(typeof(Config))]
public class GatherLoad
{
private class Config : ManualConfig
{
public Config()
{
AddFilter(new SimpleFilter(_ => Sve.IsSupported));
}
}

[Params(15, 127, 527, 10015)]
public int Size;

private uint[] _objects;
private uint[] _indices;
private ulong _result;

[GlobalSetup]
public virtual void Setup()
{
_objects = ValuesGenerator.Array<uint>(Size);
_indices = ValuesGenerator.Array<uint>(Size);
for (int i = 0; i < Size; i++)
{
_indices[i] %= (uint)Size;
}
}

[GlobalCleanup]
public virtual void Verify()
{
ulong current = _result;
Setup();
Scalar();
ulong scalar = _result;
// Check that the result is the same as the scalar result.
Debug.Assert(current == scalar);
}

// The following algorithms are adapted from the Arm simd-loops repository:
// https://gitlab.arm.com/architecture/simd-loops/-/blob/main/loops/loop_023.c

[Benchmark]
public unsafe void Scalar()
{
fixed (uint* objects = _objects, indices = _indices)
{
ulong res = 0;
for (int i = 0; i < Size; i++)
{
res += objects[indices[i]];
}
_result = res;
}
}

[Benchmark]
public unsafe void SveGatherLoad()
{
fixed (uint* objects = _objects, indices = _indices)
{
int i = 0;
int cntw = (int)Sve.Count32BitElements();

Vector<uint> ones = Vector<uint>.One;
Vector<uint> resVec = Vector<uint>.Zero;
Vector<uint> pTrue = Sve.CreateTrueMaskUInt32();
Vector<uint> pLoop = Sve.CreateWhileLessThanMask32Bit(0, Size);
while (Sve.TestFirstTrue(pTrue, pLoop))
{
// Load indices
Vector<uint> idxVec = Sve.LoadVector(pLoop, indices + i);
// Gather elements from objects using indices.
Vector<uint> objVec = Sve.GatherVector(pLoop, objects, idxVec);
// Add results to resVec.
resVec = Sve.Add(resVec, objVec);

i += cntw;
pLoop = Sve.CreateWhileLessThanMask32Bit(i, Size);
}
// Add up all elements in resVec.
ulong res = (ulong)Sve.AddAcross(resVec).ToScalar();
_result = res;
}
}

}
}
98 changes: 98 additions & 0 deletions src/benchmarks/micro/sve/ScatterStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System;
using System.Diagnostics;
using System.Numerics;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.Arm;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Extensions;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Filters;
using MicroBenchmarks;

namespace SveBenchmarks
{
[BenchmarkCategory(Categories.Runtime)]
[OperatingSystemsArchitectureFilter(allowed: true, System.Runtime.InteropServices.Architecture.Arm64)]
[Config(typeof(Config))]
public class ScatterStore
{
private class Config : ManualConfig
{
public Config()
{
AddFilter(new SimpleFilter(_ => Sve.IsSupported));
}
}

[Params(15, 127, 527, 10015)]
public int Size;

private uint[] _objects;
private uint[] _indices;

[GlobalSetup]
public virtual void Setup()
{
_objects = new uint[Size];
_indices = ValuesGenerator.Array<uint>(Size);
for (int i = 0; i < Size; i++)
{
_indices[i] %= (uint)Size;
}
}

[GlobalCleanup]
public virtual void Verify()
{
uint[] current = (uint[])_objects.Clone();
Setup();
Scalar();
uint[] scalar = (uint[])_objects.Clone();
// Check that the result is the same as the scalar result.
for (int i = 0; i < current.Length; i++)
{
Debug.Assert(current[i] == scalar[i]);
}
}

// The following algorithms are adapted from the Arm simd-loops repository:
// https://gitlab.arm.com/architecture/simd-loops/-/blob/main/loops/loop_019.c

[Benchmark]
public unsafe void Scalar()
{
fixed (uint* objects = _objects, indices = _indices)
{
for (int i = 0; i < Size; i++)
{
objects[indices[i]] = 1;
}
}
}

[Benchmark]
public unsafe void SveScatterStore()
{
fixed (uint* objects = _objects, indices = _indices)
{
int i = 0;
int cntw = (int)Sve.Count32BitElements();

Vector<uint> ones = Vector<uint>.One;
Vector<uint> pTrue = Sve.CreateTrueMaskUInt32();
Vector<uint> pLoop = Sve.CreateWhileLessThanMask32Bit(0, Size);
while (Sve.TestFirstTrue(pTrue, pLoop))
{
Vector<uint> idxVec = Sve.LoadVector(pLoop, indices + i);

// Set the indices within objects with ones.
Sve.Scatter(pLoop, objects, idxVec, ones);

i += cntw;
pLoop = Sve.CreateWhileLessThanMask32Bit(i, Size);
}
}
}

}
}