Merge pull request #157 from mlemanczyk/codex/ensure-proper-threading-for-candidate-groups

Group by-divisor CPU workloads per worker
This commit is contained in:
Marek Lemańczyk authored and GitHub committed 2025-10-12 00:16:31 +02:00
commit 4f73d89716
1 file changed
+36 -9
@@ -257,21 +257,48 @@ public static class MersenneNumberDivisorByDivisorTester
printResult(prime, true, divisorsExhausted, true);
}
if (workerCount == 1)
if (workerCount > filteredPrimes.Count)
{
workerCount = filteredPrimes.Count;
}
if (workerCount <= 1)
{
foreach (ulong prime in filteredPrimes)
{
ProcessPrime(prime);
}
}
else
{
ParallelOptions options = new()
{
MaxDegreeOfParallelism = workerCount
};
Parallel.ForEach(filteredPrimes, options, ProcessPrime);
return;
}
Task[] workers = new Task[workerCount];
int totalPrimeCount = filteredPrimes.Count;
int baseGroupSize = totalPrimeCount / workerCount;
int remaining = totalPrimeCount % workerCount;
int groupStartIndex = 0;
for (int workerIndex = 0; workerIndex < workerCount; workerIndex++)
{
int groupSize = baseGroupSize;
if (workerIndex < remaining)
{
groupSize++;
}
int startIndex = groupStartIndex;
int endIndex = startIndex + groupSize;
groupStartIndex = endIndex;
workers[workerIndex] = Task.Run(() =>
{
for (int candidateIndex = startIndex; candidateIndex < endIndex; candidateIndex++)
{
ProcessPrime(filteredPrimes[candidateIndex]);
}
});
}
Task.WaitAll(workers);
}
}