Enable multi-threaded divisor scanning
This commit is contained in:
1 parent
198b31f380
commit
bf02826f7c
1 file changed
+168
-62
@@ -63,6 +63,22 @@ internal static class Program
|
|||||||
internal bool DetailedCheck;
|
internal bool DetailedCheck;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private readonly struct PendingResult
|
||||||
|
{
|
||||||
|
internal PendingResult(ulong prime, bool detailedCheck, bool passedAllTests)
|
||||||
|
{
|
||||||
|
Prime = prime;
|
||||||
|
DetailedCheck = detailedCheck;
|
||||||
|
PassedAllTests = passedAllTests;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal ulong Prime { get; }
|
||||||
|
|
||||||
|
internal bool DetailedCheck { get; }
|
||||||
|
|
||||||
|
internal bool PassedAllTests { get; }
|
||||||
|
}
|
||||||
|
|
||||||
private static unsafe void Main(string[] args)
|
private static unsafe void Main(string[] args)
|
||||||
{
|
{
|
||||||
ulong currentP = InitialP;
|
ulong currentP = InitialP;
|
||||||
@@ -646,7 +662,7 @@ internal static class Program
|
|||||||
|
|
||||||
if (_useByDivisorMode)
|
if (_useByDivisorMode)
|
||||||
{
|
{
|
||||||
RunByDivisorMode(filterList, divisorCyclesSearchLimit);
|
RunByDivisorMode(filterList, divisorCyclesSearchLimit, threadCount);
|
||||||
FlushBuffer();
|
FlushBuffer();
|
||||||
StringBuilderPool.Return(_outputBuilder!);
|
StringBuilderPool.Return(_outputBuilder!);
|
||||||
return;
|
return;
|
||||||
@@ -734,7 +750,7 @@ internal static class Program
|
|||||||
StringBuilderPool.Return(_outputBuilder!);
|
StringBuilderPool.Return(_outputBuilder!);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void RunByDivisorMode(List<ulong> primes, ulong divisorCyclesSearchLimit)
|
private static void RunByDivisorMode(List<ulong> primes, ulong divisorCyclesSearchLimit, int threadCount)
|
||||||
{
|
{
|
||||||
if (primes.Count == 0)
|
if (primes.Count == 0)
|
||||||
{
|
{
|
||||||
@@ -777,79 +793,77 @@ internal static class Program
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
object stateLock = new();
|
||||||
|
ulong divisorLimit = _byDivisorTester!.DivisorLimit;
|
||||||
|
ulong nextDivisor = 3UL;
|
||||||
|
bool divisorsExhausted = false;
|
||||||
|
ulong finalDivisorValue = 3UL;
|
||||||
|
int remainingStates = states.Count;
|
||||||
|
|
||||||
|
Task[] workers = new Task[Math.Max(1, threadCount)];
|
||||||
|
|
||||||
|
for (int i = 0; i < workers.Length; i++)
|
||||||
|
{
|
||||||
|
workers[i] = Task.Run(() =>
|
||||||
|
{
|
||||||
using var session = _byDivisorTester!.CreateDivisorSession();
|
using var session = _byDivisorTester!.CreateDivisorSession();
|
||||||
byte[] hitsBuffer = ArrayPool<byte>.Shared.Rent(states.Count);
|
byte[] hitsBuffer = ArrayPool<byte>.Shared.Rent(states.Count);
|
||||||
ulong[] primeBuffer = ArrayPool<ulong>.Shared.Rent(states.Count);
|
ulong[] primeBuffer = ArrayPool<ulong>.Shared.Rent(states.Count);
|
||||||
int[] indexBuffer = ArrayPool<int>.Shared.Rent(states.Count);
|
int[] indexBuffer = ArrayPool<int>.Shared.Rent(states.Count);
|
||||||
|
List<PendingResult> completions = new(states.Count);
|
||||||
|
List<PendingResult> composites = new(states.Count);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ulong divisor = 3UL;
|
|
||||||
ulong divisorLimit = _byDivisorTester!.DivisorLimit;
|
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
bool anyPending = false;
|
ulong divisor;
|
||||||
int activeCount = 0;
|
int activeCount;
|
||||||
|
bool shouldTerminate;
|
||||||
|
bool hasWork;
|
||||||
|
|
||||||
for (int i = 0; i < states.Count; i++)
|
lock (stateLock)
|
||||||
{
|
{
|
||||||
var state = states[i];
|
hasWork = TryAcquireDivisorWork(states, divisorLimit, primeBuffer, indexBuffer, completions, ref nextDivisor, ref divisorsExhausted, ref finalDivisorValue, ref remainingStates, out divisor, out activeCount, out shouldTerminate);
|
||||||
if (state.Completed)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (state.AllowedMax < divisor)
|
if (completions.Count > 0)
|
||||||
{
|
{
|
||||||
state.Completed = true;
|
foreach (PendingResult result in completions)
|
||||||
state.DetailedCheck = true;
|
|
||||||
states[i] = state;
|
|
||||||
PrintResult(state.Prime, searchedMersenne: true, detailedCheck: true, passedAllTests: true);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
anyPending = true;
|
|
||||||
|
|
||||||
if (divisor > divisorLimit)
|
|
||||||
{
|
{
|
||||||
states[i] = state;
|
PrintResult(result.Prime, searchedMersenne: true, detailedCheck: result.DetailedCheck, passedAllTests: result.PassedAllTests);
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
primeBuffer[activeCount] = state.Prime;
|
completions.Clear();
|
||||||
indexBuffer[activeCount] = i;
|
|
||||||
activeCount++;
|
|
||||||
states[i] = state;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!anyPending || divisor > divisorLimit)
|
if (!hasWork)
|
||||||
|
{
|
||||||
|
if (shouldTerminate)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (activeCount == 0)
|
|
||||||
{
|
|
||||||
ulong nextDivisor = divisor + 2UL;
|
|
||||||
if (nextDivisor <= divisor)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
divisor = nextDivisor;
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
session.CheckDivisor(divisor, primeBuffer.AsSpan(0, activeCount), hitsBuffer.AsSpan(0, activeCount));
|
Span<byte> hitsSpan = hitsBuffer.AsSpan(0, activeCount);
|
||||||
|
hitsSpan.Clear();
|
||||||
|
session.CheckDivisor(divisor, primeBuffer.AsSpan(0, activeCount), hitsSpan);
|
||||||
|
|
||||||
for (int i = 0; i < activeCount; i++)
|
bool exitAfterIteration;
|
||||||
|
composites.Clear();
|
||||||
|
|
||||||
|
lock (stateLock)
|
||||||
{
|
{
|
||||||
if (hitsBuffer[i] == 0)
|
for (int j = 0; j < activeCount; j++)
|
||||||
|
{
|
||||||
|
if (hitsSpan[j] == 0)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
int index = indexBuffer[i];
|
int index = indexBuffer[j];
|
||||||
var state = states[index];
|
var state = states[index];
|
||||||
if (state.Completed)
|
if (state.Completed)
|
||||||
{
|
{
|
||||||
@@ -860,33 +874,67 @@ internal static class Program
|
|||||||
state.Composite = true;
|
state.Composite = true;
|
||||||
state.DetailedCheck = true;
|
state.DetailedCheck = true;
|
||||||
states[index] = state;
|
states[index] = state;
|
||||||
PrintResult(state.Prime, searchedMersenne: true, detailedCheck: true, passedAllTests: false);
|
remainingStates--;
|
||||||
|
composites.Add(new PendingResult(state.Prime, state.DetailedCheck, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool remaining = false;
|
exitAfterIteration = remainingStates == 0;
|
||||||
for (int i = 0; i < states.Count; i++)
|
if (exitAfterIteration && !divisorsExhausted)
|
||||||
{
|
{
|
||||||
if (!states[i].Completed)
|
divisorsExhausted = true;
|
||||||
{
|
finalDivisorValue = Math.Max(finalDivisorValue, nextDivisor);
|
||||||
remaining = true;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!remaining)
|
if (composites.Count > 0)
|
||||||
|
{
|
||||||
|
foreach (PendingResult result in composites)
|
||||||
|
{
|
||||||
|
PrintResult(result.Prime, searchedMersenne: true, detailedCheck: result.DetailedCheck, passedAllTests: result.PassedAllTests);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (exitAfterIteration)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
ulong next = divisor + 2UL;
|
}
|
||||||
if (next <= divisor)
|
finally
|
||||||
{
|
{
|
||||||
break;
|
ArrayPool<int>.Shared.Return(indexBuffer, clearArray: true);
|
||||||
|
ArrayPool<ulong>.Shared.Return(primeBuffer, clearArray: true);
|
||||||
|
ArrayPool<byte>.Shared.Return(hitsBuffer, clearArray: true);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
divisor = next;
|
Task.WaitAll(workers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static bool TryAcquireDivisorWork(List<ByDivisorPrimeState> states, ulong divisorLimit, ulong[] primeBuffer, int[] indexBuffer, List<PendingResult> completions, ref ulong nextDivisor, ref bool divisorsExhausted, ref ulong finalDivisorValue, ref int remainingStates, out ulong divisor, out int activeCount, out bool shouldTerminate)
|
||||||
|
{
|
||||||
|
completions.Clear();
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
if (remainingStates == 0)
|
||||||
|
{
|
||||||
|
divisor = 0UL;
|
||||||
|
activeCount = 0;
|
||||||
|
shouldTerminate = true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (divisorsExhausted || nextDivisor > divisorLimit)
|
||||||
|
{
|
||||||
|
if (!divisorsExhausted)
|
||||||
|
{
|
||||||
|
divisorsExhausted = true;
|
||||||
|
finalDivisorValue = nextDivisor;
|
||||||
|
}
|
||||||
|
|
||||||
|
ulong completionDivisor = finalDivisorValue;
|
||||||
for (int i = 0; i < states.Count; i++)
|
for (int i = 0; i < states.Count; i++)
|
||||||
{
|
{
|
||||||
var state = states[i];
|
var state = states[i];
|
||||||
@@ -896,16 +944,74 @@ internal static class Program
|
|||||||
}
|
}
|
||||||
|
|
||||||
state.Completed = true;
|
state.Completed = true;
|
||||||
state.DetailedCheck = divisor > state.AllowedMax;
|
state.DetailedCheck = completionDivisor > state.AllowedMax;
|
||||||
states[i] = state;
|
states[i] = state;
|
||||||
PrintResult(state.Prime, searchedMersenne: true, detailedCheck: state.DetailedCheck, passedAllTests: true);
|
remainingStates--;
|
||||||
|
completions.Add(new PendingResult(state.Prime, state.DetailedCheck, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
divisor = 0UL;
|
||||||
|
activeCount = 0;
|
||||||
|
shouldTerminate = true;
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
finally
|
|
||||||
|
ulong divisorValue = nextDivisor;
|
||||||
|
ulong candidate = divisorValue + 2UL;
|
||||||
|
if (candidate <= divisorValue)
|
||||||
{
|
{
|
||||||
ArrayPool<int>.Shared.Return(indexBuffer, clearArray: true);
|
divisorsExhausted = true;
|
||||||
ArrayPool<ulong>.Shared.Return(primeBuffer, clearArray: true);
|
finalDivisorValue = divisorValue;
|
||||||
ArrayPool<byte>.Shared.Return(hitsBuffer, clearArray: true);
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
nextDivisor = candidate;
|
||||||
|
if (nextDivisor > divisorLimit)
|
||||||
|
{
|
||||||
|
divisorsExhausted = true;
|
||||||
|
finalDivisorValue = nextDivisor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
activeCount = 0;
|
||||||
|
for (int i = 0; i < states.Count; i++)
|
||||||
|
{
|
||||||
|
var state = states[i];
|
||||||
|
if (state.Completed)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state.AllowedMax < divisorValue)
|
||||||
|
{
|
||||||
|
state.Completed = true;
|
||||||
|
state.DetailedCheck = true;
|
||||||
|
states[i] = state;
|
||||||
|
remainingStates--;
|
||||||
|
completions.Add(new PendingResult(state.Prime, state.DetailedCheck, true));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
primeBuffer[activeCount] = state.Prime;
|
||||||
|
indexBuffer[activeCount] = i;
|
||||||
|
activeCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (activeCount == 0)
|
||||||
|
{
|
||||||
|
if (completions.Count == 0)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
divisor = 0UL;
|
||||||
|
shouldTerminate = remainingStates == 0 && (divisorsExhausted || nextDivisor > divisorLimit);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
divisor = divisorValue;
|
||||||
|
shouldTerminate = false;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user