Various optimizations

This commit is contained in:
mlemanczyk committed 2025-10-29 14:00:38 +01:00
1 parent 3147728d5b
commit 4c79c4087e
9 files changed
+1179 -1229

No files matched your search

+10 -19
View File
@@ -3,7 +3,6 @@ namespace PerfectNumbers.Core.Gpu;
public static class GpuPrimeWorkLimiter
{
private static SemaphoreSlim _semaphore = new(1, int.MaxValue);
private static readonly object _lock = new();
private static int _currentLimit = 1;
public static Lease Acquire()
@@ -11,29 +10,21 @@ public static class GpuPrimeWorkLimiter
// TODO: Inline the pooled limiter guard from the GpuLimiterThroughputBenchmarks so prime-sieve
// GPU jobs stop allocating Lease instances on every acquisition.
var sem = _semaphore;
sem.Wait();
// sem.Wait();
return new Lease(sem);
}
public static void SetLimit(int value)
{
if (value < 1)
{
value = 1;
}
if (value == _currentLimit)
{
return;
}
lock (_lock)
{
if (value == _currentLimit)
{
return;
}
// TODO: Switch to a shared limiter implementation with GpuWorkLimiter so we can coordinate CPU/GPU prime work using
// the same semaphore pool without reallocating per adjustment.
_semaphore = new SemaphoreSlim(value, value);
_currentLimit = value;
}
// TODO: Switch to a shared limiter implementation with GpuWorkLimiter so we can coordinate CPU/GPU prime work using
// the same semaphore pool without reallocating per adjustment.
_semaphore = new SemaphoreSlim(value, value);
_currentLimit = value;
}
public sealed class Lease
@@ -47,7 +38,7 @@ public static class GpuPrimeWorkLimiter
public void Dispose()
{
_sem.Release();
// _sem.Release();
}
}
}