Reduce span slicing in GPU divisor checks

This commit is contained in:
Marek Lemańczyk committed 2025-09-23 23:28:32 +02:00
1 parent dc22545adb
commit 5ce67fa1c5
1 file changed
+32 -22
@@ -322,11 +322,23 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
MersenneDivisorCycles? divisorCycles = useCycles ? MersenneDivisorCycles.Shared : null; MersenneDivisorCycles? divisorCycles = useCycles ? MersenneDivisorCycles.Shared : null;
ulong cycle = useCycles ? divisorCycles!.GetCycle(divisor) : 0UL; ulong cycle = useCycles ? divisorCycles!.GetCycle(divisor) : 0UL;
MontgomeryDivisorData divisorData = CreateMontgomeryDivisorData(divisor); MontgomeryDivisorData divisorData = CreateMontgomeryDivisorData(divisor);
while (offset < primes.Length) Span<ulong> primeSpan = _primesHost.AsSpan(0, _owner._gpuBatchSize);
{ Span<int> indexSpan = _indexHost.AsSpan(0, _owner._gpuBatchSize);
int batchSize = Math.Min(_owner._gpuBatchSize, primes.Length - offset); Span<byte> hitSpan = _hitsHost.AsSpan(0, _owner._gpuBatchSize);
Span<ulong> primeSpan = _primesHost.AsSpan(0, batchSize); int currentSpanSize = _owner._gpuBatchSize;
primes.Slice(offset, batchSize).CopyTo(primeSpan);
while (offset < primes.Length)
{
int batchSize = Math.Min(_owner._gpuBatchSize, primes.Length - offset);
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) if (++consoleStatus == PerfectNumberConstants.ConsoleInterval)
{ {
@@ -334,13 +346,11 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
consoleStatus = 0; consoleStatus = 0;
} }
Span<int> indexSpan = _indexHost.AsSpan(0, batchSize); Span<byte> hitsSlice = hits.Slice(offset, batchSize);
Span<byte> hitSpan = _hitsHost.AsSpan(0, batchSize); int gpuCount = 0;
Span<byte> hitsSlice = hits.Slice(offset, batchSize);
int gpuCount = 0;
if (useCycles) if (useCycles)
{ {
for (int i = 0; i < batchSize; i++) for (int i = 0; i < batchSize; i++)
{ {
ulong primeValue = primeSpan[i]; ulong primeValue = primeSpan[i];
@@ -357,19 +367,19 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester
if (gpuCount > 0) if (gpuCount > 0)
{ {
var primesView = _primesBuffer.View.SubView(0, gpuCount); var primesView = _primesBuffer.View.SubView(0, gpuCount);
var hitsView = _hitsBuffer.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); _kernel(gpuCount, divisorData, primesView, hitsView);
_accelerator.Synchronize(); _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++) for (int i = 0; i < gpuCount; i++)
{ {
hitsSlice[indexSpan[i]] = hitSpan[i]; hitsSlice[indexSpan[i]] = hitSpan[i];
} }
} }
} }
else else
{ {