Fix residue tester filters and arithmetic utilities
This commit is contained in:
1 parent
2484d506d6
commit
615ae29fd6
4 files changed
+316
-218
No files matched your search
@@ -24,8 +24,9 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
|
|||||||
public readonly ulong MontgomeryTwo = montgomeryTwo;
|
public readonly ulong MontgomeryTwo = montgomeryTwo;
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly ConcurrentDictionary<Accelerator, Action<Index1D, ulong, ArrayView<MontgomeryDivisorData>, ArrayView<byte>>> _kernelCache = new();
|
private readonly ConcurrentDictionary<Accelerator, Action<Index1D, ulong, ArrayView<MontgomeryDivisorData>, ArrayView<byte>>> _kernelCache = new();
|
||||||
private readonly ConcurrentDictionary<Accelerator, Action<Index1D, MontgomeryDivisorData, ArrayView<ulong>, ArrayView<byte>>> _kernelByPrimeCache = new();
|
private readonly ConcurrentDictionary<Accelerator, Action<Index1D, MontgomeryDivisorData, ArrayView<ulong>, ArrayView<byte>>> _kernelByPrimeCache = new();
|
||||||
|
private readonly ConcurrentBag<DivisorScanSession> _sessionPool = new();
|
||||||
|
|
||||||
private Action<Index1D, ulong, ArrayView<MontgomeryDivisorData>, ArrayView<byte>> GetKernel(Accelerator accelerator) =>
|
private Action<Index1D, ulong, ArrayView<MontgomeryDivisorData>, ArrayView<byte>> GetKernel(Accelerator accelerator) =>
|
||||||
_kernelCache.GetOrAdd(accelerator, acc => acc.LoadAutoGroupedStreamKernel<Index1D, ulong, ArrayView<MontgomeryDivisorData>, ArrayView<byte>>(CheckKernel));
|
_kernelCache.GetOrAdd(accelerator, acc => acc.LoadAutoGroupedStreamKernel<Index1D, ulong, ArrayView<MontgomeryDivisorData>, ArrayView<byte>>(CheckKernel));
|
||||||
@@ -56,76 +57,125 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
|
|||||||
|
|
||||||
public bool IsPrime(ulong prime, out bool divisorsExhausted)
|
public bool IsPrime(ulong prime, out bool divisorsExhausted)
|
||||||
{
|
{
|
||||||
if (!_isConfigured)
|
ulong allowedMax;
|
||||||
{
|
bool useCycles;
|
||||||
throw new InvalidOperationException("ConfigureFromMaxPrime must be called before using the tester.");
|
int batchCapacity;
|
||||||
}
|
|
||||||
|
|
||||||
ulong allowedMax = ComputeAllowedMaxDivisor(prime);
|
lock (_sync)
|
||||||
if (allowedMax < 3UL)
|
{
|
||||||
{
|
if (!_isConfigured)
|
||||||
divisorsExhausted = true;
|
{
|
||||||
return true;
|
throw new InvalidOperationException("ConfigureFromMaxPrime must be called before using the tester.");
|
||||||
}
|
}
|
||||||
|
|
||||||
bool composite, coveredRange;
|
allowedMax = ComputeAllowedMaxDivisor(prime, _divisorLimit);
|
||||||
|
useCycles = _useDivisorCycles;
|
||||||
|
batchCapacity = _gpuBatchSize;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Let's rework this to check if the number is prime and check divisors outside of the lock. Lock and update the state afterwards.
|
if (allowedMax < 3UL)
|
||||||
lock (_sync)
|
{
|
||||||
{
|
divisorsExhausted = true;
|
||||||
composite = CheckDivisors(prime, allowedMax, out ulong lastProcessed, out coveredRange);
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
if (lastProcessed != 0UL)
|
bool composite;
|
||||||
{
|
bool coveredRange;
|
||||||
ReportStatus(lastProcessed);
|
ulong processedCount;
|
||||||
}
|
ulong lastProcessed;
|
||||||
}
|
|
||||||
|
|
||||||
if (composite)
|
var gpuLease = GpuContextPool.RentPreferred(preferCpu: false);
|
||||||
{
|
var accelerator = gpuLease.Accelerator;
|
||||||
divisorsExhausted = true;
|
var kernel = GetKernel(accelerator);
|
||||||
return false;
|
|
||||||
|
var divisorsBuffer = accelerator.Allocate1D<MontgomeryDivisorData>(batchCapacity);
|
||||||
|
var hitsBuffer = accelerator.Allocate1D<byte>(batchCapacity);
|
||||||
|
ulong[] divisors = ArrayPool<ulong>.Shared.Rent(batchCapacity);
|
||||||
|
byte[] hits = ArrayPool<byte>.Shared.Rent(batchCapacity);
|
||||||
|
MontgomeryDivisorData[] divisorData = ArrayPool<MontgomeryDivisorData>.Shared.Rent(batchCapacity);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
composite = CheckDivisors(
|
||||||
|
prime,
|
||||||
|
allowedMax,
|
||||||
|
useCycles,
|
||||||
|
accelerator,
|
||||||
|
kernel,
|
||||||
|
divisorsBuffer,
|
||||||
|
hitsBuffer,
|
||||||
|
divisors,
|
||||||
|
hits,
|
||||||
|
divisorData,
|
||||||
|
out lastProcessed,
|
||||||
|
out coveredRange,
|
||||||
|
out processedCount
|
||||||
|
);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
ArrayPool<byte>.Shared.Return(hits, clearArray: true);
|
||||||
|
ArrayPool<ulong>.Shared.Return(divisors, clearArray: true);
|
||||||
|
ArrayPool<MontgomeryDivisorData>.Shared.Return(divisorData, clearArray: true);
|
||||||
|
hitsBuffer.Dispose();
|
||||||
|
divisorsBuffer.Dispose();
|
||||||
|
gpuLease.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (processedCount > 0UL)
|
||||||
|
{
|
||||||
|
lock (_sync)
|
||||||
|
{
|
||||||
|
UpdateStatusUnsafe(lastProcessed, processedCount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (composite)
|
||||||
|
{
|
||||||
|
divisorsExhausted = true;
|
||||||
|
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,
|
||||||
// TODO: We should be able to calculate allowedMax outside in the parent method, unless it depends from some changing value.
|
ulong allowedMax,
|
||||||
allowedMax = Math.Min(allowedMax, _divisorLimit);
|
bool useCycles,
|
||||||
lastProcessed = 0UL;
|
Accelerator accelerator,
|
||||||
|
Action<Index1D, ulong, ArrayView<MontgomeryDivisorData>, ArrayView<byte>> kernel,
|
||||||
|
MemoryBuffer1D<MontgomeryDivisorData, Stride1D.Dense> divisorsBuffer,
|
||||||
|
MemoryBuffer1D<byte, Stride1D.Dense> hitsBuffer,
|
||||||
|
ulong[] divisors,
|
||||||
|
byte[] hits,
|
||||||
|
MontgomeryDivisorData[] divisorData,
|
||||||
|
out ulong lastProcessed,
|
||||||
|
out bool coveredRange,
|
||||||
|
out ulong processedCount)
|
||||||
|
{
|
||||||
|
lastProcessed = 0UL;
|
||||||
|
processedCount = 0UL;
|
||||||
|
|
||||||
if (allowedMax < 3UL)
|
if (allowedMax < 3UL)
|
||||||
{
|
{
|
||||||
coveredRange = true;
|
coveredRange = true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Rent these in the parent method before the loops - batch size doesn't change
|
int batchCapacity = (int)divisorsBuffer.Length;
|
||||||
var gpuLease = GpuContextPool.RentPreferred(preferCpu: false);
|
|
||||||
var accelerator = gpuLease.Accelerator;
|
|
||||||
var kernel = GetKernel(accelerator);
|
|
||||||
|
|
||||||
int batchCapacity = _gpuBatchSize;
|
bool composite = false;
|
||||||
// TODO: Rent buffers in the parent method before the loops - batch size doesn't change
|
bool processedAll = false;
|
||||||
var divisorsBuffer = accelerator.Allocate1D<MontgomeryDivisorData>(batchCapacity);
|
ulong cycle, divisor = 3UL, next;
|
||||||
var hitsBuffer = accelerator.Allocate1D<byte>(batchCapacity);
|
int batchSize, i;
|
||||||
ulong[] divisors = ArrayPool<ulong>.Shared.Rent(batchCapacity);
|
bool reachedEndInBatch;
|
||||||
byte[] hits = ArrayPool<byte>.Shared.Rent(batchCapacity);
|
|
||||||
MontgomeryDivisorData[] divisorData = ArrayPool<MontgomeryDivisorData>.Shared.Rent(batchCapacity);
|
|
||||||
|
|
||||||
bool composite = false;
|
Span<MontgomeryDivisorData> divisorDataSpan;
|
||||||
bool processedAll = false;
|
Span<ulong> divisorSpan;
|
||||||
ulong cycle, divisor = 3UL, next;
|
Span<byte> hitsSpan;
|
||||||
int batchSize, i;
|
MersenneDivisorCycles divisorCycles = MersenneDivisorCycles.Shared;
|
||||||
bool reachedEndInBatch, useCycles = _useDivisorCycles;
|
|
||||||
|
|
||||||
Span<MontgomeryDivisorData> divisorDataSpan;
|
|
||||||
Span<ulong> divisorSpan;
|
|
||||||
Span<byte> hitsSpan;
|
|
||||||
MersenneDivisorCycles divisorCycles = MersenneDivisorCycles.Shared;
|
|
||||||
ArrayView1D<MontgomeryDivisorData, Stride1D.Dense> divisorView;
|
ArrayView1D<MontgomeryDivisorData, Stride1D.Dense> divisorView;
|
||||||
ArrayView1D<byte, Stride1D.Dense> hitsView;
|
ArrayView1D<byte, Stride1D.Dense> hitsView;
|
||||||
|
|
||||||
@@ -134,31 +184,45 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
|
|||||||
batchSize = 0;
|
batchSize = 0;
|
||||||
reachedEndInBatch = false;
|
reachedEndInBatch = false;
|
||||||
|
|
||||||
while (batchSize < batchCapacity && divisor <= allowedMax)
|
while (batchSize < batchCapacity && divisor <= allowedMax)
|
||||||
{
|
{
|
||||||
if (useCycles)
|
ulong currentDivisor = divisor;
|
||||||
{
|
ulong nextDivisor = currentDivisor + 2UL;
|
||||||
cycle = divisorCycles!.GetCycle(divisor);
|
processedCount++;
|
||||||
if (cycle == 0UL || prime % cycle != 0UL)
|
|
||||||
{
|
|
||||||
ReportStatus(divisor);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
divisors[batchSize++] = divisor;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
divisors[batchSize++] = divisor;
|
|
||||||
}
|
|
||||||
|
|
||||||
next = divisor + 2UL;
|
if (useCycles)
|
||||||
|
{
|
||||||
|
cycle = divisorCycles!.GetCycle(currentDivisor);
|
||||||
|
if (cycle == 0UL || prime % cycle != 0UL)
|
||||||
|
{
|
||||||
|
lastProcessed = currentDivisor;
|
||||||
|
|
||||||
// This will be only true when we exceed ulong.MaxValue
|
if (nextDivisor <= currentDivisor)
|
||||||
if (next <= divisor)
|
{
|
||||||
{
|
reachedEndInBatch = true;
|
||||||
reachedEndInBatch = true;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nextDivisor > allowedMax)
|
||||||
|
{
|
||||||
|
divisor = nextDivisor;
|
||||||
|
reachedEndInBatch = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
divisor = nextDivisor;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
divisors[batchSize++] = currentDivisor;
|
||||||
|
lastProcessed = currentDivisor;
|
||||||
|
next = nextDivisor;
|
||||||
|
|
||||||
|
// This will be only true when we exceed ulong.MaxValue
|
||||||
|
if (next <= divisor)
|
||||||
|
{
|
||||||
|
reachedEndInBatch = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -172,10 +236,15 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
|
|||||||
divisor = next;
|
divisor = next;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (batchSize == 0)
|
if (batchSize == 0)
|
||||||
{
|
{
|
||||||
break;
|
if (reachedEndInBatch)
|
||||||
}
|
{
|
||||||
|
processedAll = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
divisorDataSpan = divisorData.AsSpan(0, batchSize);
|
divisorDataSpan = divisorData.AsSpan(0, batchSize);
|
||||||
divisorView = divisorsBuffer.View.SubView(0, batchSize);
|
divisorView = divisorsBuffer.View.SubView(0, batchSize);
|
||||||
@@ -194,16 +263,20 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
|
|||||||
accelerator.Synchronize();
|
accelerator.Synchronize();
|
||||||
hitsView.CopyToCPU(ref MemoryMarshal.GetReference(hitsSpan), batchSize);
|
hitsView.CopyToCPU(ref MemoryMarshal.GetReference(hitsSpan), batchSize);
|
||||||
|
|
||||||
for (i = 0; i < batchSize; i++)
|
for (i = 0; i < batchSize; i++)
|
||||||
{
|
{
|
||||||
if (hitsSpan[i] != 0)
|
if (hitsSpan[i] != 0)
|
||||||
{
|
{
|
||||||
composite = true;
|
composite = true;
|
||||||
break;
|
lastProcessed = divisorSpan[i];
|
||||||
}
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ReportStatus(divisorSpan[i < batchSize ? i : i - 1]);
|
if (!composite)
|
||||||
|
{
|
||||||
|
lastProcessed = divisorSpan[batchSize - 1];
|
||||||
|
}
|
||||||
if (composite)
|
if (composite)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
@@ -216,26 +289,10 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
coveredRange = composite || processedAll || divisor > allowedMax;
|
coveredRange = composite || processedAll || divisor > allowedMax;
|
||||||
|
|
||||||
ArrayPool<byte>.Shared.Return(hits, clearArray: true);
|
return composite;
|
||||||
ArrayPool<ulong>.Shared.Return(divisors, clearArray: true);
|
}
|
||||||
ArrayPool<MontgomeryDivisorData>.Shared.Return(divisorData, clearArray: true);
|
|
||||||
hitsBuffer.Dispose();
|
|
||||||
divisorsBuffer.Dispose();
|
|
||||||
gpuLease.Dispose();
|
|
||||||
|
|
||||||
return composite;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ReportStatus(ulong divisor)
|
|
||||||
{
|
|
||||||
if (++_lastStatusDivisor == PerfectNumberConstants.ConsoleInterval)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"...processed by-divisor candidate = {divisor}");
|
|
||||||
_lastStatusDivisor = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static ulong ComputeDivisorLimitFromMaxPrime(ulong maxPrime)
|
private static ulong ComputeDivisorLimitFromMaxPrime(ulong maxPrime)
|
||||||
{
|
{
|
||||||
@@ -252,20 +309,44 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
|
|||||||
return (1UL << (int)(maxPrime - 1UL)) - 1UL;
|
return (1UL << (int)(maxPrime - 1UL)) - 1UL;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ulong ComputeAllowedMaxDivisor(ulong prime)
|
private static ulong ComputeAllowedMaxDivisor(ulong prime, ulong divisorLimit)
|
||||||
{
|
{
|
||||||
if (prime <= 1UL)
|
if (prime <= 1UL)
|
||||||
{
|
{
|
||||||
return 0UL;
|
return 0UL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prime - 1UL >= 64UL)
|
if (prime - 1UL >= 64UL)
|
||||||
{
|
{
|
||||||
return _divisorLimit;
|
return divisorLimit;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Math.Min((1UL << (int)(prime - 1UL)) - 1UL, _divisorLimit);
|
return Math.Min((1UL << (int)(prime - 1UL)) - 1UL, divisorLimit);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void UpdateStatusUnsafe(ulong lastProcessed, ulong processedCount)
|
||||||
|
{
|
||||||
|
if (processedCount == 0UL)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ulong interval = (ulong)PerfectNumberConstants.ConsoleInterval;
|
||||||
|
if (interval == 0UL)
|
||||||
|
{
|
||||||
|
_lastStatusDivisor = 0UL;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ulong total = _lastStatusDivisor + processedCount;
|
||||||
|
ulong messages = total / interval;
|
||||||
|
_lastStatusDivisor = total % interval;
|
||||||
|
|
||||||
|
for (ulong i = 0; i < messages; i++)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"...processed by-divisor candidate = {lastProcessed}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static void CheckKernel(Index1D index, ulong prime, ArrayView<MontgomeryDivisorData> divisors, ArrayView<byte> hits)
|
private static void CheckKernel(Index1D index, ulong prime, ArrayView<MontgomeryDivisorData> divisors, ArrayView<byte> hits)
|
||||||
{
|
{
|
||||||
@@ -280,37 +361,41 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
|
|||||||
hits[index] = Pow2Mod(prime, divisor) == 1UL ? (byte)1 : (byte)0;
|
hits[index] = Pow2Mod(prime, divisor) == 1UL ? (byte)1 : (byte)0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Let's create a pool for these and let's rent them and return, when not needed
|
public sealed class DivisorScanSession : IDisposable
|
||||||
public sealed class DivisorScanSession : IDisposable
|
{
|
||||||
{
|
private readonly MersenneNumberDivisorByDivisorGpuTester _owner;
|
||||||
private readonly MersenneNumberDivisorByDivisorGpuTester _owner;
|
private readonly GpuContextPool.GpuContextLease _lease;
|
||||||
private readonly GpuContextPool.GpuContextLease _lease;
|
private readonly Accelerator _accelerator;
|
||||||
private readonly Accelerator _accelerator;
|
|
||||||
private readonly Action<Index1D, MontgomeryDivisorData, ArrayView<ulong>, ArrayView<byte>> _kernel;
|
private readonly Action<Index1D, MontgomeryDivisorData, ArrayView<ulong>, ArrayView<byte>> _kernel;
|
||||||
private readonly MemoryBuffer1D<ulong, Stride1D.Dense> _primesBuffer;
|
private readonly MemoryBuffer1D<ulong, Stride1D.Dense> _primesBuffer;
|
||||||
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 readonly int[] _indexHost;
|
||||||
private bool _disposed;
|
private bool _disposed;
|
||||||
|
|
||||||
internal DivisorScanSession(MersenneNumberDivisorByDivisorGpuTester owner)
|
internal DivisorScanSession(MersenneNumberDivisorByDivisorGpuTester owner)
|
||||||
{
|
{
|
||||||
_owner = owner;
|
_owner = owner;
|
||||||
_lease = GpuContextPool.RentPreferred(preferCpu: false);
|
_lease = GpuContextPool.RentPreferred(preferCpu: false);
|
||||||
_accelerator = _lease.Accelerator;
|
_accelerator = _lease.Accelerator;
|
||||||
_kernel = owner.GetKernelByPrime(_accelerator);
|
_kernel = owner.GetKernelByPrime(_accelerator);
|
||||||
_primesBuffer = _accelerator.Allocate1D<ulong>(owner._gpuBatchSize);
|
_primesBuffer = _accelerator.Allocate1D<ulong>(owner._gpuBatchSize);
|
||||||
_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);
|
_indexHost = ArrayPool<int>.Shared.Rent(owner._gpuBatchSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void CheckDivisor(ulong divisor, ReadOnlySpan<ulong> primes, Span<byte> hits)
|
internal void Reset()
|
||||||
{
|
{
|
||||||
if (_disposed)
|
_disposed = false;
|
||||||
{
|
}
|
||||||
|
|
||||||
|
public void CheckDivisor(ulong divisor, ReadOnlySpan<ulong> primes, Span<byte> hits)
|
||||||
|
{
|
||||||
|
if (_disposed)
|
||||||
|
{
|
||||||
throw new ObjectDisposedException(nameof(DivisorScanSession));
|
throw new ObjectDisposedException(nameof(DivisorScanSession));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -401,24 +486,24 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
|
|||||||
|
|
||||||
offset += batchSize;
|
offset += batchSize;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
if (_disposed)
|
if (_disposed)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_disposed = true;
|
_disposed = true;
|
||||||
ArrayPool<ulong>.Shared.Return(_primesHost, clearArray: true);
|
_owner.ReturnSession(this);
|
||||||
ArrayPool<byte>.Shared.Return(_hitsHost, clearArray: true);
|
}
|
||||||
ArrayPool<int>.Shared.Return(_indexHost, clearArray: true);
|
}
|
||||||
_primesBuffer.Dispose();
|
|
||||||
_hitsBuffer.Dispose();
|
internal void ReturnSession(DivisorScanSession session)
|
||||||
_lease.Dispose();
|
{
|
||||||
}
|
_sessionPool.Add(session);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ulong Pow2Mod(ulong exponent, in MontgomeryDivisorData divisor)
|
private static ulong Pow2Mod(ulong exponent, in MontgomeryDivisorData divisor)
|
||||||
{
|
{
|
||||||
@@ -539,26 +624,32 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
|
|||||||
{
|
{
|
||||||
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.");
|
||||||
}
|
}
|
||||||
|
|
||||||
return ComputeAllowedMaxDivisor(prime);
|
return ComputeAllowedMaxDivisor(prime, _divisorLimit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public DivisorScanSession CreateDivisorSession()
|
public DivisorScanSession CreateDivisorSession()
|
||||||
{
|
{
|
||||||
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.");
|
||||||
}
|
}
|
||||||
|
|
||||||
return new DivisorScanSession(this);
|
if (_sessionPool.TryTake(out DivisorScanSession? session))
|
||||||
}
|
{
|
||||||
}
|
session.Reset();
|
||||||
|
return session;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new DivisorScanSession(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -21,13 +21,12 @@ public class MersenneNumberResidueGpuTester(bool useGpuOrder)
|
|||||||
var smallCyclesView = GpuKernelPool.EnsureSmallCyclesOnDevice(accelerator);
|
var smallCyclesView = GpuKernelPool.EnsureSmallCyclesOnDevice(accelerator);
|
||||||
ResiduePrimeViews primeViews = GpuKernelPool.EnsureSmallPrimesOnDevice(accelerator);
|
ResiduePrimeViews primeViews = GpuKernelPool.EnsureSmallPrimesOnDevice(accelerator);
|
||||||
int batchSize = GpuConstants.ScanBatchSize;
|
int batchSize = GpuConstants.ScanBatchSize;
|
||||||
UInt128 kStart = 1UL;
|
UInt128 kStart = 1UL;
|
||||||
|
UInt128 limit = maxK + UInt128.One;
|
||||||
byte last = lastIsSeven ? (byte)1 : (byte)0;
|
byte last = lastIsSeven ? (byte)1 : (byte)0;
|
||||||
var kernel = gpuLease.Pow2ModKernel;
|
var kernel = gpuLease.Pow2ModKernel;
|
||||||
ulong step3 = exponent % 3UL,
|
twoP.Mod10_8_5_3(out ulong step10, out ulong step8, out ulong step5, out ulong step3);
|
||||||
step5 = exponent % 5UL,
|
step10 = step10.Mod10();
|
||||||
step8 = exponent % 8UL,
|
|
||||||
step10 = exponent.Mod10();
|
|
||||||
|
|
||||||
GpuUInt128 twoPGpu = (GpuUInt128)twoP;
|
GpuUInt128 twoPGpu = (GpuUInt128)twoP;
|
||||||
|
|
||||||
@@ -41,14 +40,14 @@ public class MersenneNumberResidueGpuTester(bool useGpuOrder)
|
|||||||
orderBuffer.MemSetToZero();
|
orderBuffer.MemSetToZero();
|
||||||
ulong[] orderArray = ArrayPool<ulong>.Shared.Rent(batchSize);
|
ulong[] orderArray = ArrayPool<ulong>.Shared.Rent(batchSize);
|
||||||
UInt128 batchSize128 = (UInt128)batchSize;
|
UInt128 batchSize128 = (UInt128)batchSize;
|
||||||
UInt128 fullBatchStep = twoP * batchSize128;
|
UInt128 fullBatchStep = twoP * batchSize128;
|
||||||
UInt128 q = twoP * kStart + UInt128.One;
|
UInt128 q = twoP * kStart + UInt128.One;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
while (kStart < maxK && Volatile.Read(ref isPrime))
|
while (kStart < limit && Volatile.Read(ref isPrime))
|
||||||
{
|
{
|
||||||
UInt128 remaining = maxK - kStart;
|
UInt128 remaining = limit - kStart;
|
||||||
int currentSize = remaining > batchSize128 ? batchSize : (int)remaining;
|
int currentSize = remaining > batchSize128 ? batchSize : (int)remaining;
|
||||||
Span<ulong> orders = orderArray.AsSpan(0, currentSize);
|
Span<ulong> orders = orderArray.AsSpan(0, currentSize);
|
||||||
ref ulong ordersRef = ref MemoryMarshal.GetReference(orders);
|
ref ulong ordersRef = ref MemoryMarshal.GetReference(orders);
|
||||||
@@ -60,7 +59,7 @@ public class MersenneNumberResidueGpuTester(bool useGpuOrder)
|
|||||||
kernelArgs, orderBuffer.View, smallCyclesView, primeViews.LastOne, primeViews.LastSeven, primeViews.LastOnePow2, primeViews.LastSevenPow2);
|
kernelArgs, orderBuffer.View, smallCyclesView, primeViews.LastOne, primeViews.LastSeven, primeViews.LastOnePow2, primeViews.LastSevenPow2);
|
||||||
|
|
||||||
accelerator.Synchronize();
|
accelerator.Synchronize();
|
||||||
orderBuffer.View.CopyToCPU(ref ordersRef, currentSize);
|
orderBuffer.View.CopyToCPU(ref ordersRef, currentSize);
|
||||||
if (!Volatile.Read(ref isPrime))
|
if (!Volatile.Read(ref isPrime))
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
@@ -83,11 +82,11 @@ public class MersenneNumberResidueGpuTester(bool useGpuOrder)
|
|||||||
}
|
}
|
||||||
|
|
||||||
UInt128 processed = (UInt128)currentSize;
|
UInt128 processed = (UInt128)currentSize;
|
||||||
kStart += processed;
|
kStart += processed;
|
||||||
if (kStart < maxK)
|
if (kStart < limit)
|
||||||
{
|
{
|
||||||
if (processed == batchSize128)
|
if (processed == batchSize128)
|
||||||
{
|
{
|
||||||
q += fullBatchStep;
|
q += fullBatchStep;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -146,27 +146,34 @@ public static class UInt128Extensions
|
|||||||
};
|
};
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static ulong Mod7(this UInt128 value)
|
public static ulong Mod7(this UInt128 value)
|
||||||
{
|
{
|
||||||
ulong remainder = (((ulong)value) % 7UL) + (((ulong)(value >> 64)) % 7UL) << 1;
|
ulong low = (ulong)value % 7UL;
|
||||||
while (remainder >= 7UL)
|
ulong high = (ulong)(value >> 64) % 7UL;
|
||||||
{
|
ulong remainder = low + (high * 2UL);
|
||||||
remainder -= 7UL;
|
if (remainder >= 7UL)
|
||||||
}
|
{
|
||||||
|
remainder -= 7UL;
|
||||||
|
if (remainder >= 7UL)
|
||||||
|
{
|
||||||
|
remainder -= 7UL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return remainder;
|
return remainder;
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static ulong Mod8(this UInt128 value) => (ulong)value & 7UL;
|
public static ulong Mod8(this UInt128 value) => (ulong)value & 7UL;
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static ulong Mod10(this UInt128 value128)
|
public static ulong Mod10(this UInt128 value128)
|
||||||
{
|
{
|
||||||
// Split and fold under mod 10: 2^64 ≡ 6 (mod 10)
|
// Split and fold under mod 10: 2^64 ≡ 6 (mod 10)
|
||||||
ulong value = (ulong)value128;
|
ulong low = (ulong)value128;
|
||||||
return ((value % 10UL) + ((value >> 64) % 10UL) * 6UL) % 10UL;
|
ulong high = (ulong)(value128 >> 64);
|
||||||
}
|
return ((low % 10UL) + ((high % 10UL) * 6UL)) % 10UL;
|
||||||
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static void Mod10_8_5_3(this UInt128 value, out ulong mod10, out ulong mod8, out ulong mod5, out ulong mod3)
|
public static void Mod10_8_5_3(this UInt128 value, out ulong mod10, out ulong mod8, out ulong mod5, out ulong mod3)
|
||||||
|
|||||||
@@ -46,16 +46,17 @@ public static class ULongExtensions
|
|||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static ulong FastDiv64(this ulong value, ulong divisor, ulong mul)
|
public static ulong FastDiv64(this ulong value, ulong divisor, ulong mul)
|
||||||
{
|
{
|
||||||
mul = value.MulHigh(mul);
|
ulong quotient = (ulong)(((UInt128)value * mul) >> 64);
|
||||||
if ((UInt128)value - mul.Mul64(divisor) >= divisor)
|
UInt128 remainder = (UInt128)value - ((UInt128)quotient * divisor);
|
||||||
{
|
if (remainder >= divisor)
|
||||||
mul++;
|
{
|
||||||
}
|
quotient++;
|
||||||
|
}
|
||||||
|
|
||||||
return mul;
|
return quotient;
|
||||||
}
|
}
|
||||||
|
|
||||||
public const ulong WordBitMask = 0xFFFFUL;
|
public const ulong WordBitMask = 0xFFFFUL;
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user