TODO updated and small cleanups

This commit is contained in:
mlemanczyk committed 2025-10-18 22:31:36 +02:00
1 parent 4ac2ef1e6e
commit 98afb3ff9e
2 files changed
+16 -21

No files matched your search

@@ -1,12 +1,11 @@
using System.Buffers.Text;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using PerfectNumbers.Core;
namespace EvenPerfectBitScanner;
// TODO: Convert this struct to an equivalent of a static class. Make all fields / properties / methods static and initialize it during program startup. Don't check if it's initialized within. Rely on the flow enforcing it.
internal struct CalculationResultHandler
{
private const string ResultsHeader = "p,searchedMersenne,detailedCheck,passedAllTests";
@@ -26,8 +25,8 @@ internal struct CalculationResultHandler
private int _primeCount;
private bool _primeFoundAfterInit;
private bool IsInitialized => _sync is not null;
private bool HasFileSync => _fileWriteSync is not null;
private readonly bool IsInitialized => _sync is not null;
private readonly bool HasFileSync => _fileWriteSync is not null;
internal CalculationResultHandler(string resultsFileName, int writeBatchSize)
{
@@ -43,7 +42,7 @@ internal struct CalculationResultHandler
_primeFoundAfterInit = false;
}
internal string ResultsFileName => _resultsFileName;
internal readonly string ResultsFileName => _resultsFileName;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void InitializeOutputBuffer()
@@ -262,7 +261,7 @@ internal struct CalculationResultHandler
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int TryFormatResultLine(Span<byte> destination, ulong currentP, bool searchedMersenne, bool detailedCheck, bool passedAllTests)
private readonly int TryFormatResultLine(Span<byte> destination, ulong currentP, bool searchedMersenne, bool detailedCheck, bool passedAllTests)
{
int offset = 0;
+11 -15
View File
@@ -276,7 +276,7 @@ internal static class Program
orderWarmupLimitOverride ?? 5_000_000UL,
NttGpuMath.ReductionMode,
mersenneOnGpu ? "gpu" : "cpu",
(GpuContextPool.ForceCpu ? "cpu" : "gpu"),
GpuContextPool.ForceCpu ? "cpu" : "gpu",
orderOnGpu ? "gpu" : "cpu");
if (!string.IsNullOrEmpty(_cliArguments.ResultsPrefix))
@@ -418,6 +418,7 @@ internal static class Program
GpuPrimeWorkLimiter.SetLimit(gpuPrimeThreads);
// Configure batch size for GPU primality sieve
PrimeTester.GpuBatchSize = gpuPrimeBatch;
CalculationResultHandler resultHandler = _resultHandler;
Stopwatch? stopwatch = null;
if (testMode)
@@ -436,7 +437,7 @@ internal static class Program
_byDivisorStartPrime,
static () => _lastCompositeP = true,
static () => _lastCompositeP = false,
PrintResult,
static (candidate, searchedMersenne, detailedCheck, passedAllTests) => _resultHandler.HandleResult(candidate, searchedMersenne, detailedCheck, passedAllTests, _lastCompositeP),
threadCount);
_resultHandler.FlushBuffer();
_resultHandler.ReleaseOutputBuffer();
@@ -494,7 +495,7 @@ internal static class Program
{
p = buffer[j];
passedAllTests = IsEvenPerfectCandidate(p, divisorCyclesSearchLimit, out searchedMersenne, out detailedCheck);
PrintResult(p, searchedMersenne, detailedCheck, passedAllTests);
resultHandler.HandleResult(p, searchedMersenne, detailedCheck, passedAllTests, _lastCompositeP);
}
continue;
@@ -517,7 +518,7 @@ internal static class Program
}
passedAllTests = IsEvenPerfectCandidate(p, divisorCyclesSearchLimit, out searchedMersenne, out detailedCheck);
PrintResult(p, searchedMersenne, detailedCheck, passedAllTests);
resultHandler.HandleResult(p, searchedMersenne, detailedCheck, passedAllTests, _lastCompositeP);
if (p == maxP)
{
@@ -715,6 +716,7 @@ internal static class Program
}
// TODO: Move this to CliArguments
private static void PrintHelp()
{
Console.WriteLine("Usage: EvenPerfectBitScanner [options]");
@@ -750,7 +752,6 @@ internal static class Program
Console.WriteLine(" --divisor-cycles-continue continue divisor cycles generation");
Console.WriteLine(" --divisor-cycles-limit=<value> cycle search iterations when --mersenne=divisor");
Console.WriteLine(" --use-order test primality via q order");
// mod-automaton removed
Console.WriteLine(" --filter-p=<path> process only p from previous run results (required for --mersenne=bydivisor)");
Console.WriteLine(" --write-batch-size=<value> overwrite frequency of disk writes (default 100 lines)");
Console.WriteLine(" --gcd-filter enable early sieve based on GCD");
@@ -895,16 +896,6 @@ internal static class Program
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void PrintResult(ulong currentP, bool searchedMersenne, bool detailedCheck, bool passedAllTests)
{
bool lastWasComposite = _lastCompositeP;
_resultHandler.HandleResult(currentP, searchedMersenne, detailedCheck, passedAllTests, lastWasComposite);
}
private static void ReportTestTime(TimeSpan elapsed)
{
double elapsedSeconds = elapsed.TotalSeconds;
@@ -1242,6 +1233,7 @@ internal static class Program
return false;
}
// TODO: Convert this to extension method in ULongExtensions if one doesn't already exist. We may already calculate things like these
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void ComputeBitStats(ulong value, out int bitLen, out int zeroCount, out int maxZeroBlock)
{
@@ -1403,9 +1395,11 @@ internal static class Program
}
}
// TODO: Convert this to extension method in ULongExtensions if one doesn't already exist
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool IsCompositeByGcd(ulong p)
{
// TODO: Can this ever be true on Program's execution path?
if (p < 2)
{
return true;
@@ -1415,9 +1409,11 @@ internal static class Program
return BinaryGcd(p, m) > 1UL;
}
// TODO: Convert this to extension method in ULongExtensions if one doesn't already exist
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ulong BinaryGcd(ulong a, ulong b)
{
// TODO: Are these a & b conditions ever met on Program's execution path?
if (a == 0UL)
{
return b;