We'll output to console not only primes, but also composites. Output to console standardized on ConsoleInterval

This commit is contained in:
Marek Lemańczyk committed 2025-09-21 12:56:30 +02:00
1 parent 9a054542e6
commit 458cb77227
3 files changed
+237 -240

No files matched your search

+1 -3
View File
@@ -15,8 +15,6 @@ internal static class Program
private static ThreadLocal<MersenneNumberTester> MersenneTesters = null!; private static ThreadLocal<MersenneNumberTester> MersenneTesters = null!;
private static ThreadLocal<ModResidueTracker> PResidue = null!; // p mod d tracker (per-thread) private static ThreadLocal<ModResidueTracker> PResidue = null!; // p mod d tracker (per-thread)
private const ulong InitialP = PerfectNumberConstants.BiggestKnownEvenPerfectP; private const ulong InitialP = PerfectNumberConstants.BiggestKnownEvenPerfectP;
private const int ConsoleInterval = 100_000;
private const int WriteBatchSize = 100; private const int WriteBatchSize = 100;
private const string DefaultCyclesPath = "divisor_cycles.bin"; private const string DefaultCyclesPath = "divisor_cycles.bin";
private static string ResultsFileName = "even_perfect_bit_scan_results.csv"; private static string ResultsFileName = "even_perfect_bit_scan_results.csv";
@@ -887,7 +885,7 @@ internal static class Program
lock (Sync) lock (Sync)
{ {
if (_consoleCounter >= ConsoleInterval) if (_consoleCounter >= PerfectNumberConstants.ConsoleInterval)
{ {
printToConsole = true; printToConsole = true;
primeFlag = passedAllTests && !lastWasComposite && _primeFoundAfterInit; primeFlag = passedAllTests && !lastWasComposite && _primeFoundAfterInit;
@@ -10,290 +10,288 @@ namespace PerfectNumbers.Core.Gpu;
public sealed class MersenneNumberDivisorByDivisorGpuTester public sealed class MersenneNumberDivisorByDivisorGpuTester
{ {
private int _gpuBatchSize = GpuConstants.ScanBatchSize; private int _gpuBatchSize = GpuConstants.ScanBatchSize;
private readonly object _sync = new(); private readonly object _sync = new();
private ulong _divisorLimit; private ulong _divisorLimit;
private bool _isConfigured; private bool _isConfigured;
private ulong _lastStatusDivisor; private ulong _lastStatusDivisor;
private readonly ConcurrentDictionary<Accelerator, Action<Index1D, ulong, ArrayView<ulong>, ArrayView<byte>>> _kernelCache = new(); private readonly ConcurrentDictionary<Accelerator, Action<Index1D, ulong, ArrayView<ulong>, ArrayView<byte>>> _kernelCache = new();
private Action<Index1D, ulong, ArrayView<ulong>, ArrayView<byte>> GetKernel(Accelerator accelerator) => private Action<Index1D, ulong, ArrayView<ulong>, ArrayView<byte>> GetKernel(Accelerator accelerator) =>
_kernelCache.GetOrAdd(accelerator, acc => acc.LoadAutoGroupedStreamKernel<Index1D, ulong, ArrayView<ulong>, ArrayView<byte>>(CheckKernel)); _kernelCache.GetOrAdd(accelerator, acc => acc.LoadAutoGroupedStreamKernel<Index1D, ulong, ArrayView<ulong>, ArrayView<byte>>(CheckKernel));
public int GpuBatchSize public int GpuBatchSize
{ {
get => _gpuBatchSize; get => _gpuBatchSize;
set => _gpuBatchSize = Math.Max(1, value); set => _gpuBatchSize = Math.Max(1, value);
} }
public void ConfigureFromMaxPrime(ulong maxPrime) public void ConfigureFromMaxPrime(ulong maxPrime)
{ {
lock (_sync) lock (_sync)
{ {
_divisorLimit = ComputeDivisorLimitFromMaxPrime(maxPrime); _divisorLimit = ComputeDivisorLimitFromMaxPrime(maxPrime);
_lastStatusDivisor = 0UL; _lastStatusDivisor = 0UL;
_isConfigured = true; _isConfigured = true;
} }
} }
public bool IsPrime(ulong prime, out bool divisorsExhausted) public bool IsPrime(ulong prime, out bool divisorsExhausted)
{ {
lock (_sync) lock (_sync)
{ {
if (!_isConfigured) if (!_isConfigured)
{ {
throw new InvalidOperationException("ConfigureFromMaxPrime must be called before using the tester."); throw new InvalidOperationException("ConfigureFromMaxPrime must be called before using the tester.");
} }
ulong allowedMax = ComputeAllowedMaxDivisor(prime); ulong allowedMax = ComputeAllowedMaxDivisor(prime);
if (allowedMax < 3UL) if (allowedMax < 3UL)
{ {
divisorsExhausted = true; divisorsExhausted = true;
return true; return true;
} }
bool composite = CheckDivisors(prime, allowedMax, out ulong lastProcessed, out bool coveredRange); bool composite = CheckDivisors(prime, allowedMax, out ulong lastProcessed, out bool coveredRange);
if (!composite && lastProcessed != 0UL) if (lastProcessed != 0UL)
{ {
ReportStatus(lastProcessed); ReportStatus(lastProcessed);
} }
if (composite) if (composite)
{ {
divisorsExhausted = true; divisorsExhausted = true;
return false; return false;
} }
divisorsExhausted = coveredRange; divisorsExhausted = coveredRange;
return true; return true;
} }
} }
private bool CheckDivisors(ulong prime, ulong allowedMax, out ulong lastProcessed, out bool coveredRange) private bool CheckDivisors(ulong prime, ulong allowedMax, out ulong lastProcessed, out bool coveredRange)
{ {
allowedMax = Math.Min(allowedMax, _divisorLimit); allowedMax = Math.Min(allowedMax, _divisorLimit);
lastProcessed = 0UL; lastProcessed = 0UL;
if (allowedMax < 3UL) if (allowedMax < 3UL)
{ {
coveredRange = true; coveredRange = true;
return false; return false;
} }
var gpuLease = GpuContextPool.RentPreferred(preferCpu: false); var gpuLease = GpuContextPool.RentPreferred(preferCpu: false);
var accelerator = gpuLease.Accelerator; var accelerator = gpuLease.Accelerator;
var kernel = GetKernel(accelerator); var kernel = GetKernel(accelerator);
int batchCapacity = _gpuBatchSize; int batchCapacity = _gpuBatchSize;
var divisorsBuffer = accelerator.Allocate1D<ulong>(batchCapacity); var divisorsBuffer = accelerator.Allocate1D<ulong>(batchCapacity);
var hitsBuffer = accelerator.Allocate1D<byte>(batchCapacity); var hitsBuffer = accelerator.Allocate1D<byte>(batchCapacity);
ulong[] divisors = ArrayPool<ulong>.Shared.Rent(batchCapacity); ulong[] divisors = ArrayPool<ulong>.Shared.Rent(batchCapacity);
byte[] hits = ArrayPool<byte>.Shared.Rent(batchCapacity); byte[] hits = ArrayPool<byte>.Shared.Rent(batchCapacity);
bool composite = false; bool composite = false;
bool processedAll = false; bool processedAll = false;
ulong divisor = 3UL; ulong divisor = 3UL;
try try
{ {
while (divisor <= allowedMax) while (divisor <= allowedMax)
{ {
int batchSize = 0; int batchSize = 0;
bool reachedEndInBatch = false; bool reachedEndInBatch = false;
while (batchSize < batchCapacity && divisor <= allowedMax) while (batchSize < batchCapacity && divisor <= allowedMax)
{ {
ulong current = divisor; ulong current = divisor;
divisors[batchSize++] = current; divisors[batchSize++] = current;
ulong next = current + 2UL; ulong next = current + 2UL;
if (next <= current) if (next <= current)
{ {
reachedEndInBatch = true; reachedEndInBatch = true;
break; break;
} }
if (next > allowedMax) if (next > allowedMax)
{ {
divisor = next; divisor = next;
reachedEndInBatch = true; reachedEndInBatch = true;
break; break;
} }
divisor = next; divisor = next;
} }
if (batchSize == 0) if (batchSize == 0)
{ {
break; break;
} }
var divisorView = divisorsBuffer.View.SubView(0, batchSize); var divisorView = divisorsBuffer.View.SubView(0, batchSize);
var hitsView = hitsBuffer.View.SubView(0, batchSize); var hitsView = hitsBuffer.View.SubView(0, batchSize);
Span<ulong> divisorSpan = divisors.AsSpan(0, batchSize); Span<ulong> divisorSpan = divisors.AsSpan(0, batchSize);
Span<byte> hitsSpan = hits.AsSpan(0, batchSize); Span<byte> hitsSpan = hits.AsSpan(0, batchSize);
divisorView.CopyFromCPU(ref MemoryMarshal.GetReference(divisorSpan), batchSize); divisorView.CopyFromCPU(ref MemoryMarshal.GetReference(divisorSpan), batchSize);
kernel(batchSize, prime, divisorView, hitsView); kernel(batchSize, prime, divisorView, hitsView);
accelerator.Synchronize(); accelerator.Synchronize();
hitsView.CopyToCPU(ref MemoryMarshal.GetReference(hitsSpan), batchSize); hitsView.CopyToCPU(ref MemoryMarshal.GetReference(hitsSpan), batchSize);
for (int i = 0; i < batchSize; i++) for (int i = 0; i < batchSize; i++)
{ {
lastProcessed = divisorSpan[i]; lastProcessed = divisorSpan[i];
if (hitsSpan[i] != 0) if (hitsSpan[i] != 0)
{ {
composite = true; composite = true;
break; break;
} }
} }
if (composite) if (composite)
{ {
break; break;
} }
if (reachedEndInBatch) if (reachedEndInBatch)
{ {
processedAll = true; processedAll = true;
break; break;
} }
} }
coveredRange = composite || processedAll || divisor > allowedMax; coveredRange = composite || processedAll || divisor > allowedMax;
} }
finally finally
{ {
ArrayPool<byte>.Shared.Return(hits, clearArray: true); ArrayPool<byte>.Shared.Return(hits, clearArray: true);
ArrayPool<ulong>.Shared.Return(divisors, clearArray: true); ArrayPool<ulong>.Shared.Return(divisors, clearArray: true);
hitsBuffer.Dispose(); hitsBuffer.Dispose();
divisorsBuffer.Dispose(); divisorsBuffer.Dispose();
gpuLease.Dispose(); gpuLease.Dispose();
} }
return composite; return composite;
} }
private void ReportStatus(ulong divisor) private void ReportStatus(ulong divisor)
{ {
if (divisor <= _lastStatusDivisor) if (++_lastStatusDivisor == PerfectNumberConstants.ConsoleInterval)
{ {
return; Console.WriteLine($"...processed by-divisor candidate = {divisor}");
} _lastStatusDivisor = 0;
}
}
_lastStatusDivisor = divisor; private static ulong ComputeDivisorLimitFromMaxPrime(ulong maxPrime)
Console.WriteLine($"...processed by-divisor candidate = {divisor}"); {
} if (maxPrime <= 1UL)
{
return 0UL;
}
private static ulong ComputeDivisorLimitFromMaxPrime(ulong maxPrime) if (maxPrime - 1UL >= 64UL)
{ {
if (maxPrime <= 1UL) return ulong.MaxValue;
{ }
return 0UL;
}
if (maxPrime - 1UL >= 64UL) return (1UL << (int)(maxPrime - 1UL)) - 1UL;
{ }
return ulong.MaxValue;
}
return (1UL << (int)(maxPrime - 1UL)) - 1UL; private ulong ComputeAllowedMaxDivisor(ulong prime)
} {
if (prime <= 1UL)
{
return 0UL;
}
private ulong ComputeAllowedMaxDivisor(ulong prime) if (prime - 1UL >= 64UL)
{ {
if (prime <= 1UL) return _divisorLimit;
{ }
return 0UL;
}
if (prime - 1UL >= 64UL) ulong maxByPrime = (1UL << (int)(prime - 1UL)) - 1UL;
{ return Math.Min(maxByPrime, _divisorLimit);
return _divisorLimit; }
}
ulong maxByPrime = (1UL << (int)(prime - 1UL)) - 1UL; private static void CheckKernel(Index1D index, ulong prime, ArrayView<ulong> divisors, ArrayView<byte> hits)
return Math.Min(maxByPrime, _divisorLimit); {
} ulong divisor = divisors[index];
if (divisor <= 1UL || (divisor & 1UL) == 0UL)
{
hits[index] = 0;
return;
}
private static void CheckKernel(Index1D index, ulong prime, ArrayView<ulong> divisors, ArrayView<byte> hits) ulong pow = Pow2Mod(prime, divisor);
{ hits[index] = pow == 1UL ? (byte)1 : (byte)0;
ulong divisor = divisors[index]; }
if (divisor <= 1UL || (divisor & 1UL) == 0UL)
{
hits[index] = 0;
return;
}
ulong pow = Pow2Mod(prime, divisor); private static ulong Pow2Mod(ulong exponent, ulong modulus)
hits[index] = pow == 1UL ? (byte)1 : (byte)0; {
} if (modulus <= 1UL)
{
return 0UL;
}
private static ulong Pow2Mod(ulong exponent, ulong modulus) ulong result = 1UL % modulus;
{ ulong baseVal = 2UL % modulus;
if (modulus <= 1UL) ulong exp = exponent;
{ while (exp > 0UL)
return 0UL; {
} if ((exp & 1UL) != 0UL)
{
result = MulMod(result, baseVal, modulus);
}
ulong result = 1UL % modulus; exp >>= 1;
ulong baseVal = 2UL % modulus; if (exp == 0UL)
ulong exp = exponent; {
while (exp > 0UL) break;
{ }
if ((exp & 1UL) != 0UL)
{
result = MulMod(result, baseVal, modulus);
}
exp >>= 1; baseVal = MulMod(baseVal, baseVal, modulus);
if (exp == 0UL) }
{
break;
}
baseVal = MulMod(baseVal, baseVal, modulus); return result;
} }
return result; private static ulong MulMod(ulong a, ulong b, ulong modulus)
} {
if (modulus == 0UL)
{
return 0UL;
}
private static ulong MulMod(ulong a, ulong b, ulong modulus) ulong result = 0UL;
{ ulong x = a % modulus;
if (modulus == 0UL) ulong y = b;
{ while (y > 0UL)
return 0UL; {
} if ((y & 1UL) != 0UL)
{
result += x;
if (result >= modulus)
{
result -= modulus;
}
}
ulong result = 0UL; x <<= 1;
ulong x = a % modulus; if (x >= modulus)
ulong y = b; {
while (y > 0UL) x -= modulus;
{ }
if ((y & 1UL) != 0UL)
{
result += x;
if (result >= modulus)
{
result -= modulus;
}
}
x <<= 1; y >>= 1;
if (x >= modulus) }
{
x -= modulus;
}
y >>= 1; return result;
} }
return result;
}
} }
@@ -3,8 +3,9 @@ namespace PerfectNumbers.Core;
public static class PerfectNumberConstants public static class PerfectNumberConstants
{ {
public const ulong BiggestKnownEvenPerfectP = 136279841UL; public const ulong BiggestKnownEvenPerfectP = 136279841UL;
public static readonly uint PrimesLimit = 1_000_000; //(ulong)Array.MaxLength;// 1_000_000; public const int ConsoleInterval = 100_000;
public const int MaxQForDivisorCycles = 4_000_000;
public const ulong ExtraDivisorCycleSearchLimit = 64UL; public const ulong ExtraDivisorCycleSearchLimit = 64UL;
public const int MaxQForDivisorCycles = 4_000_000;
public static readonly uint PrimesLimit = 1_000_000; //(ulong)Array.MaxLength;// 1_000_000;
} }