Benchmarks to compare Interlocked with Atomic added. Atomic replaced with Interlocked on CPU path as better performer.

This commit is contained in:
mlemanczyk committed 2025-12-11 23:45:01 +01:00
1 parent 0ec48f1f0b
commit 43376745f5
3 files changed
+247 -6

No files matched your search

+5 -2
View File
@@ -1,3 +1,4 @@
using System.Runtime.CompilerServices;
using ILGPU;
using ILGPU.Runtime;
@@ -11,10 +12,11 @@ namespace PerfectNumbers.Core.Gpu
private int _index;
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public int Rent()
{
Atomic.CompareExchange(ref _index, Capacity, 0);
var index = Atomic.Add(ref _index, 1);
_ = Interlocked.CompareExchange(ref _index, Capacity, 0);
var index = Interlocked.Increment(ref _index);
if (index >= Capacity)
{
index -= Capacity;
@@ -23,6 +25,7 @@ namespace PerfectNumbers.Core.Gpu
return index;
}
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public void Return(int acceleratorIndex)
{
// Intentionally left empty - there's nothing to do here. We're not really renting anything.
@@ -377,12 +377,12 @@ internal static partial class PrimeOrderCalculator
{
while (true)
{
if (Atomic.CompareExchange(ref _cpuCount, 0, 1) == 0)
if (Interlocked.CompareExchange(ref _cpuCount, 0, 1) == 0)
{
return true;
}
if (Atomic.CompareExchange(ref _cpuCount, 1, 0) == 1)
if (Interlocked.CompareExchange(ref _cpuCount, 1, 0) == 1)
{
return false;
}
@@ -391,10 +391,10 @@ internal static partial class PrimeOrderCalculator
}
}
int cpuCount = Atomic.Add(ref _cpuCount, 1);
int cpuCount = Interlocked.Increment(ref _cpuCount);
if (cpuCount == PerfectNumberConstants.GpuRatio)
{
Atomic.Add(ref _cpuCount, -PerfectNumberConstants.GpuRatio);
Interlocked.Add(ref _cpuCount, -PerfectNumberConstants.GpuRatio);
}
else if (cpuCount > PerfectNumberConstants.GpuRatio)
{