Merge pull request #64 from mlemanczyk/codex/analyze-optimization-paths-for-evenperfectbitscanner

Make by-divisor divisor cycle filtering optional
This commit is contained in:
Marek Lemańczyk authored and GitHub committed 2025-09-22 16:42:43 +02:00
commit 5db0c4fa95
2 files changed
+84 -4

No files matched your search

+10
View File
@@ -103,6 +103,7 @@ internal static class Program
bool useResidue = false; // M_p test via residue divisors (replaces LL/incremental) bool useResidue = false; // M_p test via residue divisors (replaces LL/incremental)
bool useDivisor = false; // M_p divisibility by specific divisor bool useDivisor = false; // M_p divisibility by specific divisor
bool useByDivisor = false; // Iterative divisor scan across primes bool useByDivisor = false; // Iterative divisor scan across primes
bool useDivisorCycles = false;
UInt128 divisor = UInt128.Zero; UInt128 divisor = UInt128.Zero;
// Device routing // Device routing
bool useGpuCycles = true; bool useGpuCycles = true;
@@ -184,6 +185,13 @@ internal static class Program
{ {
divisor = UInt128.Parse(arg.AsSpan(arg.IndexOf('=') + 1)); divisor = UInt128.Parse(arg.AsSpan(arg.IndexOf('=') + 1));
} }
else if (arg.StartsWith("--use-divisor-cycles=", StringComparison.OrdinalIgnoreCase))
{
if (bool.TryParse(arg.AsSpan(arg.IndexOf('=') + 1), out bool parsedBool))
{
useDivisorCycles = parsedBool;
}
}
else if (arg.StartsWith("--divisor-cycles-limit=", StringComparison.OrdinalIgnoreCase)) else if (arg.StartsWith("--divisor-cycles-limit=", StringComparison.OrdinalIgnoreCase))
{ {
if (ulong.TryParse(arg.AsSpan(arg.IndexOf('=') + 1), out parsedLimit)) if (ulong.TryParse(arg.AsSpan(arg.IndexOf('=') + 1), out parsedLimit))
@@ -501,6 +509,7 @@ internal static class Program
_byDivisorTester = new MersenneNumberDivisorByDivisorGpuTester _byDivisorTester = new MersenneNumberDivisorByDivisorGpuTester
{ {
GpuBatchSize = scanBatchSize, GpuBatchSize = scanBatchSize,
UseDivisorCycles = useDivisorCycles,
}; };
} }
@@ -1277,6 +1286,7 @@ internal static class Program
Console.WriteLine(" --divisor-cycles-batch-size=<value> batch size for cycles generation (default 512)"); Console.WriteLine(" --divisor-cycles-batch-size=<value> batch size for cycles generation (default 512)");
Console.WriteLine(" --divisor-cycles-continue continue divisor cycles generation"); Console.WriteLine(" --divisor-cycles-continue continue divisor cycles generation");
Console.WriteLine(" --divisor-cycles-limit=<value> cycle search iterations when --mersenne=divisor"); Console.WriteLine(" --divisor-cycles-limit=<value> cycle search iterations when --mersenne=divisor");
Console.WriteLine(" --use-divisor-cycles=true|false enable divisor cycle filtering in --mersenne=bydivisor (default false)");
Console.WriteLine(" --use-order test primality via q order"); Console.WriteLine(" --use-order test primality via q order");
Console.WriteLine(" --workaround-mod avoid '%' operator on the GPU"); Console.WriteLine(" --workaround-mod avoid '%' operator on the GPU");
@@ -6,6 +6,7 @@ using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using ILGPU; using ILGPU;
using ILGPU.Runtime; using ILGPU.Runtime;
using PerfectNumbers.Core;
namespace PerfectNumbers.Core.Gpu; namespace PerfectNumbers.Core.Gpu;
@@ -16,6 +17,7 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
private ulong _divisorLimit; private ulong _divisorLimit;
private bool _isConfigured; private bool _isConfigured;
private ulong _lastStatusDivisor; private ulong _lastStatusDivisor;
private bool _useDivisorCycles;
public readonly struct MontgomeryDivisorData public readonly struct MontgomeryDivisorData
{ {
@@ -47,6 +49,12 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
set => _gpuBatchSize = Math.Max(1, value); set => _gpuBatchSize = Math.Max(1, value);
} }
public bool UseDivisorCycles
{
get => _useDivisorCycles;
set => _useDivisorCycles = value;
}
public void ConfigureFromMaxPrime(ulong maxPrime) public void ConfigureFromMaxPrime(ulong maxPrime)
{ {
lock (_sync) lock (_sync)
@@ -120,6 +128,8 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
try try
{ {
bool useCycles = _useDivisorCycles;
MersenneDivisorCycles? divisorCycles = useCycles ? MersenneDivisorCycles.Shared : null;
while (divisor <= allowedMax) while (divisor <= allowedMax)
{ {
int batchSize = 0; int batchSize = 0;
@@ -128,10 +138,24 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
while (batchSize < batchCapacity && divisor <= allowedMax) while (batchSize < batchCapacity && divisor <= allowedMax)
{ {
ulong current = divisor; ulong current = divisor;
divisors[batchSize++] = current;
ulong next = current + 2UL; ulong next = current + 2UL;
bool include = true;
if (useCycles)
{
ulong cycle = divisorCycles!.GetCycle(current);
if (cycle == 0UL || prime % cycle != 0UL)
{
include = false;
lastProcessed = current;
ReportStatus(lastProcessed); ReportStatus(lastProcessed);
}
}
if (include)
{
divisors[batchSize++] = current;
}
if (next <= current) if (next <= current)
{ {
reachedEndInBatch = true; reachedEndInBatch = true;
@@ -272,6 +296,7 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
private readonly MemoryBuffer1D<byte, Stride1D.Dense> _hitsBuffer; private readonly MemoryBuffer1D<byte, Stride1D.Dense> _hitsBuffer;
private readonly ulong[] _primesHost; private readonly ulong[] _primesHost;
private readonly byte[] _hitsHost; private readonly byte[] _hitsHost;
private readonly int[] _indexHost;
private bool _disposed; private bool _disposed;
internal DivisorScanSession(MersenneNumberDivisorByDivisorGpuTester owner) internal DivisorScanSession(MersenneNumberDivisorByDivisorGpuTester owner)
@@ -284,6 +309,7 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
_hitsBuffer = _accelerator.Allocate1D<byte>(owner._gpuBatchSize); _hitsBuffer = _accelerator.Allocate1D<byte>(owner._gpuBatchSize);
_primesHost = ArrayPool<ulong>.Shared.Rent(owner._gpuBatchSize); _primesHost = ArrayPool<ulong>.Shared.Rent(owner._gpuBatchSize);
_hitsHost = ArrayPool<byte>.Shared.Rent(owner._gpuBatchSize); _hitsHost = ArrayPool<byte>.Shared.Rent(owner._gpuBatchSize);
_indexHost = ArrayPool<int>.Shared.Rent(owner._gpuBatchSize);
} }
public void CheckDivisor(ulong divisor, ReadOnlySpan<ulong> primes, Span<byte> hits) public void CheckDivisor(ulong divisor, ReadOnlySpan<ulong> primes, Span<byte> hits)
@@ -300,6 +326,9 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
int offset = 0; int offset = 0;
int consoleStatus = 0; int consoleStatus = 0;
bool useCycles = _owner._useDivisorCycles;
MersenneDivisorCycles? divisorCycles = useCycles ? MersenneDivisorCycles.Shared : null;
ulong cycle = useCycles ? divisorCycles!.GetCycle(divisor) : 0UL;
MontgomeryDivisorData divisorData = CreateMontgomeryDivisorData(divisor); MontgomeryDivisorData divisorData = CreateMontgomeryDivisorData(divisor);
while (offset < primes.Length) while (offset < primes.Length)
{ {
@@ -313,6 +342,45 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
consoleStatus = 0; consoleStatus = 0;
} }
Span<int> indexSpan = _indexHost.AsSpan(0, batchSize);
Span<byte> hitSpan = _hitsHost.AsSpan(0, batchSize);
Span<byte> hitsSlice = hits.Slice(offset, batchSize);
int gpuCount = 0;
if (useCycles)
{
for (int i = 0; i < batchSize; i++)
{
ulong primeValue = primeSpan[i];
if (cycle == 0UL || primeValue % cycle != 0UL)
{
hitsSlice[i] = 0;
continue;
}
indexSpan[gpuCount] = i;
primeSpan[gpuCount] = primeValue;
gpuCount++;
}
if (gpuCount > 0)
{
var primesView = _primesBuffer.View.SubView(0, gpuCount);
var hitsView = _hitsBuffer.View.SubView(0, gpuCount);
primesView.CopyFromCPU(ref MemoryMarshal.GetReference(primeSpan.Slice(0, gpuCount)), gpuCount);
_kernel(gpuCount, divisorData, primesView, hitsView);
_accelerator.Synchronize();
hitsView.CopyToCPU(ref MemoryMarshal.GetReference(hitSpan.Slice(0, gpuCount)), gpuCount);
for (int i = 0; i < gpuCount; i++)
{
hitsSlice[indexSpan[i]] = hitSpan[i];
}
}
}
else
{
var primesView = _primesBuffer.View.SubView(0, batchSize); var primesView = _primesBuffer.View.SubView(0, batchSize);
var hitsView = _hitsBuffer.View.SubView(0, batchSize); var hitsView = _hitsBuffer.View.SubView(0, batchSize);
@@ -320,9 +388,10 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
_kernel(batchSize, divisorData, primesView, hitsView); _kernel(batchSize, divisorData, primesView, hitsView);
_accelerator.Synchronize(); _accelerator.Synchronize();
Span<byte> hitSpan = _hitsHost.AsSpan(0, batchSize);
hitsView.CopyToCPU(ref MemoryMarshal.GetReference(hitSpan), batchSize); hitsView.CopyToCPU(ref MemoryMarshal.GetReference(hitSpan), batchSize);
hitSpan.CopyTo(hits.Slice(offset, batchSize)); hitSpan.CopyTo(hitsSlice);
}
offset += batchSize; offset += batchSize;
} }
} }
@@ -337,6 +406,7 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
_disposed = true; _disposed = true;
ArrayPool<ulong>.Shared.Return(_primesHost, clearArray: true); ArrayPool<ulong>.Shared.Return(_primesHost, clearArray: true);
ArrayPool<byte>.Shared.Return(_hitsHost, clearArray: true); ArrayPool<byte>.Shared.Return(_hitsHost, clearArray: true);
ArrayPool<int>.Shared.Return(_indexHost, clearArray: true);
_primesBuffer.Dispose(); _primesBuffer.Dispose();
_hitsBuffer.Dispose(); _hitsBuffer.Dispose();
_lease.Dispose(); _lease.Dispose();