Breaking change partially reverted

This commit is contained in:
Marek Lemańczyk committed 2025-09-21 02:48:51 +02:00
1 parent 8a6fc0c6a5
commit 23e8d306b1
3 files changed
+62 -67

No files matched your search

-2
View File
@@ -372,8 +372,6 @@ internal static class Program
} }
} }
residueKMax = ResolveResidueMaxK(useResidue, residueKMaxExplicit, divisorCyclesLimitExplicit, residueKMax, divisorCyclesSearchLimit128);
if (showHelp) if (showHelp)
{ {
PrintHelp(); PrintHelp();
@@ -15,39 +15,40 @@ public class MersenneNumberResidueGpuTester(bool useGpuOrder)
public void Scan(ulong exponent, UInt128 twoP, bool lastIsSeven, UInt128 maxK, ref bool isPrime) public void Scan(ulong exponent, UInt128 twoP, bool lastIsSeven, UInt128 maxK, ref bool isPrime)
{ {
var gpuLease = GpuKernelPool.GetKernel(_useGpuOrder); var gpuLease = GpuKernelPool.GetKernel(_useGpuOrder);
var accelerator = gpuLease.Accelerator; var accelerator = gpuLease.Accelerator;
// Ensure device has small cycles and primes tables for in-kernel lookup var stream = gpuLease.Stream;
var smallCyclesView = GpuKernelPool.EnsureSmallCyclesOnDevice(accelerator); // Ensure device has small cycles and primes tables for in-kernel lookup
ResiduePrimeViews primeViews = GpuKernelPool.EnsureSmallPrimesOnDevice(accelerator); var smallCyclesView = GpuKernelPool.EnsureSmallCyclesOnDevice(accelerator);
int batchSize = GpuConstants.ScanBatchSize; ResiduePrimeViews primeViews = GpuKernelPool.EnsureSmallPrimesOnDevice(accelerator);
UInt128 kStart = 1UL; int batchSize = GpuConstants.ScanBatchSize;
byte last = lastIsSeven ? (byte)1 : (byte)0; UInt128 kStart = 1UL;
var kernel = gpuLease.Pow2ModKernel; byte last = lastIsSeven ? (byte)1 : (byte)0;
ulong step10 = ((exponent % 10UL) << 1) % 10UL; var kernel = gpuLease.Pow2ModKernel;
ulong step8 = ((exponent & 7UL) << 1) & 7UL; ulong step10 = ((exponent % 10UL) << 1) % 10UL;
ulong step3 = ((exponent % 3UL) << 1) % 3UL; ulong step8 = ((exponent & 7UL) << 1) & 7UL;
ulong step5 = ((exponent % 5UL) << 1) % 5UL; ulong step3 = ((exponent % 3UL) << 1) % 3UL;
GpuUInt128 twoPGpu = (GpuUInt128)twoP; ulong step5 = ((exponent % 5UL) << 1) % 5UL;
GpuUInt128 twoPGpu = (GpuUInt128)twoP;
var orderBuffer = accelerator.Allocate1D<ulong>(batchSize); var orderBuffer = accelerator.Allocate1D<ulong>(batchSize);
// Device memory returned by Allocate1D is not guaranteed to be zeroed. // Device memory returned by Allocate1D is not guaranteed to be zeroed.
// Ensure the buffer starts with a known state so that lanes skipped by the // Ensure the buffer starts with a known state so that lanes skipped by the
// kernel (for example due to early rejections) do not leave stale garbage // kernel (for example due to early rejections) do not leave stale garbage
// values that would be interpreted as composite witnesses when copied back // values that would be interpreted as composite witnesses when copied back
// to the host. This mirrors the explicit zeroing performed by other GPU // to the host. This mirrors the explicit zeroing performed by other GPU
// testers such as the order kernels. // testers such as the order kernels.
orderBuffer.MemSetToZero(); orderBuffer.MemSetToZero();
ulong[] orderArray = ArrayPool<ulong>.Shared.Rent(batchSize); ulong[] orderArray = ArrayPool<ulong>.Shared.Rent(batchSize);
UInt128 remaining; UInt128 remaining;
int currentSize; int currentSize;
int i; int i;
UInt128 batchSize128 = (UInt128)batchSize, q; UInt128 batchSize128 = (UInt128)batchSize, q;
Span<ulong> orders = orderArray.AsSpan(0, batchSize); Span<ulong> orders = orderArray.AsSpan(0, batchSize);
try try
{ {
while (kStart < maxK && Volatile.Read(ref isPrime)) while (kStart < maxK && Volatile.Read(ref isPrime))
{ {
remaining = maxK - kStart; remaining = maxK - kStart;
if (remaining > batchSize128) if (remaining > batchSize128)
{ {
currentSize = batchSize; currentSize = batchSize;
@@ -58,35 +59,36 @@ public class MersenneNumberResidueGpuTester(bool useGpuOrder)
orders = orderArray.AsSpan(0, currentSize); orders = orderArray.AsSpan(0, currentSize);
} }
q = twoP * kStart + UInt128.One; q = twoP * kStart + UInt128.One;
q.Mod10_8_5_3(out ulong q0m10, out ulong q0m8, out ulong q0m5, out ulong q0m3); q.Mod10_8_5_3(out ulong q0m10, out ulong q0m8, out ulong q0m5, out ulong q0m3);
var ra = new ResidueAutomatonArgs(q0m10, step10, q0m8, step8, q0m3, step3, q0m5, step5); var ra = new ResidueAutomatonArgs(q0m10, step10, q0m8, step8, q0m3, step3, q0m5, step5);
kernel(currentSize, exponent, twoPGpu, (GpuUInt128)kStart, last, 0UL, kernel(stream, currentSize, exponent, twoPGpu, (GpuUInt128)kStart, last, 0UL,
ra, orderBuffer.View, smallCyclesView, primeViews.LastOne, primeViews.LastSeven, primeViews.LastOnePow2, primeViews.LastSevenPow2); ra, orderBuffer.View, smallCyclesView, primeViews.LastOne, primeViews.LastSeven, primeViews.LastOnePow2, primeViews.LastSevenPow2); // kernel(currentSize, exponent, twoPGpu, (GpuUInt128)kStart, last, 0UL,
// ra, orderBuffer.View, smallCyclesView, primeViews.LastOne, primeViews.LastSeven, primeViews.LastOnePow2, primeViews.LastSevenPow2);
accelerator.Synchronize(); accelerator.Synchronize();
orderBuffer.View.CopyToCPU(ref MemoryMarshal.GetReference(orders), currentSize); orderBuffer.View.CopyToCPU(ref MemoryMarshal.GetReference(orders), currentSize);
if (!Volatile.Read(ref isPrime)) if (!Volatile.Read(ref isPrime))
{ {
break; break;
} }
for (i = 0; i < currentSize; i++) for (i = 0; i < currentSize; i++)
{ {
if (orders[i] != 0UL) if (orders[i] != 0UL)
{ {
Volatile.Write(ref isPrime, false); Volatile.Write(ref isPrime, false);
break; break;
} }
} }
kStart += batchSize128; kStart += batchSize128;
} }
} }
finally finally
{ {
ArrayPool<ulong>.Shared.Return(orderArray); ArrayPool<ulong>.Shared.Return(orderArray);
orderBuffer.Dispose(); orderBuffer.Dispose();
gpuLease.Dispose(); gpuLease.Dispose();
} }
+3 -8
View File
@@ -16,6 +16,7 @@ public enum GpuKernelType
} }
public sealed class MersenneNumberTester( public sealed class MersenneNumberTester(
UInt128 maxK,
bool useIncremental = true, bool useIncremental = true,
bool useOrderCache = false, bool useOrderCache = false,
GpuKernelType kernelType = GpuKernelType.Incremental, GpuKernelType kernelType = GpuKernelType.Incremental,
@@ -24,12 +25,11 @@ public sealed class MersenneNumberTester(
bool useGpuLucas = true, bool useGpuLucas = true,
bool useGpuScan = true, bool useGpuScan = true,
bool useGpuOrder = true, bool useGpuOrder = true,
bool useResidue = true, bool useResidue = true)
ulong maxK = 5_000_000UL)
{ {
private readonly bool _useResidue = useResidue; private readonly bool _useResidue = useResidue;
private readonly bool _useIncremental = useIncremental && !useResidue; private readonly bool _useIncremental = useIncremental && !useResidue;
private readonly ulong _maxK = maxK; private readonly UInt128 _maxK = maxK;
private readonly GpuKernelType _kernelType = kernelType; private readonly GpuKernelType _kernelType = kernelType;
private readonly bool _useModuloWorkaround = useModuloWorkaround; private readonly bool _useModuloWorkaround = useModuloWorkaround;
private readonly bool _useGpuLucas = useGpuLucas; private readonly bool _useGpuLucas = useGpuLucas;
@@ -228,11 +228,6 @@ public sealed class MersenneNumberTester(
// In residue mode the residue scan is the final primality check. // In residue mode the residue scan is the final primality check.
if (_useResidue) if (_useResidue)
{ {
if ((UInt128)_maxK < maxK)
{
maxK = _maxK;
}
if (_useGpuScan) if (_useGpuScan)
{ {
_residueGpuTester!.Scan(exponent, twoP, lastIsSeven, maxK, ref prePrime); _residueGpuTester!.Scan(exponent, twoP, lastIsSeven, maxK, ref prePrime);