Merge pull request #156 from mlemanczyk/codex/analyze-gc-pressure-in-evenperfectbitscanner
Reduce GC churn in CPU Montgomery pow2 ladder
This commit is contained in:
3 files changed
+217
-23
No files matched your search
@@ -0,0 +1,161 @@
|
|||||||
|
using System;
|
||||||
|
using System.Numerics;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
|
namespace PerfectNumbers.Core;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Mutable helper for 128-bit arithmetic derived from the .NET runtime UInt128 implementation.
|
||||||
|
/// Portions adapted from https://github.com/dotnet/runtime/blob/v8.0.0/src/libraries/System.Private.CoreLib/src/System/UInt128.cs.
|
||||||
|
/// </summary>
|
||||||
|
internal struct MutableUInt128
|
||||||
|
{
|
||||||
|
public ulong High;
|
||||||
|
public ulong Low;
|
||||||
|
|
||||||
|
public MutableUInt128(ulong value)
|
||||||
|
{
|
||||||
|
High = 0UL;
|
||||||
|
Low = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MutableUInt128(ulong high, ulong low)
|
||||||
|
{
|
||||||
|
High = high;
|
||||||
|
Low = low;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MutableUInt128(UInt128 value)
|
||||||
|
{
|
||||||
|
High = (ulong)(value >> 64);
|
||||||
|
Low = (ulong)value;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void Set(ulong value)
|
||||||
|
{
|
||||||
|
High = 0UL;
|
||||||
|
Low = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void Set(UInt128 value)
|
||||||
|
{
|
||||||
|
High = (ulong)(value >> 64);
|
||||||
|
Low = (ulong)value;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void Set(ulong high, ulong low)
|
||||||
|
{
|
||||||
|
High = high;
|
||||||
|
Low = low;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public UInt128 ToUInt128() => ((UInt128)High << 64) | Low;
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void Add(ulong value)
|
||||||
|
{
|
||||||
|
ulong low = Low + value;
|
||||||
|
if (low < value)
|
||||||
|
{
|
||||||
|
High++;
|
||||||
|
}
|
||||||
|
|
||||||
|
Low = low;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void Add(in MutableUInt128 value)
|
||||||
|
{
|
||||||
|
ulong low = Low + value.Low;
|
||||||
|
ulong carry = low < Low ? 1UL : 0UL;
|
||||||
|
ulong high = High + value.High + carry;
|
||||||
|
|
||||||
|
Low = low;
|
||||||
|
High = high;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void Subtract(ulong value)
|
||||||
|
{
|
||||||
|
ulong low = Low;
|
||||||
|
ulong result = low - value;
|
||||||
|
ulong borrow = result > low ? 1UL : 0UL;
|
||||||
|
|
||||||
|
Low = result;
|
||||||
|
High -= borrow;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void Subtract(in MutableUInt128 value)
|
||||||
|
{
|
||||||
|
ulong low = Low;
|
||||||
|
ulong result = low - value.Low;
|
||||||
|
ulong borrow = low < value.Low ? 1UL : 0UL;
|
||||||
|
|
||||||
|
Low = result;
|
||||||
|
High = High - value.High - borrow;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void Multiply(ulong value)
|
||||||
|
{
|
||||||
|
ulong currentHigh = High;
|
||||||
|
ulong currentLow = Low;
|
||||||
|
|
||||||
|
ulong carry = Multiply64(currentLow, value, out ulong newLow);
|
||||||
|
Multiply64(currentHigh, value, out ulong shiftedHigh);
|
||||||
|
ulong newHigh = shiftedHigh + carry;
|
||||||
|
|
||||||
|
Low = newLow;
|
||||||
|
High = newHigh;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
private static ulong Multiply64(ulong left, ulong right, out ulong low)
|
||||||
|
{
|
||||||
|
const ulong Mask32 = 0xFFFF_FFFFUL;
|
||||||
|
|
||||||
|
ulong leftLow = left & Mask32;
|
||||||
|
ulong leftHigh = left >> 32;
|
||||||
|
ulong rightLow = right & Mask32;
|
||||||
|
ulong rightHigh = right >> 32;
|
||||||
|
|
||||||
|
ulong lowLow = leftLow * rightLow;
|
||||||
|
ulong lowHigh = leftLow * rightHigh;
|
||||||
|
ulong highLow = leftHigh * rightLow;
|
||||||
|
ulong highHigh = leftHigh * rightHigh;
|
||||||
|
|
||||||
|
ulong carry = (lowLow >> 32) + (lowHigh & Mask32) + (highLow & Mask32);
|
||||||
|
ulong high = highHigh + (lowHigh >> 32) + (highLow >> 32) + (carry >> 32);
|
||||||
|
|
||||||
|
low = ((carry & Mask32) << 32) | (lowLow & Mask32);
|
||||||
|
return high;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void MultiplyAdd(ulong multiplier, ulong addend)
|
||||||
|
{
|
||||||
|
Multiply(multiplier);
|
||||||
|
Add(addend);
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public ulong Mod(ulong modulus)
|
||||||
|
{
|
||||||
|
if (modulus == 0UL)
|
||||||
|
{
|
||||||
|
throw new DivideByZeroException();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (High == 0UL)
|
||||||
|
{
|
||||||
|
return Low % modulus;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (ulong)(ToUInt128() % modulus);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
using System.Buffers;
|
using System.Buffers;
|
||||||
using System.Buffers.Binary;
|
using System.Buffers.Binary;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Numerics;
|
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using PerfectNumbers.Core.Gpu;
|
using PerfectNumbers.Core.Gpu;
|
||||||
@@ -73,6 +72,9 @@ internal static partial class PrimeOrderCalculator
|
|||||||
[ThreadStatic]
|
[ThreadStatic]
|
||||||
private static int s_divisorCycleSuppressionDepth;
|
private static int s_divisorCycleSuppressionDepth;
|
||||||
|
|
||||||
|
[ThreadStatic]
|
||||||
|
private static MutableUInt128 s_pollardRhoPolynomialBuffer;
|
||||||
|
|
||||||
private static bool IsGpuHeuristicDevice => s_pow2ModeInitialized && s_deviceMode == PrimeOrderHeuristicDevice.Gpu;
|
private static bool IsGpuHeuristicDevice => s_pow2ModeInitialized && s_deviceMode == PrimeOrderHeuristicDevice.Gpu;
|
||||||
|
|
||||||
private readonly struct Pow2ModeScope
|
private readonly struct Pow2ModeScope
|
||||||
@@ -318,6 +320,8 @@ internal static partial class PrimeOrderCalculator
|
|||||||
private static ArrayPool<FactorEntry>? _factorPool;
|
private static ArrayPool<FactorEntry>? _factorPool;
|
||||||
private static ArrayPool<FactorEntry> FactorPool => _factorPool ??= ArrayPool<FactorEntry>.Create();
|
private static ArrayPool<FactorEntry> FactorPool => _factorPool ??= ArrayPool<FactorEntry>.Create();
|
||||||
|
|
||||||
|
private const int FactorEntryStackThreshold = 32;
|
||||||
|
|
||||||
[ThreadStatic]
|
[ThreadStatic]
|
||||||
private static ArrayPool<ulong>? _ulongPool;
|
private static ArrayPool<ulong>? _ulongPool;
|
||||||
private static ArrayPool<ulong> ULongPool => _ulongPool ??= ArrayPool<ulong>.Create();
|
private static ArrayPool<ulong> ULongPool => _ulongPool ??= ArrayPool<ulong>.Create();
|
||||||
@@ -331,8 +335,19 @@ internal static partial class PrimeOrderCalculator
|
|||||||
{
|
{
|
||||||
ReadOnlySpan<FactorEntry> factorSpan = factors.Factors;
|
ReadOnlySpan<FactorEntry> factorSpan = factors.Factors;
|
||||||
int length = factors.Count;
|
int length = factors.Count;
|
||||||
tempArray = factorPool.Rent(length + 1);
|
int capacity = length + 1;
|
||||||
Span<FactorEntry> buffer = tempArray;
|
|
||||||
|
Span<FactorEntry> buffer;
|
||||||
|
if (capacity <= FactorEntryStackThreshold)
|
||||||
|
{
|
||||||
|
buffer = stackalloc FactorEntry[capacity];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
tempArray = factorPool.Rent(capacity);
|
||||||
|
buffer = tempArray.AsSpan(0, capacity);
|
||||||
|
}
|
||||||
|
|
||||||
factorSpan.CopyTo(buffer);
|
factorSpan.CopyTo(buffer);
|
||||||
|
|
||||||
bool isPrime = Open.Numeric.Primes.Prime.Numbers.IsPrime(factors.Cofactor);
|
bool isPrime = Open.Numeric.Primes.Prime.Numbers.IsPrime(factors.Cofactor);
|
||||||
@@ -343,12 +358,14 @@ internal static partial class PrimeOrderCalculator
|
|||||||
length++;
|
length++;
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer.Slice(0, length).Sort(static (a, b) => a.Value.CompareTo(b.Value));
|
buffer = buffer.Slice(0, length); // Reuse the same span restricted to populated entries.
|
||||||
|
buffer.Sort(static (a, b) => a.Value.CompareTo(b.Value));
|
||||||
|
|
||||||
for (int i = 0; i < length; i++)
|
for (int i = 0; i < buffer.Length; i++)
|
||||||
{
|
{
|
||||||
ulong primeFactor = buffer[i].Value;
|
FactorEntry entry = buffer[i];
|
||||||
int exponent = buffer[i].Exponent;
|
ulong primeFactor = entry.Value;
|
||||||
|
int exponent = entry.Exponent;
|
||||||
for (int iteration = 0; iteration < exponent; iteration++)
|
for (int iteration = 0; iteration < exponent; iteration++)
|
||||||
{
|
{
|
||||||
if ((order % primeFactor) != 0UL)
|
if ((order % primeFactor) != 0UL)
|
||||||
@@ -1311,8 +1328,11 @@ internal static partial class PrimeOrderCalculator
|
|||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
private static ulong AdvancePolynomial(ulong x, ulong c, ulong modulus)
|
private static ulong AdvancePolynomial(ulong x, ulong c, ulong modulus)
|
||||||
{
|
{
|
||||||
UInt128 value = (UInt128)x * x + c;
|
ref MutableUInt128 polynomial = ref s_pollardRhoPolynomialBuffer;
|
||||||
return (ulong)(value % modulus);
|
polynomial.Set(x);
|
||||||
|
polynomial.Multiply(x);
|
||||||
|
polynomial.Add(c);
|
||||||
|
return polynomial.Mod(modulus);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ulong BinaryGcd(ulong a, ulong b)
|
private static ulong BinaryGcd(ulong a, ulong b)
|
||||||
|
|||||||
@@ -346,7 +346,26 @@ public static class ULongExtensions
|
|||||||
|
|
||||||
ulong result = divisor.MontgomeryOne;
|
ulong result = divisor.MontgomeryOne;
|
||||||
ulong nPrime = divisor.NPrime;
|
ulong nPrime = divisor.NPrime;
|
||||||
ulong[] oddPowers = InitializeMontgomeryOddPowersCpu(divisor, modulus, nPrime, oddPowerCount);
|
|
||||||
|
Span<ulong> oddPowerStorage;
|
||||||
|
ulong[]? pooledOddPowers = null;
|
||||||
|
|
||||||
|
if (oddPowerCount <= PerfectNumberConstants.MaxOddPowersCount)
|
||||||
|
{
|
||||||
|
oddPowerStorage = stackalloc ulong[PerfectNumberConstants.MaxOddPowersCount];
|
||||||
|
}
|
||||||
|
else if (oddPowerCount < PerfectNumberConstants.PooledArrayThreshold)
|
||||||
|
{
|
||||||
|
oddPowerStorage = new ulong[oddPowerCount];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
pooledOddPowers = ArrayPool<ulong>.Shared.Rent(oddPowerCount);
|
||||||
|
oddPowerStorage = pooledOddPowers;
|
||||||
|
}
|
||||||
|
|
||||||
|
Span<ulong> oddPowers = oddPowerStorage[..oddPowerCount];
|
||||||
|
InitializeMontgomeryOddPowersCpu(divisor, modulus, nPrime, oddPowers);
|
||||||
|
|
||||||
int index = bitLength - 1;
|
int index = bitLength - 1;
|
||||||
while (index >= 0)
|
while (index >= 0)
|
||||||
@@ -384,9 +403,9 @@ public static class ULongExtensions
|
|||||||
index = windowStart - 1;
|
index = windowStart - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (oddPowerCount >= PerfectNumberConstants.PooledArrayThreshold)
|
if (pooledOddPowers is not null)
|
||||||
{
|
{
|
||||||
ArrayPool<ulong>.Shared.Return(oddPowers);
|
ArrayPool<ulong>.Shared.Return(pooledOddPowers);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (keepMontgomery)
|
if (keepMontgomery)
|
||||||
@@ -533,29 +552,25 @@ public static class ULongExtensions
|
|||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
private static ulong[] InitializeMontgomeryOddPowersCpu(in MontgomeryDivisorData divisor, ulong modulus, ulong nPrime, int oddPowerCount)
|
private static void InitializeMontgomeryOddPowersCpu(in MontgomeryDivisorData divisor, ulong modulus, ulong nPrime, Span<ulong> destination)
|
||||||
{
|
{
|
||||||
ulong[] oddPowers = oddPowerCount < PerfectNumberConstants.PooledArrayThreshold
|
int oddPowerCount = destination.Length;
|
||||||
? new ulong[oddPowerCount]
|
|
||||||
: ArrayPool<ulong>.Shared.Rent(oddPowerCount);
|
|
||||||
|
|
||||||
if (oddPowerCount == 0)
|
if (oddPowerCount == 0)
|
||||||
{
|
{
|
||||||
return oddPowers;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Span<ulong> destination = oddPowers.AsSpan(0, oddPowerCount);
|
|
||||||
bool computedOnGpu = oddPowerCount <= PerfectNumberConstants.MaxOddPowersCount
|
bool computedOnGpu = oddPowerCount <= PerfectNumberConstants.MaxOddPowersCount
|
||||||
&& MontgomeryOddPowerGpu.TryCompute(divisor, oddPowerCount, destination);
|
&& MontgomeryOddPowerGpu.TryCompute(divisor, oddPowerCount, destination);
|
||||||
if (computedOnGpu)
|
if (computedOnGpu)
|
||||||
{
|
{
|
||||||
return oddPowers;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
destination[0] = divisor.MontgomeryTwo;
|
destination[0] = divisor.MontgomeryTwo;
|
||||||
if (oddPowerCount == 1)
|
if (oddPowerCount == 1)
|
||||||
{
|
{
|
||||||
return oddPowers;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ulong square = divisor.MontgomeryTwoSquared;
|
ulong square = divisor.MontgomeryTwoSquared;
|
||||||
@@ -565,8 +580,6 @@ public static class ULongExtensions
|
|||||||
current = current.MontgomeryMultiply(square, modulus, nPrime);
|
current = current.MontgomeryMultiply(square, modulus, nPrime);
|
||||||
destination[i] = current;
|
destination[i] = current;
|
||||||
}
|
}
|
||||||
|
|
||||||
return oddPowers;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
|||||||
Reference in new issue
Block a user