Split CPU and GPU exponent steppers

This commit is contained in:
Marek Lemańczyk committed 2025-10-24 08:49:53 +02:00
1 parent 035956cdb6
commit 1e5a9128cb
9 files changed
+366 -128

No files matched your search

@@ -0,0 +1,156 @@
using System.Runtime.CompilerServices;
namespace PerfectNumbers.Core.Gpu;
/// <summary>
/// Tracks successive residues for a single divisor while kernels advance exponent candidates on the GPU path.
/// Callers must seed the stepper once per divisor and then supply exponents in strictly ascending order.
/// </summary>
internal struct ExponentRemainderStepperGpu
{
private readonly MontgomeryDivisorData _divisor;
private readonly ulong _modulus;
private readonly ulong _nPrime;
private readonly ulong _montgomeryOne;
private ulong _previousExponent;
private ulong _currentMontgomery;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ExponentRemainderStepperGpu(in MontgomeryDivisorData divisor)
{
_divisor = divisor;
_modulus = divisor.Modulus;
_nPrime = divisor.NPrime;
_montgomeryOne = divisor.MontgomeryOne;
_previousExponent = 0UL;
_currentMontgomery = divisor.MontgomeryOne;
}
public ulong PreviousExponent
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _previousExponent;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MatchesDivisor(in MontgomeryDivisorData divisor)
{
return _modulus == divisor.Modulus && _nPrime == divisor.NPrime && _montgomeryOne == divisor.MontgomeryOne;
}
/// <summary>
/// Clears the cached Montgomery residue so the stepper can be reused for another divisor.
/// Callers must reinitialize the stepper before consuming any residues after a reset.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Reset()
{
_previousExponent = 0UL;
_currentMontgomery = _montgomeryOne;
}
/// <summary>
/// Initializes the stepper for GPU calculations using the first exponent in a strictly ascending sequence.
/// The returned value is the canonical residue corresponding to the provided exponent.
/// </summary>
/// <param name="exponent">First exponent evaluated for the current divisor. The caller must reuse the same divisor metadata.</param>
/// <returns>The canonical residue produced by <c>2^exponent mod divisor</c>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ulong InitializeGpu(ulong exponent)
{
InitializeGpuState(exponent);
return ReduceCurrent();
}
/// <summary>
/// Initializes the GPU stepping state and reports whether the seed exponent already yields unity.
/// </summary>
/// <param name="exponent">First exponent evaluated for the current divisor. Must match the divisor passed to the constructor.</param>
/// <returns><see langword="true"/> when the seed exponent produces a Montgomery residue equal to one.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool InitializeGpuIsUnity(ulong exponent)
{
InitializeGpuState(exponent);
return _currentMontgomery == _montgomeryOne;
}
/// <summary>
/// Advances the GPU stepping state to the next exponent in a strictly ascending sequence that shares the same divisor.
/// Callers must invoke <see cref="InitializeGpu(ulong)"/> beforehand.
/// </summary>
/// <param name="exponent">Next exponent in the ascending sequence. Callers must never move backwards or reuse an older exponent.</param>
/// <returns>The canonical residue produced by <c>2^exponent mod divisor</c>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ulong ComputeNextGpu(ulong exponent)
{
AdvanceGpu(exponent);
return ReduceCurrent();
}
/// <summary>
/// Advances the GPU stepping state to the next exponent and reports whether it evaluates to unity.
/// Callers must seed the stepper through <see cref="InitializeGpu(ulong)"/> or <see cref="InitializeGpuIsUnity(ulong)"/> and then
/// pass exponents in strictly ascending order.
/// </summary>
/// <param name="exponent">Next exponent in the ascending sequence.</param>
/// <returns><see langword="true"/> when the canonical residue equals one.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool ComputeNextGpuIsUnity(ulong exponent)
{
AdvanceGpu(exponent);
return _currentMontgomery == _montgomeryOne;
}
/// <summary>
/// Seeds the stepper with a precomputed Montgomery residue produced by the caller.
/// The provided exponent becomes the new baseline and all subsequent calls must continue with greater exponents.
/// </summary>
/// <param name="exponent">Exponent associated with <paramref name="montgomeryResult"/>.</param>
/// <param name="montgomeryResult">Residue in Montgomery form produced externally for the configured divisor.</param>
/// <param name="isUnity">Outputs whether the supplied residue equals Montgomery one.</param>
/// <returns><see langword="true"/> when the residue is accepted and cached.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryInitializeFromMontgomeryResult(ulong exponent, ulong montgomeryResult, out bool isUnity)
{
_currentMontgomery = montgomeryResult;
_previousExponent = exponent;
isUnity = _currentMontgomery == _montgomeryOne;
return true;
}
/// <summary>
/// Advances the stepper using a caller-supplied Montgomery delta that represents the gap to the next exponent.
/// Callers must seed the stepper first, reuse the same divisor metadata, and continue feeding exponents in ascending order so the delta remains valid.
/// </summary>
/// <param name="exponent">Next exponent in the ascending sequence.</param>
/// <param name="montgomeryDelta">Montgomery residue delta corresponding to the exponent gap.</param>
/// <param name="isUnity">Outputs whether the resulting residue equals Montgomery one.</param>
/// <returns><see langword="true"/> when the advance succeeds.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryAdvanceWithMontgomeryDelta(ulong exponent, ulong montgomeryDelta, out bool isUnity)
{
_currentMontgomery = _currentMontgomery.MontgomeryMultiply(montgomeryDelta, _modulus, _nPrime);
_previousExponent = exponent;
isUnity = _currentMontgomery == _montgomeryOne;
return true;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void InitializeGpuState(ulong exponent)
{
_currentMontgomery = ULongExtensions.Pow2MontgomeryModWindowedGpuKeepMontgomery(_divisor, exponent);
_previousExponent = exponent;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void AdvanceGpu(ulong exponent)
{
ulong delta = exponent - _previousExponent;
ulong multiplier = ULongExtensions.Pow2MontgomeryModWindowedGpuKeepMontgomery(_divisor, delta);
_currentMontgomery = _currentMontgomery.MontgomeryMultiply(multiplier, _modulus, _nPrime);
_previousExponent = exponent;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private ulong ReduceCurrent() => _currentMontgomery.MontgomeryMultiply(1UL, _modulus, _nPrime);
}
@@ -41,37 +41,34 @@ internal static class DivisorByDivisorKernels
while (index >= 0)
{
result = ULongExtensions.MulMod64Gpu(result, result, modulus);
ulong currentBit = (exponent >> index) & 1UL;
ulong squared = MultiplyMod(result, result, modulus);
bool processWindow = currentBit != 0UL;
if (((exponent >> index) & 1UL) == 0UL)
result = processWindow ? result : squared;
index -= (int)(currentBit ^ 1UL);
int windowStartCandidate = index - windowSize + 1;
int negativeMask = windowStartCandidate >> 31;
windowStartCandidate &= ~negativeMask;
int windowStart = processWindow ? GetNextSetBitIndex(exponent, windowStartCandidate) : windowStartCandidate;
int windowLength = processWindow ? index - windowStart + 1 : 0;
for (int square = 0; square < windowLength; square++)
{
result = MultiplyMod(result, result, modulus);
}
if (!processWindow)
{
index--;
continue;
}
int windowStart = index - windowSize + 1;
if (windowStart < 0)
{
windowStart = 0;
}
while (((exponent >> windowStart) & 1UL) == 0UL)
{
windowStart++;
}
int windowLength = index - windowStart + 1;
for (int square = 1; square < windowLength; square++)
{
result = ULongExtensions.MulMod64Gpu(result, result, modulus);
}
ulong mask = (1UL << windowLength) - 1UL;
ulong windowValue = (exponent >> windowStart) & mask;
ulong multiplier = ComputeWindowedOddPower(windowValue, modulus);
result = ULongExtensions.MulMod64Gpu(result, multiplier, modulus);
result = MultiplyMod(result, multiplier, modulus);
index = windowStart - 1;
}
@@ -82,25 +79,29 @@ internal static class DivisorByDivisorKernels
private static ulong ComputeWindowedOddPower(ulong windowValue, ulong modulus)
{
ulong baseValue = 2UL % modulus;
if (windowValue == 1UL)
{
return baseValue;
}
ulong result = baseValue;
ulong remaining = (windowValue - 1UL) >> 1;
ulong squareBase = ULongExtensions.MulMod64Gpu(baseValue, baseValue, modulus);
ulong squareBase = MultiplyMod(baseValue, baseValue, modulus);
while (remaining != 0UL)
{
if ((remaining & 1UL) != 0UL)
{
result = ULongExtensions.MulMod64Gpu(result, squareBase, modulus);
result = MultiplyMod(result, squareBase, modulus);
}
remaining >>= 1;
if (remaining == 0UL)
{
break;
}
squareBase = ULongExtensions.MulMod64Gpu(squareBase, squareBase, modulus);
squareBase = MultiplyMod(squareBase, squareBase, modulus);
}
return result;
@@ -124,6 +125,65 @@ internal static class DivisorByDivisorKernels
window = bitLength <= 6 ? clamped : window;
return window;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int GetNextSetBitIndex(ulong exponent, int startIndex)
{
ulong guard = (ulong)(((long)startIndex - 64) >> 63);
int shift = startIndex & 63;
ulong mask = (~0UL << shift) & guard;
ulong masked = exponent & mask;
return XMath.TrailingZeroCount(masked);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ulong MultiplyMod(ulong left, ulong right, ulong modulus)
{
if (left == 0UL || right == 0UL)
{
return 0UL;
}
ulong threshold = ulong.MaxValue / left;
if (right <= threshold)
{
ulong product = left * right;
return product % modulus;
}
return MultiplyModWide(left, right, modulus);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ulong MultiplyModWide(ulong left, ulong right, ulong modulus)
{
ulong result = 0UL;
ulong addend = left;
ulong remaining = right;
while (remaining != 0UL)
{
if ((remaining & 1UL) != 0UL)
{
result += addend;
if (result >= modulus)
{
result -= modulus;
}
}
addend <<= 1;
if (addend >= modulus)
{
addend -= modulus;
}
remaining >>= 1;
}
return result;
}
public static void EvaluateDivisorWithStepperKernel(
Index1D _,
MontgomeryDivisorData divisor,
@@ -140,7 +200,7 @@ internal static class DivisorByDivisorKernels
// return;
// }
var exponentStepper = new ExponentRemainderStepper(divisor);
var exponentStepper = new ExponentRemainderStepperGpu(divisor);
ulong firstPrime = exponents[0];
bool firstUnity = exponentStepper.InitializeGpuIsUnity(firstPrime);
// EvenPerfectBitScanner only schedules divisors with non-zero cycle lengths (equal to the tested prime),
@@ -307,7 +307,7 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester : IMersenneNumberDiv
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ulong ResolveDivisorCycle(ulong divisor, ulong prime, in MontgomeryDivisorData divisorData)
{
if (!MersenneDivisorCycles.TryCalculateCycleLengthForExponent(divisor, prime, divisorData, out ulong computedCycle, out bool primeOrderFailed) || computedCycle == 0UL)
if (!MersenneDivisorCycles.TryCalculateCycleLengthForExponentGpu(divisor, prime, divisorData, out ulong computedCycle, out bool primeOrderFailed) || computedCycle == 0UL)
{
return MersenneDivisorCycles.CalculateCycleLength(divisor, divisorData, skipPrimeOrderHeuristic: primeOrderFailed);
}
@@ -318,7 +318,8 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester : IMersenneNumberDiv
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool CandidatePassesHeuristics(ulong candidate, bool lastIsSeven)
{
// Mirror the CPU heuristics so the GPU path rejects obvious composites before computing cycles.
// Mirror the CPU-side divisor screen so the GPU path rejects multiples of 3, 5, 7, and 11
// before resolving cycles, keeping cache lookups aligned with the persisted snapshot.
ulong remainder10 = candidate % 10UL;
bool accept10 = lastIsSeven
? (remainder10 == 3UL || remainder10 == 7UL || remainder10 == 9UL)
@@ -469,7 +470,7 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester : IMersenneNumberDiv
if (cycle == 0UL)
{
if (!MersenneDivisorCycles.TryCalculateCycleLengthForExponent(divisor, firstPrime, cachedData, out ulong computedCycle, out bool primeOrderFailed) || computedCycle == 0UL)
if (!MersenneDivisorCycles.TryCalculateCycleLengthForExponentGpu(divisor, firstPrime, cachedData, out ulong computedCycle, out bool primeOrderFailed) || computedCycle == 0UL)
{
cycle = MersenneDivisorCycles.CalculateCycleLength(divisor, cachedData, skipPrimeOrderHeuristic: primeOrderFailed);
}