GpuScratchBuffer / -Pool naively integrated with PriemOrderCalculatorAccelerator (formerly Pow2MontgomeryAccelerator). FactorEntry replaced with 2 arrays - unused field removed. Small cleanup.

This commit is contained in:
mlemanczyk committed 2025-11-13 14:41:27 +01:00
1 parent 43863d03fb
commit c53aa517f7
14 files changed
+295 -420

No files matched your search

+62 -22
View File
@@ -1,5 +1,3 @@
using System.Buffers;
namespace PerfectNumbers.Core
{
public sealed class PartialFactorResult
@@ -7,7 +5,8 @@ namespace PerfectNumbers.Core
[ThreadStatic]
private static PartialFactorResult? s_poolHead;
public FactorEntry[]? Factors;
public ulong[]? Factors;
public int[]? Exponents;
private PartialFactorResult? _next;
public ulong Cofactor;
public bool FullyFactored;
@@ -18,7 +17,7 @@ namespace PerfectNumbers.Core
{
}
public static PartialFactorResult Rent(in FactorEntry[]? factors, ulong cofactor, bool fullyFactored, int count, bool cofactorIsPrime)
public static PartialFactorResult Rent(in ulong[]? factors, in int[]? exponents, ulong cofactor, bool fullyFactored, int count, bool cofactorIsPrime)
{
PartialFactorResult? instance = s_poolHead;
if (instance is null)
@@ -31,8 +30,31 @@ namespace PerfectNumbers.Core
}
instance._next = null;
instance.Factors = factors;
instance.Cofactor = cofactor;
instance.Factors = factors;
instance.Exponents = exponents;
instance.Cofactor = cofactor;
instance.FullyFactored = fullyFactored;
instance.Count = count;
instance.CofactorIsPrime = cofactorIsPrime;
return instance;
}
public static PartialFactorResult Rent(ulong cofactor, bool fullyFactored, int count, bool cofactorIsPrime)
{
PartialFactorResult? instance = s_poolHead;
if (instance is null)
{
instance = new PartialFactorResult();
}
else
{
s_poolHead = instance._next;
}
instance._next = null;
instance.Factors = null;
instance.Exponents = null;
instance.Cofactor = cofactor;
instance.FullyFactored = fullyFactored;
instance.Count = count;
instance.CofactorIsPrime = cofactorIsPrime;
@@ -46,32 +68,50 @@ namespace PerfectNumbers.Core
};
public PartialFactorResult WithAdditionalPrime(ulong prime)
{
FactorEntry[]? source = Factors;
if (source is null || Count == 0)
{
FactorEntry[] local = ThreadStaticPools.FactorEntryPool.Rent(1);
local[0] = new FactorEntry(prime, 1, true);
return Rent(local, 1UL, true, 1, false);
{
ulong[]? source = Factors;
int sourceCount = Count;
int[] exponents;
if (source is null || sourceCount == 0)
{
source = ThreadStaticPools.UlongPool.Rent(1);
exponents = ThreadStaticPools.IntPool.Rent(1);
// source = new ulong[1];
// exponents = new int[1];
source[0] = prime;
exponents[0] = 1;
return Rent(source, exponents, 1UL, true, 1, false);
}
FactorEntry[] extended = ThreadStaticPools.FactorEntryPool.Rent(Count + 1);
Array.Copy(source, 0, extended, 0, Count);
extended[Count] = new FactorEntry(prime, 1, true);
Span<FactorEntry> span = extended.AsSpan(0, Count + 1);
span.Sort(static (a, b) => a.Value.CompareTo(b.Value));
return Rent(extended, 1UL, true, Count + 1, false);
ulong[] extended = ThreadStaticPools.UlongPool.Rent(sourceCount + 1);
Array.Copy(source, 0, extended, 0, sourceCount);
extended[sourceCount] = prime;
exponents = ThreadStaticPools.IntPool.Rent(sourceCount + 1);
Array.Copy(Exponents!, 0, exponents, 0, sourceCount);
exponents[sourceCount] = 1;
Array.Sort(extended, exponents, 0, sourceCount + 1);
return Rent(extended, exponents, 1UL, true, sourceCount + 1, false);
}
public void Dispose()
{
if (this != Empty)
{
FactorEntry[]? factors = Factors;
ulong[]? factors = Factors;
if (factors is not null)
{
Factors = null;
ThreadStaticPools.FactorEntryPool.Return(factors, clearArray: false);
int[] exponents = Exponents!;
Factors = null;
Exponents = null;
// if (factors.Length > 1)
// {
ThreadStaticPools.UlongPool.Return(factors, clearArray: false);
ThreadStaticPools.IntPool.Return(exponents, clearArray: false);
// }
}
_next = s_poolHead;