Merge pull request #70 from mlemanczyk/codex/update-checkdivisor-to-create-span-instances-conditionally

Reduce span creation in GPU divisor scan session
This commit is contained in:
Marek Lemańczyk authored and GitHub committed 2025-09-23 23:28:45 +02:00
commit 4e5dd35a15
1 file changed
+15 -5
@@ -322,10 +322,22 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
MersenneDivisorCycles? divisorCycles = useCycles ? MersenneDivisorCycles.Shared : null;
ulong cycle = useCycles ? divisorCycles!.GetCycle(divisor) : 0UL;
MontgomeryDivisorData divisorData = CreateMontgomeryDivisorData(divisor);
Span<ulong> primeSpan = _primesHost.AsSpan(0, _owner._gpuBatchSize);
Span<int> indexSpan = _indexHost.AsSpan(0, _owner._gpuBatchSize);
Span<byte> hitSpan = _hitsHost.AsSpan(0, _owner._gpuBatchSize);
int currentSpanSize = _owner._gpuBatchSize;
while (offset < primes.Length)
{
int batchSize = Math.Min(_owner._gpuBatchSize, primes.Length - offset);
Span<ulong> primeSpan = _primesHost.AsSpan(0, batchSize);
if (batchSize != currentSpanSize)
{
primeSpan = _primesHost.AsSpan(0, batchSize);
indexSpan = _indexHost.AsSpan(0, batchSize);
hitSpan = _hitsHost.AsSpan(0, batchSize);
currentSpanSize = batchSize;
}
primes.Slice(offset, batchSize).CopyTo(primeSpan);
if (++consoleStatus == PerfectNumberConstants.ConsoleInterval)
@@ -334,8 +346,6 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
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;
@@ -360,11 +370,11 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
var primesView = _primesBuffer.View.SubView(0, gpuCount);
var hitsView = _hitsBuffer.View.SubView(0, gpuCount);
primesView.CopyFromCPU(ref MemoryMarshal.GetReference(primeSpan.Slice(0, gpuCount)), gpuCount);
primesView.CopyFromCPU(ref MemoryMarshal.GetReference(primeSpan), gpuCount);
_kernel(gpuCount, divisorData, primesView, hitsView);
_accelerator.Synchronize();
hitsView.CopyToCPU(ref MemoryMarshal.GetReference(hitSpan.Slice(0, gpuCount)), gpuCount);
hitsView.CopyToCPU(ref MemoryMarshal.GetReference(hitSpan), gpuCount);
for (int i = 0; i < gpuCount; i++)
{
hitsSlice[indexSpan[i]] = hitSpan[i];