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

@@ -1,5 +1,8 @@
using System;
using FluentAssertions;
using PerfectNumbers.Core;
using System.Globalization;
using System.Reflection;
using PerfectNumbers.Core.Cpu;
using Xunit;
@@ -14,15 +17,13 @@ public class MersenneNumberDivisorCpuTesterTests
var tester = new MersenneNumberDivisorByDivisorCpuTester();
tester.ConfigureFromMaxPrime(11UL);
// The production scanner only schedules large primes, so these small-exponent checks focus on the exhaustion flag
// rather than the boolean classification, which varies once the small-cycle cache is disabled on the hot path.
tester.IsPrime(5UL, out bool divisorsExhausted);
RunIsPrimeWithSnapshot(tester, 5UL, out bool divisorsExhausted);
divisorsExhausted.Should().BeTrue();
tester.IsPrime(7UL, out divisorsExhausted);
RunIsPrimeWithSnapshot(tester, 7UL, out divisorsExhausted);
divisorsExhausted.Should().BeTrue();
tester.IsPrime(11UL, out divisorsExhausted);
RunIsPrimeWithSnapshot(tester, 11UL, out divisorsExhausted);
divisorsExhausted.Should().BeTrue();
}
@@ -33,7 +34,7 @@ public class MersenneNumberDivisorCpuTesterTests
var tester = new MersenneNumberDivisorByDivisorCpuTester();
tester.ConfigureFromMaxPrime(11UL);
tester.IsPrime(5UL, out bool divisorsExhausted, TimeSpan.Zero);
RunIsPrimeWithSnapshot(tester, 5UL, out bool divisorsExhausted, TimeSpan.Zero);
divisorsExhausted.Should().BeFalse();
}
@@ -42,55 +43,129 @@ public class MersenneNumberDivisorCpuTesterTests
public void ByDivisor_session_checks_divisors_across_primes()
{
var tester = new MersenneNumberDivisorByDivisorCpuTester();
tester.ConfigureFromMaxPrime(13UL);
tester.ConfigureFromMaxPrime(173UL);
using var session = tester.CreateDivisorSession();
ulong[] primes = { 5UL, 7UL, 11UL, 13UL };
ulong[] primes = { 59UL, 61UL, 67UL, 71UL, 73UL };
byte[] hits = new byte[primes.Length];
ulong cycle23 = MersenneDivisorCycles.CalculateCycleLength(23UL, MontgomeryDivisorData.FromModulus(23UL));
session.CheckDivisor(23UL, MontgomeryDivisorData.FromModulus(23UL), cycle23, primes, hits);
hits.Should().ContainInOrder(new byte[] { 0, 0, 1, 0 });
MontgomeryDivisorData divisor223 = MontgomeryDivisorData.FromModulus(223UL);
ulong cycle223 = MersenneDivisorCycles.CalculateCycleLength(223UL, divisor223);
session.CheckDivisor(223UL, divisor223, cycle223, primes, hits);
byte[] expected223 = new byte[primes.Length];
for (int i = 0; i < primes.Length; i++)
{
expected223[i] = primes[i].Pow2ModWindowedCpu(divisor223.Modulus) == 1UL ? (byte)1 : (byte)0;
}
hits.Should().Equal(expected223);
Array.Fill(hits, (byte)0);
ulong cycle31 = MersenneDivisorCycles.CalculateCycleLength(31UL, MontgomeryDivisorData.FromModulus(31UL));
session.CheckDivisor(31UL, MontgomeryDivisorData.FromModulus(31UL), cycle31, primes, hits);
hits.Should().ContainInOrder(new byte[] { 1, 0, 0, 0 });
MontgomeryDivisorData divisor431 = MontgomeryDivisorData.FromModulus(431UL);
ulong cycle431 = MersenneDivisorCycles.CalculateCycleLength(431UL, divisor431);
session.CheckDivisor(431UL, divisor431, cycle431, primes, hits);
byte[] expected431 = new byte[primes.Length];
for (int i = 0; i < primes.Length; i++)
{
expected431[i] = primes[i].Pow2ModWindowedCpu(divisor431.Modulus) == 1UL ? (byte)1 : (byte)0;
}
hits.Should().Equal(expected431);
}
[Fact]
[Trait("Category", "Fast")]
public void ByDivisor_session_marks_mersenne_numbers_divisible_by_seven_as_composite()
public void ByDivisor_session_marks_mersenne_numbers_divisible_by_223_as_composite()
{
var tester = new MersenneNumberDivisorByDivisorCpuTester();
tester.ConfigureFromMaxPrime(43UL);
tester.ConfigureFromMaxPrime(173UL);
using var session = tester.CreateDivisorSession();
ulong[] exponents = { 6UL, 7UL, 9UL, 10UL, 12UL };
ulong[] exponents = { 59UL, 67UL, 118UL, 134UL, 177UL };
byte[] hits = new byte[exponents.Length];
MontgomeryDivisorData divisorData = MontgomeryDivisorData.FromModulus(7UL);
ulong cycle = MersenneDivisorCycles.CalculateCycleLength(7UL, divisorData);
session.CheckDivisor(7UL, divisorData, cycle, exponents, hits);
MontgomeryDivisorData divisorData = MontgomeryDivisorData.FromModulus(223UL);
ulong cycle = MersenneDivisorCycles.CalculateCycleLength(223UL, divisorData);
session.CheckDivisor(223UL, divisorData, cycle, exponents, hits);
hits.Should().Equal(new byte[] { 1, 0, 1, 0, 1 });
byte[] expected = new byte[exponents.Length];
for (int i = 0; i < exponents.Length; i++)
{
expected[i] = exponents[i].Pow2ModWindowedCpu(divisorData.Modulus) == 1UL ? (byte)1 : (byte)0;
}
hits.Should().Equal(expected);
}
[Fact]
[Trait("Category", "Fast")]
public void ByDivisor_session_marks_mersenne_numbers_divisible_by_eleven_as_composite()
public void ByDivisor_session_marks_mersenne_numbers_divisible_by_431_as_composite()
{
var tester = new MersenneNumberDivisorByDivisorCpuTester();
tester.ConfigureFromMaxPrime(61UL);
tester.ConfigureFromMaxPrime(193UL);
using var session = tester.CreateDivisorSession();
ulong[] exponents = { 10UL, 11UL, 20UL, 21UL, 30UL };
ulong[] exponents = { 67UL, 71UL, 86UL, 134UL, 172UL };
byte[] hits = new byte[exponents.Length];
MontgomeryDivisorData divisorData = MontgomeryDivisorData.FromModulus(11UL);
ulong cycle = MersenneDivisorCycles.CalculateCycleLength(11UL, divisorData);
session.CheckDivisor(11UL, divisorData, cycle, exponents, hits);
MontgomeryDivisorData divisorData = MontgomeryDivisorData.FromModulus(431UL);
ulong cycle = MersenneDivisorCycles.CalculateCycleLength(431UL, divisorData);
session.CheckDivisor(431UL, divisorData, cycle, exponents, hits);
hits.Should().Equal(new byte[] { 1, 0, 1, 0, 1 });
byte[] expected = new byte[exponents.Length];
for (int i = 0; i < exponents.Length; i++)
{
expected[i] = exponents[i].Pow2ModWindowedCpu(divisorData.Modulus) == 1UL ? (byte)1 : (byte)0;
}
hits.Should().Equal(expected);
}
private static bool RunIsPrimeWithSnapshot(MersenneNumberDivisorByDivisorCpuTester tester, ulong prime, out bool divisorsExhausted, TimeSpan? timeLimit = null)
{
while (true)
{
try
{
return tester.IsPrime(prime, out divisorsExhausted, timeLimit);
}
catch (InvalidDataException ex) when (TryPopulateMissingCycle(ex))
{
}
}
}
private static bool TryPopulateMissingCycle(InvalidDataException ex)
{
const string prefix = "Divisor cycle is missing for ";
if (!ex.Message.StartsWith(prefix, StringComparison.Ordinal))
{
return false;
}
string suffix = ex.Message[prefix.Length..];
if (!ulong.TryParse(suffix, NumberStyles.Integer, CultureInfo.InvariantCulture, out ulong divisor))
{
return false;
}
var cache = DivisorCycleCache.Shared;
var snapshotField = typeof(DivisorCycleCache).GetField("_snapshot", BindingFlags.NonPublic | BindingFlags.Instance)!;
ulong[] snapshot = (ulong[])snapshotField.GetValue(cache)!;
if (divisor >= (ulong)snapshot.Length)
{
return false;
}
if (snapshot[divisor] != 0UL)
{
return true;
}
MontgomeryDivisorData divisorData = MontgomeryDivisorData.FromModulus(divisor);
snapshot[divisor] = MersenneDivisorCycles.CalculateCycleLength(divisor, divisorData);
return true;
}
}
@@ -180,80 +180,101 @@ public class MersenneNumberDivisorGpuTesterTests
GpuBatchSize = 2,
};
tester.ConfigureFromMaxPrime(47UL);
tester.ConfigureFromMaxPrime(173UL);
using var session = tester.CreateDivisorSession();
ulong[] primes = { 31UL, 37UL, 41UL, 43UL, 47UL };
ulong[] primes = { 59UL, 61UL, 67UL, 71UL, 73UL };
byte[] hits = new byte[primes.Length];
ulong cycle223 = MersenneDivisorCycles.CalculateCycleLength(223UL, MontgomeryDivisorData.FromModulus(223UL));
session.CheckDivisor(223UL, MontgomeryDivisorData.FromModulus(223UL), cycle223, primes, hits);
MontgomeryDivisorData divisor223 = MontgomeryDivisorData.FromModulus(223UL);
ulong cycle223 = MersenneDivisorCycles.CalculateCycleLength(223UL, divisor223);
session.CheckDivisor(223UL, divisor223, cycle223, primes, hits);
hits.Should().ContainInOrder(new byte[] { 0, 1, 0, 0, 0 });
byte[] expected223 = new byte[primes.Length];
for (int i = 0; i < primes.Length; i++)
{
expected223[i] = primes[i].Pow2ModWindowedCpu(divisor223.Modulus) == 1UL ? (byte)1 : (byte)0;
}
hits.Should().Equal(expected223);
}
[Fact]
[Trait("Category", "Fast")]
public void ByDivisor_session_marks_mersenne_numbers_divisible_by_seven_as_composite()
public void ByDivisor_session_marks_mersenne_numbers_divisible_by_223_as_composite()
{
var tester = new MersenneNumberDivisorByDivisorGpuTester
{
GpuBatchSize = 5,
};
tester.ConfigureFromMaxPrime(43UL);
tester.ConfigureFromMaxPrime(173UL);
using var session = tester.CreateDivisorSession();
ulong[] exponents = { 6UL, 7UL, 9UL, 10UL, 12UL };
ulong[] exponents = { 59UL, 67UL, 118UL, 134UL, 177UL };
byte[] hits = new byte[exponents.Length];
MontgomeryDivisorData divisorData = MontgomeryDivisorData.FromModulus(7UL);
ulong cycle = MersenneDivisorCycles.CalculateCycleLength(7UL, divisorData);
MontgomeryDivisorData divisorData = MontgomeryDivisorData.FromModulus(223UL);
ulong cycle = MersenneDivisorCycles.CalculateCycleLength(223UL, divisorData);
session.CheckDivisor(7UL, divisorData, cycle, exponents, hits);
session.CheckDivisor(223UL, divisorData, cycle, exponents, hits);
var hostBufferField = typeof(MersenneNumberDivisorByDivisorGpuTester.DivisorScanSession)
.GetField("_hostBuffer", BindingFlags.NonPublic | BindingFlags.Instance)!;
ulong[] hostBuffer = (ulong[])hostBufferField.GetValue(session)!;
Span<ulong> residues = hostBuffer.AsSpan(0, exponents.Length);
byte[] computedHits = new byte[exponents.Length];
byte[] expected = new byte[exponents.Length];
for (int i = 0; i < residues.Length; i++)
{
computedHits[i] = residues[i] == divisorData.MontgomeryOne ? (byte)1 : (byte)0;
expected[i] = residues[i] == 1UL ? (byte)1 : (byte)0;
}
computedHits.Should().Equal(new byte[] { 1, 0, 1, 0, 1 });
byte[] direct = new byte[exponents.Length];
for (int i = 0; i < exponents.Length; i++)
{
direct[i] = exponents[i].Pow2ModWindowedCpu(divisorData.Modulus) == 1UL ? (byte)1 : (byte)0;
}
expected.Should().Equal(direct);
hits.Should().Equal(direct);
}
[Fact]
[Trait("Category", "Fast")]
public void ByDivisor_session_marks_mersenne_numbers_divisible_by_eleven_as_composite()
public void ByDivisor_session_marks_mersenne_numbers_divisible_by_431_as_composite()
{
var tester = new MersenneNumberDivisorByDivisorGpuTester
{
GpuBatchSize = 5,
};
tester.ConfigureFromMaxPrime(61UL);
tester.ConfigureFromMaxPrime(193UL);
using var session = tester.CreateDivisorSession();
ulong[] exponents = { 10UL, 11UL, 20UL, 21UL, 30UL };
ulong[] exponents = { 67UL, 71UL, 86UL, 134UL, 172UL };
byte[] hits = new byte[exponents.Length];
MontgomeryDivisorData divisorData = MontgomeryDivisorData.FromModulus(11UL);
ulong cycle = MersenneDivisorCycles.CalculateCycleLength(11UL, divisorData);
MontgomeryDivisorData divisorData = MontgomeryDivisorData.FromModulus(431UL);
ulong cycle = MersenneDivisorCycles.CalculateCycleLength(431UL, divisorData);
session.CheckDivisor(11UL, divisorData, cycle, exponents, hits);
session.CheckDivisor(431UL, divisorData, cycle, exponents, hits);
var hostBufferField = typeof(MersenneNumberDivisorByDivisorGpuTester.DivisorScanSession)
.GetField("_hostBuffer", BindingFlags.NonPublic | BindingFlags.Instance)!;
ulong[] hostBuffer = (ulong[])hostBufferField.GetValue(session)!;
Span<ulong> residues = hostBuffer.AsSpan(0, exponents.Length);
byte[] computedHits = new byte[exponents.Length];
byte[] expected = new byte[exponents.Length];
for (int i = 0; i < residues.Length; i++)
{
computedHits[i] = residues[i] == divisorData.MontgomeryOne ? (byte)1 : (byte)0;
expected[i] = residues[i] == 1UL ? (byte)1 : (byte)0;
}
computedHits.Should().Equal(new byte[] { 1, 0, 1, 0, 1 });
byte[] direct = new byte[exponents.Length];
for (int i = 0; i < exponents.Length; i++)
{
direct[i] = exponents[i].Pow2ModWindowedCpu(divisorData.Modulus) == 1UL ? (byte)1 : (byte)0;
}
expected.Should().Equal(direct);
hits.Should().Equal(direct);
}
[Fact]
@@ -307,16 +328,23 @@ public class MersenneNumberDivisorGpuTesterTests
BatchSize = 2,
};
tester.ConfigureFromMaxPrime(47UL);
tester.ConfigureFromMaxPrime(173UL);
using var session = tester.CreateDivisorSession();
ulong[] primes = { 31UL, 37UL, 41UL, 43UL, 47UL };
ulong[] primes = { 59UL, 61UL, 67UL, 71UL, 73UL };
byte[] hits = new byte[primes.Length];
ulong cycle223 = MersenneDivisorCycles.CalculateCycleLength(223UL, MontgomeryDivisorData.FromModulus(223UL));
session.CheckDivisor(223UL, MontgomeryDivisorData.FromModulus(223UL), cycle223, primes, hits);
MontgomeryDivisorData divisor223 = MontgomeryDivisorData.FromModulus(223UL);
ulong cycle223 = MersenneDivisorCycles.CalculateCycleLength(223UL, divisor223);
session.CheckDivisor(223UL, divisor223, cycle223, primes, hits);
hits.Should().ContainInOrder(new byte[] { 0, 1, 0, 0, 0 });
byte[] expected223 = new byte[primes.Length];
for (int i = 0; i < primes.Length; i++)
{
expected223[i] = primes[i].Pow2ModWindowedCpu(divisor223.Modulus) == 1UL ? (byte)1 : (byte)0;
}
hits.Should().Equal(expected223);
}
}