Merge pull request #13 from mlemanczyk/codex/check-thread-initialization-in-evenperfectbitscanner

Assign contiguous p ranges to worker threads
This commit is contained in:
Marek Lemańczyk authored and GitHub committed 2025-09-11 02:36:41 +02:00
commit 40477eca5d
1 file changed
+19 -13
+19 -13
View File
@@ -599,13 +599,7 @@ internal static class Program
ulong[] buffer = new ulong[blockSize];
while (!Volatile.Read(ref _limitReached))
{
int count = 0;
while (count < blockSize && !Volatile.Read(ref _limitReached))
{
buffer[count] = ReserveNextP();
count++;
}
int count = ReserveBlock(buffer, blockSize);
if (count == 0)
{
break;
@@ -623,7 +617,7 @@ internal static class Program
Console.WriteLine($"Testing {p}");
}
bool isPerfect = IsEvenPerfectCandidate(p, out bool searchedMersenne, out bool detailedCheck);
bool isPerfect = IsEvenPerfectCandidate(p, divisorCyclesSearchLimit, out bool searchedMersenne, out bool detailedCheck);
PrintResult(p, searchedMersenne, detailedCheck, isPerfect);
}
}
@@ -743,7 +737,7 @@ internal static class Program
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe ulong ReserveNextP()
private static unsafe int ReserveBlock(ulong[] buffer, int blockSize)
{
while (true)
{
@@ -751,12 +745,24 @@ internal static class Program
ulong current = (ulong)state >> 3;
ulong remainder = ((ulong)state) & 7UL;
ulong next = _transformP(current, ref remainder);
long newState = ((long)next << 3) | (long)remainder;
ulong p = current;
int count = 0;
while (count < blockSize && !Volatile.Read(ref _limitReached))
{
buffer[count++] = p;
p = _transformP(p, ref remainder);
}
if (count == 0)
{
return 0;
}
long newState = ((long)p << 3) | (long)remainder;
long original = Interlocked.CompareExchange(ref _state, newState, state);
if (original == state)
{
return current;
return count;
}
}
}
@@ -951,7 +957,7 @@ internal static class Program
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool IsEvenPerfectCandidate(ulong p)
{
return IsEvenPerfectCandidate(p, out _, out _);
return IsEvenPerfectCandidate(p, 0, out _, out _);
}
internal static bool IsEvenPerfectCandidate(ulong p, int divisorCyclesSearchLimit, out bool searchedMersenne, out bool detailedCheck)