-
Notifications
You must be signed in to change notification settings - Fork 612
Support and accelerate bitmap operations on more platforms #942
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
27 commits
Select commit
Hold shift + click to select a range
8127004
Initial BITOP refactor
PaulusParssinen e691692
Remove TensorPrimitives invoke logic for now
PaulusParssinen ca04c55
Merge branch 'main' into bitop
PaulusParssinen 810b8af
format whitespace & remove incorrect todo comment
PaulusParssinen 693f504
Remove unnecessary usings
PaulusParssinen 5aa410e
Add HW acceleration guards and V128 core path for non V256 CPUs
PaulusParssinen c4d7fc2
Merge branch 'main' into bitop
PaulusParssinen 4a4c259
fix
PaulusParssinen 2928ff7
Add S.N.TensorPrimitives and use it for BITOP NOT
PaulusParssinen bfcbbdb
Use TensorPrimitives to specialize two key scenario
PaulusParssinen e9acc2f
Make BitOp tests more granular with parameterization
PaulusParssinen b21aa39
Merge branch 'main' into bitop
PaulusParssinen 6c7eb04
Merge branch 'main' into bitop
PaulusParssinen 365d9f9
Make uneven 2-input specialization fallback to the multi-key path for…
PaulusParssinen ba37ada
Simplify bitmap operation result memory logic
PaulusParssinen 1d56721
Add out-of-proc tests for the bitop HW accelerated paths
PaulusParssinen ebd0192
Add benchmarks
PaulusParssinen d772183
dotnet format
PaulusParssinen 52140a7
Merge branch 'main' into bitop
PaulusParssinen 3e62352
Add Vector512 path
PaulusParssinen 1576d35
Handle environment variables containing '='
PaulusParssinen 95b4ae0
Merge branch 'main' into bitop
PaulusParssinen 292f650
Remove the two-input special path
PaulusParssinen 21ed296
Add BITOP DIFF support
PaulusParssinen 1c0d084
Wire up DIFF with rest of the BITOPs
PaulusParssinen 4693c48
forgot to set the Lua registry constant
PaulusParssinen 70993bf
Merge branch 'main' into bitop
vazois 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
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,81 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
using System.Runtime.InteropServices; | ||
using BenchmarkDotNet.Attributes; | ||
using Garnet.server; | ||
|
||
namespace BDN.benchmark.Bitmap | ||
{ | ||
public unsafe class BinaryOperations | ||
{ | ||
private const int Alignment = 64; | ||
|
||
[ParamsSource(nameof(GetBitmapSizes))] | ||
public Sizes BitmapSizes { get; set; } | ||
|
||
[Params(BitmapOperation.XOR)] | ||
public BitmapOperation Op { get; set; } | ||
|
||
public IEnumerable<Sizes> GetBitmapSizes() | ||
{ | ||
yield return new([1 << 21, 1 << 21]); | ||
yield return new([1 << 21, (1 << 21) + 1]); | ||
|
||
yield return new([1 << 21, 1 << 21, 1 << 21]); | ||
yield return new([1 << 21, 1 << 21, (1 << 21) + 1]); | ||
|
||
yield return new([256, 6 * 512 + 7, 512, 1024]); | ||
} | ||
|
||
private int minBitmapSize; | ||
private byte** srcPtrs; | ||
private byte** srcEndPtrs; | ||
|
||
private int dstLength; | ||
private byte* dstPtr; | ||
|
||
[GlobalSetup] | ||
public void GlobalSetup_Binary() | ||
{ | ||
minBitmapSize = BitmapSizes.Values.Min(); | ||
srcPtrs = (byte**)NativeMemory.AllocZeroed((nuint)BitmapSizes.Values.Length, (nuint)sizeof(byte*)); | ||
srcEndPtrs = (byte**)NativeMemory.AllocZeroed((nuint)BitmapSizes.Values.Length, (nuint)sizeof(byte*)); | ||
|
||
for (var i = 0; i < BitmapSizes.Values.Length; i++) | ||
{ | ||
srcPtrs[i] = (byte*)NativeMemory.AlignedAlloc((nuint)BitmapSizes.Values[i], Alignment); | ||
srcEndPtrs[i] = srcPtrs[i] + BitmapSizes.Values[i]; | ||
|
||
new Random(i).NextBytes(new Span<byte>(srcPtrs[i], BitmapSizes.Values[i])); | ||
} | ||
|
||
dstLength = BitmapSizes.Values.Max(); | ||
dstPtr = (byte*)NativeMemory.AlignedAlloc((nuint)dstLength, Alignment); | ||
} | ||
|
||
[Benchmark] | ||
public void BinaryOperation() | ||
{ | ||
BitmapManager.InvokeBitOperationUnsafe(Op, BitmapSizes.Values.Length, srcPtrs, srcEndPtrs, dstPtr, dstLength, minBitmapSize); | ||
} | ||
|
||
[GlobalCleanup] | ||
public void GlobalCleanup() | ||
{ | ||
for (var i = 0; i < BitmapSizes.Values.Length; i++) | ||
{ | ||
NativeMemory.AlignedFree(srcPtrs[i]); | ||
} | ||
|
||
NativeMemory.Free(srcPtrs); | ||
NativeMemory.Free(srcEndPtrs); | ||
NativeMemory.AlignedFree(dstPtr); | ||
} | ||
|
||
public record struct Sizes(int[] Values) | ||
{ | ||
public override string ToString() => string.Join(", ", Values); | ||
} | ||
} | ||
} |
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,59 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
using System.Runtime.InteropServices; | ||
using BenchmarkDotNet.Attributes; | ||
using Garnet.server; | ||
|
||
namespace BDN.benchmark.Bitmap | ||
{ | ||
public unsafe partial class UnaryOperations | ||
{ | ||
private const int Alignment = 64; | ||
|
||
[ParamsSource(nameof(GetBitmapSize))] | ||
public int BitmapSize { get; set; } | ||
|
||
public IEnumerable<int> GetBitmapSize() | ||
{ | ||
yield return 256; | ||
yield return 1 << 21; | ||
} | ||
|
||
private const int Keys = 1; | ||
private byte** srcPtrs; | ||
private byte** srcEndPtrs; | ||
|
||
private byte* dstPtr; | ||
|
||
[GlobalSetup] | ||
public void GlobalSetup_Unary() | ||
{ | ||
srcPtrs = (byte**)NativeMemory.AllocZeroed(Keys, (nuint)sizeof(byte*)); | ||
srcEndPtrs = (byte**)NativeMemory.AllocZeroed(Keys, (nuint)sizeof(byte*)); | ||
|
||
srcPtrs[0] = (byte*)NativeMemory.AlignedAlloc((uint)BitmapSize, Alignment); | ||
srcEndPtrs[0] = srcPtrs[0] + (uint)BitmapSize; | ||
|
||
new Random(0).NextBytes(new Span<byte>(srcPtrs[0], BitmapSize)); | ||
|
||
dstPtr = (byte*)NativeMemory.AlignedAlloc((nuint)BitmapSize, Alignment); | ||
} | ||
|
||
[Benchmark] | ||
public void BitOperation_NOT() | ||
{ | ||
BitmapManager.InvokeBitOperationUnsafe(BitmapOperation.NOT, Keys, srcPtrs, srcEndPtrs, dstPtr, BitmapSize, BitmapSize); | ||
} | ||
|
||
[GlobalCleanup] | ||
public void GlobalCleanup() | ||
{ | ||
NativeMemory.AlignedFree(srcPtrs[0]); | ||
|
||
NativeMemory.Free(srcPtrs); | ||
NativeMemory.Free(srcEndPtrs); | ||
NativeMemory.AlignedFree(dstPtr); | ||
} | ||
} | ||
} |
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,80 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
using System.Numerics; | ||
using System.Runtime.Intrinsics; | ||
|
||
namespace Garnet.common.Numerics | ||
{ | ||
/// <summary>Operator that takes two input values and returns a single value.</summary> | ||
public interface IBinaryOperator | ||
{ | ||
/// <summary> | ||
/// Computes the binary operation of two scalar values. | ||
/// </summary> | ||
static abstract T Invoke<T>(T x, T y) where T : IBinaryInteger<T>; | ||
PaulusParssinen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// <summary> | ||
/// Computes the binary operation of two vectors. | ||
/// </summary> | ||
static abstract Vector128<byte> Invoke(Vector128<byte> x, Vector128<byte> y); | ||
|
||
/// <inheritdoc cref="Invoke(Vector128{byte}, Vector128{byte})"/> | ||
static abstract Vector256<byte> Invoke(Vector256<byte> x, Vector256<byte> y); | ||
|
||
/// <inheritdoc cref="Invoke(Vector128{byte}, Vector128{byte})"/> | ||
static abstract Vector512<byte> Invoke(Vector512<byte> x, Vector512<byte> y); | ||
} | ||
|
||
/// <summary><c>x & y</c></summary> | ||
public readonly struct BitwiseAndOperator : IBinaryOperator | ||
{ | ||
/// <inheritdoc/> | ||
public static T Invoke<T>(T x, T y) where T : IBinaryInteger<T> => x & y; | ||
/// <inheritdoc/> | ||
public static Vector128<byte> Invoke(Vector128<byte> x, Vector128<byte> y) => x & y; | ||
/// <inheritdoc/> | ||
public static Vector256<byte> Invoke(Vector256<byte> x, Vector256<byte> y) => x & y; | ||
/// <inheritdoc/> | ||
public static Vector512<byte> Invoke(Vector512<byte> x, Vector512<byte> y) => x & y; | ||
} | ||
|
||
/// <summary><c>x | y</c></summary> | ||
public readonly struct BitwiseOrOperator : IBinaryOperator | ||
{ | ||
/// <inheritdoc/> | ||
public static T Invoke<T>(T x, T y) where T : IBinaryInteger<T> => x | y; | ||
/// <inheritdoc/> | ||
public static Vector128<byte> Invoke(Vector128<byte> x, Vector128<byte> y) => x | y; | ||
/// <inheritdoc/> | ||
public static Vector256<byte> Invoke(Vector256<byte> x, Vector256<byte> y) => x | y; | ||
/// <inheritdoc/> | ||
public static Vector512<byte> Invoke(Vector512<byte> x, Vector512<byte> y) => x | y; | ||
} | ||
|
||
/// <summary><c>x ^ y</c></summary> | ||
public readonly struct BitwiseXorOperator : IBinaryOperator | ||
{ | ||
/// <inheritdoc/> | ||
public static T Invoke<T>(T x, T y) where T : IBinaryInteger<T> => x ^ y; | ||
/// <inheritdoc/> | ||
public static Vector128<byte> Invoke(Vector128<byte> x, Vector128<byte> y) => x ^ y; | ||
/// <inheritdoc/> | ||
public static Vector256<byte> Invoke(Vector256<byte> x, Vector256<byte> y) => x ^ y; | ||
/// <inheritdoc/> | ||
public static Vector512<byte> Invoke(Vector512<byte> x, Vector512<byte> y) => x ^ y; | ||
} | ||
|
||
/// <summary><c>x & ~y</c></summary> | ||
public readonly struct BitwiseAndNotOperator : IBinaryOperator | ||
{ | ||
/// <inheritdoc/> | ||
public static T Invoke<T>(T x, T y) where T : IBinaryInteger<T> => x & ~y; | ||
/// <inheritdoc/> | ||
public static Vector128<byte> Invoke(Vector128<byte> x, Vector128<byte> y) => x & ~y; | ||
/// <inheritdoc/> | ||
public static Vector256<byte> Invoke(Vector256<byte> x, Vector256<byte> y) => x & ~y; | ||
/// <inheritdoc/> | ||
public static Vector512<byte> Invoke(Vector512<byte> x, Vector512<byte> y) => x & ~y; | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.
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.