Remove Montgomery divisor cache
This commit is contained in:
1 parent
fca650b285
commit
cd69c08fb4
3 files changed
+26
-31
No files matched your search
@@ -192,6 +192,10 @@ public sealed class MersenneNumberDivisorByDivisorCpuTester : IMersenneNumberDiv
|
|||||||
Dictionary<ulong, MersenneDivisorCycles.FactorCacheEntry>? factorCache = null;
|
Dictionary<ulong, MersenneDivisorCycles.FactorCacheEntry>? factorCache = null;
|
||||||
DivisorCycleCache cycleCache = DivisorCycleCache.Shared;
|
DivisorCycleCache cycleCache = DivisorCycleCache.Shared;
|
||||||
|
|
||||||
|
ulong cachedModulus = 0UL;
|
||||||
|
MontgomeryDivisorData cachedDivisorData = default;
|
||||||
|
bool hasCachedDivisorData = false;
|
||||||
|
|
||||||
byte step10 = (byte)(step % 10UL);
|
byte step10 = (byte)(step % 10UL);
|
||||||
byte step8 = (byte)(step % 8UL);
|
byte step8 = (byte)(step % 8UL);
|
||||||
byte step5 = (byte)(step % 5UL);
|
byte step5 = (byte)(step % 5UL);
|
||||||
@@ -222,7 +226,14 @@ public sealed class MersenneNumberDivisorByDivisorCpuTester : IMersenneNumberDiv
|
|||||||
|
|
||||||
if (admissible && (remainder8 == 1 || remainder8 == 7) && remainder3 != 0 && remainder5 != 0 && remainder7 != 0 && remainder11 != 0)
|
if (admissible && (remainder8 == 1 || remainder8 == 7) && remainder3 != 0 && remainder5 != 0 && remainder7 != 0 && remainder11 != 0)
|
||||||
{
|
{
|
||||||
MontgomeryDivisorData divisorData = MontgomeryDivisorDataCache.Get(candidate);
|
if (!hasCachedDivisorData || cachedModulus != candidate)
|
||||||
|
{
|
||||||
|
cachedDivisorData = MontgomeryDivisorData.FromModulus(candidate);
|
||||||
|
cachedModulus = candidate;
|
||||||
|
hasCachedDivisorData = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
MontgomeryDivisorData divisorData = cachedDivisorData;
|
||||||
ulong divisorCycle;
|
ulong divisorCycle;
|
||||||
|
|
||||||
if (candidate <= PerfectNumberConstants.MaxQForDivisorCycles)
|
if (candidate <= PerfectNumberConstants.MaxQForDivisorCycles)
|
||||||
@@ -340,6 +351,9 @@ public sealed class MersenneNumberDivisorByDivisorCpuTester : IMersenneNumberDiv
|
|||||||
{
|
{
|
||||||
private readonly MersenneNumberDivisorByDivisorCpuTester _owner;
|
private readonly MersenneNumberDivisorByDivisorCpuTester _owner;
|
||||||
private bool _disposed;
|
private bool _disposed;
|
||||||
|
private bool _hasCachedDivisorData;
|
||||||
|
private ulong _cachedDivisor;
|
||||||
|
private MontgomeryDivisorData _cachedDivisorData;
|
||||||
|
|
||||||
internal DivisorScanSession(MersenneNumberDivisorByDivisorCpuTester owner)
|
internal DivisorScanSession(MersenneNumberDivisorByDivisorCpuTester owner)
|
||||||
{
|
{
|
||||||
@@ -349,6 +363,9 @@ public sealed class MersenneNumberDivisorByDivisorCpuTester : IMersenneNumberDiv
|
|||||||
internal void Reset()
|
internal void Reset()
|
||||||
{
|
{
|
||||||
_disposed = false;
|
_disposed = false;
|
||||||
|
_hasCachedDivisorData = false;
|
||||||
|
_cachedDivisor = 0UL;
|
||||||
|
_cachedDivisorData = default;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void CheckDivisor(ulong divisor, in MontgomeryDivisorData divisorData, ulong divisorCycle, ReadOnlySpan<ulong> primes, Span<byte> hits)
|
public void CheckDivisor(ulong divisor, in MontgomeryDivisorData divisorData, ulong divisorCycle, ReadOnlySpan<ulong> primes, Span<byte> hits)
|
||||||
@@ -367,7 +384,14 @@ public sealed class MersenneNumberDivisorByDivisorCpuTester : IMersenneNumberDiv
|
|||||||
MontgomeryDivisorData cachedData = divisorData;
|
MontgomeryDivisorData cachedData = divisorData;
|
||||||
if (cachedData.Modulus != divisor)
|
if (cachedData.Modulus != divisor)
|
||||||
{
|
{
|
||||||
cachedData = MontgomeryDivisorDataCache.Get(divisor);
|
if (!_hasCachedDivisorData || _cachedDivisor != divisor)
|
||||||
|
{
|
||||||
|
_cachedDivisorData = MontgomeryDivisorData.FromModulus(divisor);
|
||||||
|
_cachedDivisor = divisor;
|
||||||
|
_hasCachedDivisorData = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
cachedData = _cachedDivisorData;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (divisorCycle == 0UL)
|
if (divisorCycle == 0UL)
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ using System.Numerics;
|
|||||||
using ILGPU;
|
using ILGPU;
|
||||||
using ILGPU.Runtime;
|
using ILGPU.Runtime;
|
||||||
using MontgomeryDivisorData = PerfectNumbers.Core.MontgomeryDivisorData;
|
using MontgomeryDivisorData = PerfectNumbers.Core.MontgomeryDivisorData;
|
||||||
using MontgomeryDivisorDataCache = PerfectNumbers.Core.MontgomeryDivisorDataCache;
|
|
||||||
using CycleRemainderStepper = PerfectNumbers.Core.CycleRemainderStepper;
|
using CycleRemainderStepper = PerfectNumbers.Core.CycleRemainderStepper;
|
||||||
|
|
||||||
namespace PerfectNumbers.Core.Gpu;
|
namespace PerfectNumbers.Core.Gpu;
|
||||||
|
|||||||
@@ -45,32 +45,4 @@ public readonly struct MontgomeryDivisorData(ulong modulus, ulong nPrime, ulong
|
|||||||
montgomeryTwo,
|
montgomeryTwo,
|
||||||
montgomeryTwoSquared);
|
montgomeryTwoSquared);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static class MontgomeryDivisorDataCache
|
|
||||||
{
|
|
||||||
[ThreadStatic]
|
|
||||||
private static bool s_hasCachedValue;
|
|
||||||
|
|
||||||
[ThreadStatic]
|
|
||||||
private static ulong s_cachedModulus;
|
|
||||||
|
|
||||||
[ThreadStatic]
|
|
||||||
private static MontgomeryDivisorData s_cachedData;
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public static MontgomeryDivisorData Get(ulong modulus)
|
|
||||||
{
|
|
||||||
if (s_hasCachedValue && s_cachedModulus == modulus)
|
|
||||||
{
|
|
||||||
return s_cachedData;
|
|
||||||
}
|
|
||||||
|
|
||||||
MontgomeryDivisorData computed = MontgomeryDivisorData.FromModulus(modulus);
|
|
||||||
s_cachedModulus = modulus;
|
|
||||||
s_cachedData = computed;
|
|
||||||
s_hasCachedValue = true;
|
|
||||||
return computed;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user