Simplify prime guards and adopt unified mulmod

This commit is contained in:
Marek Lemańczyk committed 2025-10-13 16:56:50 +02:00
1 parent 3f5904c340
commit af909e9e2d
11 files changed
+236 -189

No files matched your search

@@ -530,34 +530,44 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester : IMersenneNumberDiv
private static ulong ComputeDivisorLimitFromMaxPrime(ulong maxPrime)
{
if (maxPrime <= 1UL)
{
return 0UL;
}
// The production configuration seeds the scan with exponents above the first odd prime, so the defensive guard remains
// commented to document the invariant without branching.
// if (maxPrime <= 1UL)
// {
// return 0UL;
// }
if (maxPrime - 1UL >= 64UL)
{
return ulong.MaxValue;
}
// Real workloads push maxPrime well past the 64-bit shift boundary, so keep the historical implementation commented out
// and return ulong.MaxValue directly.
// if (maxPrime - 1UL >= 64UL)
// {
// return ulong.MaxValue;
// }
//
// return (1UL << (int)(maxPrime - 1UL)) - 1UL;
return (1UL << (int)(maxPrime - 1UL)) - 1UL;
return ulong.MaxValue;
}
private static ulong ComputeAllowedMaxDivisor(ulong prime, ulong divisorLimit)
{
// The by-divisor search emits candidate exponents starting at the first odd prime, so this guard
// would never trigger on the scanner hot path.
// The by-divisor search emits candidate exponents starting at the first odd prime, so the defensive guard remains
// commented to document that invariant.
// if (prime <= 1UL)
// {
// return 0UL;
// }
if (prime - 1UL >= 64UL)
{
return divisorLimit;
}
// Production exponents are always large enough that the shift-based clamp would overflow, so skip the legacy branch and
// return the configured divisor limit directly.
// if (prime - 1UL >= 64UL)
// {
// return divisorLimit;
// }
//
// return Math.Min((1UL << (int)(prime - 1UL)) - 1UL, divisorLimit);
return Math.Min((1UL << (int)(prime - 1UL)) - 1UL, divisorLimit);
return divisorLimit;
}
private static void CheckKernel(Index1D index, ArrayView<MontgomeryDivisorData> divisors, ArrayView<ulong> exponents, ArrayView<byte> hits)
@@ -613,10 +623,12 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester : IMersenneNumberDiv
}
int length = primes.Length;
if (length == 0)
{
return;
}
// Production batches always contain at least one exponent, so the empty-span guard remains commented out to remove
// the redundant branch.
// if (length == 0)
// {
// return;
// }
if (divisorData.Modulus <= 1UL || (divisorData.Modulus & 1UL) == 0UL)
{
@@ -801,15 +813,12 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester : IMersenneNumberDiv
{
get
{
lock (_sync)
if (!_isConfigured)
{
if (!_isConfigured)
{
throw new InvalidOperationException("ConfigureFromMaxPrime must be called before using the tester.");
}
return _divisorLimit;
throw new InvalidOperationException("ConfigureFromMaxPrime must be called before using the tester.");
}
return _divisorLimit;
}
}
@@ -118,7 +118,7 @@ internal sealed class MersenneNumberDivisorMontgomeryGpuBuilder : IDisposable
ulong nPrime = ComputeMontgomeryNPrime(modulus);
ulong montgomeryOne = ComputeMontgomeryResidue64(modulus);
ulong montgomeryTwo = montgomeryOne.MulMod64GpuCompatible(2UL, modulus);
ulong montgomeryTwo = montgomeryOne.MulMod64(2UL, modulus);
ulong montgomeryTwoSquared = MontgomeryMultiplyGpu(montgomeryTwo, montgomeryTwo, modulus, nPrime);
destination[idx] = new MontgomeryDivisorData(
@@ -103,10 +103,12 @@ internal sealed class MersenneNumberDivisorResidueGpuEvaluator : IDisposable
{
int idx = index;
int length = (int)exponents.Length;
if (idx >= length)
{
return;
}
// Batched launches match the buffer length on production workloads, so the bounds guard stays commented out to remove
// the redundant comparison.
// if (idx >= length)
// {
// return;
// }
ulong exponent = exponents[idx];
ulong modulus = divisorData.Modulus;
@@ -850,10 +850,11 @@ internal static partial class PrimeOrderGpuHeuristics
{
ulong primeFactor = factors[i];
int exponent = exponents[i];
if (primeFactor <= 1UL)
{
continue;
}
// Factorization never yields unity on the scanning path, so skip the redundant guard.
// if (primeFactor <= 1UL)
// {
// continue;
// }
for (int iteration = 0; iteration < exponent; iteration++)
{
@@ -889,10 +890,12 @@ internal static partial class PrimeOrderGpuHeuristics
ArrayView1D<ulong, Stride1D.Dense> compositeStack,
ArrayView1D<byte, Stride1D.Dense> statusOut)
{
if (order == 0UL)
{
return false;
}
// Order values handed to the kernel are strictly positive on the production path, so the zero guard stays commented
// out to remove the redundant branch.
// if (order == 0UL)
// {
// return false;
// }
if ((order).Pow2ModWindowedGpu(divisor.Modulus) != 1UL)
{
@@ -924,10 +927,11 @@ internal static partial class PrimeOrderGpuHeuristics
{
ulong primeFactor = factors[i];
int exponent = exponents[i];
if (primeFactor <= 1UL)
{
continue;
}
// Factorization never yields one on the by-divisor path, so the defensive continue stays commented out.
// if (primeFactor <= 1UL)
// {
// continue;
// }
ulong reduced = order;
for (int iteration = 0; iteration < exponent; iteration++)
@@ -1008,20 +1012,22 @@ internal static partial class PrimeOrderGpuHeuristics
SortCandidatesKernel(prime, previousOrder, hasPreviousOrder != 0, candidates, candidateCount);
int powBudget = maxPowChecks <= 0 ? candidateCount : maxPowChecks;
if (powBudget <= 0)
{
powBudget = candidateCount;
}
// The heuristics always reserve at least one check, so the zero-budget fallback remains commented out.
// if (powBudget <= 0)
// {
// powBudget = candidateCount;
// }
int powUsed = 0;
for (int i = 0; i < candidateCount && powUsed < powBudget; i++)
{
ulong candidate = candidates[i];
if (candidate <= 1UL)
{
continue;
}
// Candidate generation skips 1 by construction, so the defensive continue stays commented out.
// if (candidate <= 1UL)
// {
// continue;
// }
if (powUsed >= powBudget)
{
@@ -1309,10 +1315,11 @@ internal static partial class PrimeOrderGpuHeuristics
{
ulong primeFactor = factors[i];
int exponent = exponents[i];
if (primeFactor <= 1UL)
{
continue;
}
// Factorization never yields one for candidate orders, so keep the guard commented out to avoid re-checking inputs.
// if (primeFactor <= 1UL)
// {
// continue;
// }
ulong reduced = candidate;
for (int iteration = 0; iteration < exponent; iteration++)
@@ -1351,10 +1358,12 @@ internal static partial class PrimeOrderGpuHeuristics
ArrayView1D<ulong, Stride1D.Dense> compositeStack,
ArrayView1D<byte, Stride1D.Dense> statusOut)
{
if (initial <= 1UL)
{
return true;
}
// Composite candidates sent here are always greater than one during production scans, so the trivial-return branch stays
// commented out.
// if (initial <= 1UL)
// {
// return true;
// }
int stackCapacity = (int)compositeStack.Length;
if (stackCapacity <= 0)
@@ -1616,10 +1625,11 @@ internal static partial class PrimeOrderGpuHeuristics
private static GpuPow2ModStatus TryPow2ModBatchInternal(ReadOnlySpan<UInt128> exponents, UInt128 prime, Span<UInt128> remainders)
{
if (exponents.Length == 0)
{
return GpuPow2ModStatus.Success;
}
// Residue batches are never empty on production workloads, so skip the zero-length success shortcut.
// if (exponents.Length == 0)
// {
// return GpuPow2ModStatus.Success;
// }
if (remainders.Length < exponents.Length)
{
@@ -1706,10 +1716,11 @@ internal static partial class PrimeOrderGpuHeuristics
private static bool TryComputeOnGpuWide(ReadOnlySpan<UInt128> exponents, UInt128 prime, Span<UInt128> results)
{
int length = exponents.Length;
if (length == 0)
{
return true;
}
// Production launches always carry work, so keep the empty-batch shortcut commented out.
// if (length == 0)
// {
// return true;
// }
GpuUInt128[]? rentedExponents = null;
GpuUInt128[]? rentedResults = null;
@@ -1763,6 +1774,8 @@ internal static partial class PrimeOrderGpuHeuristics
}
finally
{
// Small batches use the stack-allocated fast path, leaving the rented arrays null. Retain the guards so pooled
// buffers are only returned when they were actually leased.
if (rentedExponents is not null)
{
ArrayPool<GpuUInt128>.Shared.Return(rentedExponents, clearArray: false);