-
Notifications
You must be signed in to change notification settings - Fork 297
Add ScatterStore and GatherLoad to SVE microbenchmark #5041
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
13ba98a
Add ScatterStore and GatherLoad to SVE microbenchmark
ylpoonlg 3b657a4
Merge branch 'main' into github-scattergather
ylpoonlg be8aca8
Add SVE Category
ylpoonlg 84bac1b
Remove unused variable
ylpoonlg 2ad287d
Use uint for gather result
ylpoonlg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,103 @@ | ||
| 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.Sve)] | ||
| [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 uint _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() | ||
| { | ||
| uint current = _result; | ||
| Setup(); | ||
| Scalar(); | ||
| uint 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) | ||
| { | ||
| uint 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> 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. | ||
| uint res = (uint)Sve.AddAcross(resVec).ToScalar(); | ||
| _result = res; | ||
| } | ||
| } | ||
|
|
||
| } | ||
| } | ||
This file contains hidden or 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,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.Sve)] | ||
| [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); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.