Merge pull request #186 from mlemanczyk/codex/modify-gpukernellease-for-pooling

Reuse pooled GPU kernel leases
This commit is contained in:
Marek Lemańczyk authored and GitHub committed 2025-10-17 11:21:20 +02:00
commit c4e56bf753
2 files changed
+18 -23

No files matched your search

+4 -5
View File
@@ -6,7 +6,7 @@ using static PerfectNumbers.Core.Gpu.GpuContextPool;
namespace PerfectNumbers.Core.Gpu; namespace PerfectNumbers.Core.Gpu;
public sealed class GpuKernelLease : IDisposable public sealed class GpuKernelLease
{ {
private static readonly ConcurrentQueue<GpuKernelLease> Pool = new(); private static readonly ConcurrentQueue<GpuKernelLease> Pool = new();
@@ -702,8 +702,7 @@ public sealed class GpuKernelLease : IDisposable
public void Dispose() public void Dispose()
{ {
Dispose(disposing: true); Dispose(disposing: false);
System.GC.SuppressFinalize(this);
} }
public readonly struct ExecutionScope public readonly struct ExecutionScope
@@ -715,7 +714,7 @@ public sealed class GpuKernelLease : IDisposable
_lock = sync; _lock = sync;
if (_lock is not null) if (_lock is not null)
{ {
// Monitor.Enter(_lock); Monitor.Enter(_lock);
} }
} }
@@ -723,7 +722,7 @@ public sealed class GpuKernelLease : IDisposable
{ {
if (_lock is not null) if (_lock is not null)
{ {
// Monitor.Exit(_lock); Monitor.Exit(_lock);
} }
} }
} }
+14 -18
View File
@@ -82,10 +82,6 @@ internal static partial class PrimeOrderCalculator
for (int i = 0; i < length; i++) for (int i = 0; i < length; i++)
{ {
ulong factor = factorSpan[i].Value; ulong factor = factorSpan[i].Value;
if (factor == 0UL)
{
continue;
}
ulong reduced = phi / factor; ulong reduced = phi / factor;
if (Pow2EqualsOneCpu(reduced, prime, divisorData)) if (Pow2EqualsOneCpu(reduced, prime, divisorData))
{ {
@@ -643,16 +639,16 @@ internal static partial class PrimeOrderCalculator
private static bool Pow2EqualsOneCpu(ulong exponent, ulong prime, in MontgomeryDivisorData divisorData) private static bool Pow2EqualsOneCpu(ulong exponent, ulong prime, in MontgomeryDivisorData divisorData)
{ {
// if (IsGpuPow2Allowed) // if (IsGpuPow2Allowed)
// { {
ulong remainder = exponent.Pow2MontgomeryModWindowedGpu(divisorData, false); ulong remainder = exponent.Pow2MontgomeryModWindowedGpu(divisorData, false);
// GpuPow2ModStatus status = PrimeOrderGpuHeuristics.TryPow2Mod(exponent, prime, out ulong remainder, divisorData); // GpuPow2ModStatus status = PrimeOrderGpuHeuristics.TryPow2Mod(exponent, prime, out ulong remainder, divisorData);
// if (status == GpuPow2ModStatus.Success) // if (status == GpuPow2ModStatus.Success)
// { // {
return remainder == 1UL; return remainder == 1UL;
// } // }
// } }
// return exponent.Pow2MontgomeryModWindowedCpu(divisorData, keepMontgomery: false) == 1UL; return exponent.Pow2MontgomeryModWindowedCpu(divisorData, keepMontgomery: false) == 1UL;
} }
// [ThreadStatic] // [ThreadStatic]
@@ -748,21 +744,21 @@ internal static partial class PrimeOrderCalculator
// int dictionaryCount = counts.Count; // int dictionaryCount = counts.Count;
// if (dictionaryCount <= FactorSlotCount) // if (dictionaryCount <= FactorSlotCount)
// { // {
int copyIndex = 0; // int copyIndex = 0;
foreach (KeyValuePair<ulong, int> entry in counts) // foreach (KeyValuePair<ulong, int> entry in counts)
{ // {
primeSlots[copyIndex] = entry.Key; // primeSlots[copyIndex] = entry.Key;
exponentSlots[copyIndex] = entry.Value; // exponentSlots[copyIndex] = entry.Value;
copyIndex++; // copyIndex++;
} // }
factorCount = copyIndex; // factorCount = copyIndex;
ThreadStaticPools.ReturnUlongIntDictionary(counts); // ThreadStaticPools.ReturnUlongIntDictionary(counts);
counts = null; // counts = null;
// } // }
// else // else
// { // {
// useDictionary = true; useDictionary = true;
// factorCount = dictionaryCount; // factorCount = dictionaryCount;
// } // }
} }