Compare commits

..
Author SHA1 Message Date
Marek Lemańczyk ef078df83d Merge pull request #144 from mlemanczyk/codex/optimize-division-in-primeordercalculator
Cache Pollard rho reciprocals for faster Montgomery steps
2025-10-10 13:46:15 +02:00
Marek Lemańczyk 645969309c Reuse Pollard rho reciprocals for fast Montgomery stepping 2025-10-10 13:44:23 +02:00
Marek Lemańczyk 01ac80466b Merge pull request #143 from mlemanczyk/codex/optimize-random-byte-generation-in-pollardrho
Improve Pollard-Rho random generation performance
2025-10-10 10:42:01 +02:00
Marek Lemańczyk 2b0d7278db Replace Pollard-Rho random sources with deterministic generator 2025-10-10 10:41:47 +02:00
Marek Lemańczyk 1c0bd3f3d3 Merge pull request #142 from mlemanczyk/codex/analyze-random-byte-generation-performance
Improve Pollard Rho random sampling throughput
2025-10-10 10:31:55 +02:00
Marek Lemańczyk 4ed9db7d0b Optimize Pollard Rho random sampling 2025-10-10 10:31:14 +02:00
mlemanczyk 99a7ec5b70 Tiny cleanup 2025-10-10 10:23:45 +02:00
mlemanczyk ca0cadc8a3 Various optimizations and cleanups. "in" removed from GpuUInt128 to avoid defensive copies from IL. 2025-10-10 09:26:25 +02:00
mlemanczyk 70eca5126b Formatting fix 2025-10-10 03:06:01 +02:00
Marek Lemańczyk 0be9596bd5 Merge pull request #141 from mlemanczyk/codex/optimize-evenperfectbitscanner-cpu-path
Remove IDisposable from prime order scopes
2025-10-10 03:03:58 +02:00
Marek Lemańczyk 4f68c90d88 Merge branch 'pollardrhoplus2' into codex/optimize-evenperfectbitscanner-cpu-path 2025-10-10 03:03:48 +02:00
Marek Lemańczyk 7aaccb5feb Stop using IDisposable on prime order scopes 2025-10-10 02:57:54 +02:00
mlemanczyk c334b9e878 Compilation errors fixed. 2025-10-10 01:29:30 +02:00
mlemanczyk e7ac18ea0b struct performance improvements 2025-10-10 01:19:53 +02:00
mlemanczyk ef5a77cedd IDisposable removed for better performance 2025-10-10 01:09:12 +02:00
Marek Lemańczyk 58c546cd87 Merge pull request #140 from mlemanczyk/codex/optimize-partialfactor-in-evenperfectbitscanner
Reduce garbage collector pressure in CPU partial factoring
2025-10-10 00:08:21 +02:00
Marek Lemańczyk 8054f9fe59 Reduce CPU partial factor allocations 2025-10-10 00:07:39 +02:00
mlemanczyk b2ab47e0ee Recording of failed heuristic cases turned off to speed up calculations 2025-10-09 23:57:10 +02:00
Marek Lemańczyk 74e9e88977 Merge pull request #139 from mlemanczyk/codex/adjust-heuristics-for-divisor-detection
Assume full order when heuristics cannot find divisors
2025-10-09 23:13:50 +02:00
Marek Lemańczyk f196f1fb96 Assume heuristic order when divisors are unresolved 2025-10-09 23:13:37 +02:00
mlemanczyk a1a9c5f033 Comleted TODO removed 2025-10-09 23:05:48 +02:00
Marek Lemańczyk 0bae2cb3e6 Merge pull request #138 from mlemanczyk/codex/optimize-primeordercalculator-calculations
Log heuristic fallback primes
2025-10-09 22:49:32 +02:00
Marek Lemańczyk 9e0c90a281 Log heuristic fallback primes 2025-10-09 22:49:20 +02:00
Marek Lemańczyk 40bef43f0e Merge pull request #137 from mlemanczyk/codex/optimize-cpu-threads-for-p-division
Assign contiguous prime ranges for CPU by-divisor scanning
2025-10-09 22:09:26 +02:00
Marek Lemańczyk ebfdc7d8b8 Assign contiguous by-divisor ranges per worker 2025-10-09 22:09:05 +02:00
Marek Lemańczyk bcf50cf763 Merge pull request #136 from mlemanczyk/codex/analyze-evenperfectbitscanner-cpu-path
Extend CPU bit skipping to UInt128 strides
2025-10-09 21:39:36 +02:00
Marek Lemańczyk ce8c3a660a Extend bit skipping to UInt128 strides 2025-10-09 21:18:18 +02:00
Marek Lemańczyk 321157cf65 Merge pull request #135 from mlemanczyk/codex/optimize-prime-filtering-method
Optimize filtering strategy for previous Mersenne results
2025-10-09 05:30:22 +02:00
Marek Lemańczyk e1d4bbf12d Optimize filtering strategy for previous Mersenne results 2025-10-09 05:28:59 +02:00
Marek Lemańczyk 77cc1fc280 Merge pull request #134 from mlemanczyk/codex/complete-delta-driven-montgomery-updates
Fix divisor scan session compilation issues
2025-10-09 04:46:15 +02:00
Marek Lemańczyk 668e161554 Fix divisor scan session compilation issues 2025-10-09 04:46:01 +02:00
Marek Lemańczyk ce4c59beaa Merge pull request #133 from mlemanczyk/codex/analyze-evenperfectbitscanner-performance-with-mersenne
Implement analytic delta stepping for CPU divisor scans
2025-10-09 04:31:51 +02:00
Marek Lemańczyk 6bd8945565 Use analytic delta updates in CPU divisor sessions 2025-10-09 04:31:03 +02:00
mlemanczyk 99c9ddf73d Merge branch 'pollardrhoplus2' of https://github.com/mlemanczyk/even-perfect-numbers-scanner into pollardrhoplus2 2025-10-09 03:57:18 +02:00
mlemanczyk 32de7ebc75 DebugLog disabled for profiling 2025-10-09 03:56:49 +02:00
Marek Lemańczyk fd0c978079 Merge pull request #132 from mlemanczyk/codex/analyze-pollard-rho-optimizations-hwu58f
Streamline CPU divisor status accounting
2025-10-09 03:52:27 +02:00
Marek Lemańczyk a19cbd245d Prune completed CPU status TODO entry 2025-10-09 03:52:11 +02:00
23 changed files with 1903 additions and 384 deletions

No files matched your search

@@ -377,11 +377,10 @@ public class Pow2MontgomeryModCycleComputationBenchmarks
#if DEBUG
Console.WriteLine("Trying heuristic. Wide prime order calculation");
#endif
UInt128 order = PrimeOrderCalculator.Calculate(
modulus,
_previousWidePrimeOrder,
PrimeOrderCalculator.PrimeOrderSearchConfig.HeuristicDefault,
PrimeOrderCalculator.PrimeOrderHeuristicDevice.Gpu);
UInt128 order = PrimeOrderCalculator.Calculate(
modulus,
_previousWidePrimeOrder,
PrimeOrderCalculator.PrimeOrderSearchConfig.HeuristicDefault);
if (order != UInt128.Zero)
{
@@ -15,15 +15,9 @@ public class GpuContextPoolTests
{
GpuContextPool.DisposeAll();
GpuContextPool.GpuContextLease lease = GpuContextPool.RentPreferred(preferCpu: true);
try
{
lease.Accelerator.AcceleratorType.Should().Be(AcceleratorType.CPU);
}
finally
{
lease.Dispose();
}
using var lease = GpuContextPool.RentPreferred(preferCpu: true);
lease.Accelerator.AcceleratorType.Should().Be(AcceleratorType.CPU);
GpuContextPool.DisposeAll();
}
@@ -36,27 +30,17 @@ public class GpuContextPoolTests
Context firstContext;
Accelerator firstAccelerator;
GpuContextPool.GpuContextLease lease = GpuContextPool.RentPreferred(preferCpu: true);
try
using (var lease = GpuContextPool.RentPreferred(preferCpu: true))
{
firstContext = lease.Context;
firstAccelerator = lease.Accelerator;
}
finally
{
lease.Dispose();
}
lease = GpuContextPool.RentPreferred(preferCpu: true);
try
using (var lease = GpuContextPool.RentPreferred(preferCpu: true))
{
lease.Context.Should().BeSameAs(firstContext);
lease.Accelerator.Should().BeSameAs(firstAccelerator);
}
finally
{
lease.Dispose();
}
GpuContextPool.DisposeAll();
}
@@ -68,27 +52,17 @@ public class GpuContextPoolTests
GpuContextPool.DisposeAll();
Context firstContext;
GpuContextPool.GpuContextLease lease = GpuContextPool.RentPreferred(preferCpu: true);
try
using (var lease = GpuContextPool.RentPreferred(preferCpu: true))
{
firstContext = lease.Context;
}
finally
{
lease.Dispose();
}
GpuContextPool.DisposeAll();
lease = GpuContextPool.RentPreferred(preferCpu: true);
try
using (var lease = GpuContextPool.RentPreferred(preferCpu: true))
{
lease.Context.Should().NotBeSameAs(firstContext);
}
finally
{
lease.Dispose();
}
GpuContextPool.DisposeAll();
}
@@ -199,17 +199,10 @@ public class GpuKernelCompilationTests
[MemberData(nameof(KernelLoaders))]
public void Kernel_compiles_without_internal_compiler_errors(string name, Action<Accelerator> compile)
{
GpuContextPool.GpuContextLease lease = GpuContextPool.RentPreferred(preferCpu: true);
try
{
var accelerator = lease.Accelerator;
Action action = () => compile(accelerator);
action.Should().NotThrow($"kernel {name} should compile");
}
finally
{
lease.Dispose();
}
using var lease = GpuContextPool.RentPreferred(preferCpu: true);
var accelerator = lease.Accelerator;
Action action = () => compile(accelerator);
action.Should().NotThrow($"kernel {name} should compile");
}
private static void CompileDivisorCycleCacheKernel(Accelerator accelerator)
@@ -150,36 +150,29 @@ public class NttGpuMathTests
var modulus = new GpuUInt128(0UL, 17UL);
var primitiveRoot = new GpuUInt128(0UL, 3UL);
GpuContextPool.GpuContextLease gpu = GpuContextPool.Rent();
try
using var gpu = GpuContextPool.Rent();
var accelerator = gpu.Accelerator;
var method = typeof(NttGpuMath).GetMethod("GetSquareCache", BindingFlags.NonPublic | BindingFlags.Static);
method.Should().NotBeNull();
var cache = method!.Invoke(null, new object[] { accelerator, 8, modulus, primitiveRoot })!;
var cacheType = cache.GetType();
var twiddlesMont = (MemoryBuffer1D<GpuUInt128, Stride1D.Dense>)cacheType.GetProperty("TwiddlesMont")!.GetValue(cache)!;
var twiddlesInvMont = (MemoryBuffer1D<GpuUInt128, Stride1D.Dense>)cacheType.GetProperty("TwiddlesInvMont")!.GetValue(cache)!;
var r2 = (ulong)cacheType.GetProperty("MontR2Mod64")!.GetValue(cache)!;
var mod = (ulong)cacheType.GetProperty("ModulusLow")!.GetValue(cache)!;
int count = 7; // length 8 -> 7 twiddles
var forward = new GpuUInt128[count];
var inverse = new GpuUInt128[count];
twiddlesMont.CopyToCPU(forward);
twiddlesInvMont.CopyToCPU(inverse);
for (int i = 0; i < count; i++)
{
var accelerator = gpu.Accelerator;
var method = typeof(NttGpuMath).GetMethod("GetSquareCache", BindingFlags.NonPublic | BindingFlags.Static);
method.Should().NotBeNull();
var cache = method!.Invoke(null, new object[] { accelerator, 8, modulus, primitiveRoot })!;
var cacheType = cache.GetType();
var twiddlesMont = (MemoryBuffer1D<GpuUInt128, Stride1D.Dense>)cacheType.GetProperty("TwiddlesMont")!.GetValue(cache)!;
var twiddlesInvMont = (MemoryBuffer1D<GpuUInt128, Stride1D.Dense>)cacheType.GetProperty("TwiddlesInvMont")!.GetValue(cache)!;
var r2 = (ulong)cacheType.GetProperty("MontR2Mod64")!.GetValue(cache)!;
var mod = (ulong)cacheType.GetProperty("ModulusLow")!.GetValue(cache)!;
int count = 7; // length 8 -> 7 twiddles
var forward = new GpuUInt128[count];
var inverse = new GpuUInt128[count];
twiddlesMont.CopyToCPU(forward);
twiddlesInvMont.CopyToCPU(inverse);
for (int i = 0; i < count; i++)
{
ulong prod = (ulong)((((BigInteger)forward[i].Low) * inverse[i].Low) % mod);
prod.Should().Be(r2);
}
}
finally
{
gpu.Dispose();
ulong prod = (ulong)((((BigInteger)forward[i].Low) * inverse[i].Low) % mod);
prod.Should().Be(r2);
}
}
@@ -345,23 +338,12 @@ public class NttGpuMathTests
values[1] = new GpuUInt128(0UL, 2UL);
var modulus = new GpuUInt128(0UL, 17UL);
var primitiveRoot = new GpuUInt128(0UL, 3UL);
GpuContextPool.GpuContextLease gpu = GpuContextPool.Rent();
try
using (var gpu = GpuContextPool.Rent())
{
var buffer = gpu.Accelerator.Allocate1D<GpuUInt128>(values.Length);
try
{
buffer.View.CopyFromCPU(ref values[0], values.Length);
NttGpuMath.SquareDevice(gpu.Accelerator, buffer.View, modulus, primitiveRoot);
}
finally
{
buffer.Dispose();
}
}
finally
{
gpu.Dispose();
buffer.View.CopyFromCPU(ref values[0], values.Length);
NttGpuMath.SquareDevice(gpu.Accelerator, buffer.View, modulus, primitiveRoot);
buffer.Dispose();
}
// Verify that the square cache now holds entries for the accelerator.
@@ -33,23 +33,16 @@ public class NttMontgomeryTests
NttGpuMath.Square(cpu, modulus, primitiveRoot);
// Device execution
GpuContextPool.GpuContextLease context = GpuContextPool.Rent();
try
{
var accelerator = context.Accelerator;
using var buffer = accelerator.Allocate1D<GpuUInt128>(gpu.Length);
buffer.View.CopyFromCPU(ref gpu[0], gpu.Length);
NttGpuMath.SquareDevice(accelerator, buffer.View, modulus, primitiveRoot);
buffer.View.CopyToCPU(ref gpu[0], gpu.Length);
using var context = GpuContextPool.Rent();
var accelerator = context.Accelerator;
using var buffer = accelerator.Allocate1D<GpuUInt128>(gpu.Length);
buffer.View.CopyFromCPU(ref gpu[0], gpu.Length);
NttGpuMath.SquareDevice(accelerator, buffer.View, modulus, primitiveRoot);
buffer.View.CopyToCPU(ref gpu[0], gpu.Length);
for (int i = 0; i < cpu.Length; i++)
{
gpu[i].Should().Be(cpu[i]);
}
}
finally
for (int i = 0; i < cpu.Length; i++)
{
context.Dispose();
gpu[i].Should().Be(cpu[i]);
}
}
@@ -11,21 +11,14 @@ public class GpuKernelPoolTests
[Trait("Category", "Fast")]
public void EnsureSmallCyclesOnDevice_returns_same_view_on_subsequent_calls()
{
GpuContextPool.GpuContextLease lease = GpuContextPool.RentPreferred(preferCpu: true);
try
{
var accelerator = lease.Accelerator;
using var lease = GpuContextPool.RentPreferred(preferCpu: true);
var accelerator = lease.Accelerator;
var first = GpuKernelPool.EnsureSmallCyclesOnDevice(accelerator);
var second = GpuKernelPool.EnsureSmallCyclesOnDevice(accelerator);
var first = GpuKernelPool.EnsureSmallCyclesOnDevice(accelerator);
var second = GpuKernelPool.EnsureSmallCyclesOnDevice(accelerator);
second.Equals(first).Should().BeTrue();
second.Length.Should().Be(MersenneDivisorCycles.SmallDivisorsMax + 1);
}
finally
{
lease.Dispose();
}
second.Equals(first).Should().BeTrue();
second.Length.Should().Be(MersenneDivisorCycles.SmallDivisorsMax + 1);
}
[Fact]
@@ -158,10 +158,9 @@ public class PrimeOrderCalculatorTests
UInt128 prime = UInt128.Parse("18446744073709641691");
UInt128 result = PrimeOrderCalculator.Calculate(
prime,
previousOrder: null,
PrimeOrderCalculator.PrimeOrderSearchConfig.HeuristicDefault,
PrimeOrderCalculator.PrimeOrderHeuristicDevice.Gpu);
prime,
previousOrder: null,
PrimeOrderCalculator.PrimeOrderSearchConfig.HeuristicDefault);
result.Should().Be((UInt128)1229782938247309446UL);
}
File diff suppressed because it is too large. Load diff
+11 -22
View File
@@ -1,31 +1,20 @@
namespace PerfectNumbers.Core;
internal struct ExponentRemainderStepper
internal struct ExponentRemainderStepper(in MontgomeryDivisorData divisor)
{
private readonly MontgomeryDivisorData _divisor;
private readonly ulong _modulus;
private readonly ulong _nPrime;
private readonly ulong _montgomeryOne;
private ulong _previousExponent;
private ulong _currentMontgomery;
private bool _hasState;
private readonly MontgomeryDivisorData _divisor = divisor;
private readonly ulong _modulus = divisor.Modulus;
private readonly ulong _nPrime = divisor.NPrime;
private readonly ulong _montgomeryOne = divisor.MontgomeryOne;
private ulong _previousExponent = 0UL;
private ulong _currentMontgomery = divisor.MontgomeryOne;
private bool _hasState = false;
public ExponentRemainderStepper(in MontgomeryDivisorData divisor)
{
_divisor = divisor;
_modulus = divisor.Modulus;
_nPrime = divisor.NPrime;
_montgomeryOne = divisor.MontgomeryOne;
_previousExponent = 0UL;
_currentMontgomery = divisor.MontgomeryOne;
_hasState = false;
}
public readonly bool IsValidModulus => _modulus > 1UL && (_modulus & 1UL) != 0UL;
public bool IsValidModulus => _modulus > 1UL && (_modulus & 1UL) != 0UL;
public readonly bool HasState => _hasState;
public bool HasState => _hasState;
public ulong PreviousExponent => _previousExponent;
public readonly ulong PreviousExponent => _previousExponent;
public void Reset()
{
+1 -1
View File
@@ -126,7 +126,7 @@ public static class GpuContextPool
}
}
public struct GpuContextLease
public struct GpuContextLease : IDisposable
{
private readonly PooledContext _ctx;
private bool disposed;
+1 -1
View File
@@ -677,7 +677,7 @@ public ref struct GpuKernelLease(IDisposable limiter, GpuContextLease gpu, Kerne
// GC.SuppressFinalize(this);
}
public readonly struct ExecutionScope
public readonly struct ExecutionScope : IDisposable
{
private readonly object? _lock;
@@ -0,0 +1,98 @@
using System.Collections.Concurrent;
using System.Globalization;
using System.Text;
namespace PerfectNumbers.Core
{
internal static class HeuristicFailureLog
{
private const string LogFileName = "prime-order-heuristic-fallbacks.log";
private const int FlushThreshold = 4096;
private const int MaxBufferedEntries = 16;
private static readonly ThreadLocal<StringBuilder?> s_threadBuilder = new(() => StringBuilderPool.Rent(), trackAllValues: true);
private static readonly ThreadLocal<int> s_threadEntryCount = new(() => 0, trackAllValues: true);
private static readonly object s_fileLock = new();
private static readonly string s_logPath = Path.Combine(AppContext.BaseDirectory, LogFileName);
private static readonly UTF8Encoding s_utf8NoBom = new(false);
static HeuristicFailureLog()
{
AppDomain.CurrentDomain.ProcessExit += static (_, _) => FlushAll(force: true);
}
public static void Record(ulong prime, ulong? candidateOrder, HeuristicFailureReason reason)
{
string primeText = prime.ToString(CultureInfo.InvariantCulture);
string? candidateText = candidateOrder.HasValue ? candidateOrder.Value.ToString(CultureInfo.InvariantCulture) : null;
RecordInternal(primeText, candidateText, reason);
}
public static void Record(UInt128 prime, UInt128? candidateOrder, HeuristicFailureReason reason)
{
string primeText = prime.ToString(CultureInfo.InvariantCulture);
string? candidateText = candidateOrder.HasValue ? candidateOrder.Value.ToString(CultureInfo.InvariantCulture) : null;
RecordInternal(primeText, candidateText, reason);
}
private static void RecordInternal(string primeText, string? candidateText, HeuristicFailureReason reason)
{
StringBuilder? existing = s_threadBuilder.Value;
StringBuilder builder = existing ?? StringBuilderPool.Rent();
int count = s_threadEntryCount.Value + 1;
bool forceFlush = count >= MaxBufferedEntries;
builder.Append(DateTime.UtcNow.ToString("o", CultureInfo.InvariantCulture));
builder.Append(" | prime=");
builder.Append(primeText);
builder.Append(" | reason=");
builder.Append(reason);
if (candidateText is not null)
{
builder.Append(" | candidateOrder=");
builder.Append(candidateText);
}
builder.AppendLine();
FlushBuilderIfNeeded(builder, forceFlush);
if (builder.Length == 0)
{
count = 0;
}
s_threadEntryCount.Value = count;
s_threadBuilder.Value = builder;
}
private static void FlushAll(bool force)
{
foreach (StringBuilder? builder in s_threadBuilder.Values)
{
if (builder is not null)
{
FlushBuilderIfNeeded(builder, force);
}
}
}
private static void FlushBuilderIfNeeded(StringBuilder builder, bool force)
{
if (builder.Length == 0)
{
return;
}
if (!force && builder.Length < FlushThreshold)
{
return;
}
string text = builder.ToString();
builder.Clear();
lock (s_fileLock)
{
using FileStream stream = new FileStream(s_logPath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
using StreamWriter writer = new StreamWriter(stream, s_utf8NoBom);
writer.Write(text);
}
}
}
}
@@ -0,0 +1,11 @@
namespace PerfectNumbers.Core
{
internal enum HeuristicFailureReason
{
PhiPartialFactorizationFailed,
StrictModeRequested,
HeuristicPipelineUnresolved,
HeuristicPipelineAssumedFullOrder,
}
}
+34 -40
View File
@@ -1,11 +1,8 @@
using System.Buffers;
using System.Buffers.Binary;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using PerfectNumbers.Core.Gpu;
using ILGPU;
using ILGPU.Runtime;
@@ -54,15 +51,15 @@ public class MersenneDivisorCycles
return snapshot;
}
public static bool CycleEqualsExponent(ulong divisor, in MontgomeryDivisorData divisorData, ulong exponent)
public static bool CycleEqualsExponentCpu(ulong divisor, in MontgomeryDivisorData divisorData, ulong exponent)
{
Dictionary<ulong, FactorCacheEntry>? cache = null;
return CycleEqualsExponent(divisor, exponent, divisorData, ref cache);
return CycleEqualsExponentCpu(divisor, exponent, divisorData, ref cache);
}
public static bool CycleEqualsExponent(ulong divisor, ulong exponent, in MontgomeryDivisorData divisorData, ref Dictionary<ulong, FactorCacheEntry>? factorCache)
public static bool CycleEqualsExponentCpu(ulong divisor, ulong exponent, in MontgomeryDivisorData divisorData, ref Dictionary<ulong, FactorCacheEntry>? factorCache)
{
if (CycleEqualsExponentForMersenneCandidate(divisor, divisorData, exponent))
if (CycleEqualsExponentForMersenneCandidateCpu(divisor, divisorData, exponent))
{
return true;
}
@@ -97,7 +94,7 @@ public class MersenneDivisorCycles
factorCache = cache;
}
if (TryCalculateCycleLengthForExponent(divisor, exponent, divisorData, cache, out ulong cycleLength) && cycleLength != 0UL)
if (TryCalculateCycleLengthForExponentCpu(divisor, exponent, divisorData, cache, out ulong cycleLength) && cycleLength != 0UL)
{
return cycleLength == exponent;
}
@@ -105,7 +102,7 @@ public class MersenneDivisorCycles
return exponent.Pow2MontgomeryModWindowedCpu(divisorData, keepMontgomery: false) == 1UL;
}
public static bool CycleEqualsExponentForMersenneCandidate(ulong divisor, in MontgomeryDivisorData divisorData, ulong exponent)
public static bool CycleEqualsExponentForMersenneCandidateCpu(ulong divisor, in MontgomeryDivisorData divisorData, ulong exponent)
{
if (!IsValidMersenneDivisorCandidate(divisor, exponent))
{
@@ -264,7 +261,7 @@ public class MersenneDivisorCycles
return order;
}
public static bool TryCalculateCycleLengthForExponent(
public static bool TryCalculateCycleLengthForExponentCpu(
ulong divisor,
ulong exponent,
in MontgomeryDivisorData divisorData,
@@ -285,15 +282,15 @@ public class MersenneDivisorCycles
return true;
}
ulong computedOrder = PrimeOrderCalculator.Calculate(
ulong order = PrimeOrderCalculator.Calculate(
divisor,
previousOrder: null,
divisorData,
PrimeOrderCalculator.PrimeOrderSearchConfig.HeuristicDefault,
PrimeOrderCalculator.PrimeOrderHeuristicDevice.Cpu);
if (computedOrder != 0UL)
if (order != 0UL)
{
cycleLength = computedOrder;
cycleLength = order;
return true;
}
@@ -327,7 +324,7 @@ public class MersenneDivisorCycles
return false;
}
cycleLength = ReduceOrder(divisorData, phi, factorCounts);
cycleLength = ReduceOrderCpu(divisorData, phi, factorCounts);
return true;
}
@@ -471,7 +468,7 @@ public class MersenneDivisorCycles
}
}
private static ulong ReduceOrder(in MontgomeryDivisorData divisorData, ulong initialOrder, Dictionary<ulong, int> factorCounts)
private static ulong ReduceOrderCpu(in MontgomeryDivisorData divisorData, ulong initialOrder, Dictionary<ulong, int> factorCounts)
{
if (factorCounts.Count == 0)
{
@@ -530,20 +527,31 @@ public class MersenneDivisorCycles
return 2UL;
}
PollardRhoMontgomeryReducer reducer = PollardRhoMontgomeryReducer.Create(n);
ulong diff = 0UL;
ulong d = 0UL;
while (true)
{
ulong c = (NextRandomUInt64() % (n - 1UL)) + 1UL;
ulong x = (NextRandomUInt64() % (n - 2UL)) + 2UL;
ulong y = x;
ulong d = 1UL;
ulong c = (DeterministicRandom.NextUInt64() % (n - 1UL)) + 1UL;
ulong xStandard = (DeterministicRandom.NextUInt64() % (n - 2UL)) + 2UL;
ulong yStandard = xStandard;
ulong cMontgomery = reducer.ConvertToMontgomery(c);
ulong xMontgomery = reducer.ConvertToMontgomery(xStandard);
ulong yMontgomery = xMontgomery;
d = 1UL;
while (d == 1UL)
{
x = AdvancePolynomial(x, c, n);
y = AdvancePolynomial(y, c, n);
y = AdvancePolynomial(y, c, n);
xMontgomery = reducer.Advance(xMontgomery, cMontgomery);
xStandard = reducer.AdvanceStandard(xStandard, c);
ulong diff = x > y ? x - y : y - x;
yMontgomery = reducer.Advance(yMontgomery, cMontgomery);
yStandard = reducer.AdvanceStandard(yStandard, c);
yMontgomery = reducer.Advance(yMontgomery, cMontgomery);
yStandard = reducer.AdvanceStandard(yStandard, c);
diff = xStandard > yStandard ? xStandard - yStandard : yStandard - xStandard;
d = BinaryGcd(diff, n);
}
@@ -554,13 +562,6 @@ public class MersenneDivisorCycles
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ulong AdvancePolynomial(ulong x, ulong c, ulong modulus)
{
UInt128 value = (UInt128)x * x + c;
return (ulong)(value % modulus);
}
private static ulong BinaryGcd(ulong a, ulong b)
{
if (a == 0UL)
@@ -592,13 +593,6 @@ public class MersenneDivisorCycles
}
}
private static ulong NextRandomUInt64()
{
Span<byte> buffer = stackalloc byte[8];
RandomNumberGenerator.Fill(buffer);
return BinaryPrimitives.ReadUInt64LittleEndian(buffer);
}
public readonly struct FactorCacheEntry
{
public FactorCacheEntry(ulong[] primes, byte[] exponents, int count)
@@ -940,15 +934,15 @@ public class MersenneDivisorCycles
if (PrimeTester.IsPrimeInternal(divisor, CancellationToken.None))
{
ulong computedOrder = PrimeOrderCalculator.Calculate(
ulong order = PrimeOrderCalculator.Calculate(
divisor,
previousOrder: null,
divisorData,
PrimeOrderCalculator.PrimeOrderSearchConfig.HeuristicDefault,
PrimeOrderCalculator.PrimeOrderHeuristicDevice.Cpu);
if (computedOrder != 0UL)
if (order != 0UL)
{
cycleLength = computedOrder;
cycleLength = order;
return true;
}
}
@@ -26,8 +26,6 @@ public static class MersenneNumberDivisorByDivisorTester
return;
}
_ = threadCount;
candidates.Sort();
Span<ulong> candidateSpan = CollectionsMarshal.AsSpan(candidates);
int candidateCount = candidateSpan.Length;
@@ -38,34 +36,65 @@ public static class MersenneNumberDivisorByDivisorTester
if (previousResults is not null && previousResults.Count > 0)
{
int recordedCount = previousResults.Count;
ulong[] recordedCandidates = ArrayPool<ulong>.Shared.Rent(recordedCount);
try
bool preferSortedMerge = recordedCount <= 4096 || (recordedCount * 4L) <= candidateCount;
if (preferSortedMerge)
{
Span<ulong> recordedSpan = recordedCandidates.AsSpan(0, recordedCount);
int recordedIndex = 0;
foreach (ulong value in previousResults.Keys)
ulong[] recordedCandidates = ArrayPool<ulong>.Shared.Rent(recordedCount);
try
{
recordedSpan[recordedIndex++] = value;
Span<ulong> recordedSpan = recordedCandidates.AsSpan(0, recordedCount);
int recordedIndex = 0;
foreach (ulong value in previousResults.Keys)
{
recordedSpan[recordedIndex++] = value;
}
recordedSpan.Sort();
int writeIndex = 0;
int skipIndex = 0;
for (int readIndex = 0; readIndex < candidateCount; readIndex++)
{
ulong candidate = candidateSpan[readIndex];
while (skipIndex < recordedCount && recordedSpan[skipIndex] < candidate)
{
skipIndex++;
}
if (skipIndex < recordedCount && recordedSpan[skipIndex] == candidate)
{
skippedByPreviousResults++;
skipIndex++;
continue;
}
candidateSpan[writeIndex++] = candidate;
}
if (writeIndex < candidateCount)
{
candidates.RemoveRange(writeIndex, candidateCount - writeIndex);
candidateSpan = CollectionsMarshal.AsSpan(candidates); // candidateSpan now tracks the reduced list.
candidateCount = candidateSpan.Length;
}
}
recordedSpan.Sort();
finally
{
ArrayPool<ulong>.Shared.Return(recordedCandidates, clearArray: true);
}
}
else
{
int writeIndex = 0;
int skipIndex = 0;
for (int readIndex = 0; readIndex < candidateCount; readIndex++)
{
ulong candidate = candidateSpan[readIndex];
while (skipIndex < recordedCount && recordedSpan[skipIndex] < candidate)
{
skipIndex++;
}
if (skipIndex < recordedCount && recordedSpan[skipIndex] == candidate)
if (previousResults.ContainsKey(candidate))
{
skippedByPreviousResults++;
skipIndex++;
continue;
}
@@ -75,14 +104,10 @@ public static class MersenneNumberDivisorByDivisorTester
if (writeIndex < candidateCount)
{
candidates.RemoveRange(writeIndex, candidateCount - writeIndex);
candidateSpan = CollectionsMarshal.AsSpan(candidates);
candidateSpan = CollectionsMarshal.AsSpan(candidates); // candidateSpan now tracks the reduced list.
candidateCount = candidateSpan.Length;
}
}
finally
{
ArrayPool<ulong>.Shared.Return(recordedCandidates, clearArray: true);
}
}
if (skippedByPreviousResults > 0)
@@ -112,12 +137,9 @@ public static class MersenneNumberDivisorByDivisorTester
ulong maxPrime = 0UL;
int primeWriteIndex = 0;
// This implementation is terribly slow, while this method expect prime p given as --filter-p input already.
// We don't need to additionally check it.
// using IEnumerator<ulong> primeEnumerator = Prime.Numbers.GetEnumerator();
// bool hasPrime = primeEnumerator.MoveNext();
// ulong currentPrime = hasPrime ? primeEnumerator.Current : 0UL;
using IEnumerator<ulong> primeEnumerator = Prime.Numbers.GetEnumerator();
bool hasPrime = primeEnumerator.MoveNext();
ulong currentPrime = hasPrime ? primeEnumerator.Current : 0UL;
for (int index = startIndex; index < candidateCount; index++)
{
@@ -130,29 +152,25 @@ public static class MersenneNumberDivisorByDivisorTester
continue;
}
// This implementation is terribly slow, while this method expect prime p given as --filter-p input already.
// We don't need to additionally check it.
// while (hasPrime && currentPrime < candidate)
// {
// hasPrime = primeEnumerator.MoveNext();
// if (!hasPrime)
// {
// break;
// }
while (hasPrime && currentPrime < candidate)
{
hasPrime = primeEnumerator.MoveNext();
if (!hasPrime)
{
break;
}
// currentPrime = primeEnumerator.Current;
// }
currentPrime = primeEnumerator.Current;
}
// if (!hasPrime || currentPrime != candidate)
// if (!Number.IsPrime(candidate))
// {
// markComposite();
// printResult(candidate, false, false, false);
// continue;
// }
if (!hasPrime || currentPrime != candidate)
{
markComposite();
printResult(candidate, false, false, false);
continue;
}
primesSpan[primeWriteIndex++] = candidate;
primesSpan[primeWriteIndex++] = candidate;
if (candidate > maxPrime)
{
@@ -270,7 +288,32 @@ public static class MersenneNumberDivisorByDivisorTester
MaxDegreeOfParallelism = workerCount
};
Parallel.ForEach(filteredPrimes, options, ProcessPrime);
primeCount = filteredPrimes.Count; // Reusing primeCount to track the filtered count.
int primesPerWorker = (primeCount + workerCount - 1) / workerCount;
Parallel.For(
0,
workerCount,
options,
workerIndex =>
{
int start = workerIndex * primesPerWorker;
if (start >= primeCount)
{
return;
}
int end = start + primesPerWorker;
if (end > primeCount)
{
end = primeCount;
}
for (int i = start; i < end; i++)
{
ProcessPrime(filteredPrimes[i]);
}
});
}
}
}
+32 -37
View File
@@ -1,4 +1,3 @@
using System;
using System.Buffers;
namespace PerfectNumbers.Core
@@ -18,31 +17,27 @@ namespace PerfectNumbers.Core
{
}
public static PartialFactorResult Rent(in FactorEntry[]? factors, ulong cofactor, bool fullyFactored, int count)
{
PartialFactorResult? instance = s_poolHead;
if (instance is null)
{
instance = new PartialFactorResult();
}
else
{
s_poolHead = instance._next;
}
public static PartialFactorResult Rent(in FactorEntry[]? factors, ulong cofactor, bool fullyFactored, int count)
{
PartialFactorResult? instance = s_poolHead;
if (instance is null)
{
instance = new();
}
else
{
s_poolHead = instance._next;
}
instance._next = null;
instance.Factors = factors;
instance.Cofactor = cofactor;
instance.FullyFactored = fullyFactored;
instance.Count = count;
return instance;
}
instance._next = null;
instance.Factors = factors;
instance.Cofactor = cofactor;
instance.FullyFactored = fullyFactored;
instance.Count = count;
return instance;
}
public static readonly PartialFactorResult Empty = new PartialFactorResult
{
Cofactor = 1UL,
FullyFactored = true,
};
public static readonly PartialFactorResult Empty = new() { Cofactor = 1UL, FullyFactored = true };
public PartialFactorResult WithAdditionalPrime(ulong prime)
{
@@ -63,19 +58,19 @@ namespace PerfectNumbers.Core
}
public void Dispose()
{
if (this != Empty)
{
FactorEntry[]? factors = Factors;
if (factors is not null)
{
Factors = null;
ArrayPool<FactorEntry>.Shared.Return(factors, clearArray: false);
}
{
if (this != Empty)
{
FactorEntry[]? factors = Factors;
if (factors is not null)
{
Factors = null;
ArrayPool<FactorEntry>.Shared.Return(factors, clearArray: false);
}
_next = s_poolHead;
s_poolHead = this;
}
_next = s_poolHead;
s_poolHead = this;
}
}
}
}
}
@@ -0,0 +1,119 @@
using System;
using System.Runtime.CompilerServices;
namespace PerfectNumbers.Core;
internal readonly struct PollardRhoMontgomeryReducer
{
private readonly MontgomeryDivisorData _divisorData;
private readonly ulong _reciprocal;
[ThreadStatic]
private static bool s_hasCachedReciprocal;
[ThreadStatic]
private static ulong s_cachedReciprocal;
public PollardRhoMontgomeryReducer(in MontgomeryDivisorData divisorData, ulong reciprocal)
{
_divisorData = divisorData;
_reciprocal = reciprocal == 0UL || reciprocal == ulong.MaxValue
? ReciprocalMath.ComputeReciprocalEstimate(divisorData.Modulus)
: reciprocal;
}
public ulong Modulus => _divisorData.Modulus;
private ulong NPrime => _divisorData.NPrime;
private ulong MontgomeryTwoSquared => _divisorData.MontgomeryTwoSquared;
private ulong MontgomeryOne => _divisorData.MontgomeryOne;
private ulong MontgomeryTwo => _divisorData.MontgomeryTwo;
public ulong Reciprocal => _reciprocal;
public static PollardRhoMontgomeryReducer Create(ulong modulus)
{
MontgomeryDivisorData divisorData = MontgomeryDivisorDataCache.Get(modulus);
ulong reciprocal;
if (s_hasCachedReciprocal)
{
reciprocal = ReciprocalMath.RefineReciprocalEstimate(s_cachedReciprocal, modulus);
if (reciprocal == 0UL || reciprocal == ulong.MaxValue)
{
reciprocal = ReciprocalMath.ComputeReciprocalEstimate(modulus);
}
}
else
{
reciprocal = ReciprocalMath.ComputeReciprocalEstimate(modulus);
}
s_cachedReciprocal = reciprocal;
s_hasCachedReciprocal = true;
return new PollardRhoMontgomeryReducer(divisorData, reciprocal);
}
public static PollardRhoMontgomeryReducer Create(in MontgomeryDivisorData divisorData, ulong previousReciprocal)
{
ulong reciprocal = ReciprocalMath.RefineReciprocalEstimate(previousReciprocal, divisorData.Modulus);
if (reciprocal == 0UL || reciprocal == ulong.MaxValue)
{
reciprocal = ReciprocalMath.ComputeReciprocalEstimate(divisorData.Modulus);
}
s_cachedReciprocal = reciprocal;
s_hasCachedReciprocal = true;
return new PollardRhoMontgomeryReducer(divisorData, reciprocal);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ulong ConvertToMontgomery(ulong value)
{
if (value == 0UL)
{
return 0UL;
}
if (value == 1UL)
{
return MontgomeryOne;
}
if (value == 2UL)
{
return MontgomeryTwo;
}
return value.MontgomeryMultiply(MontgomeryTwoSquared, Modulus, NPrime);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ulong ReduceFromMontgomery(ulong montgomeryValue)
{
return montgomeryValue.MontgomeryMultiply(1UL, Modulus, NPrime);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ulong Advance(ulong montgomeryValue, ulong cMontgomery)
{
ulong squared = montgomeryValue.MontgomeryMultiply(montgomeryValue, Modulus, NPrime);
return MontgomeryAdd(squared, cMontgomery, Modulus);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ulong AdvanceStandard(ulong standardValue, ulong c)
{
UInt128 polynomial = (UInt128)standardValue * standardValue + c;
return ReciprocalMath.Reduce(polynomial, Modulus, _reciprocal);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ulong MontgomeryAdd(ulong a, ulong b, ulong modulus)
{
UInt128 total = (UInt128)a + b;
if (total >= modulus)
{
total -= modulus;
}
return (ulong)total;
}
}
@@ -9,6 +9,9 @@ namespace PerfectNumbers.Core;
internal static partial class PrimeOrderCalculator
{
public static UInt128 Calculate(UInt128 prime, UInt128? previousOrder, PrimeOrderSearchConfig config)
=> Calculate(prime, previousOrder, config, PrimeOrderHeuristicDevice.Gpu);
public static UInt128 Calculate(
UInt128 prime,
UInt128? previousOrder,
+87 -63
View File
@@ -17,14 +17,14 @@ internal static partial class PrimeOrderCalculator
}
internal readonly struct PrimeOrderSearchConfig(uint smallFactorLimit, int pollardRhoMilliseconds, int maxPowChecks, PrimeOrderMode mode)
{
public static readonly PrimeOrderSearchConfig HeuristicDefault = new(smallFactorLimit: 4_000_000, pollardRhoMilliseconds: 24 * 10_240, maxPowChecks: 24, PrimeOrderMode.Heuristic);
public static readonly PrimeOrderSearchConfig StrictDefault = new(smallFactorLimit: 1_000_000, pollardRhoMilliseconds: 0, maxPowChecks: 0, PrimeOrderMode.Strict);
{
public static PrimeOrderSearchConfig HeuristicDefault => new(smallFactorLimit: 100_000, pollardRhoMilliseconds: 128, maxPowChecks: 24, PrimeOrderMode.Heuristic);
public static PrimeOrderSearchConfig StrictDefault => new(smallFactorLimit: 1_000_000, pollardRhoMilliseconds: 0, maxPowChecks: 0, PrimeOrderMode.Strict);
public readonly uint SmallFactorLimit = smallFactorLimit;
public readonly int PollardRhoMilliseconds = pollardRhoMilliseconds;
public readonly int MaxPowChecks = maxPowChecks;
public readonly PrimeOrderMode Mode = mode;
public readonly uint SmallFactorLimit = smallFactorLimit;
public readonly int PollardRhoMilliseconds = pollardRhoMilliseconds;
public readonly int MaxPowChecks = maxPowChecks;
public readonly PrimeOrderMode Mode = mode;
}
internal enum PrimeOrderHeuristicDevice
@@ -41,6 +41,7 @@ internal static partial class PrimeOrderCalculator
private static bool IsGpuPow2Allowed => s_pow2ModeInitialized && s_allowGpuPow2;
[ThreadStatic]
private static PrimeOrderHeuristicDevice s_deviceMode;
@@ -58,29 +59,29 @@ internal static partial class PrimeOrderCalculator
private static bool IsGpuHeuristicDevice => s_pow2ModeInitialized && s_deviceMode == PrimeOrderHeuristicDevice.Gpu;
private readonly struct Pow2ModeScope
{
private readonly bool _previousInitialized;
private readonly bool _previousAllow;
private readonly PrimeOrderHeuristicDevice _previousDevice;
private readonly struct Pow2ModeScope
{
private readonly bool _previousInitialized;
private readonly bool _previousAllow;
private readonly PrimeOrderHeuristicDevice _previousDevice;
public Pow2ModeScope(PrimeOrderHeuristicDevice device)
{
_previousInitialized = s_pow2ModeInitialized;
_previousAllow = s_allowGpuPow2;
_previousDevice = s_deviceMode;
s_pow2ModeInitialized = true;
s_allowGpuPow2 = device == PrimeOrderHeuristicDevice.Gpu;
s_deviceMode = device;
}
public Pow2ModeScope(PrimeOrderHeuristicDevice device)
{
_previousInitialized = s_pow2ModeInitialized;
_previousAllow = s_allowGpuPow2;
_previousDevice = s_deviceMode;
s_pow2ModeInitialized = true;
s_allowGpuPow2 = device == PrimeOrderHeuristicDevice.Gpu;
s_deviceMode = device;
}
public void Dispose()
{
s_allowGpuPow2 = _previousAllow;
s_pow2ModeInitialized = _previousInitialized;
s_deviceMode = _previousDevice;
}
}
public void Dispose()
{
s_allowGpuPow2 = _previousAllow;
s_pow2ModeInitialized = _previousInitialized;
s_deviceMode = _previousDevice;
}
}
private readonly struct DebugLoggingScope
{
@@ -120,6 +121,13 @@ internal static partial class PrimeOrderCalculator
}
}
public static ulong Calculate(
ulong prime,
ulong? previousOrder,
in MontgomeryDivisorData divisorData,
in PrimeOrderSearchConfig config)
=> Calculate(prime, previousOrder, divisorData, config, PrimeOrderHeuristicDevice.Cpu);
public static ulong Calculate(
ulong prime,
ulong? previousOrder,
@@ -134,8 +142,7 @@ internal static partial class PrimeOrderCalculator
}
private static ulong CalculateInternal(ulong prime, ulong? previousOrder, in MontgomeryDivisorData divisorData, in PrimeOrderSearchConfig config)
{
// TODO: Is this condition ever met on EvenPerfectBitScanner's execution path? If not, we can add a clarification comment and comment out the entire block. We want to support p candidates at least greater or equal to 31.
{
if (prime <= 3UL)
{
return prime == 3UL ? 2UL : 1UL;
@@ -150,17 +157,17 @@ internal static partial class PrimeOrderCalculator
PartialFactorResult phiFactors = PartialFactor(phi, config);
ulong result;
if (phiFactors.Factors is null)
{
result = FinishStrictlyCpu(prime, divisorData);
phiFactors.Dispose();
return result;
}
ulong result;
if (phiFactors.Factors is null)
{
result = FinishStrictlyCpu(prime, divisorData);
phiFactors.Dispose();
return result;
}
result = RunHeuristicPipelineCpu(prime, previousOrder, config, divisorData, phi, phiFactors);
phiFactors.Dispose();
return result;
result = RunHeuristicPipelineCpu(prime, previousOrder, config, divisorData, phi, phiFactors);
phiFactors.Dispose();
return result;
}
private static ulong RunHeuristicPipelineCpu(
@@ -429,8 +436,8 @@ internal static partial class PrimeOrderCalculator
// DebugLog(() => $"Checking candidates ({candidateCount} candidates, {powBudget} pow budget)");
int index = 0;
const int MaxGpuBatchSize = 256;
const int StackGpuBatchSize = 64;
const int MaxGpuBatchSize = 1024;
const int StackGpuBatchSize = 1024;
Span<ulong> stackGpuRemainders = stackalloc ulong[StackGpuBatchSize];
while (index < candidateCount && powUsed < powBudget)
{
@@ -1157,6 +1164,10 @@ internal static partial class PrimeOrderCalculator
return true;
}
PollardRhoMontgomeryReducer reducer = PollardRhoMontgomeryReducer.Create(n);
ulong diff = 0UL;
ulong d = 0UL;
while (true)
{
if (stopwatch.ElapsedTicks > budgetTicks)
@@ -1165,9 +1176,12 @@ internal static partial class PrimeOrderCalculator
}
ulong c = (DeterministicRandom.NextUInt64() % (n - 1UL)) + 1UL;
ulong x = (DeterministicRandom.NextUInt64() % (n - 2UL)) + 2UL;
ulong y = x;
ulong d = 1UL;
ulong xStandard = (DeterministicRandom.NextUInt64() % (n - 2UL)) + 2UL;
ulong yStandard = xStandard;
ulong cMontgomery = reducer.ConvertToMontgomery(c);
ulong xMontgomery = reducer.ConvertToMontgomery(xStandard);
ulong yMontgomery = xMontgomery;
d = 1UL;
while (d == 1UL)
{
@@ -1176,10 +1190,15 @@ internal static partial class PrimeOrderCalculator
return false;
}
x = AdvancePolynomial(x, c, n);
y = AdvancePolynomial(y, c, n);
y = AdvancePolynomial(y, c, n);
ulong diff = x > y ? x - y : y - x;
xMontgomery = reducer.Advance(xMontgomery, cMontgomery);
xStandard = reducer.AdvanceStandard(xStandard, c);
yMontgomery = reducer.Advance(yMontgomery, cMontgomery);
yStandard = reducer.AdvanceStandard(yStandard, c);
yMontgomery = reducer.Advance(yMontgomery, cMontgomery);
yStandard = reducer.AdvanceStandard(yStandard, c);
diff = xStandard > yStandard ? xStandard - yStandard : yStandard - xStandard;
d = BinaryGcd(diff, n);
}
@@ -1191,13 +1210,6 @@ internal static partial class PrimeOrderCalculator
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ulong AdvancePolynomial(ulong x, ulong c, ulong modulus)
{
UInt128 value = (UInt128)x * x + c;
return (ulong)(value % modulus);
}
private static ulong BinaryGcd(ulong a, ulong b)
{
if (a == 0UL)
@@ -1384,19 +1396,31 @@ internal static partial class PrimeOrderCalculator
return 2UL;
}
PollardRhoMontgomeryReducer reducer = PollardRhoMontgomeryReducer.Create(n);
ulong diff = 0UL;
ulong d = 0UL;
while (true)
{
ulong c = (DeterministicRandom.NextUInt64() % (n - 1UL)) + 1UL;
ulong x = (DeterministicRandom.NextUInt64() % (n - 2UL)) + 2UL;
ulong y = x;
ulong d = 1UL;
ulong xStandard = (DeterministicRandom.NextUInt64() % (n - 2UL)) + 2UL;
ulong yStandard = xStandard;
ulong cMontgomery = reducer.ConvertToMontgomery(c);
ulong xMontgomery = reducer.ConvertToMontgomery(xStandard);
ulong yMontgomery = xMontgomery;
d = 1UL;
while (d == 1UL)
{
x = AdvancePolynomial(x, c, n);
y = AdvancePolynomial(y, c, n);
y = AdvancePolynomial(y, c, n);
ulong diff = x > y ? x - y : y - x;
xMontgomery = reducer.Advance(xMontgomery, cMontgomery);
xStandard = reducer.AdvanceStandard(xStandard, c);
yMontgomery = reducer.Advance(yMontgomery, cMontgomery);
yStandard = reducer.AdvanceStandard(yStandard, c);
yMontgomery = reducer.Advance(yMontgomery, cMontgomery);
yStandard = reducer.AdvanceStandard(yStandard, c);
diff = xStandard > yStandard ? xStandard - yStandard : yStandard - xStandard;
d = BinaryGcd(diff, n);
}
+125
View File
@@ -0,0 +1,125 @@
using System.Numerics;
using System.Runtime.CompilerServices;
namespace PerfectNumbers.Core;
internal static class ReciprocalMath
{
private const int ReciprocalShift = 64;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong ComputeReciprocalEstimate(ulong divisor)
{
if (divisor <= 1UL)
{
return ulong.MaxValue;
}
UInt128 scaledOne = UInt128.One << ReciprocalShift;
UInt128 estimate = scaledOne / divisor;
if ((scaledOne % divisor) != UInt128.Zero)
{
estimate += UInt128.One;
}
if (estimate == UInt128.Zero)
{
return 1UL;
}
if (estimate > ulong.MaxValue)
{
return ulong.MaxValue;
}
return (ulong)estimate;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong RefineReciprocalEstimate(ulong reciprocal, ulong divisor)
{
if (divisor <= 1UL)
{
return ulong.MaxValue;
}
if (reciprocal == 0UL || reciprocal == ulong.MaxValue)
{
return ComputeReciprocalEstimate(divisor);
}
UInt128 reciprocal128 = reciprocal;
UInt128 product = (UInt128)divisor * reciprocal128;
UInt128 scaled = product >> ReciprocalShift;
UInt128 twoScaled = UInt128.One << (ReciprocalShift + 1);
if (scaled >= twoScaled)
{
return ComputeReciprocalEstimate(divisor);
}
UInt128 correction = twoScaled - scaled;
UInt128 refined = (reciprocal128 * correction) >> ReciprocalShift;
if (refined == UInt128.Zero)
{
return 1UL;
}
return refined > ulong.MaxValue ? ulong.MaxValue : (ulong)refined;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong Reduce(UInt128 numerator, ulong divisor, ulong reciprocal)
{
if (divisor <= 1UL)
{
return 0UL;
}
if (numerator == UInt128.Zero)
{
return 0UL;
}
if (reciprocal == 0UL || reciprocal == ulong.MaxValue)
{
return (ulong)(numerator % divisor);
}
ulong high = (ulong)(numerator >> ReciprocalShift);
ulong low = (ulong)numerator;
if (high >= divisor)
{
return (ulong)(numerator % divisor);
}
UInt128 highProduct = (UInt128)high * reciprocal;
UInt128 lowProduct = (UInt128)low * reciprocal;
UInt128 estimate = highProduct + (lowProduct >> ReciprocalShift);
if (estimate == UInt128.Zero)
{
return (ulong)(numerator % divisor);
}
UInt128 product = estimate * divisor;
while (product > numerator)
{
if (estimate == UInt128.Zero)
{
return (ulong)(numerator % divisor);
}
estimate--;
product -= divisor;
}
UInt128 remainder = numerator - product;
while (remainder >= divisor)
{
remainder -= divisor;
}
return (ulong)remainder;
}
}
+11 -11
View File
@@ -582,11 +582,11 @@ public static class ULongExtensions
return Pow2MontgomeryModWindowedCpu(rotationCount, divisor, keepMontgomery: false);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong Pow2MontgomeryModFromCycleRemainderCpu(this ulong reducedExponent, in MontgomeryDivisorData divisor)
{
return Pow2MontgomeryModWindowedCpu(reducedExponent, divisor, keepMontgomery: false);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong Pow2MontgomeryModFromCycleRemainderCpu(this ulong reducedExponent, in MontgomeryDivisorData divisor)
{
return Pow2MontgomeryModWindowedCpu(reducedExponent, divisor, keepMontgomery: false);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong Pow2MontgomeryModWithCycleGpu(this ulong exponent, ulong cycleLength, in MontgomeryDivisorData divisor)
@@ -595,12 +595,12 @@ public static class ULongExtensions
return Pow2MontgomeryModWindowedGpu(rotationCount, divisor, keepMontgomery: false);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong Pow2MontgomeryModFromCycleRemainderGpu(this ulong reducedExponent, in MontgomeryDivisorData divisor)
{
return Pow2MontgomeryModWindowedGpu(reducedExponent, divisor, keepMontgomery: false);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong Pow2MontgomeryModFromCycleRemainderGpu(this ulong reducedExponent, in MontgomeryDivisorData divisor)
{
return Pow2MontgomeryModWindowedGpu(reducedExponent, divisor, keepMontgomery: false);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt128 PowMod(this ulong exponent, UInt128 modulus)
{
+196
View File
@@ -0,0 +1,196 @@
# CPU `--mersenne=bydivisor` pipeline notes
This document summarizes how the `EvenPerfectBitScanner` executable executes the
`--mersenne=bydivisor` mode on the CPU, and how the analytic observations we
derived for the quotients
\[
Q_k = \frac{N}{d_k}, \qquad d_k = 2pk + 1, \qquad N = 2^p - 1,
\]
can help us identify concrete optimization opportunities.
## High-level control flow
* `Program.Main` wires `_byDivisorTester` to a CPU implementation whenever the
CLI switches resolve to `--mersenne=bydivisor` and
`--mersenne-device=cpu`. The CLI also forces a block size of 1 so that the
pipeline pushes exponents to the tester one at a time.
(`Program.cs`, lines 459-520.)
* When the mode starts, `RunByDivisorMode` delegates to
`MersenneNumberDivisorByDivisorTester.Run`, passing the candidate list from
`--filter-p`, the CPU tester instance and the optional replay of previous
outcomes. (`Program.cs`, lines 809-826.)
* `MersenneNumberDivisorByDivisorTester.Run` sorts and deduplicates the
candidate exponents, drops the ones already confirmed in prior runs, validates
primality with the `Open.Numeric.Primes` enumerator and configures the tester
with the largest remaining exponent. (`MersenneNumberDivisorByDivisorTester.cs`,
lines 14-162.)
* For each confirmed prime exponent, the CPU tester computes the largest divisor
it needs to probe through `ComputeAllowedMaxDivisor`. If the range is non-empty
it enters the `CheckDivisors` loop. (`MersenneNumberDivisorByDivisorCpuTester.cs`,
lines 83-110 and 161-266.)
## Inner loop behaviour (`CheckDivisors`)
The hot path is the `while (divisor <= limit)` loop in
`MersenneNumberDivisorByDivisorCpuTester.CheckDivisors`.
1. The loop enumerates divisor candidates of the form `d_k = 2pk + 1` by
incrementing `divisor` by `step = 2p` every iteration. (`MersenneNumberDivisorByDivisorCpuTester.cs`,
lines 177-256.)
2. Small modulus filters track `d_k (mod 3·5·7·8·10·11)` to discard impossible
candidates without touching the divisor-cycle cache. (`MersenneNumberDivisorByDivisorCpuTester.cs`,
lines 195-263.)
3. For every admissible divisor the tester fetches its Montgomery data and the
cached cycle length. When the cache does not contain an entry the
implementation recomputes the cycle by factoring the divisor. (`MersenneNumberDivisorByDivisorCpuTester.cs`,
lines 223-254.)
4. A cycle equal to the tested exponent proves divisibility, so the loop aborts
as soon as one hit is found. (`MersenneNumberDivisorByDivisorCpuTester.cs`,
lines 241-247.)
`DivisorScanSession.CheckDivisor` follows the same `d_k` progression when a
batch API is used. It keeps a running Montgomery residue via
`ExponentRemainderStepper`, but the stepper currently recomputes the delta by
calling a full `Pow2MontgomeryModWindowed` whenever the exponent jumps.
(`MersenneNumberDivisorByDivisorCpuTester.cs`, lines 358-405 and
`ExponentRemainderStepper.cs`, lines 26-95.)
## Where the analytic model fits
The formulas we established for the quotient sequence
\[
\frac{Q_{k+1}}{Q_k} = \frac{2pk + 1}{2p(k+1) + 1}, \qquad
Q_k - Q_{k+1} = \frac{N·2p}{(2pk + 1)(2p(k+1) + 1)}
\]
tell us that the successive quotients and remainders shrink smoothly and that the
per-step update depends only on the previous `(Q_k, r_k)` state and the next
`d_{k+1}`. This lines up with several TODO comments that highlight missing
incremental updates:
* `ExponentRemainderStepper` already caches the previous exponent and a running
Montgomery residue, but it falls back to a full modular exponentiation to
compute the delta between consecutive divisors. (`ExponentRemainderStepper.cs`,
lines 49-72.)
* `CheckDivisors` rebuilds the Montgomery data for every hit candidate even
though the sequence of `d_k` values is strictly monotone.
The quotient identity gives us a way to compute `Δ_k = Q_k - Q_{k+1}` by reading
`Q_k` and the inexpensive division `(2pQ_k - r_k) / d_{k+1}`. Once we know `Δ_k`
we can update
\[
Q_{k+1} = Q_k - Δ_k, \qquad r_{k+1} = r_k - 2p·Q_k + Δ_k · d_{k+1},
\]
without ever evaluating `2^p mod d_{k+1}` from scratch. Incorporating this into
`ExponentRemainderStepper` would let the CPU path reuse a single Montgomery state
across the entire scan and swap the current delta exponentiation (which costs a
full modular ladder) for one multiply-high with a precomputed reciprocal of
`d_{k+1}`. The loop already maintains `d_k` and `step`, so the extra bookkeeping
fits naturally.
## Optimization ideas grounded in the code
1. **Delta-driven Montgomery updates.** Extend `DivisorScanSession` so it tracks
`(Q_k, r_k)` alongside the Montgomery residue and uses the analytic delta
formula to derive the next residue. This replaces the per-step powmod in
`ExponentRemainderStepper.ComputeNext` with a handful of 64-bit operations.
(`MersenneNumberDivisorByDivisorCpuTester.cs`, lines 358-405;
`ExponentRemainderStepper.cs`, lines 49-72.)
2. **Bit-range skipping.** Because `Q_{k+1} / Q_k` is exactly the rational
`(2pk + 1)/(2p(k+1) + 1)`, we can predict when `⌊log₂ Q_k⌋` loses a bit and
jump the loop counter to the first `k` that crosses a target quotient bound
instead of checking each divisor. This is compatible with the existing
`allowedMax` guard because it only modifies how we arrive at the stopping
divisor. (`MersenneNumberDivisorByDivisorCpuTester.cs`, lines 161-266.)
3. **Reuse Montgomery metadata.** The loop already calls `MontgomeryDivisorDataCache.Get`
for each divisor, but the cache lookup is repeated even though the next
divisor is a small increment away. Persisting the previous `MontgomeryDivisorData`
together with the `(Q_k, r_k)` tuple lets us skip redundant cache reads when
the divisor stays within the hot range. (`MersenneNumberDivisorByDivisorCpuTester.cs`,
lines 223-256.)
4. **Reciprocal-based delta evaluation.** `DivisorScanSession.TryComputeAnalyticHit` already keeps the `(Q_k, r_k)` tuple
per exponent, but the current update loop advances one step at a time and divides by `nextDivisor` through `Int128`
arithmetic. (`MersenneNumberDivisorByDivisorCpuTester.cs`, lines 531-594.) We can remove the per-iteration division by
storing a 64-bit reciprocal of `d_k` alongside the state and updating it with a single Newton step when the divisor grows
by `2p`. Once the reciprocal is known, `Δ_k` follows from a multiply-high of `2p·Q_k - r_k`. This turns the loop into a
straight-line sequence of multiplies and subtractions and makes the state update viable even when we unroll multiple
increments at once.
5. **Carry the Montgomery residue forward.** `PrimeDivisorState` already tracks `MontgomeryResidue`, yet the analytic path
currently recomputes it after the quotient update by calling `ToMontgomeryResidue`. (`MersenneNumberDivisorByDivisorCpuTester.cs`,
lines 602-618.) We can instead reuse the residue by multiplying it with the precomputed `R^2 mod d_k` factor that the
cache returns for every divisor. Because the divisor grows deterministically, we only need to fetch the updated Montgomery
constants when `d_k` crosses a cache boundary, letting the fast path avoid modular reductions entirely.
These adjustments directly leverage the quotient progression we analysed and
align with the existing TODO list, giving us a clear path to reduce the number of
modular exponentiations and cache lookups in the CPU by-divisor path.
## Implementation status
* **Delta-driven Montgomery updates:** Implemented. The CPU session now keeps per-prime quotients, remainders, and last divisors so it can advance the analytic state across consecutive candidates, the reciprocal-corrected loop avoids per-step `UInt128` division, and zero cycle remainders short-circuit to direct Montgomery hits without invoking powmods.
* **Bit-range skipping:** Implemented. The CPU divisor sweep now predicts when the quotient loses a bit across both 64-bit and wider strides by deriving the next jump with `UInt128` arithmetic,
and it only falls back to single-step increments when a projected jump would overshoot the configured limit or exceed `ulong.MaxValue`.
* **Reuse Montgomery metadata:** Implemented. Each prime state now persists the divisor's `n'`, `R`, and `R^2` factors so resynchronization seeds the exponent stepper directly, and divisor sessions rebuild the Montgomery tuple from the cached state before touching the shared cache, meaning cache lookups only occur when no stored snapshot matches the new divisor.
* **Reciprocal-based delta evaluation:** Implemented. The analytic updater now retries with a direct 128-bit division when the reciprocal corrections exhaust their tolerance, refreshes the cached reciprocal from that exact quotient, and keeps the progression on the analytic path without rebuilding the state.
* **Carry the Montgomery residue forward:** Implemented. Each analytic update now normalizes the refreshed remainder and multiplies it by the cached `R^2 mod d` factor immediately, so the exponent stepper always has a Montgomery residue ready without recomputing modular reductions.
## Prime-order factoring bottleneck and planned fixes
The Pollard rho fallback in `PrimeOrderCalculator` still performs every quadratic
polynomial advance by issuing a full-width division. The tight loop in
`AdvancePolynomial` squares the iterate and takes a `% modulus`, which triggers
hardware division for each step and dominates the runtime once we test large
Mersenne divisors.【F:PerfectNumbers.Core/PrimeOrderCalculator.cs†L1180-L1211】
The by-divisor CPU path consequently spends a measurable portion of its wall
clock time inside the factorization helper whenever it needs to refresh a cycle
or validate that a divisor candidate is prime.
To bring this code in line with the Montgomery-first approach we use elsewhere
we should:
* **Introduce a Montgomery-backed polynomial step.** `PrimeDivisorState` already
carries the full set of Montgomery constants (`n'`, `R`, `R^2`) for each
divisor we touch in the by-divisor scan, and `DivisorScanSession` rebuilds a
`MontgomeryDivisorData` snapshot whenever it seeds the analytic stepper or the
exponent synchronizer.【F:PerfectNumbers.Core/Cpu/MersenneNumberDivisorByDivisorCpuTester.cs†L645-L779】【F:PerfectNumbers.Core/Cpu/MersenneNumberDivisorByDivisorCpuTester.cs†L1246-L1398】
We can surface the same data to the Pollard rho helper and rewrite
`AdvancePolynomial` so it accepts a precomputed reducer `(modulus, n', R^2)`.
The update then becomes `x = MontgomerySquareAdd(x, cMontgomery)`—a multiply
followed by one Montgomery reduction instead of a general-purpose remainder.
This eliminates the `% modulus` call entirely and lets the CPU factorizer reuse
the same fast path as the analytic delta updates.
* **Reuse reciprocal estimates between neighbouring divisors.** The analytic path
already stores a reciprocal for each divisor and refines it with Newton steps
to divide by `d_k` without hitting the 128-bit fallback.【F:PerfectNumbers.Core/Cpu/MersenneNumberDivisorByDivisorCpuTester.cs†L876-L939】
Pollard rho can share the very same multiplier. We can stash the `FastDiv64`
multiplier inside the Pollard rho state and update it when `q` grows by `2p`
using a single Newton refinement, mirroring `DivideRoundUpUsingReciprocal`.
Once the reciprocal is in place, the `(x * x + c) mod q` step reduces to a
pair of `UInt128` products and one multiply-high to estimate the quotient—a
drop-in use of `ULongExtensions.FastDiv64`.【F:PerfectNumbers.Core/ULongExtensions.cs†L69-L101】
* **Warm-start the Montgomery context from the monotone `q` stream.** Each scan
step increments the divisor by the fixed stride `2p`, so the cached Montgomery
tuple only changes slowly. We can therefore hand the next divisor’s
`MontgomeryDivisorData` directly to the Pollard rho routine instead of forcing
it to recompute the reducer. This is already how `DivisorScanSession` avoids
redundant cache lookups; exporting the cached tuple keeps the factorizer in
lock-step with the rest of the pipeline.【F:PerfectNumbers.Core/Cpu/MersenneNumberDivisorByDivisorCpuTester.cs†L645-L779】
With these changes the divisor-refresh path would no longer spend most of its
cycles on integer divisions, and the monotone growth of `q = 2kp + 1` for
`p ≥ 138,000,000` would guarantee that the reciprocal refinements stay stable
across thousands of consecutive candidates.
### Pollard rho factoring implementation status
* **Montgomery-backed polynomial step:** Completed. `PollardRhoMontgomeryReducer` now squares and adds in Montgomery space,
and the Pollard rho loops in `PrimeOrderCalculator` and `MersenneDivisorCycles` reuse the shared reducer instead of issuing `%` operations.
* **Reciprocal reuse:** Completed. The reducer tracks a cached FastDiv64 reciprocal, refines it for each monotone modulus, and exposes `AdvanceStandard` so the standard-domain iterates reuse the cached multiplier without `%` or `UInt128` division.
* **Warm-started Montgomery context:** Completed. The reducer accepts precomputed `MontgomeryDivisorData` snapshots, letting callers seed Pollard rho directly from the cached divisor state when the by-divisor pipeline advances to the next `q`.
-2
View File
@@ -123,7 +123,6 @@ PerfectNumbers.Core\Cpu\MersenneNumberDivisorByDivisorCpuTester.cs:207:
PerfectNumbers.Core\Cpu\MersenneNumberDivisorByDivisorCpuTester.cs:214: // TODO: Remove this no-cycle branch and require on-demand cycle computation (without
PerfectNumbers.Core\Cpu\MersenneNumberDivisorByDivisorCpuTester.cs:240: // TODO: Replace this linear increment with the batched divisor-cycle walker validated in the
PerfectNumbers.Core\Cpu\MersenneNumberDivisorByDivisorCpuTester.cs:262: : prime.Pow2MontgomeryMod(divisorData); // TODO: Switch to the upcoming ProcessEightBitWindows helper once the
PerfectNumbers.Core\Cpu\MersenneNumberDivisorByDivisorCpuTester.cs:283: // TODO: Replace this modulo with the ring-buffer style counter (subtract loop) used in the fast CLI
PerfectNumbers.Core\Cpu\MersenneNumberDivisorByDivisorCpuTester.cs:356: // TODO: Replace this guard with an on-demand CPU/GPU cycle computation path that skips
PerfectNumbers.Core\Cpu\MersenneNumberDivisorByDivisorCpuTester.cs:396: // TODO: Remove this no-cycle fallback once divisor cycles are mandatory; the per-prime pow2 ladder here
PerfectNumbers.Core\IMersenneNumberDivisorByDivisorTester.cs:7: bool UseDivisorCycles { get; set; } // TODO: Remove the setter once divisor cycle usage becomes mandatory so all implementations always leverage the cached cycles.
@@ -267,7 +266,6 @@ todo_list.txt:122:PerfectNumbers.Core\Cpu\MersenneNumberDivisorByDivisorCpuTeste
todo_list.txt:123:PerfectNumbers.Core\Cpu\MersenneNumberDivisorByDivisorCpuTester.cs:214: // TODO: Remove this no-cycle branch and require on-demand cycle computation (without
todo_list.txt:124:PerfectNumbers.Core\Cpu\MersenneNumberDivisorByDivisorCpuTester.cs:240: // TODO: Replace this linear increment with the batched divisor-cycle walker validated in the
todo_list.txt:125:PerfectNumbers.Core\Cpu\MersenneNumberDivisorByDivisorCpuTester.cs:262: : prime.Pow2MontgomeryMod(divisorData); // TODO: Switch to the upcoming ProcessEightBitWindows helper once the
todo_list.txt:126:PerfectNumbers.Core\Cpu\MersenneNumberDivisorByDivisorCpuTester.cs:283: // TODO: Replace this modulo with the ring-buffer style counter (subtract loop) used in the fast CLI
todo_list.txt:127:PerfectNumbers.Core\Cpu\MersenneNumberDivisorByDivisorCpuTester.cs:356: // TODO: Replace this guard with an on-demand CPU/GPU cycle computation path that skips
todo_list.txt:128:PerfectNumbers.Core\Cpu\MersenneNumberDivisorByDivisorCpuTester.cs:396: // TODO: Remove this no-cycle fallback once divisor cycles are mandatory; the per-prime pow2 ladder here
todo_list.txt:129:PerfectNumbers.Core\IMersenneNumberDivisorByDivisorTester.cs:7: bool UseDivisorCycles { get; set; } // TODO: Remove the setter once divisor cycle usage becomes mandatory so all implementations always leverage the cached cycles.