DeeterministicRandom types optimized.

This commit is contained in:
mlemanczyk committed 2025-12-13 16:00:28 +01:00
1 parent 1b3882addc
commit e8cca74f68
3 files changed
+9 -20

No files matched your search

@@ -13,11 +13,6 @@ internal static class DeterministicRandomCpu
public static ulong NextUInt64() public static ulong NextUInt64()
{ {
ulong state = s_threadState; ulong state = s_threadState;
if (state == 0UL)
{
state = InitializeThreadState();
}
state ^= state >> 12; state ^= state >> 12;
state ^= state << 25; state ^= state << 25;
state ^= state >> 27; state ^= state >> 27;
@@ -34,7 +29,7 @@ internal static class DeterministicRandomCpu
} }
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
private static ulong InitializeThreadState() public static ulong Initialize()
{ {
ulong seed = (ulong)Interlocked.Increment(ref s_seedCounter); ulong seed = (ulong)Interlocked.Increment(ref s_seedCounter);
seed += 0x9E3779B97F4A7C15UL; seed += 0x9E3779B97F4A7C15UL;
@@ -14,33 +14,25 @@ internal struct DeterministicRandomGpu
{ {
private static long s_seedCounter; private static long s_seedCounter;
private ulong _state; public ulong State;
public readonly ulong State
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return _state;
}
}
public DeterministicRandomGpu() public DeterministicRandomGpu()
{ {
_state = CreateThreadState(); State = CreateThreadState();
} }
public DeterministicRandomGpu(ulong initialState) public DeterministicRandomGpu(ulong initialState)
{ {
_state = initialState; State = initialState;
} }
public ulong NextUInt64() public ulong NextUInt64()
{ {
ulong state = _state; ulong state = State;
state ^= state >> 12; state ^= state >> 12;
state ^= state << 25; state ^= state << 25;
state ^= state >> 27; state ^= state >> 27;
_state = state; State = state;
return state * 2685821657736338717UL; return state * 2685821657736338717UL;
} }
@@ -55,7 +47,7 @@ internal struct DeterministicRandomGpu
public void SetState(ulong state) public void SetState(ulong state)
{ {
_state = state; State = state;
} }
private static ulong CreateThreadState() private static ulong CreateThreadState()
@@ -324,6 +324,7 @@ public static class MersenneNumberDivisorByDivisorTester
if (workerCount == 1) if (workerCount == 1)
{ {
DeterministicRandomCpu.Initialize();
var gpu = PrimeOrderCalculatorAccelerator.Rent(1); var gpu = PrimeOrderCalculatorAccelerator.Rent(1);
foreach (ulong prime in filteredPrimes) foreach (ulong prime in filteredPrimes)
{ {
@@ -360,6 +361,7 @@ public static class MersenneNumberDivisorByDivisorTester
tasks[taskIndex++] = Task.Factory.StartNew( tasks[taskIndex++] = Task.Factory.StartNew(
() => () =>
{ {
DeterministicRandomCpu.Initialize();
startGate.Wait(); startGate.Wait();
GpuPrimeWorkLimiter.Acquire(); GpuPrimeWorkLimiter.Acquire();
Console.WriteLine($"Task started for range {rangeStart}"); Console.WriteLine($"Task started for range {rangeStart}");