Tiny cleanup
This commit is contained in:
1 parent
d472d48a84
commit
a4bda0f0ee
1 file changed
+254
-254
@@ -24,9 +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 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));
|
||||||
@@ -57,125 +57,125 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
|
|||||||
|
|
||||||
public bool IsPrime(ulong prime, out bool divisorsExhausted)
|
public bool IsPrime(ulong prime, out bool divisorsExhausted)
|
||||||
{
|
{
|
||||||
ulong allowedMax;
|
ulong allowedMax;
|
||||||
bool useCycles;
|
bool useCycles;
|
||||||
int batchCapacity;
|
int batchCapacity;
|
||||||
|
|
||||||
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.");
|
||||||
}
|
}
|
||||||
|
|
||||||
allowedMax = ComputeAllowedMaxDivisor(prime, _divisorLimit);
|
allowedMax = ComputeAllowedMaxDivisor(prime, _divisorLimit);
|
||||||
useCycles = _useDivisorCycles;
|
useCycles = _useDivisorCycles;
|
||||||
batchCapacity = _gpuBatchSize;
|
batchCapacity = _gpuBatchSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (allowedMax < 3UL)
|
if (allowedMax < 3UL)
|
||||||
{
|
{
|
||||||
divisorsExhausted = true;
|
divisorsExhausted = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool composite;
|
bool composite;
|
||||||
bool coveredRange;
|
bool coveredRange;
|
||||||
ulong processedCount;
|
ulong processedCount;
|
||||||
ulong lastProcessed;
|
ulong lastProcessed;
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
var divisorsBuffer = accelerator.Allocate1D<MontgomeryDivisorData>(batchCapacity);
|
var divisorsBuffer = accelerator.Allocate1D<MontgomeryDivisorData>(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);
|
||||||
MontgomeryDivisorData[] divisorData = ArrayPool<MontgomeryDivisorData>.Shared.Rent(batchCapacity);
|
MontgomeryDivisorData[] divisorData = ArrayPool<MontgomeryDivisorData>.Shared.Rent(batchCapacity);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
composite = CheckDivisors(
|
composite = CheckDivisors(
|
||||||
prime,
|
prime,
|
||||||
allowedMax,
|
allowedMax,
|
||||||
useCycles,
|
useCycles,
|
||||||
accelerator,
|
accelerator,
|
||||||
kernel,
|
kernel,
|
||||||
divisorsBuffer,
|
divisorsBuffer,
|
||||||
hitsBuffer,
|
hitsBuffer,
|
||||||
divisors,
|
divisors,
|
||||||
hits,
|
hits,
|
||||||
divisorData,
|
divisorData,
|
||||||
out lastProcessed,
|
out lastProcessed,
|
||||||
out coveredRange,
|
out coveredRange,
|
||||||
out processedCount
|
out processedCount
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
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);
|
||||||
ArrayPool<MontgomeryDivisorData>.Shared.Return(divisorData, clearArray: true);
|
ArrayPool<MontgomeryDivisorData>.Shared.Return(divisorData, clearArray: true);
|
||||||
hitsBuffer.Dispose();
|
hitsBuffer.Dispose();
|
||||||
divisorsBuffer.Dispose();
|
divisorsBuffer.Dispose();
|
||||||
gpuLease.Dispose();
|
gpuLease.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (processedCount > 0UL)
|
if (processedCount > 0UL)
|
||||||
{
|
{
|
||||||
lock (_sync)
|
lock (_sync)
|
||||||
{
|
{
|
||||||
UpdateStatusUnsafe(lastProcessed, processedCount);
|
UpdateStatusUnsafe(lastProcessed, processedCount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (composite)
|
if (composite)
|
||||||
{
|
{
|
||||||
divisorsExhausted = true;
|
divisorsExhausted = true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
divisorsExhausted = coveredRange;
|
divisorsExhausted = coveredRange;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool CheckDivisors(
|
private bool CheckDivisors(
|
||||||
ulong prime,
|
ulong prime,
|
||||||
ulong allowedMax,
|
ulong allowedMax,
|
||||||
bool useCycles,
|
bool useCycles,
|
||||||
Accelerator accelerator,
|
Accelerator accelerator,
|
||||||
Action<Index1D, ulong, ArrayView<MontgomeryDivisorData>, ArrayView<byte>> kernel,
|
Action<Index1D, ulong, ArrayView<MontgomeryDivisorData>, ArrayView<byte>> kernel,
|
||||||
MemoryBuffer1D<MontgomeryDivisorData, Stride1D.Dense> divisorsBuffer,
|
MemoryBuffer1D<MontgomeryDivisorData, Stride1D.Dense> divisorsBuffer,
|
||||||
MemoryBuffer1D<byte, Stride1D.Dense> hitsBuffer,
|
MemoryBuffer1D<byte, Stride1D.Dense> hitsBuffer,
|
||||||
ulong[] divisors,
|
ulong[] divisors,
|
||||||
byte[] hits,
|
byte[] hits,
|
||||||
MontgomeryDivisorData[] divisorData,
|
MontgomeryDivisorData[] divisorData,
|
||||||
out ulong lastProcessed,
|
out ulong lastProcessed,
|
||||||
out bool coveredRange,
|
out bool coveredRange,
|
||||||
out ulong processedCount)
|
out ulong processedCount)
|
||||||
{
|
{
|
||||||
lastProcessed = 0UL;
|
lastProcessed = 0UL;
|
||||||
processedCount = 0UL;
|
processedCount = 0UL;
|
||||||
|
|
||||||
if (allowedMax < 3UL)
|
if (allowedMax < 3UL)
|
||||||
{
|
{
|
||||||
coveredRange = true;
|
coveredRange = true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int batchCapacity = (int)divisorsBuffer.Length;
|
int batchCapacity = (int)divisorsBuffer.Length;
|
||||||
|
|
||||||
bool composite = false;
|
bool composite = false;
|
||||||
bool processedAll = false;
|
bool processedAll = false;
|
||||||
ulong cycle, divisor = 3UL, next;
|
ulong cycle, divisor = 3UL, next;
|
||||||
int batchSize, i;
|
int batchSize, i;
|
||||||
bool reachedEndInBatch;
|
bool reachedEndInBatch;
|
||||||
|
|
||||||
Span<MontgomeryDivisorData> divisorDataSpan;
|
Span<MontgomeryDivisorData> divisorDataSpan;
|
||||||
Span<ulong> divisorSpan;
|
Span<ulong> divisorSpan;
|
||||||
Span<byte> hitsSpan;
|
Span<byte> hitsSpan;
|
||||||
MersenneDivisorCycles divisorCycles = MersenneDivisorCycles.Shared;
|
MersenneDivisorCycles divisorCycles = MersenneDivisorCycles.Shared;
|
||||||
ArrayView1D<MontgomeryDivisorData, Stride1D.Dense> divisorView;
|
ArrayView1D<MontgomeryDivisorData, Stride1D.Dense> divisorView;
|
||||||
ArrayView1D<byte, Stride1D.Dense> hitsView;
|
ArrayView1D<byte, Stride1D.Dense> hitsView;
|
||||||
|
|
||||||
@@ -184,45 +184,45 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
|
|||||||
batchSize = 0;
|
batchSize = 0;
|
||||||
reachedEndInBatch = false;
|
reachedEndInBatch = false;
|
||||||
|
|
||||||
while (batchSize < batchCapacity && divisor <= allowedMax)
|
while (batchSize < batchCapacity && divisor <= allowedMax)
|
||||||
{
|
{
|
||||||
ulong currentDivisor = divisor;
|
ulong currentDivisor = divisor;
|
||||||
ulong nextDivisor = currentDivisor + 2UL;
|
ulong nextDivisor = currentDivisor + 2UL;
|
||||||
processedCount++;
|
processedCount++;
|
||||||
|
|
||||||
if (useCycles)
|
if (useCycles)
|
||||||
{
|
{
|
||||||
cycle = divisorCycles!.GetCycle(currentDivisor);
|
cycle = divisorCycles!.GetCycle(currentDivisor);
|
||||||
if (cycle == 0UL || prime % cycle != 0UL)
|
if (cycle == 0UL || prime % cycle != 0UL)
|
||||||
{
|
{
|
||||||
lastProcessed = currentDivisor;
|
lastProcessed = currentDivisor;
|
||||||
|
|
||||||
if (nextDivisor <= currentDivisor)
|
if (nextDivisor <= currentDivisor)
|
||||||
{
|
{
|
||||||
reachedEndInBatch = true;
|
reachedEndInBatch = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nextDivisor > allowedMax)
|
if (nextDivisor > allowedMax)
|
||||||
{
|
{
|
||||||
divisor = nextDivisor;
|
divisor = nextDivisor;
|
||||||
reachedEndInBatch = true;
|
reachedEndInBatch = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
divisor = nextDivisor;
|
divisor = nextDivisor;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
divisors[batchSize++] = currentDivisor;
|
divisors[batchSize++] = currentDivisor;
|
||||||
lastProcessed = currentDivisor;
|
lastProcessed = currentDivisor;
|
||||||
next = nextDivisor;
|
next = nextDivisor;
|
||||||
|
|
||||||
// This will be only true when we exceed ulong.MaxValue
|
// This will be only true when we exceed ulong.MaxValue
|
||||||
if (next <= divisor)
|
if (next <= divisor)
|
||||||
{
|
{
|
||||||
reachedEndInBatch = true;
|
reachedEndInBatch = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -236,15 +236,15 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
|
|||||||
divisor = next;
|
divisor = next;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (batchSize == 0)
|
if (batchSize == 0)
|
||||||
{
|
{
|
||||||
if (reachedEndInBatch)
|
if (reachedEndInBatch)
|
||||||
{
|
{
|
||||||
processedAll = true;
|
processedAll = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
divisorDataSpan = divisorData.AsSpan(0, batchSize);
|
divisorDataSpan = divisorData.AsSpan(0, batchSize);
|
||||||
divisorView = divisorsBuffer.View.SubView(0, batchSize);
|
divisorView = divisorsBuffer.View.SubView(0, batchSize);
|
||||||
@@ -263,20 +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;
|
||||||
lastProcessed = divisorSpan[i];
|
lastProcessed = divisorSpan[i];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!composite)
|
if (!composite)
|
||||||
{
|
{
|
||||||
lastProcessed = divisorSpan[batchSize - 1];
|
lastProcessed = divisorSpan[batchSize - 1];
|
||||||
}
|
}
|
||||||
if (composite)
|
if (composite)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
@@ -289,10 +289,10 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
coveredRange = composite || processedAll || divisor > allowedMax;
|
coveredRange = composite || processedAll || divisor > allowedMax;
|
||||||
|
|
||||||
return composite;
|
return composite;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ulong ComputeDivisorLimitFromMaxPrime(ulong maxPrime)
|
private static ulong ComputeDivisorLimitFromMaxPrime(ulong maxPrime)
|
||||||
{
|
{
|
||||||
@@ -309,44 +309,44 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
|
|||||||
return (1UL << (int)(maxPrime - 1UL)) - 1UL;
|
return (1UL << (int)(maxPrime - 1UL)) - 1UL;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ulong ComputeAllowedMaxDivisor(ulong prime, ulong divisorLimit)
|
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)
|
private void UpdateStatusUnsafe(ulong lastProcessed, ulong processedCount)
|
||||||
{
|
{
|
||||||
if (processedCount == 0UL)
|
if (processedCount == 0UL)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ulong interval = (ulong)PerfectNumberConstants.ConsoleInterval;
|
ulong interval = (ulong)PerfectNumberConstants.ConsoleInterval;
|
||||||
if (interval == 0UL)
|
if (interval == 0UL)
|
||||||
{
|
{
|
||||||
_lastStatusDivisor = 0UL;
|
_lastStatusDivisor = 0UL;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ulong total = _lastStatusDivisor + processedCount;
|
ulong total = _lastStatusDivisor + processedCount;
|
||||||
ulong messages = total / interval;
|
ulong messages = total / interval;
|
||||||
_lastStatusDivisor = total % interval;
|
_lastStatusDivisor = total % interval;
|
||||||
|
|
||||||
for (ulong i = 0; i < messages; i++)
|
for (ulong i = 0; i < messages; i++)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"...processed by-divisor candidate = {lastProcessed}");
|
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)
|
||||||
{
|
{
|
||||||
@@ -361,41 +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;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void Reset()
|
internal void Reset()
|
||||||
{
|
{
|
||||||
_disposed = false;
|
_disposed = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void CheckDivisor(ulong divisor, ReadOnlySpan<ulong> primes, Span<byte> hits)
|
public void CheckDivisor(ulong divisor, ReadOnlySpan<ulong> primes, Span<byte> hits)
|
||||||
{
|
{
|
||||||
if (_disposed)
|
if (_disposed)
|
||||||
{
|
{
|
||||||
throw new ObjectDisposedException(nameof(DivisorScanSession));
|
throw new ObjectDisposedException(nameof(DivisorScanSession));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -486,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;
|
||||||
_owner.ReturnSession(this);
|
_owner.ReturnSession(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void ReturnSession(DivisorScanSession session)
|
internal void ReturnSession(DivisorScanSession session)
|
||||||
{
|
{
|
||||||
_sessionPool.Add(session);
|
_sessionPool.Add(session);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ulong Pow2Mod(ulong exponent, in MontgomeryDivisorData divisor)
|
private static ulong Pow2Mod(ulong exponent, in MontgomeryDivisorData divisor)
|
||||||
{
|
{
|
||||||
@@ -540,7 +540,7 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
|
|||||||
{
|
{
|
||||||
ulong tLow = unchecked(a * b);
|
ulong tLow = unchecked(a * b);
|
||||||
ulong m = unchecked(tLow * nPrime);
|
ulong m = unchecked(tLow * nPrime);
|
||||||
|
|
||||||
// We're reusing tLow variable as the result to perform just a little bit better
|
// We're reusing tLow variable as the result to perform just a little bit better
|
||||||
tLow = unchecked(MultiplyHigh(a, b) + MultiplyHigh(m, modulus) + (unchecked(tLow + m * modulus) < tLow ? 1UL : 0UL));
|
tLow = unchecked(MultiplyHigh(a, b) + MultiplyHigh(m, modulus) + (unchecked(tLow + m * modulus) < tLow ? 1UL : 0UL));
|
||||||
if (tLow >= modulus)
|
if (tLow >= modulus)
|
||||||
@@ -624,32 +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, _divisorLimit);
|
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.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_sessionPool.TryTake(out DivisorScanSession? session))
|
if (_sessionPool.TryTake(out DivisorScanSession? session))
|
||||||
{
|
{
|
||||||
session.Reset();
|
session.Reset();
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new DivisorScanSession(this);
|
return new DivisorScanSession(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Reference in new issue
Block a user