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

Memory alignment benchmarks #19

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
64 changes: 64 additions & 0 deletions Benchmarks/MemoryAlignment/AddArrays.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

using BenchmarkDotNet.Attributes;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;

public unsafe class AddArrays
{
private static T* Allocate<T>(int elemCount, int offsetFromAligned) where T : unmanaged
{
var toalloc = (nuint)(elemCount * sizeof(T) + offsetFromAligned);
return (T*)((byte*)NativeMemory.AlignedAlloc(toalloc, 64) + offsetFromAligned);
}

[Params(0x1000)]
public int ElemCount;

[Params(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63)]
public int Offset;

private int* a;
private int* b;
private int* c;

[GlobalSetup]
public void Setup()
{
a = Allocate<int>(ElemCount, Offset);
b = Allocate<int>(ElemCount, Offset);
c = Allocate<int>(ElemCount, Offset);
var rand = new Random(100);
for (int i = 0; i < ElemCount; i++)
{
a[i] = rand.Next();
b[i] = rand.Next();
}
}

/*
[Benchmark]
public int NaiveAdd()
{
var e = ElemCount;
for (int i = 0; i < e; i++)
c[i] = a[i] + b[i];
return c[Math.Abs(a[0] + b[e / 2]) % e];
}*/

[Benchmark]
public int SimdAdd()
{
var e = ElemCount;
for (int i = 0; i < e; i += Vector256<int>.Count)
{
var av = Avx2.LoadVector256(a + i);
var bv = Avx2.LoadVector256(b + i);
Avx2.Store(c + i, Avx2.Add(av, bv));
}
return c[Math.Abs(a[0] + b[e / 2]) % e];
}
}
15 changes: 15 additions & 0 deletions Benchmarks/MemoryAlignment/MemoryAlignment.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.0" />
</ItemGroup>

</Project>
19 changes: 19 additions & 0 deletions Benchmarks/MemoryAlignment/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
var b = new AddArrays();

b.ElemCount = 0x10;
b.Offset = 0;
b.Setup();
Console.WriteLine(b.NaiveAdd());
Console.WriteLine(b.SimdAdd());

b.ElemCount = 0x10;
b.Offset = 46;
b.Setup();
Console.WriteLine(b.NaiveAdd());
Console.WriteLine(b.SimdAdd());
*/

using BenchmarkDotNet.Running;

BenchmarkRunner.Run<AddArrays>();
Loading