PrimeOrderCalculator.PartiaFactorCpu optimized to use one-time allocation list and stack.
This commit is contained in:
1 parent
9fcd21b734
commit
a185149eba
3 files changed
+7
-12
No files matched your search
@@ -2,7 +2,7 @@ using System.Runtime.CompilerServices;
|
|||||||
|
|
||||||
namespace PerfectNumbers.Core;
|
namespace PerfectNumbers.Core;
|
||||||
|
|
||||||
internal sealed class PartialFactorPendingEntry(ulong value, bool knownComposite)
|
public sealed class PartialFactorPendingEntry(ulong value, bool knownComposite)
|
||||||
{
|
{
|
||||||
[ThreadStatic]
|
[ThreadStatic]
|
||||||
private static PartialFactorPendingEntry? _poolHead;
|
private static PartialFactorPendingEntry? _poolHead;
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ namespace PerfectNumbers.Core
|
|||||||
|
|
||||||
public readonly ulong[] Factors = new ulong[PrimeOrderConstants.GpuSmallPrimeFactorSlots];
|
public readonly ulong[] Factors = new ulong[PrimeOrderConstants.GpuSmallPrimeFactorSlots];
|
||||||
public readonly int[] Exponents = new int[PrimeOrderConstants.GpuSmallPrimeFactorSlots];
|
public readonly int[] Exponents = new int[PrimeOrderConstants.GpuSmallPrimeFactorSlots];
|
||||||
|
public readonly FixedCapacityStack<ulong> CompositeStack = new (PrimeOrderConstants.GpuSmallPrimeFactorSlots);
|
||||||
|
public readonly List<PartialFactorPendingEntry> PendingFactors = new (PrimeOrderConstants.GpuSmallPrimeFactorSlots);
|
||||||
private PartialFactorResult? _next;
|
private PartialFactorResult? _next;
|
||||||
public ulong Cofactor;
|
public ulong Cofactor;
|
||||||
public bool FullyFactored;
|
public bool FullyFactored;
|
||||||
|
|||||||
@@ -1310,7 +1310,8 @@ internal static partial class PrimeOrderCalculator
|
|||||||
// primeSlots.Clear();
|
// primeSlots.Clear();
|
||||||
// exponentSlots.Clear();
|
// exponentSlots.Clear();
|
||||||
|
|
||||||
List<PartialFactorPendingEntry> pending = ThreadStaticPools.RentPrimeOrderPendingEntryList(PrimeOrderConstants.GpuSmallPrimeFactorSlots);
|
List<PartialFactorPendingEntry> pending = result.PendingFactors;
|
||||||
|
pending.Clear();
|
||||||
|
|
||||||
PopulateSmallPrimeFactorsCpu(
|
PopulateSmallPrimeFactorsCpu(
|
||||||
value,
|
value,
|
||||||
@@ -1325,7 +1326,8 @@ internal static partial class PrimeOrderCalculator
|
|||||||
if (config.PollardRhoMilliseconds > 0)
|
if (config.PollardRhoMilliseconds > 0)
|
||||||
{
|
{
|
||||||
long deadlineTimestamp = CreateDeadlineTimestamp(config.PollardRhoMilliseconds);
|
long deadlineTimestamp = CreateDeadlineTimestamp(config.PollardRhoMilliseconds);
|
||||||
FixedCapacityStack<ulong> compositeStack = ThreadStaticPools.RentUlongStack(PrimeOrderConstants.GpuSmallPrimeFactorSlots);
|
FixedCapacityStack<ulong> compositeStack = result.CompositeStack;
|
||||||
|
compositeStack.Clear();
|
||||||
compositeStack.Push(remaining);
|
compositeStack.Push(remaining);
|
||||||
|
|
||||||
CollectFactorsCpu(primeSlots, exponentSlots, ref factorCount, pending, compositeStack, deadlineTimestamp, out bool limitReached);
|
CollectFactorsCpu(primeSlots, exponentSlots, ref factorCount, pending, compositeStack, deadlineTimestamp, out bool limitReached);
|
||||||
@@ -1337,9 +1339,6 @@ internal static partial class PrimeOrderCalculator
|
|||||||
pending.Add(PartialFactorPendingEntry.Rent(compositeStack.Pop(), knownComposite: false));
|
pending.Add(PartialFactorPendingEntry.Rent(compositeStack.Pop(), knownComposite: false));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
compositeStack.Clear();
|
|
||||||
ThreadStaticPools.ReturnUlongStack(compositeStack);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -1351,10 +1350,6 @@ internal static partial class PrimeOrderCalculator
|
|||||||
bool cofactorContainsComposite = false;
|
bool cofactorContainsComposite = false;
|
||||||
int pendingCount = pending.Count;
|
int pendingCount = pending.Count;
|
||||||
int index = 0;
|
int index = 0;
|
||||||
if (pending.Capacity > PrimeOrderConstants.GpuSmallPrimeFactorSlots)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"Initial capacity is too small. Required capacity: {pending.Capacity}");
|
|
||||||
}
|
|
||||||
|
|
||||||
for (; index < pendingCount; index++)
|
for (; index < pendingCount; index++)
|
||||||
{
|
{
|
||||||
@@ -1427,8 +1422,6 @@ internal static partial class PrimeOrderCalculator
|
|||||||
result.Count = factorCount;
|
result.Count = factorCount;
|
||||||
result.CofactorIsPrime = cofactorIsPrime;
|
result.CofactorIsPrime = cofactorIsPrime;
|
||||||
|
|
||||||
pending.Clear();
|
|
||||||
ThreadStaticPools.ReturnPrimeOrderPendingEntryList(pending);
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user