Allocate unique p ranges per thread

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