Merge pull request #137 from mlemanczyk/codex/optimize-cpu-threads-for-p-division

Assign contiguous prime ranges for CPU by-divisor scanning
This commit is contained in:
Marek Lemańczyk authored and GitHub committed 2025-10-09 22:09:26 +02:00
commit 40bef43f0e
1 file changed
+26 -1
@@ -290,7 +290,32 @@ public static class MersenneNumberDivisorByDivisorTester
MaxDegreeOfParallelism = workerCount
};
Parallel.ForEach(filteredPrimes, options, ProcessPrime);
primeCount = filteredPrimes.Count; // Reusing primeCount to track the filtered count.
int primesPerWorker = (primeCount + workerCount - 1) / workerCount;
Parallel.For(
0,
workerCount,
options,
workerIndex =>
{
int start = workerIndex * primesPerWorker;
if (start >= primeCount)
{
return;
}
int end = start + primesPerWorker;
if (end > primeCount)
{
end = primeCount;
}
for (int i = start; i < end; i++)
{
ProcessPrime(filteredPrimes[i]);
}
});
}
}
}