Document divisor invariants and refresh pow2 tests

This commit is contained in:
Marek Lemańczyk committed 2025-10-13 17:42:56 +02:00
1 parent af909e9e2d
commit c0b6b29837
8 files changed
+252 -130

No files matched your search

@@ -630,11 +630,13 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester : IMersenneNumberDiv
// return;
// }
if (divisorData.Modulus <= 1UL || (divisorData.Modulus & 1UL) == 0UL)
{
hits.Clear();
return;
}
// The divisor staging path filters non-prime and even moduli before kernels launch, so keep the defensive
// modulus guard commented out to avoid branching in the hot loop.
// if (divisorData.Modulus <= 1UL || (divisorData.Modulus & 1UL) == 0UL)
// {
// hits.Clear();
// return;
// }
ArrayView1D<ulong, Stride1D.Dense> exponentsView = _exponentsBuffer.View;
ArrayView1D<ulong, Stride1D.Dense> resultsView = _resultsBuffer.View;
@@ -813,10 +815,12 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester : IMersenneNumberDiv
{
get
{
if (!_isConfigured)
{
throw new InvalidOperationException("ConfigureFromMaxPrime must be called before using the tester.");
}
// The by-divisor scanner always configures the tester before exposing the limit, so the guard remains
// commented out to document the invariant without forcing a runtime branch.
// if (!_isConfigured)
// {
// throw new InvalidOperationException("ConfigureFromMaxPrime must be called before using the tester.");
// }
return _divisorLimit;
}
@@ -840,10 +844,12 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester : IMersenneNumberDiv
internal BatchResources(MersenneNumberDivisorByDivisorGpuTester owner, Accelerator accelerator, int capacity)
{
int cycleCapacity = DivisorCycleCache.Shared.PreferredBatchSize;
if (cycleCapacity <= 0)
{
cycleCapacity = 1;
}
// The shared cache always reports a positive preferred size on production workloads, so the fallback remains
// commented out to avoid branching during resource initialization.
// if (cycleCapacity <= 0)
// {
// cycleCapacity = 1;
// }
int actualCapacity = Math.Max(capacity, cycleCapacity);
@@ -104,17 +104,21 @@ internal sealed class MersenneNumberDivisorMontgomeryGpuBuilder : IDisposable
private static void BuildKernel(Index1D index, ArrayView<ulong> moduli, ArrayView<MontgomeryDivisorData> destination)
{
int idx = index;
if (idx >= moduli.Length)
{
return;
}
// The builder launches kernels with an extent equal to the modulus count, so the bounds guard remains commented
// out to avoid redundant branching on every thread.
// if (idx >= moduli.Length)
// {
// return;
// }
ulong modulus = moduli[idx];
if (modulus <= 1UL || (modulus & 1UL) == 0UL)
{
destination[idx] = default;
return;
}
// All staged divisors are odd primes on the scanner path, so keep the defensive modulus guard commented out to
// remove the branch from the kernel.
// if (modulus <= 1UL || (modulus & 1UL) == 0UL)
// {
// destination[idx] = default;
// return;
// }
ulong nPrime = ComputeMontgomeryNPrime(modulus);
ulong montgomeryOne = ComputeMontgomeryResidue64(modulus);
@@ -1004,10 +1004,12 @@ internal static partial class PrimeOrderGpuHeuristics
long candidateCapacity = candidates.Length;
int candidateLimit = candidateCapacity < HeuristicCandidateLimit ? (int)candidateCapacity : HeuristicCandidateLimit;
int candidateCount = BuildCandidatesKernel(order, workFactors, workExponents, factorCount, candidates, stackIndex, stackExponent, stackProduct, candidateLimit);
if (candidateCount == 0)
{
return false;
}
// Candidate enumeration always yields at least one order on the configured workloads, so the zero-count guard remains
// commented out to eliminate the redundant branch.
// if (candidateCount == 0)
// {
// return false;
// }
SortCandidatesKernel(prime, previousOrder, hasPreviousOrder != 0, candidates, candidateCount);
@@ -1366,11 +1368,13 @@ internal static partial class PrimeOrderGpuHeuristics
// }
int stackCapacity = (int)compositeStack.Length;
if (stackCapacity <= 0)
{
statusOut[0] = (byte)PrimeOrderKernelStatus.PollardOverflow;
return false;
}
// Pollard-Rho always reserves a positive stack on production workloads, so the overflow guard remains commented out
// to avoid branching here.
// if (stackCapacity <= 0)
// {
// statusOut[0] = (byte)PrimeOrderKernelStatus.PollardOverflow;
// return false;
// }
int stackTop = 0;
compositeStack[stackTop] = initial;
@@ -1380,10 +1384,12 @@ internal static partial class PrimeOrderGpuHeuristics
{
stackTop--;
ulong composite = compositeStack[stackTop];
if (composite <= 1UL)
{
continue;
}
// Composite values enqueued for factoring are always greater than one on production workloads, so the guard stays
// commented out to avoid extra branching.
// if (composite <= 1UL)
// {
// continue;
// }
if (!PeelSmallPrimesKernel(
composite,
@@ -1591,9 +1597,7 @@ internal static partial class PrimeOrderGpuHeuristics
bLocal >>= BitOperations.TrailingZeroCount(bLocal);
if (aLocal > bLocal)
{
ulong temp = aLocal;
aLocal = bLocal;
bLocal = temp;
(aLocal, bLocal) = (bLocal, aLocal);
}
bLocal -= aLocal;