Enable parallel by-divisor scanning threads

This commit is contained in:
Marek Lemańczyk committed 2025-10-08 22:45:53 +02:00
1 parent 686d482417
commit 9a81f5f685
2 files changed
+26 -3

No files matched your search

-1
View File
@@ -458,7 +458,6 @@ internal static class Program
if (useByDivisor) if (useByDivisor)
{ {
threadCount = 1;
blockSize = 1; blockSize = 1;
} }
@@ -2,6 +2,7 @@ using System;
using System.Buffers; using System.Buffers;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Threading.Tasks;
using Open.Numeric.Primes; using Open.Numeric.Primes;
namespace PerfectNumbers.Core; namespace PerfectNumbers.Core;
@@ -158,7 +159,13 @@ public static class MersenneNumberDivisorByDivisorTester
return; return;
} }
foreach (ulong prime in filteredPrimes) int workerCount = threadCount <= 0 ? Environment.ProcessorCount : threadCount;
if (workerCount < 1)
{
workerCount = 1;
}
void ProcessPrime(ulong prime)
{ {
bool isPrime = tester.IsPrime(prime, out bool divisorsExhausted); bool isPrime = tester.IsPrime(prime, out bool divisorsExhausted);
@@ -166,11 +173,28 @@ public static class MersenneNumberDivisorByDivisorTester
{ {
markComposite(); markComposite();
printResult(prime, true, true, false); printResult(prime, true, true, false);
continue; return;
} }
clearComposite(); clearComposite();
printResult(prime, true, divisorsExhausted, true); printResult(prime, true, divisorsExhausted, true);
} }
if (workerCount == 1)
{
foreach (ulong prime in filteredPrimes)
{
ProcessPrime(prime);
}
}
else
{
ParallelOptions options = new()
{
MaxDegreeOfParallelism = workerCount
};
Parallel.ForEach(filteredPrimes, options, ProcessPrime);
}
} }
} }