Guard residue GPU scanning with execution lock

This commit is contained in:
Marek Lemańczyk committed 2025-10-21 21:26:53 +02:00
1 parent 5dd9eb636d
commit 819cb7b00b
1 file changed
+17 -5
@@ -17,11 +17,16 @@ public class MersenneNumberResidueGpuTester(bool useGpuOrder)
public void Scan(ulong exponent, UInt128 twoP, bool lastIsSeven, UInt128 maxK, ref bool isPrime)
{
var gpuLease = GpuKernelPool.GetKernel(_useGpuOrder);
var execution = gpuLease.EnterExecutionScope();
try
{
var accelerator = gpuLease.Accelerator;
var stream = gpuLease.Stream;
// Ensure device has small cycles and primes tables for in-kernel lookup
var smallCyclesView = GpuKernelPool.EnsureSmallCyclesOnDevice(accelerator);
ResiduePrimeViews primeViews = GpuKernelPool.EnsureSmallPrimesOnDevice(accelerator);
int batchSize = GpuConstants.ScanBatchSize;
UInt128 kStart = 1UL;
UInt128 limit = maxK + UInt128.One;
@@ -41,13 +46,15 @@ public class MersenneNumberResidueGpuTester(bool useGpuOrder)
// testers such as the order kernels.
orderBuffer.MemSetToZero();
ulong[] orderArray = ArrayPool<ulong>.Shared.Rent(batchSize);
UInt128 batchSize128 = (UInt128)batchSize;
UInt128 fullBatchStep = twoP * batchSize128;
UInt128 q = twoP * kStart + UInt128.One;
// TODO: Replace the direct UInt128 multiply/add updates below with the residue-cycle stepping helper so that q advances reuse cached cycle increments rather than recomputing twoP multiples on every iteration.
try
{
UInt128 batchSize128 = (UInt128)batchSize;
UInt128 fullBatchStep = twoP * batchSize128;
UInt128 q = twoP * kStart + UInt128.One;
// TODO: Replace the direct UInt128 multiply/add updates below with the residue-cycle stepping helper so that q advances
// reuse cached cycle increments rather than recomputing twoP multiples on every iteration.
while (kStart < limit && Volatile.Read(ref isPrime))
{
UInt128 remaining = limit - kStart;
@@ -101,8 +108,13 @@ public class MersenneNumberResidueGpuTester(bool useGpuOrder)
}
finally
{
ArrayPool<ulong>.Shared.Return(orderArray);
ArrayPool<ulong>.Shared.Return(orderArray, clearArray: false);
orderBuffer.Dispose();
}
}
finally
{
execution.Dispose();
gpuLease.Dispose();
}
}