Merge pull request #226 from mlemanczyk/codex/analyze-and-optimize-memory-cleaning-in-evenperfectbitscanne

Avoid redundant GPU compaction buffer clearing
This commit is contained in:
Marek Lemańczyk authored and GitHub committed 2025-10-23 16:34:41 +02:00
commit c03aa53689
4 files changed
+10 -10

No files matched your search

@@ -35,8 +35,8 @@ internal sealed class MersenneCpuDivisorScanSession : IMersenneNumberDivisorByDi
divisorCycle = DivisorCycleCache.Shared.GetCycleLength(divisor); divisorCycle = DivisorCycleCache.Shared.GetCycleLength(divisor);
if (divisorCycle == 0UL) if (divisorCycle == 0UL)
{ {
hits.Clear(); // DivisorCycleCache guarantees a positive cycle for divisors greater than one.
return; throw new InvalidOperationException($"Divisor cycle solver returned zero for divisor {divisor}.");
} }
} }
@@ -45,8 +45,7 @@ internal sealed class MersenneCpuDivisorScanSession : IMersenneNumberDivisorByDi
var exponentStepper = new ExponentRemainderStepper(cachedData); var exponentStepper = new ExponentRemainderStepper(cachedData);
if (!exponentStepper.IsValidModulus) if (!exponentStepper.IsValidModulus)
{ {
hits.Clear(); throw new InvalidOperationException($"Invalid modulus {cachedData.Modulus} for divisor scans.");
return;
} }
var cycleStepper = new CycleRemainderStepper(divisorCycle); var cycleStepper = new CycleRemainderStepper(divisorCycle);
@@ -232,8 +232,10 @@ internal static class DivisorByDivisorKernels
int lastLaneMask = globalIndex == length - 1 ? 1 : 0; int lastLaneMask = globalIndex == length - 1 ? 1 : 0;
int countValue = shared[prefixBase + localIndex]; int countValue = shared[prefixBase + localIndex];
int storedCount = lastLaneMask != 0 ? countValue : 0; if (lastLaneMask != 0)
Atomic.Max(ref compactedCount[0], storedCount); {
compactedCount[0] = countValue;
}
} }
public static void PublishFirstHitKernel( public static void PublishFirstHitKernel(
@@ -600,7 +600,6 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester : IMersenneNumberDiv
// return 0; // return 0;
// } // }
countView.MemSetToZero();
SharedMemoryConfig sharedMemoryConfig = SharedMemoryConfig.RequestDynamic<int>(count * 3); SharedMemoryConfig sharedMemoryConfig = SharedMemoryConfig.RequestDynamic<int>(count * 3);
Index1D gridDim = new Index1D(1); Index1D gridDim = new Index1D(1);
Index1D groupDim = new Index1D(count); Index1D groupDim = new Index1D(count);
@@ -73,7 +73,7 @@ public static class MersenneNumberDivisorByDivisorTester
candidateCount = candidateSpan.Length; candidateCount = candidateSpan.Length;
} }
pool.Return(recordedCandidates, clearArray: true); pool.Return(recordedCandidates, clearArray: false);
} }
if (skippedByPreviousResults > 0) if (skippedByPreviousResults > 0)
@@ -211,8 +211,8 @@ public static class MersenneNumberDivisorByDivisorTester
filteredPrimes.Add(prime); filteredPrimes.Add(prime);
} }
pool.Return(allowedMaxBatch, clearArray: true); pool.Return(allowedMaxBatch, clearArray: false);
pool.Return(primeBatch, clearArray: true); pool.Return(primeBatch, clearArray: false);
// The filtered list mirrors primesToTest in the CPU flow, so the guard below never triggers after the earlier // The filtered list mirrors primesToTest in the CPU flow, so the guard below never triggers after the earlier
// emptiness checks. // emptiness checks.