Pools renamed to FixedCapacityPools. Other fixed capacity collections added. Queues, concurrent queues and standard stacks replaced with fixed capacity stacks. MethodImplOptions.AggressiveInlining and .AggressiveOptimization added to simple methods and getters / setters. Variables optimized in few places.
This commit is contained in:
1 parent
547d43de84
commit
260aa19e44
25 files changed
+1372
-1300
No files matched your search
@@ -1,4 +1,4 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ILGPU.Runtime;
|
||||
|
||||
namespace PerfectNumbers.Core.Gpu.Accelerators;
|
||||
@@ -7,13 +7,14 @@ public static class AcceleratorStreamPool
|
||||
{
|
||||
private static readonly Accelerator[] _accelerators = AcceleratorPool.Shared.Accelerators;
|
||||
private static readonly SemaphoreSlim[] _locks = new SemaphoreSlim[PerfectNumberConstants.RollingAccelerators];
|
||||
private static readonly ConcurrentQueue<AcceleratorStream>[] _streams = new ConcurrentQueue<AcceleratorStream> [PerfectNumberConstants.RollingAccelerators];
|
||||
private static readonly ConcurrentFixedCapacityStack<AcceleratorStream>[] _streams = new ConcurrentFixedCapacityStack<AcceleratorStream>[PerfectNumberConstants.RollingAccelerators];
|
||||
|
||||
private static SemaphoreSlim CreateLock() => new(PerfectNumberConstants.ThreadsByAccelerator);
|
||||
|
||||
|
||||
// private static int _rented;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static AcceleratorStream Rent(int acceleratorIndex)
|
||||
{
|
||||
var streamLock = _locks[acceleratorIndex];
|
||||
@@ -21,22 +22,23 @@ public static class AcceleratorStreamPool
|
||||
|
||||
streamLock.Wait();
|
||||
|
||||
return queue.TryDequeue(out var stream)
|
||||
return queue.Pop() is { } stream
|
||||
? stream
|
||||
: _accelerators[acceleratorIndex].CreateStream();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static void Return(int acceleratorIndex, AcceleratorStream stream)
|
||||
{
|
||||
var streamLock = _locks[acceleratorIndex];
|
||||
var queue = _streams[acceleratorIndex];
|
||||
queue.Enqueue(stream);
|
||||
queue.Push(stream);
|
||||
streamLock.Release();
|
||||
}
|
||||
|
||||
public static void WarmUp(int acceleratorIndex)
|
||||
{
|
||||
_streams[acceleratorIndex] = new();
|
||||
_streams[acceleratorIndex] = new(PerfectNumberConstants.DefaultPoolCapacity);
|
||||
_locks[acceleratorIndex] = CreateLock();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ILGPU;
|
||||
using ILGPU.Runtime;
|
||||
@@ -11,34 +10,31 @@ public sealed class PrimeOrderCalculatorAccelerator
|
||||
#region Pool
|
||||
|
||||
// [ThreadStatic]
|
||||
private static ConcurrentQueue<PrimeOrderCalculatorAccelerator>? _pool;
|
||||
private static ConcurrentFixedCapacityStack<PrimeOrderCalculatorAccelerator>? _pool;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static PrimeOrderCalculatorAccelerator Rent(int primeTesterCapacity)
|
||||
{
|
||||
var pool = _pool ??= [];
|
||||
if (!pool.TryDequeue(out var gpu))
|
||||
{
|
||||
int acceleratorIndex = AcceleratorPool.Shared.Rent();
|
||||
gpu = new(acceleratorIndex, PerfectNumberConstants.DefaultFactorsBuffer, primeTesterCapacity, PerfectNumberConstants.DefaultSmallPrimeFactorSlotCount, PerfectNumberConstants.DefaultSpecialMaxFactorCapacity);
|
||||
}
|
||||
else
|
||||
var pool = _pool ??= new(PerfectNumberConstants.DefaultThreadPoolCapacity);
|
||||
if (pool.Pop() is { } gpu)
|
||||
{
|
||||
gpu.EnsureCapacity(PerfectNumberConstants.DefaultFactorsBuffer, primeTesterCapacity);
|
||||
return gpu;
|
||||
}
|
||||
|
||||
return gpu;
|
||||
int acceleratorIndex = AcceleratorPool.Shared.Rent();
|
||||
return new(acceleratorIndex, PerfectNumberConstants.DefaultFactorsBuffer, primeTesterCapacity, PerfectNumberConstants.DefaultSmallPrimeFactorSlotCount, PerfectNumberConstants.DefaultSpecialMaxFactorCapacity);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Return(PrimeOrderCalculatorAccelerator gpu) => _pool!.Enqueue(gpu);
|
||||
public static void Return(PrimeOrderCalculatorAccelerator gpu) => _pool!.Push(gpu);
|
||||
|
||||
internal static void Clear()
|
||||
{
|
||||
var pool = _pool;
|
||||
if (pool != null)
|
||||
{
|
||||
while (pool.TryDequeue(out var gpu))
|
||||
while (pool.Pop() is { } gpu)
|
||||
{
|
||||
gpu.Dispose();
|
||||
}
|
||||
@@ -50,7 +46,7 @@ public sealed class PrimeOrderCalculatorAccelerator
|
||||
var pool = _pool;
|
||||
if (pool != null)
|
||||
{
|
||||
while (pool.TryDequeue(out var gpu))
|
||||
while (pool.Pop() is { } gpu)
|
||||
{
|
||||
gpu.Dispose();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user