Tiny cleanup
This commit is contained in:
1 parent
74b41aad85
commit
eeab8deea7
1 file changed
+321
-321
@@ -26,15 +26,15 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
|
|||||||
public readonly ulong MontgomeryTwo = montgomeryTwo;
|
public readonly ulong MontgomeryTwo = montgomeryTwo;
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly ConcurrentDictionary<Accelerator, Action<Index1D, ulong, byte, ArrayView<MontgomeryDivisorData>, ArrayView<byte>>> _kernelCache = new();
|
private readonly ConcurrentDictionary<Accelerator, Action<Index1D, ulong, byte, ArrayView<MontgomeryDivisorData>, ArrayView<byte>>> _kernelCache = new();
|
||||||
private readonly ConcurrentDictionary<Accelerator, Action<Index1D, MontgomeryDivisorData, ArrayView<ulong>, ArrayView<ulong>>> _kernelDeltaCache = new();
|
private readonly ConcurrentDictionary<Accelerator, Action<Index1D, MontgomeryDivisorData, ArrayView<ulong>, ArrayView<ulong>>> _kernelDeltaCache = new();
|
||||||
private readonly ConcurrentDictionary<Accelerator, ConcurrentBag<BatchResources>> _resourcePools = new(AcceleratorReferenceComparer.Instance);
|
private readonly ConcurrentDictionary<Accelerator, ConcurrentBag<BatchResources>> _resourcePools = new(AcceleratorReferenceComparer.Instance);
|
||||||
private readonly ConcurrentBag<DivisorScanSession> _sessionPool = new();
|
private readonly ConcurrentBag<DivisorScanSession> _sessionPool = new();
|
||||||
|
|
||||||
private Action<Index1D, ulong, byte, ArrayView<MontgomeryDivisorData>, ArrayView<byte>> GetKernel(Accelerator accelerator) =>
|
private Action<Index1D, ulong, byte, ArrayView<MontgomeryDivisorData>, ArrayView<byte>> GetKernel(Accelerator accelerator) =>
|
||||||
_kernelCache.GetOrAdd(accelerator, acc => acc.LoadAutoGroupedStreamKernel<Index1D, ulong, byte, ArrayView<MontgomeryDivisorData>, ArrayView<byte>>(CheckKernel));
|
_kernelCache.GetOrAdd(accelerator, acc => acc.LoadAutoGroupedStreamKernel<Index1D, ulong, byte, ArrayView<MontgomeryDivisorData>, ArrayView<byte>>(CheckKernel));
|
||||||
private Action<Index1D, MontgomeryDivisorData, ArrayView<ulong>, ArrayView<ulong>> GetKernelByPrimeDelta(Accelerator accelerator) =>
|
private Action<Index1D, MontgomeryDivisorData, ArrayView<ulong>, ArrayView<ulong>> GetKernelByPrimeDelta(Accelerator accelerator) =>
|
||||||
_kernelDeltaCache.GetOrAdd(accelerator, acc => acc.LoadAutoGroupedStreamKernel<Index1D, MontgomeryDivisorData, ArrayView<ulong>, ArrayView<ulong>>(ComputePrimeDeltaKernel));
|
_kernelDeltaCache.GetOrAdd(accelerator, acc => acc.LoadAutoGroupedStreamKernel<Index1D, MontgomeryDivisorData, ArrayView<ulong>, ArrayView<ulong>>(ComputePrimeDeltaKernel));
|
||||||
|
|
||||||
public int GpuBatchSize
|
public int GpuBatchSize
|
||||||
{
|
{
|
||||||
@@ -89,38 +89,38 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
|
|||||||
|
|
||||||
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);
|
||||||
BatchResources resources = RentBatchResources(accelerator, batchCapacity);
|
BatchResources resources = RentBatchResources(accelerator, batchCapacity);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
composite = CheckDivisors(
|
composite = CheckDivisors(
|
||||||
prime,
|
prime,
|
||||||
allowedMax,
|
allowedMax,
|
||||||
useCycles,
|
useCycles,
|
||||||
accelerator,
|
accelerator,
|
||||||
kernel,
|
kernel,
|
||||||
resources.DivisorsBuffer,
|
resources.DivisorsBuffer,
|
||||||
resources.HitsBuffer,
|
resources.HitsBuffer,
|
||||||
resources.Divisors,
|
resources.Divisors,
|
||||||
resources.Hits,
|
resources.Hits,
|
||||||
resources.DivisorData,
|
resources.DivisorData,
|
||||||
out lastProcessed,
|
out lastProcessed,
|
||||||
out coveredRange,
|
out coveredRange,
|
||||||
out processedCount
|
out processedCount
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
ReturnBatchResources(accelerator, resources);
|
ReturnBatchResources(accelerator, resources);
|
||||||
gpuLease.Dispose();
|
gpuLease.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (processedCount > 0UL)
|
if (processedCount > 0UL)
|
||||||
{
|
{
|
||||||
lock (_sync)
|
lock (_sync)
|
||||||
{
|
{
|
||||||
UpdateStatusUnsafe(lastProcessed, processedCount);
|
UpdateStatusUnsafe(lastProcessed, processedCount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,7 +139,7 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
|
|||||||
ulong allowedMax,
|
ulong allowedMax,
|
||||||
bool useCycles,
|
bool useCycles,
|
||||||
Accelerator accelerator,
|
Accelerator accelerator,
|
||||||
Action<Index1D, ulong, byte, ArrayView<MontgomeryDivisorData>, ArrayView<byte>> kernel,
|
Action<Index1D, ulong, byte, 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,
|
||||||
@@ -162,17 +162,17 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
|
|||||||
|
|
||||||
bool composite = false;
|
bool composite = false;
|
||||||
bool processedAll = false;
|
bool processedAll = false;
|
||||||
ulong currentDivisor, divisor = 3UL, next, nextDivisor;
|
ulong currentDivisor, divisor = 3UL, next, nextDivisor;
|
||||||
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;
|
||||||
ArrayView1D<MontgomeryDivisorData, Stride1D.Dense> divisorView;
|
ArrayView1D<MontgomeryDivisorData, Stride1D.Dense> divisorView;
|
||||||
ArrayView1D<byte, Stride1D.Dense> hitsView;
|
ArrayView1D<byte, Stride1D.Dense> hitsView;
|
||||||
|
|
||||||
while (divisor <= allowedMax)
|
while (divisor <= allowedMax)
|
||||||
{
|
{
|
||||||
batchSize = 0;
|
batchSize = 0;
|
||||||
reachedEndInBatch = false;
|
reachedEndInBatch = false;
|
||||||
@@ -181,7 +181,7 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
|
|||||||
{
|
{
|
||||||
currentDivisor = divisor;
|
currentDivisor = divisor;
|
||||||
nextDivisor = currentDivisor + 2UL;
|
nextDivisor = currentDivisor + 2UL;
|
||||||
processedCount++;
|
processedCount++;
|
||||||
|
|
||||||
divisors[batchSize++] = currentDivisor;
|
divisors[batchSize++] = currentDivisor;
|
||||||
lastProcessed = currentDivisor;
|
lastProcessed = currentDivisor;
|
||||||
@@ -227,18 +227,18 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
|
|||||||
}
|
}
|
||||||
|
|
||||||
divisorView.CopyFromCPU(ref MemoryMarshal.GetReference(divisorDataSpan), batchSize);
|
divisorView.CopyFromCPU(ref MemoryMarshal.GetReference(divisorDataSpan), batchSize);
|
||||||
kernel(batchSize, prime, useCycles ? (byte)1 : (byte)0, divisorView, hitsView);
|
kernel(batchSize, prime, useCycles ? (byte)1 : (byte)0, divisorView, hitsView);
|
||||||
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)
|
||||||
@@ -306,230 +306,230 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ulong total = _lastStatusDivisor + processedCount;
|
ulong total = _lastStatusDivisor + processedCount;
|
||||||
_lastStatusDivisor = total % interval;
|
_lastStatusDivisor = total % interval;
|
||||||
|
|
||||||
// Removed noisy console output; status is tracked internally only.
|
// Removed noisy console output; status is tracked internally only.
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void CheckKernel(Index1D index, ulong prime, byte useCycleChecks, ArrayView<MontgomeryDivisorData> divisors, ArrayView<byte> hits)
|
private static void CheckKernel(Index1D index, ulong prime, byte useCycleChecks, ArrayView<MontgomeryDivisorData> divisors, ArrayView<byte> hits)
|
||||||
{
|
{
|
||||||
MontgomeryDivisorData divisor = divisors[index];
|
MontgomeryDivisorData divisor = divisors[index];
|
||||||
ulong modulus = divisor.Modulus;
|
ulong modulus = divisor.Modulus;
|
||||||
if (modulus <= 1UL || (modulus & 1UL) == 0UL)
|
if (modulus <= 1UL || (modulus & 1UL) == 0UL)
|
||||||
{
|
{
|
||||||
hits[index] = 0;
|
hits[index] = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (useCycleChecks != 0)
|
if (useCycleChecks != 0)
|
||||||
{
|
{
|
||||||
ulong cycle = MersenneDivisorCycles.CalculateCycleLengthGpu(modulus);
|
ulong cycle = MersenneDivisorCycles.CalculateCycleLengthGpu(modulus);
|
||||||
if (cycle == 0UL || prime % cycle != 0UL)
|
if (cycle == 0UL || prime % cycle != 0UL)
|
||||||
{
|
{
|
||||||
hits[index] = 0;
|
hits[index] = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
hits[index] = prime.Pow2MontgomeryMod(divisor) == 1UL ? (byte)1 : (byte)0;
|
hits[index] = prime.Pow2MontgomeryMod(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<ulong>> _kernel;
|
private readonly Action<Index1D, MontgomeryDivisorData, ArrayView<ulong>, ArrayView<ulong>> _kernel;
|
||||||
private MemoryBuffer1D<ulong, Stride1D.Dense> _exponentsBuffer;
|
private MemoryBuffer1D<ulong, Stride1D.Dense> _exponentsBuffer;
|
||||||
private MemoryBuffer1D<ulong, Stride1D.Dense> _resultsBuffer;
|
private MemoryBuffer1D<ulong, Stride1D.Dense> _resultsBuffer;
|
||||||
private ulong[] _hostBuffer;
|
private ulong[] _hostBuffer;
|
||||||
private int[] _positionBuffer;
|
private int[] _positionBuffer;
|
||||||
private int _capacity;
|
private int _capacity;
|
||||||
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.GetKernelByPrimeDelta(_accelerator);
|
_kernel = owner.GetKernelByPrimeDelta(_accelerator);
|
||||||
_capacity = Math.Max(1, owner._gpuBatchSize);
|
_capacity = Math.Max(1, owner._gpuBatchSize);
|
||||||
_exponentsBuffer = _accelerator.Allocate1D<ulong>(_capacity);
|
_exponentsBuffer = _accelerator.Allocate1D<ulong>(_capacity);
|
||||||
_resultsBuffer = _accelerator.Allocate1D<ulong>(_capacity);
|
_resultsBuffer = _accelerator.Allocate1D<ulong>(_capacity);
|
||||||
_hostBuffer = ArrayPool<ulong>.Shared.Rent(_capacity);
|
_hostBuffer = ArrayPool<ulong>.Shared.Rent(_capacity);
|
||||||
_positionBuffer = ArrayPool<int>.Shared.Rent(_capacity);
|
_positionBuffer = ArrayPool<int>.Shared.Rent(_capacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void Reset()
|
internal void Reset()
|
||||||
{
|
{
|
||||||
_disposed = false;
|
_disposed = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void EnsureCapacity(int requiredCapacity)
|
private void EnsureCapacity(int requiredCapacity)
|
||||||
{
|
{
|
||||||
if (requiredCapacity <= _capacity)
|
if (requiredCapacity <= _capacity)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_exponentsBuffer.Dispose();
|
_exponentsBuffer.Dispose();
|
||||||
_resultsBuffer.Dispose();
|
_resultsBuffer.Dispose();
|
||||||
ArrayPool<ulong>.Shared.Return(_hostBuffer, clearArray: false);
|
ArrayPool<ulong>.Shared.Return(_hostBuffer, clearArray: false);
|
||||||
ArrayPool<int>.Shared.Return(_positionBuffer, clearArray: false);
|
ArrayPool<int>.Shared.Return(_positionBuffer, clearArray: false);
|
||||||
|
|
||||||
_capacity = requiredCapacity;
|
_capacity = requiredCapacity;
|
||||||
_exponentsBuffer = _accelerator.Allocate1D<ulong>(_capacity);
|
_exponentsBuffer = _accelerator.Allocate1D<ulong>(_capacity);
|
||||||
_resultsBuffer = _accelerator.Allocate1D<ulong>(_capacity);
|
_resultsBuffer = _accelerator.Allocate1D<ulong>(_capacity);
|
||||||
_hostBuffer = ArrayPool<ulong>.Shared.Rent(_capacity);
|
_hostBuffer = ArrayPool<ulong>.Shared.Rent(_capacity);
|
||||||
_positionBuffer = ArrayPool<int>.Shared.Rent(_capacity);
|
_positionBuffer = ArrayPool<int>.Shared.Rent(_capacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
int primesLength = primes.Length;
|
int primesLength = primes.Length;
|
||||||
if (primesLength == 0)
|
if (primesLength == 0)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
MersenneNumberDivisorByDivisorGpuTester owner = _owner;
|
MersenneNumberDivisorByDivisorGpuTester owner = _owner;
|
||||||
int gpuBatchSize = owner._gpuBatchSize;
|
int gpuBatchSize = owner._gpuBatchSize;
|
||||||
EnsureCapacity(gpuBatchSize);
|
EnsureCapacity(gpuBatchSize);
|
||||||
|
|
||||||
MontgomeryDivisorData divisorData = CreateMontgomeryDivisorData(divisor);
|
MontgomeryDivisorData divisorData = CreateMontgomeryDivisorData(divisor);
|
||||||
ulong modulus = divisorData.Modulus;
|
ulong modulus = divisorData.Modulus;
|
||||||
if (modulus <= 1UL || (modulus & 1UL) == 0UL)
|
if (modulus <= 1UL || (modulus & 1UL) == 0UL)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool useCycles = owner._useDivisorCycles;
|
bool useCycles = owner._useDivisorCycles;
|
||||||
ulong cycle = 0UL;
|
ulong cycle = 0UL;
|
||||||
bool cycleEnabled = false;
|
bool cycleEnabled = false;
|
||||||
if (useCycles)
|
if (useCycles)
|
||||||
{
|
{
|
||||||
cycle = MersenneDivisorCycles.CalculateCycleLengthGpu(modulus);
|
cycle = MersenneDivisorCycles.CalculateCycleLengthGpu(modulus);
|
||||||
cycleEnabled = cycle != 0UL;
|
cycleEnabled = cycle != 0UL;
|
||||||
}
|
}
|
||||||
|
|
||||||
Accelerator accelerator = _accelerator;
|
Accelerator accelerator = _accelerator;
|
||||||
Action<Index1D, MontgomeryDivisorData, ArrayView<ulong>, ArrayView<ulong>> kernel = _kernel;
|
Action<Index1D, MontgomeryDivisorData, ArrayView<ulong>, ArrayView<ulong>> kernel = _kernel;
|
||||||
ArrayView1D<ulong, Stride1D.Dense> exponentsView = _exponentsBuffer.View;
|
ArrayView1D<ulong, Stride1D.Dense> exponentsView = _exponentsBuffer.View;
|
||||||
ArrayView1D<ulong, Stride1D.Dense> resultsView = _resultsBuffer.View;
|
ArrayView1D<ulong, Stride1D.Dense> resultsView = _resultsBuffer.View;
|
||||||
|
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
int batchSize;
|
int batchSize;
|
||||||
ReadOnlySpan<ulong> primesSlice;
|
ReadOnlySpan<ulong> primesSlice;
|
||||||
Span<byte> hitsSlice;
|
Span<byte> hitsSlice;
|
||||||
Span<ulong> hostSpan = _hostBuffer.AsSpan();
|
Span<ulong> hostSpan = _hostBuffer.AsSpan();
|
||||||
Span<int> positionSpan = _positionBuffer.AsSpan();
|
Span<int> positionSpan = _positionBuffer.AsSpan();
|
||||||
bool hasResidue = false;
|
bool hasResidue = false;
|
||||||
ulong lastPrimeWithResidue = 0UL;
|
ulong lastPrimeWithResidue = 0UL;
|
||||||
ulong lastResidue = 0UL;
|
ulong lastResidue = 0UL;
|
||||||
|
|
||||||
while (offset < primesLength)
|
while (offset < primesLength)
|
||||||
{
|
{
|
||||||
batchSize = Math.Min(gpuBatchSize, primesLength - offset);
|
batchSize = Math.Min(gpuBatchSize, primesLength - offset);
|
||||||
primesSlice = primes.Slice(offset, batchSize);
|
primesSlice = primes.Slice(offset, batchSize);
|
||||||
hitsSlice = hits.Slice(offset, batchSize);
|
hitsSlice = hits.Slice(offset, batchSize);
|
||||||
|
|
||||||
int computeCount = 0;
|
int computeCount = 0;
|
||||||
ulong deltaBasePrime = lastPrimeWithResidue;
|
ulong deltaBasePrime = lastPrimeWithResidue;
|
||||||
bool deltaHasResidue = hasResidue;
|
bool deltaHasResidue = hasResidue;
|
||||||
|
|
||||||
for (int i = 0; i < batchSize; i++)
|
for (int i = 0; i < batchSize; i++)
|
||||||
{
|
{
|
||||||
ulong prime = primesSlice[i];
|
ulong prime = primesSlice[i];
|
||||||
if (cycleEnabled && prime % cycle != 0UL)
|
if (cycleEnabled && prime % cycle != 0UL)
|
||||||
{
|
{
|
||||||
hitsSlice[i] = 0;
|
hitsSlice[i] = 0;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
ulong delta;
|
ulong delta;
|
||||||
if (deltaHasResidue)
|
if (deltaHasResidue)
|
||||||
{
|
{
|
||||||
delta = prime >= deltaBasePrime ? prime - deltaBasePrime : prime;
|
delta = prime >= deltaBasePrime ? prime - deltaBasePrime : prime;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
delta = prime;
|
delta = prime;
|
||||||
}
|
}
|
||||||
hostSpan[computeCount] = delta;
|
hostSpan[computeCount] = delta;
|
||||||
positionSpan[computeCount] = i;
|
positionSpan[computeCount] = i;
|
||||||
computeCount++;
|
computeCount++;
|
||||||
deltaBasePrime = prime;
|
deltaBasePrime = prime;
|
||||||
deltaHasResidue = true;
|
deltaHasResidue = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (computeCount > 0)
|
if (computeCount > 0)
|
||||||
{
|
{
|
||||||
Span<ulong> exponentSlice = hostSpan.Slice(0, computeCount);
|
Span<ulong> exponentSlice = hostSpan.Slice(0, computeCount);
|
||||||
ArrayView1D<ulong, Stride1D.Dense> exponentView = exponentsView.SubView(0, computeCount);
|
ArrayView1D<ulong, Stride1D.Dense> exponentView = exponentsView.SubView(0, computeCount);
|
||||||
exponentView.CopyFromCPU(ref MemoryMarshal.GetReference(exponentSlice), computeCount);
|
exponentView.CopyFromCPU(ref MemoryMarshal.GetReference(exponentSlice), computeCount);
|
||||||
ArrayView1D<ulong, Stride1D.Dense> resultView = resultsView.SubView(0, computeCount);
|
ArrayView1D<ulong, Stride1D.Dense> resultView = resultsView.SubView(0, computeCount);
|
||||||
kernel(computeCount, divisorData, exponentView, resultView);
|
kernel(computeCount, divisorData, exponentView, resultView);
|
||||||
accelerator.Synchronize();
|
accelerator.Synchronize();
|
||||||
resultView.CopyToCPU(ref MemoryMarshal.GetReference(exponentSlice), computeCount);
|
resultView.CopyToCPU(ref MemoryMarshal.GetReference(exponentSlice), computeCount);
|
||||||
|
|
||||||
for (int i = 0; i < computeCount; i++)
|
for (int i = 0; i < computeCount; i++)
|
||||||
{
|
{
|
||||||
int position = positionSpan[i];
|
int position = positionSpan[i];
|
||||||
ulong prime = primesSlice[position];
|
ulong prime = primesSlice[position];
|
||||||
ulong deltaResult = exponentSlice[i];
|
ulong deltaResult = exponentSlice[i];
|
||||||
ulong residue = hasResidue
|
ulong residue = hasResidue
|
||||||
? lastResidue.MulMod64(deltaResult, modulus)
|
? lastResidue.MulMod64(deltaResult, modulus)
|
||||||
: deltaResult % modulus;
|
: deltaResult % modulus;
|
||||||
|
|
||||||
hitsSlice[position] = residue == 1UL ? (byte)1 : (byte)0;
|
hitsSlice[position] = residue == 1UL ? (byte)1 : (byte)0;
|
||||||
hasResidue = true;
|
hasResidue = true;
|
||||||
lastResidue = residue;
|
lastResidue = residue;
|
||||||
lastPrimeWithResidue = prime;
|
lastPrimeWithResidue = prime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 void ComputePrimeDeltaKernel(Index1D index, MontgomeryDivisorData divisor, ArrayView<ulong> deltas, ArrayView<ulong> results)
|
private static void ComputePrimeDeltaKernel(Index1D index, MontgomeryDivisorData divisor, ArrayView<ulong> deltas, ArrayView<ulong> results)
|
||||||
{
|
{
|
||||||
ulong modulus = divisor.Modulus;
|
ulong modulus = divisor.Modulus;
|
||||||
if (modulus <= 1UL || (modulus & 1UL) == 0UL)
|
if (modulus <= 1UL || (modulus & 1UL) == 0UL)
|
||||||
{
|
{
|
||||||
results[index] = 0UL;
|
results[index] = 0UL;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ulong exponent = deltas[index];
|
ulong exponent = deltas[index];
|
||||||
results[index] = exponent.Pow2MontgomeryMod(divisor);
|
results[index] = exponent.Pow2MontgomeryMod(divisor);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static MontgomeryDivisorData CreateMontgomeryDivisorData(ulong modulus)
|
private static MontgomeryDivisorData CreateMontgomeryDivisorData(ulong modulus)
|
||||||
{
|
{
|
||||||
if (modulus <= 1UL || (modulus & 1UL) == 0UL)
|
if (modulus <= 1UL || (modulus & 1UL) == 0UL)
|
||||||
{
|
{
|
||||||
@@ -558,112 +558,112 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
|
|||||||
return unchecked(0UL - inv);
|
return unchecked(0UL - inv);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ulong DivisorLimit
|
public ulong DivisorLimit
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
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 _divisorLimit;
|
return _divisorLimit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private BatchResources RentBatchResources(Accelerator accelerator, int capacity)
|
private BatchResources RentBatchResources(Accelerator accelerator, int capacity)
|
||||||
{
|
{
|
||||||
var bag = _resourcePools.GetOrAdd(accelerator, static _ => new ConcurrentBag<BatchResources>());
|
var bag = _resourcePools.GetOrAdd(accelerator, static _ => new ConcurrentBag<BatchResources>());
|
||||||
if (bag.TryTake(out BatchResources? resources))
|
if (bag.TryTake(out BatchResources? resources))
|
||||||
{
|
{
|
||||||
resources.EnsureCapacity(capacity);
|
resources.EnsureCapacity(capacity);
|
||||||
return resources;
|
return resources;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new BatchResources(accelerator, capacity);
|
return new BatchResources(accelerator, capacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ReturnBatchResources(Accelerator accelerator, BatchResources resources)
|
private void ReturnBatchResources(Accelerator accelerator, BatchResources resources)
|
||||||
{
|
{
|
||||||
_resourcePools.GetOrAdd(accelerator, static _ => new ConcurrentBag<BatchResources>()).Add(resources);
|
_resourcePools.GetOrAdd(accelerator, static _ => new ConcurrentBag<BatchResources>()).Add(resources);
|
||||||
}
|
}
|
||||||
|
|
||||||
private sealed class BatchResources : IDisposable
|
private sealed class BatchResources : IDisposable
|
||||||
{
|
{
|
||||||
private readonly Accelerator _accelerator;
|
private readonly Accelerator _accelerator;
|
||||||
|
|
||||||
internal BatchResources(Accelerator accelerator, int capacity)
|
internal BatchResources(Accelerator accelerator, int capacity)
|
||||||
{
|
{
|
||||||
_accelerator = accelerator;
|
_accelerator = accelerator;
|
||||||
DivisorsBuffer = accelerator.Allocate1D<MontgomeryDivisorData>(capacity);
|
DivisorsBuffer = accelerator.Allocate1D<MontgomeryDivisorData>(capacity);
|
||||||
HitsBuffer = accelerator.Allocate1D<byte>(capacity);
|
HitsBuffer = accelerator.Allocate1D<byte>(capacity);
|
||||||
Divisors = ArrayPool<ulong>.Shared.Rent(capacity);
|
Divisors = ArrayPool<ulong>.Shared.Rent(capacity);
|
||||||
Hits = ArrayPool<byte>.Shared.Rent(capacity);
|
Hits = ArrayPool<byte>.Shared.Rent(capacity);
|
||||||
DivisorData = ArrayPool<MontgomeryDivisorData>.Shared.Rent(capacity);
|
DivisorData = ArrayPool<MontgomeryDivisorData>.Shared.Rent(capacity);
|
||||||
Capacity = capacity;
|
Capacity = capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal MemoryBuffer1D<MontgomeryDivisorData, Stride1D.Dense> DivisorsBuffer { get; private set; }
|
internal MemoryBuffer1D<MontgomeryDivisorData, Stride1D.Dense> DivisorsBuffer { get; private set; }
|
||||||
|
|
||||||
internal MemoryBuffer1D<byte, Stride1D.Dense> HitsBuffer { get; private set; }
|
internal MemoryBuffer1D<byte, Stride1D.Dense> HitsBuffer { get; private set; }
|
||||||
|
|
||||||
internal ulong[] Divisors { get; private set; }
|
internal ulong[] Divisors { get; private set; }
|
||||||
|
|
||||||
internal byte[] Hits { get; private set; }
|
internal byte[] Hits { get; private set; }
|
||||||
|
|
||||||
internal MontgomeryDivisorData[] DivisorData { get; private set; }
|
internal MontgomeryDivisorData[] DivisorData { get; private set; }
|
||||||
|
|
||||||
internal int Capacity { get; private set; }
|
internal int Capacity { get; private set; }
|
||||||
|
|
||||||
public void EnsureCapacity(int requiredCapacity)
|
public void EnsureCapacity(int requiredCapacity)
|
||||||
{
|
{
|
||||||
if (requiredCapacity <= Capacity)
|
if (requiredCapacity <= Capacity)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Resize(requiredCapacity);
|
Resize(requiredCapacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Resize(int newCapacity)
|
private void Resize(int newCapacity)
|
||||||
{
|
{
|
||||||
DivisorsBuffer.Dispose();
|
DivisorsBuffer.Dispose();
|
||||||
HitsBuffer.Dispose();
|
HitsBuffer.Dispose();
|
||||||
ArrayPool<ulong>.Shared.Return(Divisors, clearArray: false);
|
ArrayPool<ulong>.Shared.Return(Divisors, clearArray: false);
|
||||||
ArrayPool<byte>.Shared.Return(Hits, clearArray: false);
|
ArrayPool<byte>.Shared.Return(Hits, clearArray: false);
|
||||||
ArrayPool<MontgomeryDivisorData>.Shared.Return(DivisorData, clearArray: false);
|
ArrayPool<MontgomeryDivisorData>.Shared.Return(DivisorData, clearArray: false);
|
||||||
|
|
||||||
DivisorsBuffer = _accelerator.Allocate1D<MontgomeryDivisorData>(newCapacity);
|
DivisorsBuffer = _accelerator.Allocate1D<MontgomeryDivisorData>(newCapacity);
|
||||||
HitsBuffer = _accelerator.Allocate1D<byte>(newCapacity);
|
HitsBuffer = _accelerator.Allocate1D<byte>(newCapacity);
|
||||||
Divisors = ArrayPool<ulong>.Shared.Rent(newCapacity);
|
Divisors = ArrayPool<ulong>.Shared.Rent(newCapacity);
|
||||||
Hits = ArrayPool<byte>.Shared.Rent(newCapacity);
|
Hits = ArrayPool<byte>.Shared.Rent(newCapacity);
|
||||||
DivisorData = ArrayPool<MontgomeryDivisorData>.Shared.Rent(newCapacity);
|
DivisorData = ArrayPool<MontgomeryDivisorData>.Shared.Rent(newCapacity);
|
||||||
Capacity = newCapacity;
|
Capacity = newCapacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
DivisorsBuffer.Dispose();
|
DivisorsBuffer.Dispose();
|
||||||
HitsBuffer.Dispose();
|
HitsBuffer.Dispose();
|
||||||
ArrayPool<ulong>.Shared.Return(Divisors, clearArray: false);
|
ArrayPool<ulong>.Shared.Return(Divisors, clearArray: false);
|
||||||
ArrayPool<byte>.Shared.Return(Hits, clearArray: false);
|
ArrayPool<byte>.Shared.Return(Hits, clearArray: false);
|
||||||
ArrayPool<MontgomeryDivisorData>.Shared.Return(DivisorData, clearArray: false);
|
ArrayPool<MontgomeryDivisorData>.Shared.Return(DivisorData, clearArray: false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private sealed class AcceleratorReferenceComparer : IEqualityComparer<Accelerator>
|
private sealed class AcceleratorReferenceComparer : IEqualityComparer<Accelerator>
|
||||||
{
|
{
|
||||||
public static AcceleratorReferenceComparer Instance { get; } = new();
|
public static AcceleratorReferenceComparer Instance { get; } = new();
|
||||||
|
|
||||||
public bool Equals(Accelerator? x, Accelerator? y) => ReferenceEquals(x, y);
|
public bool Equals(Accelerator? x, Accelerator? y) => ReferenceEquals(x, y);
|
||||||
|
|
||||||
public int GetHashCode(Accelerator obj) => RuntimeHelpers.GetHashCode(obj);
|
public int GetHashCode(Accelerator obj) => RuntimeHelpers.GetHashCode(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ulong GetAllowedMaxDivisor(ulong prime)
|
public ulong GetAllowedMaxDivisor(ulong prime)
|
||||||
{
|
{
|
||||||
lock (_sync)
|
lock (_sync)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user