Refactor heuristic pooling to cut GC pressure

This commit is contained in:
Marek Lemańczyk committed 2025-10-16 00:37:01 +02:00
1 parent 0a23502441
commit 3e7bffc812
3 files changed
+356 -315

No files matched your search

+231 -292
View File
@@ -48,17 +48,9 @@ internal static partial class PrimeOrderCalculator
[ThreadStatic] [ThreadStatic]
private static bool s_debugLoggingEnabled; private static bool s_debugLoggingEnabled;
[ThreadStatic]
private static List<ulong>? s_partialFactorPending;
[ThreadStatic]
private static Stack<ulong>? s_partialFactorCompositeStack;
[ThreadStatic]
private static Dictionary<ulong, int>? s_partialFactorCounts;
[ThreadStatic]
private static List<ulong>? s_heuristicCandidateList;
private static bool IsGpuHeuristicDevice => s_pow2ModeInitialized && s_deviceMode == PrimeOrderHeuristicDevice.Gpu; private static bool IsGpuHeuristicDevice => s_pow2ModeInitialized && s_deviceMode == PrimeOrderHeuristicDevice.Gpu;
@@ -406,13 +398,13 @@ internal static partial class PrimeOrderCalculator
} }
int capacity = config.MaxPowChecks <= 0 ? 64 : config.MaxPowChecks * 4; int capacity = config.MaxPowChecks <= 0 ? 64 : config.MaxPowChecks * 4;
List<ulong> candidates = AcquireHeuristicCandidateList(capacity); List<ulong> candidates = ThreadStaticPools.RentUlongList(capacity);
FactorEntry[] factorArray = orderFactors.Factors!; FactorEntry[] factorArray = orderFactors.Factors!;
// DebugLog("Building candidates list"); // DebugLog("Building candidates list");
BuildCandidates(order, factorArray, orderFactors.Count, candidates, capacity); BuildCandidates(order, factorArray, orderFactors.Count, candidates, capacity);
if (candidates.Count == 0) if (candidates.Count == 0)
{ {
candidates.Clear(); ThreadStaticPools.ReturnUlongList(candidates);
return false; return false;
} }
@@ -513,7 +505,7 @@ internal static partial class PrimeOrderCalculator
pool.Return(gpuPool, clearArray: false); pool.Return(gpuPool, clearArray: false);
} }
candidates.Clear(); ThreadStaticPools.ReturnUlongList(candidates);
result = candidate; result = candidate;
return true; return true;
} }
@@ -526,7 +518,7 @@ internal static partial class PrimeOrderCalculator
index += batchSize; index += batchSize;
} }
candidates.Clear(); ThreadStaticPools.ReturnUlongList(candidates);
// DebugLog("No candidate confirmed"); // DebugLog("No candidate confirmed");
return false; return false;
} }
@@ -781,324 +773,271 @@ internal static partial class PrimeOrderCalculator
return exponent.Pow2MontgomeryModWindowedCpu(divisorData, keepMontgomery: false) == 1UL; return exponent.Pow2MontgomeryModWindowedCpu(divisorData, keepMontgomery: false) == 1UL;
} }
private static List<ulong> AcquirePendingCompositeList(int capacityHint)
{
List<ulong>? list = s_partialFactorPending;
if (list is null)
{
list = new List<ulong>(capacityHint);
s_partialFactorPending = list;
return list;
}
list.Clear();
if (list.Capacity < capacityHint)
{
list.Capacity = capacityHint;
}
return list;
}
private static Stack<ulong> AcquireCompositeStack(int capacityHint)
{
Stack<ulong>? stack = s_partialFactorCompositeStack;
if (stack is null)
{
stack = new Stack<ulong>(capacityHint);
s_partialFactorCompositeStack = stack;
return stack;
}
stack.Clear();
stack.EnsureCapacity(capacityHint);
return stack;
}
private static Dictionary<ulong, int> AcquireFactorCountDictionary(int capacityHint)
{
Dictionary<ulong, int>? dictionary = s_partialFactorCounts;
if (dictionary is null)
{
dictionary = new Dictionary<ulong, int>(capacityHint);
s_partialFactorCounts = dictionary;
return dictionary;
}
dictionary.Clear();
dictionary.EnsureCapacity(capacityHint);
return dictionary;
}
private static List<ulong> AcquireHeuristicCandidateList(int capacityHint)
{
List<ulong>? list = s_heuristicCandidateList;
if (list is null)
{
list = new List<ulong>(capacityHint);
s_heuristicCandidateList = list;
return list;
}
list.Clear();
list.EnsureCapacity(capacityHint);
return list;
}
private static PartialFactorResult PartialFactor(ulong value, in PrimeOrderSearchConfig config) private static PartialFactorResult PartialFactor(ulong value, in PrimeOrderSearchConfig config)
{ {
if (value <= 1UL) if (value <= 1UL)
{ {
return PartialFactorResult.Empty; return PartialFactorResult.Empty;
} }
const int FactorSlotCount = GpuSmallPrimeFactorSlots; const int FactorSlotCount = GpuSmallPrimeFactorSlots;
Span<ulong> primeSlots = stackalloc ulong[FactorSlotCount]; Span<ulong> primeSlots = stackalloc ulong[FactorSlotCount];
Span<int> exponentSlots = stackalloc int[FactorSlotCount]; Span<int> exponentSlots = stackalloc int[FactorSlotCount];
primeSlots.Clear(); primeSlots.Clear();
exponentSlots.Clear(); exponentSlots.Clear();
int factorCount = 0; int factorCount = 0;
Dictionary<ulong, int>? counts = null; Dictionary<ulong, int>? counts = null;
bool useDictionary = false; bool useDictionary = false;
uint limit = config.SmallFactorLimit == 0 ? uint.MaxValue : config.SmallFactorLimit; uint limit = config.SmallFactorLimit == 0 ? uint.MaxValue : config.SmallFactorLimit;
ulong remaining = value; ulong remaining = value;
bool gpuFactored = false; bool gpuFactored = false;
if (IsGpuHeuristicDevice) if (IsGpuHeuristicDevice)
{ {
gpuFactored = PrimeOrderGpuHeuristics.TryPartialFactor(value, limit, primeSlots, exponentSlots, out factorCount, out remaining, out _); gpuFactored = PrimeOrderGpuHeuristics.TryPartialFactor(value, limit, primeSlots, exponentSlots, out factorCount, out remaining, out _);
} }
List<ulong> pending = AcquirePendingCompositeList(2); List<ulong> pending = ThreadStaticPools.RentUlongList(2);
Stack<ulong>? compositeStack = null; Stack<ulong>? compositeStack = null;
PartialFactorResult result;
try if (!gpuFactored)
{ {
if (!gpuFactored) primeSlots.Clear();
{ exponentSlots.Clear();
primeSlots.Clear(); if (!TryPopulateSmallPrimeFactorsCpu(value, limit, primeSlots, exponentSlots, out factorCount, out remaining))
exponentSlots.Clear(); {
if (!TryPopulateSmallPrimeFactorsCpu(value, limit, primeSlots, exponentSlots, out factorCount, out remaining)) useDictionary = true;
{ counts = ThreadStaticPools.RentUlongIntDictionary(Math.Max(factorCount, 8));
useDictionary = true; remaining = PopulateSmallPrimeFactorsCpu(value, limit, counts);
counts = AcquireFactorCountDictionary(Math.Max(factorCount, 8)); }
remaining = PopulateSmallPrimeFactorsCpu(value, limit, counts); }
}
}
if (remaining > 1UL) if (remaining > 1UL)
{ {
pending.Add(remaining); pending.Add(remaining);
} }
if (config.PollardRhoMilliseconds > 0 && pending.Count > 0) if (config.PollardRhoMilliseconds > 0 && pending.Count > 0)
{ {
long deadlineTimestamp = CreateDeadlineTimestamp(config.PollardRhoMilliseconds); long deadlineTimestamp = CreateDeadlineTimestamp(config.PollardRhoMilliseconds);
compositeStack = AcquireCompositeStack(Math.Max(pending.Count * 2, 4)); compositeStack = ThreadStaticPools.RentUlongStack(Math.Max(pending.Count * 2, 4));
compositeStack.Push(remaining); compositeStack.Push(remaining);
pending.Clear(); pending.Clear();
long timestamp = 0L; // reused for deadline checks. long timestamp = 0L;
while (compositeStack.Count > 0) while (compositeStack.Count > 0)
{ {
ulong composite = compositeStack.Pop(); ulong composite = compositeStack.Pop();
if (composite <= 1UL) if (composite <= 1UL)
{ {
continue; continue;
} }
bool isPrime = Open.Numeric.Primes.Prime.Numbers.IsPrime(composite); bool isPrime = Open.Numeric.Primes.Prime.Numbers.IsPrime(composite);
if (isPrime) if (isPrime)
{ {
AddFactorToCollector(ref useDictionary, ref counts, primeSlots, exponentSlots, ref factorCount, composite, 1); AddFactorToCollector(ref useDictionary, ref counts, primeSlots, exponentSlots, ref factorCount, composite, 1);
continue; continue;
} }
timestamp = Stopwatch.GetTimestamp(); timestamp = Stopwatch.GetTimestamp();
if (timestamp > deadlineTimestamp) if (timestamp > deadlineTimestamp)
{ {
pending.Add(composite); pending.Add(composite);
continue; continue;
} }
if (!TryPollardRho(composite, deadlineTimestamp, out ulong factor)) if (!TryPollardRho(composite, deadlineTimestamp, out ulong factor))
{ {
pending.Add(composite); pending.Add(composite);
continue; continue;
} }
ulong quotient = composite / factor; ulong quotient = composite / factor;
compositeStack.Push(factor); compositeStack.Push(factor);
compositeStack.Push(quotient); compositeStack.Push(quotient);
} }
} }
ulong cofactor = 1UL; ulong cofactor = 1UL;
int pendingCount = pending.Count; int pendingCount = pending.Count;
for (int i = 0; i < pendingCount; i++) for (int i = 0; i < pendingCount; i++)
{ {
ulong composite = pending[i]; ulong composite = pending[i];
bool isPrime = Open.Numeric.Primes.Prime.Numbers.IsPrime(composite); bool isPrime = Open.Numeric.Primes.Prime.Numbers.IsPrime(composite);
if (isPrime) if (isPrime)
{ {
AddFactorToCollector(ref useDictionary, ref counts, primeSlots, exponentSlots, ref factorCount, composite, 1); AddFactorToCollector(ref useDictionary, ref counts, primeSlots, exponentSlots, ref factorCount, composite, 1);
} }
else if (composite > 1UL) else if (composite > 1UL)
{ {
cofactor = checked(cofactor * composite); cofactor = checked(cofactor * composite);
} }
} }
ArrayPool<FactorEntry> pool = ThreadStaticPools.FactorEntryPool; ArrayPool<FactorEntry> pool = ThreadStaticPools.FactorEntryPool;
if (useDictionary) if (useDictionary)
{ {
if ((counts is null || counts.Count == 0) && cofactor == value) if ((counts is null || counts.Count == 0) && cofactor == value)
{ {
return PartialFactorResult.Rent(null, value, false, 0); result = PartialFactorResult.Rent(null, value, false, 0);
} goto ReturnResult;
}
if (counts is null || counts.Count == 0) if (counts is null || counts.Count == 0)
{ {
return PartialFactorResult.Rent(null, cofactor, cofactor == 1UL, 0); result = PartialFactorResult.Rent(null, cofactor, cofactor == 1UL, 0);
} goto ReturnResult;
}
FactorEntry[] factors = pool.Rent(counts.Count); FactorEntry[] factors = pool.Rent(counts.Count);
int index = 0; int index = 0;
foreach (KeyValuePair<ulong, int> entry in counts) foreach (KeyValuePair<ulong, int> entry in counts)
{ {
factors[index] = new FactorEntry(entry.Key, entry.Value); factors[index] = new FactorEntry(entry.Key, entry.Value);
index++; index++;
} }
Span<FactorEntry> span = factors.AsSpan(0, index); Span<FactorEntry> span = factors.AsSpan(0, index);
span.Sort(static (a, b) => a.Value.CompareTo(b.Value)); span.Sort(static (a, b) => a.Value.CompareTo(b.Value));
bool fullyFactored = cofactor == 1UL; bool fullyFactored = cofactor == 1UL;
return PartialFactorResult.Rent(factors, cofactor, fullyFactored, index); result = PartialFactorResult.Rent(factors, cofactor, fullyFactored, index);
} goto ReturnResult;
}
int actualCount = 0; int actualCount = 0;
for (int i = 0; i < factorCount; i++) for (int i = 0; i < factorCount; i++)
{ {
if (primeSlots[i] != 0UL && exponentSlots[i] != 0) if (primeSlots[i] != 0UL && exponentSlots[i] != 0)
{ {
actualCount++; actualCount++;
} }
} }
if (actualCount == 0) if (actualCount == 0)
{ {
if (cofactor == value) if (cofactor == value)
{ {
return PartialFactorResult.Rent(null, value, false, 0); result = PartialFactorResult.Rent(null, value, false, 0);
} goto ReturnResult;
}
return PartialFactorResult.Rent(null, cofactor, cofactor == 1UL, 0); result = PartialFactorResult.Rent(null, cofactor, cofactor == 1UL, 0);
} goto ReturnResult;
}
FactorEntry[] array = pool.Rent(actualCount); FactorEntry[] array = pool.Rent(actualCount);
int arrayIndex = 0; int arrayIndex = 0;
for (int i = 0; i < factorCount; i++) for (int i = 0; i < factorCount; i++)
{ {
ulong primeValue = primeSlots[i]; ulong primeValue = primeSlots[i];
int exponentValue = exponentSlots[i]; int exponentValue = exponentSlots[i];
if (primeValue == 0UL || exponentValue == 0) if (primeValue == 0UL || exponentValue == 0)
{ {
continue; continue;
} }
array[arrayIndex] = new FactorEntry(primeValue, exponentValue); array[arrayIndex] = new FactorEntry(primeValue, exponentValue);
arrayIndex++; arrayIndex++;
} }
Span<FactorEntry> arraySpan = array.AsSpan(0, arrayIndex); Span<FactorEntry> arraySpan = array.AsSpan(0, arrayIndex);
arraySpan.Sort(static (a, b) => a.Value.CompareTo(b.Value)); arraySpan.Sort(static (a, b) => a.Value.CompareTo(b.Value));
bool fullyFactoredArray = cofactor == 1UL; bool fullyFactoredArray = cofactor == 1UL;
return PartialFactorResult.Rent(array, cofactor, fullyFactoredArray, arrayIndex); result = PartialFactorResult.Rent(array, cofactor, fullyFactoredArray, arrayIndex);
}
finally
{
pending.Clear();
counts?.Clear();
compositeStack?.Clear();
}
}
private const int GpuSmallPrimeFactorSlots = 64; ReturnResult:
if (counts is not null)
{
ThreadStaticPools.ReturnUlongIntDictionary(counts);
}
private static bool TryPopulateSmallPrimeFactorsCpu( ThreadStaticPools.ReturnUlongList(pending);
ulong value, if (compositeStack is not null)
uint limit, {
Span<ulong> primeTargets, ThreadStaticPools.ReturnUlongStack(compositeStack);
Span<int> exponentTargets, }
out int factorCount,
out ulong remaining)
{
factorCount = 0;
remaining = value;
if (value <= 1UL) return result;
{ }
return true;
}
int capacity = Math.Min(primeTargets.Length, exponentTargets.Length); private const int GpuSmallPrimeFactorSlots = 64;
if (capacity == 0)
{
return false;
}
uint[] primes = PrimesGenerator.SmallPrimes; private static bool TryPopulateSmallPrimeFactorsCpu(
ulong[] squares = PrimesGenerator.SmallPrimesPow2; ulong value,
int primeCount = primes.Length; uint limit,
ulong remainingLocal = value; Span<ulong> primeTargets,
uint effectiveLimit = limit == 0 ? uint.MaxValue : limit; Span<int> exponentTargets,
out int factorCount,
out ulong remaining)
{
factorCount = 0;
remaining = value;
for (int i = 0; i < primeCount && remainingLocal > 1UL; i++) if (value <= 1UL)
{ {
uint primeCandidate = primes[i]; return true;
if (primeCandidate > effectiveLimit) }
{
break;
}
ulong primeSquare = squares[i]; int capacity = Math.Min(primeTargets.Length, exponentTargets.Length);
if (primeSquare != 0UL && primeSquare > remainingLocal) if (capacity == 0)
{ {
break; return false;
} }
ulong primeValue = primeCandidate; uint[] primes = PrimesGenerator.SmallPrimes;
if ((remainingLocal % primeValue) != 0UL) ulong[] squares = PrimesGenerator.SmallPrimesPow2;
{ int primeCount = primes.Length;
continue; ulong remainingLocal = value;
} uint effectiveLimit = limit == 0 ? uint.MaxValue : limit;
int exponent = 0; for (int i = 0; i < primeCount && remainingLocal > 1UL; i++)
do {
{ uint primeCandidate = primes[i];
remainingLocal /= primeValue; if (primeCandidate > effectiveLimit)
exponent++; {
} break;
while ((remainingLocal % primeValue) == 0UL); }
if (factorCount >= capacity) ulong primeSquare = squares[i];
{ if (primeSquare != 0UL && primeSquare > remainingLocal)
return false; {
} break;
}
primeTargets[factorCount] = primeValue; ulong primeValue = primeCandidate;
exponentTargets[factorCount] = exponent; if ((remainingLocal % primeValue) != 0UL)
factorCount++; {
} continue;
}
remaining = remainingLocal; int exponent = 0;
return true; do
} {
remainingLocal /= primeValue;
exponent++;
}
while ((remainingLocal % primeValue) == 0UL);
private static ulong PopulateSmallPrimeFactorsCpu(ulong value, uint limit, Dictionary<ulong, int> counts) if (factorCount >= capacity)
{
return false;
}
primeTargets[factorCount] = primeValue;
exponentTargets[factorCount] = exponent;
factorCount++;
}
remaining = remainingLocal;
return true;
}
private static ulong PopulateSmallPrimeFactorsCpu(ulong value, uint limit, Dictionary<ulong, int> counts)
{ {
ulong remaining = value; ulong remaining = value;
uint[] primes = PrimesGenerator.SmallPrimes; uint[] primes = PrimesGenerator.SmallPrimes;
@@ -1340,7 +1279,7 @@ internal static partial class PrimeOrderCalculator
return; return;
} }
counts ??= AcquireFactorCountDictionary(Math.Max(count, 8)); counts ??= ThreadStaticPools.RentUlongIntDictionary(Math.Max(count, 8));
CopyFactorsToDictionary(primes, exponents, count, counts); CopyFactorsToDictionary(primes, exponents, count, counts);
count = 0; count = 0;
useDictionary = true; useDictionary = true;
+124
View File
@@ -79,6 +79,130 @@ namespace PerfectNumbers.Core
} }
} }
[ThreadStatic]
private static List<List<ulong>>? _ulongListPool;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static List<ulong> RentUlongList(int capacityHint)
{
List<List<ulong>>? pool = _ulongListPool;
if (pool is not null && pool.Count > 0)
{
int lastIndex = pool.Count - 1;
List<ulong> list = pool[lastIndex];
pool.RemoveAt(lastIndex);
if (list.Count > 0)
{
list.Clear();
}
if (list.Capacity < capacityHint)
{
list.Capacity = capacityHint;
}
return list;
}
return new List<ulong>(capacityHint);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ReturnUlongList(List<ulong> list)
{
List<List<ulong>>? pool = _ulongListPool;
if (pool is null)
{
pool = new List<List<ulong>>(4);
_ulongListPool = pool;
}
pool.Add(list);
}
[ThreadStatic]
private static List<Stack<ulong>>? _ulongStackPool;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Stack<ulong> RentUlongStack(int capacityHint)
{
List<Stack<ulong>>? pool = _ulongStackPool;
if (pool is not null && pool.Count > 0)
{
int lastIndex = pool.Count - 1;
Stack<ulong> stack = pool[lastIndex];
pool.RemoveAt(lastIndex);
if (stack.Count > 0)
{
stack.Clear();
}
if (stack.Count < capacityHint)
{
stack.EnsureCapacity(capacityHint);
}
return stack;
}
return new Stack<ulong>(capacityHint);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ReturnUlongStack(Stack<ulong> stack)
{
List<Stack<ulong>>? pool = _ulongStackPool;
if (pool is null)
{
pool = new List<Stack<ulong>>(4);
_ulongStackPool = pool;
}
pool.Add(stack);
}
[ThreadStatic]
private static List<Dictionary<ulong, int>>? _ulongIntDictionaryPool;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Dictionary<ulong, int> RentUlongIntDictionary(int capacityHint)
{
List<Dictionary<ulong, int>>? pool = _ulongIntDictionaryPool;
if (pool is not null && pool.Count > 0)
{
int lastIndex = pool.Count - 1;
Dictionary<ulong, int> dictionary = pool[lastIndex];
pool.RemoveAt(lastIndex);
if (dictionary.Count > 0)
{
dictionary.Clear();
}
if (dictionary.Count < capacityHint)
{
dictionary.EnsureCapacity(capacityHint);
}
return dictionary;
}
return new Dictionary<ulong, int>(capacityHint);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ReturnUlongIntDictionary(Dictionary<ulong, int> dictionary)
{
List<Dictionary<ulong, int>>? pool = _ulongIntDictionaryPool;
if (pool is null)
{
pool = new List<Dictionary<ulong, int>>(4);
_ulongIntDictionaryPool = pool;
}
pool.Add(dictionary);
}
[ThreadStatic] [ThreadStatic]
private static Dictionary<ulong, MersenneDivisorCycles.FactorCacheEntry>? _mersenneFactorCacheDictionary; private static Dictionary<ulong, MersenneDivisorCycles.FactorCacheEntry>? _mersenneFactorCacheDictionary;
+1 -23
View File
@@ -1,5 +1,4 @@
using System; using System;
using System.Buffers;
using System.Numerics; using System.Numerics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using ILGPU.Algorithms; using ILGPU.Algorithms;
@@ -347,24 +346,7 @@ public static class ULongExtensions
ulong result = divisor.MontgomeryOne; ulong result = divisor.MontgomeryOne;
ulong nPrime = divisor.NPrime; ulong nPrime = divisor.NPrime;
const int StackOddPowerThreshold = PerfectNumberConstants.PooledArrayThreshold; Span<ulong> oddPowers = stackalloc ulong[oddPowerCount];
Span<ulong> oddPowersSpan = oddPowerCount <= StackOddPowerThreshold
? stackalloc ulong[StackOddPowerThreshold]
: default;
Span<ulong> oddPowers;
ulong[]? oddPowersArray = null;
if (!oddPowersSpan.IsEmpty)
{
oddPowers = oddPowersSpan.Slice(0, oddPowerCount);
}
else
{
oddPowersArray = ThreadStaticPools.UlongPool.Rent(oddPowerCount);
oddPowers = oddPowersArray.AsSpan(0, oddPowerCount);
}
InitializeMontgomeryOddPowersCpu(divisor, modulus, nPrime, oddPowers); InitializeMontgomeryOddPowersCpu(divisor, modulus, nPrime, oddPowers);
int index = bitLength - 1; int index = bitLength - 1;
@@ -403,10 +385,6 @@ public static class ULongExtensions
index = windowStart - 1; index = windowStart - 1;
} }
if (oddPowersArray is not null)
{
ThreadStaticPools.UlongPool.Return(oddPowersArray, clearArray: false);
}
if (keepMontgomery) if (keepMontgomery)
{ {