Various optimizations
This commit is contained in:
1 parent
3147728d5b
commit
4c79c4087e
9 files changed
+1179
-1229
No files matched your search
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester : IMersenneNumberDiv
|
||||
var gpuLease = RentAccelerator();
|
||||
var accelerator = gpuLease.Accelerator;
|
||||
|
||||
Monitor.Enter(gpuLease.ExecutionLock);
|
||||
// Monitor.Enter(gpuLease.ExecutionLock);
|
||||
|
||||
var kernel = GetKernel(accelerator);
|
||||
var resources = RentBatchResources(accelerator, batchCapacity);
|
||||
@@ -121,7 +121,7 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester : IMersenneNumberDiv
|
||||
out processedCount);
|
||||
|
||||
ReturnBatchResources(resources);
|
||||
Monitor.Exit(gpuLease.ExecutionLock);
|
||||
// Monitor.Exit(gpuLease.ExecutionLock);
|
||||
ReturnAccelerator(gpuLease);
|
||||
|
||||
if (composite)
|
||||
@@ -560,7 +560,7 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester : IMersenneNumberDiv
|
||||
}
|
||||
}
|
||||
|
||||
Monitor.Enter(_lease.ExecutionLock);
|
||||
// Monitor.Enter(_lease.ExecutionLock);
|
||||
|
||||
EnsureExecutionResourcesLocked(length);
|
||||
|
||||
@@ -601,7 +601,7 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester : IMersenneNumberDiv
|
||||
ref byte hitRef = ref MemoryMarshal.GetReference(hitSlice);
|
||||
hitView.CopyToCPU(ref hitRef, length);
|
||||
|
||||
Monitor.Exit(_lease.ExecutionLock);
|
||||
// Monitor.Exit(_lease.ExecutionLock);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
||||
@@ -30,7 +30,7 @@ public class MersenneNumberResidueGpuTester(bool useGpuOrder)
|
||||
var gpuLease = RentAccelerator();
|
||||
var accelerator = gpuLease.Accelerator;
|
||||
|
||||
Monitor.Enter(gpuLease.ExecutionLock);
|
||||
// Monitor.Enter(gpuLease.ExecutionLock);
|
||||
|
||||
var stream = accelerator.CreateStream();
|
||||
var kernel = GetPow2ModKernel(accelerator);
|
||||
@@ -108,7 +108,7 @@ public class MersenneNumberResidueGpuTester(bool useGpuOrder)
|
||||
|
||||
ReturnResources(accelerator, resources);
|
||||
stream.Dispose();
|
||||
Monitor.Exit(gpuLease.ExecutionLock);
|
||||
// Monitor.Exit(gpuLease.ExecutionLock);
|
||||
ReturnAccelerator(gpuLease);
|
||||
limiter.Dispose();
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -122,10 +122,10 @@ public sealed class HeuristicPrimeTester
|
||||
return EvaluateWithOpenNumericFallback(n);
|
||||
}
|
||||
|
||||
if (!UseHeuristicGroupBTrialDivision)
|
||||
{
|
||||
return HeuristicTrialDivisionCpu(n, sqrtLimit, nMod10, includeGroupB: false);
|
||||
}
|
||||
// if (!UseHeuristicGroupBTrialDivision)
|
||||
// {
|
||||
// return HeuristicTrialDivisionCpu(n, sqrtLimit, nMod10, includeGroupB: false);
|
||||
// }
|
||||
|
||||
bool compositeDetected = HeuristicTrialDivisionGpuDetectsDivisor(n, sqrtLimit, nMod10);
|
||||
return !compositeDetected;
|
||||
@@ -260,8 +260,8 @@ public sealed class HeuristicPrimeTester
|
||||
|
||||
private bool HeuristicTrialDivisionGpuDetectsDivisor(ulong n, ulong sqrtLimit, byte nMod10)
|
||||
{
|
||||
int batchCapacity = Math.Max(1, HeuristicGpuDivisorBatchSize);
|
||||
int interleaveBatchSize = Math.Max(1, HeuristicDivisorInterleaveBatchSize);
|
||||
int batchCapacity = HeuristicGpuDivisorBatchSize;
|
||||
int interleaveBatchSize = HeuristicDivisorInterleaveBatchSize;
|
||||
var divisorPool = ThreadStaticPools.UlongPool;
|
||||
var hitPool = ThreadStaticPools.BytePool;
|
||||
|
||||
@@ -277,7 +277,7 @@ public sealed class HeuristicPrimeTester
|
||||
bool compositeDetected = false;
|
||||
int count = 0;
|
||||
|
||||
lock (gpu.ExecutionLock)
|
||||
// lock (gpu.ExecutionLock)
|
||||
{
|
||||
var input = scratch.Input;
|
||||
var output = scratch.Output;
|
||||
|
||||
+497
-516
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user