GpuKernelLease removed

This commit is contained in:
mlemanczyk committed 2025-11-07 15:36:45 +01:00
1 parent 0410fd2c91
commit 8e779c5c9e
12 files changed
+59 -106

No files matched your search

@@ -349,32 +349,32 @@ public class GpuKernelCompilationTests
private static void CompileGpuKernelPoolKernels(Accelerator accelerator) private static void CompileGpuKernelPoolKernels(Accelerator accelerator)
{ {
_ = accelerator; _ = accelerator;
var lease = GpuKernelPool.Rent(); GpuPrimeWorkLimiter.Acquire();
try try
{ {
_ = GpuKernelLease.OrderKernel; _ = GpuKernelPool.Kernels.Order;
_ = GpuKernelLease.IncrementalKernel; _ = GpuKernelPool.Kernels.Incremental;
_ = GpuKernelLease.Pow2ModKernel; _ = GpuKernelPool.Kernels.Pow2Mod;
_ = GpuKernelLease.IncrementalOrderKernel; _ = GpuKernelPool.Kernels.IncrementalOrder;
_ = GpuKernelLease.Pow2ModOrderKernel; _ = GpuKernelPool.Kernels.Pow2ModOrder;
} }
finally finally
{ {
lease.Dispose(); GpuPrimeWorkLimiter.Release();
} }
var orderLease = GpuKernelPool.Rent(); GpuPrimeWorkLimiter.Acquire();
try try
{ {
_ = GpuKernelLease.OrderKernel; _ = GpuKernelPool.Kernels.Order;
_ = GpuKernelLease.IncrementalKernel; _ = GpuKernelPool.Kernels.Incremental;
_ = GpuKernelLease.Pow2ModKernel; _ = GpuKernelPool.Kernels.Pow2Mod;
_ = GpuKernelLease.IncrementalOrderKernel; _ = GpuKernelPool.Kernels.IncrementalOrder;
_ = GpuKernelLease.Pow2ModOrderKernel; _ = GpuKernelPool.Kernels.Pow2ModOrder;
} }
finally finally
{ {
orderLease.Dispose(); GpuPrimeWorkLimiter.Release();
} }
} }
} }
+2 -2
View File
@@ -267,7 +267,7 @@ public sealed class DivisorCycleCache
private void ComputeCyclesGpuCore(ReadOnlySpan<ulong> divisors, Span<ulong> destination) private void ComputeCyclesGpuCore(ReadOnlySpan<ulong> divisors, Span<ulong> destination)
{ {
int length = divisors.Length; int length = divisors.Length;
var gpuLease = GpuKernelPool.Rent(); GpuPrimeWorkLimiter.Acquire();
Accelerator accelerator = SharedGpuContext.Accelerator; Accelerator accelerator = SharedGpuContext.Accelerator;
var stream = accelerator.CreateStream(); var stream = accelerator.CreateStream();
@@ -349,7 +349,7 @@ public sealed class DivisorCycleCache
ThreadStaticPools.BytePool.Return(rentedStatus!, clearArray: false); ThreadStaticPools.BytePool.Return(rentedStatus!, clearArray: false);
} }
gpuLease.Dispose(); GpuPrimeWorkLimiter.Release();
} }
private static Action<AcceleratorStream, Index1D, int, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<byte, Stride1D.Dense>> LoadKernel(Accelerator accelerator) private static Action<AcceleratorStream, Index1D, int, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<byte, Stride1D.Dense>> LoadKernel(Accelerator accelerator)
-40
View File
@@ -1,40 +0,0 @@
using System.Collections.Concurrent;
using ILGPU;
using ILGPU.Runtime;
namespace PerfectNumbers.Core.Gpu;
public sealed class GpuKernelLease
{
private static readonly ConcurrentQueue<GpuKernelLease> Pool = new();
internal static GpuKernelLease Rent()
{
if (!Pool.TryDequeue(out var lease))
{
lease = new GpuKernelLease();
}
return lease;
}
public static readonly Action<AcceleratorStream, Index1D, ulong, ulong, ArrayView<GpuUInt128>, ArrayView<ulong>> OrderKernel = GpuKernelPool.Kernels.Order!;
public static readonly Action<AcceleratorStream, Index1D, ulong, GpuUInt128, GpuUInt128, byte, ulong, ResidueAutomatonArgs, ArrayView<ulong>, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<uint, Stride1D.Dense>, ArrayView1D<uint, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>> Pow2ModKernel = GpuKernelPool.Kernels.Pow2Mod!;
public static readonly Action<AcceleratorStream, Index1D, ulong, GpuUInt128, GpuUInt128, byte, ulong, ulong, ulong, ulong, ulong, ArrayView<ulong>, ArrayView1D<ulong, Stride1D.Dense>> IncrementalKernel = GpuKernelPool.Kernels.Incremental!;
public static readonly Action<AcceleratorStream, Index1D, ulong, GpuUInt128, GpuUInt128, byte, ulong, ResidueAutomatonArgs, ArrayView<int>, ArrayView1D<ulong, Stride1D.Dense>> IncrementalOrderKernel = GpuKernelPool.Kernels.IncrementalOrder!;
public static readonly Action<AcceleratorStream, Index1D, ulong, GpuUInt128, GpuUInt128, byte, ulong, ResidueAutomatonArgs, ArrayView<int>, ArrayView1D<ulong, Stride1D.Dense>> Pow2ModOrderKernel = GpuKernelPool.Kernels.Pow2ModOrder!;
public static readonly Action<AcceleratorStream, Index1D, ulong, ArrayView1D<ulong, Stride1D.Dense>, int, MontgomeryDivisorData, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>> SpecialMaxKernel = GpuKernelPool.Kernels.SpecialMax!;
public static readonly Action<AcceleratorStream, Index1D, ulong, uint, ArrayView1D<uint, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>, int, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<int, Stride1D.Dense>, ArrayView1D<int, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>> SmallPrimeFactorKernel = GpuKernelPool.Kernels.SmallPrimeFactor!;
public void Dispose()
{
Pool.Enqueue(this);
GpuPrimeWorkLimiter.Release();
}
}
+4 -10
View File
@@ -22,7 +22,6 @@ public class GpuKernelPool
{ {
var kernels = new KernelContainer(); var kernels = new KernelContainer();
var stream = SharedGpuContext.Accelerator.CreateStream(); var stream = SharedGpuContext.Accelerator.CreateStream();
PreloadStaticTables(kernels, stream);
InitOnce(ref kernels.Order, () => InitOnce(ref kernels.Order, () =>
{ {
@@ -88,6 +87,8 @@ public class GpuKernelPool
return kernel.CreateLauncherDelegate<Action<AcceleratorStream, Index1D, ulong, uint, ArrayView1D<uint, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>, int, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<int, Stride1D.Dense>, ArrayView1D<int, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>>>(); return kernel.CreateLauncherDelegate<Action<AcceleratorStream, Index1D, ulong, uint, ArrayView1D<uint, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>, int, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<int, Stride1D.Dense>, ArrayView1D<int, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>>>();
}); });
PreloadStaticTables(kernels, stream);
stream.Synchronize(); stream.Synchronize();
stream.Dispose(); stream.Dispose();
return kernels; return kernels;
@@ -201,25 +202,18 @@ public class GpuKernelPool
return new SmallPrimeFactorViews(devicePrimes, deviceSquares); return new SmallPrimeFactorViews(devicePrimes, deviceSquares);
} }
public static GpuKernelLease Rent()
{
GpuPrimeWorkLimiter.Acquire();
var stream = SharedGpuContext.Accelerator.CreateStream();
return GpuKernelLease.Rent();
}
/// <summary> /// <summary>
/// Runs a GPU action with an acquired accelerator and stream. /// Runs a GPU action with an acquired accelerator and stream.
/// </summary> /// </summary>
/// <param name="action">Action to run with (Accelerator, Stream).</param> /// <param name="action">Action to run with (Accelerator, Stream).</param>
public static void Run(Action<Accelerator, AcceleratorStream> action) public static void Run(Action<Accelerator, AcceleratorStream> action)
{ {
var lease = Rent(); GpuPrimeWorkLimiter.Acquire();
var accelerator = SharedGpuContext.Accelerator; var accelerator = SharedGpuContext.Accelerator;
var stream = accelerator.CreateStream(); var stream = accelerator.CreateStream();
action(accelerator, stream); action(accelerator, stream);
stream.Synchronize(); stream.Synchronize();
stream.Dispose(); stream.Dispose();
lease.Dispose(); GpuPrimeWorkLimiter.Release();
} }
} }
@@ -95,7 +95,7 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester : IMersenneNumberDiv
ulong processedCount; ulong processedCount;
ulong lastProcessed; ulong lastProcessed;
var gpuLease = GpuKernelPool.Rent(); GpuPrimeWorkLimiter.Acquire();
var accelerator = SharedGpuContext.Accelerator; var accelerator = SharedGpuContext.Accelerator;
var stream = accelerator.CreateStream(); var stream = accelerator.CreateStream();
@@ -129,7 +129,7 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester : IMersenneNumberDiv
stream.Dispose(); stream.Dispose();
ReturnBatchResources(resources); ReturnBatchResources(resources);
// Monitor.Exit(gpuLease.ExecutionLock); // Monitor.Exit(gpuLease.ExecutionLock);
gpuLease.Dispose(); GpuPrimeWorkLimiter.Release();
if (composite) if (composite)
{ {
@@ -485,7 +485,6 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester : IMersenneNumberDiv
public sealed class DivisorScanSession : IMersenneNumberDivisorByDivisorTester.IDivisorScanSession public sealed class DivisorScanSession : IMersenneNumberDivisorByDivisorTester.IDivisorScanSession
{ {
private readonly MersenneNumberDivisorByDivisorGpuTester _owner; private readonly MersenneNumberDivisorByDivisorGpuTester _owner;
private readonly GpuKernelLease _lease;
private readonly Accelerator _accelerator; private readonly Accelerator _accelerator;
private readonly AcceleratorStream _stream; private readonly AcceleratorStream _stream;
private Action<AcceleratorStream, Index1D, ArrayView<GpuDivisorPartialData>, ArrayView<int>, ArrayView<int>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<byte>, ArrayView<int>> _kernel = null!; private Action<AcceleratorStream, Index1D, ArrayView<GpuDivisorPartialData>, ArrayView<int>, ArrayView<int>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<byte>, ArrayView<int>> _kernel = null!;
@@ -502,7 +501,7 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester : IMersenneNumberDiv
internal DivisorScanSession(MersenneNumberDivisorByDivisorGpuTester owner) internal DivisorScanSession(MersenneNumberDivisorByDivisorGpuTester owner)
{ {
_owner = owner; _owner = owner;
_lease = GpuKernelPool.Rent(); GpuPrimeWorkLimiter.Acquire();
Accelerator accelerator = SharedGpuContext.Accelerator; Accelerator accelerator = SharedGpuContext.Accelerator;
_accelerator = accelerator; _accelerator = accelerator;
_stream = accelerator.CreateStream(); _stream = accelerator.CreateStream();
@@ -16,7 +16,7 @@ public class MersenneNumberIncrementalGpuTester(GpuKernelType kernelType, bool u
{ {
throw new NotImplementedException($"GPU incremental scanning requires the device cycle heuristics implementation (kernel: {_kernelType}, GPU order: {_useGpuOrder})."); throw new NotImplementedException($"GPU incremental scanning requires the device cycle heuristics implementation (kernel: {_kernelType}, GPU order: {_useGpuOrder}).");
var gpuLease = GpuKernelPool.Rent(); GpuPrimeWorkLimiter.Acquire();
var accelerator = SharedGpuContext.Accelerator; var accelerator = SharedGpuContext.Accelerator;
var stream = accelerator.CreateStream(); var stream = accelerator.CreateStream();
int batchSize = GpuConstants.ScanBatchSize; // large batch improves GPU occupancy int batchSize = GpuConstants.ScanBatchSize; // large batch improves GPU occupancy
@@ -24,8 +24,9 @@ public class MersenneNumberIncrementalGpuTester(GpuKernelType kernelType, bool u
ulong divMul = (ulong)((((UInt128)1 << 64) - UInt128.One) / exponent) + 1UL; ulong divMul = (ulong)((((UInt128)1 << 64) - UInt128.One) / exponent) + 1UL;
byte last = lastDigit == LastDigit.Seven ? (byte)1 : (byte)0; // ILGPU kernels do not support bool parameters byte last = lastDigit == LastDigit.Seven ? (byte)1 : (byte)0; // ILGPU kernels do not support bool parameters
var pow2Kernel = GpuKernelLease.Pow2ModKernel; KernelContainer kernels = GpuKernelPool.Kernels;
var incKernel = GpuKernelLease.IncrementalKernel; var pow2Kernel = kernels.Pow2Mod!;
var incKernel = kernels.Incremental!;
ulong step10 = (exponent.Mod10() << 1).Mod10(); ulong step10 = (exponent.Mod10() << 1).Mod10();
ulong step8 = ((exponent & 7UL) << 1) & 7UL; ulong step8 = ((exponent & 7UL) << 1) & 7UL;
ulong step3 = ((exponent % 3UL) << 1) % 3UL; ulong step3 = ((exponent % 3UL) << 1) % 3UL;
@@ -124,6 +125,6 @@ public class MersenneNumberIncrementalGpuTester(GpuKernelType kernelType, bool u
stream.Dispose(); stream.Dispose();
ulongPool.Return(orderArray); ulongPool.Return(orderArray);
orderBuffer.Dispose(); orderBuffer.Dispose();
gpuLease.Dispose(); GpuPrimeWorkLimiter.Release();
} }
} }
@@ -9,7 +9,7 @@ public class MersenneNumberOrderGpuTester(GpuKernelType kernelType, bool useGpuO
public void Scan(ulong exponent, UInt128 twoP, LastDigit lastDigit, UInt128 maxK, ref bool isPrime) public void Scan(ulong exponent, UInt128 twoP, LastDigit lastDigit, UInt128 maxK, ref bool isPrime)
{ {
var gpuLease = GpuKernelPool.Rent(); GpuPrimeWorkLimiter.Acquire();
var accelerator = SharedGpuContext.Accelerator; var accelerator = SharedGpuContext.Accelerator;
var stream = accelerator.CreateStream(); var stream = accelerator.CreateStream();
int batchSize = GpuConstants.ScanBatchSize; // large batch improves GPU occupancy and avoids TDR int batchSize = GpuConstants.ScanBatchSize; // large batch improves GPU occupancy and avoids TDR
@@ -17,10 +17,11 @@ public class MersenneNumberOrderGpuTester(GpuKernelType kernelType, bool useGpuO
ulong divMul = (ulong)((((UInt128)1 << 64) - UInt128.One) / exponent) + 1UL; ulong divMul = (ulong)((((UInt128)1 << 64) - UInt128.One) / exponent) + 1UL;
byte last = lastDigit == LastDigit.Seven ? (byte)1 : (byte)0; // ILGPU kernels do not support bool parameters byte last = lastDigit == LastDigit.Seven ? (byte)1 : (byte)0; // ILGPU kernels do not support bool parameters
KernelContainer kernels = GpuKernelPool.Kernels;
var kernel = _kernelType switch var kernel = _kernelType switch
{ {
GpuKernelType.Pow2Mod => GpuKernelLease.Pow2ModOrderKernel, GpuKernelType.Pow2Mod => kernels.Pow2ModOrder!,
_ => GpuKernelLease.IncrementalOrderKernel, _ => kernels.IncrementalOrder!,
}; };
var foundBuffer = accelerator.Allocate1D<int>(1); // TODO: Replace this allocation with the pooled device buffer from GpuOrderKernelBenchmarks so repeated scans reuse the pinned staging memory instead of allocating per run. var foundBuffer = accelerator.Allocate1D<int>(1); // TODO: Replace this allocation with the pooled device buffer from GpuOrderKernelBenchmarks so repeated scans reuse the pinned staging memory instead of allocating per run.
@@ -46,8 +47,10 @@ public class MersenneNumberOrderGpuTester(GpuKernelType kernelType, bool useGpuO
var kernelArgs = new ResidueAutomatonArgs(q0m10, step10, q0m8, step8, q0m3, step3, q0m5, step5); var kernelArgs = new ResidueAutomatonArgs(q0m10, step10, q0m8, step8, q0m3, step3, q0m5, step5);
// Ensure device has small cycles table for early rejections in order kernels // Ensure device has small cycles table for early rejections in order kernels
kernel(stream, currentSize, exponent, (GpuUInt128)twoP, (GpuUInt128)kStart, last, divMul, kernel(
kernelArgs, foundBuffer.View, smallCyclesView); stream, currentSize, exponent, (GpuUInt128)twoP, (GpuUInt128)kStart,
last, divMul, kernelArgs, foundBuffer.View, smallCyclesView
);
foundBuffer.View.CopyToCPU(stream, ref found, 1); foundBuffer.View.CopyToCPU(stream, ref found, 1);
stream.Synchronize(); stream.Synchronize();
@@ -62,6 +65,6 @@ public class MersenneNumberOrderGpuTester(GpuKernelType kernelType, bool useGpuO
stream.Dispose(); stream.Dispose();
foundBuffer.Dispose(); foundBuffer.Dispose();
gpuLease.Dispose(); GpuPrimeWorkLimiter.Release();
} }
} }
@@ -2,10 +2,7 @@ using System.Buffers;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using ILGPU; using ILGPU;
using ILGPU.Runtime; using ILGPU.Runtime;
using System.Numerics;
using PerfectNumbers.Core;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using ILGPU.Runtime.OpenCL; using ILGPU.Runtime.OpenCL;
namespace PerfectNumbers.Core.Gpu; namespace PerfectNumbers.Core.Gpu;
@@ -198,7 +195,7 @@ internal static partial class PrimeOrderGpuHeuristics
try try
{ {
var lease = GpuKernelPool.Rent(); GpuPrimeWorkLimiter.Acquire();
Accelerator accelerator = SharedGpuContext.Accelerator; Accelerator accelerator = SharedGpuContext.Accelerator;
AcceleratorStream stream = accelerator.CreateStream(); AcceleratorStream stream = accelerator.CreateStream();
@@ -254,7 +251,7 @@ internal static partial class PrimeOrderGpuHeuristics
countBuffer.Dispose(); countBuffer.Dispose();
remainingBuffer.Dispose(); remainingBuffer.Dispose();
fullyFactoredBuffer.Dispose(); fullyFactoredBuffer.Dispose();
lease.Dispose(); GpuPrimeWorkLimiter.Release();
return true; return true;
} }
catch (CLException ex) catch (CLException ex)
@@ -366,7 +363,7 @@ internal static partial class PrimeOrderGpuHeuristics
{ {
order = 0UL; order = 0UL;
var lease = GpuKernelPool.Rent(); GpuPrimeWorkLimiter.Acquire();
Accelerator accelerator = SharedGpuContext.Accelerator; Accelerator accelerator = SharedGpuContext.Accelerator;
AcceleratorStream stream = accelerator.CreateStream(); AcceleratorStream stream = accelerator.CreateStream();
@@ -444,7 +441,7 @@ internal static partial class PrimeOrderGpuHeuristics
stackProductBuffer.Dispose(); stackProductBuffer.Dispose();
resultBuffer.Dispose(); resultBuffer.Dispose();
statusBuffer.Dispose(); statusBuffer.Dispose();
lease.Dispose(); GpuPrimeWorkLimiter.Release();
PrimeOrderKernelStatus kernelStatus = (PrimeOrderKernelStatus)status; PrimeOrderKernelStatus kernelStatus = (PrimeOrderKernelStatus)status;
if (kernelStatus == PrimeOrderKernelStatus.Fallback) if (kernelStatus == PrimeOrderKernelStatus.Fallback)
@@ -519,7 +516,7 @@ internal static partial class PrimeOrderGpuHeuristics
private static bool TryComputeOnGpu(ReadOnlySpan<ulong> exponents, ulong prime, in MontgomeryDivisorData divisorData, Span<ulong> results) private static bool TryComputeOnGpu(ReadOnlySpan<ulong> exponents, ulong prime, in MontgomeryDivisorData divisorData, Span<ulong> results)
{ {
var lease = GpuKernelPool.Rent(); GpuPrimeWorkLimiter.Acquire();
Accelerator accelerator = SharedGpuContext.Accelerator; Accelerator accelerator = SharedGpuContext.Accelerator;
AcceleratorStream? stream = accelerator.CreateStream(); AcceleratorStream? stream = accelerator.CreateStream();
var kernel = GetPow2ModKernel(accelerator); var kernel = GetPow2ModKernel(accelerator);
@@ -544,7 +541,7 @@ internal static partial class PrimeOrderGpuHeuristics
stream = null; stream = null;
exponentBuffer.Dispose(); exponentBuffer.Dispose();
remainderBuffer.Dispose(); remainderBuffer.Dispose();
lease.Dispose(); GpuPrimeWorkLimiter.Release();
} }
catch (Exception) catch (Exception)
{ {
@@ -552,7 +549,7 @@ internal static partial class PrimeOrderGpuHeuristics
stream?.Dispose(); stream?.Dispose();
exponentBuffer.Dispose(); exponentBuffer.Dispose();
remainderBuffer.Dispose(); remainderBuffer.Dispose();
lease.Dispose(); GpuPrimeWorkLimiter.Release();
throw; throw;
} }
@@ -578,7 +575,7 @@ internal static partial class PrimeOrderGpuHeuristics
GpuUInt128[]? rentedExponents = null; GpuUInt128[]? rentedExponents = null;
GpuUInt128[]? rentedResults = null; GpuUInt128[]? rentedResults = null;
var lease = GpuKernelPool.Rent(); GpuPrimeWorkLimiter.Acquire();
ArrayPool<GpuUInt128> gpuUInt128Pool = ThreadStaticPools.GpuUInt128Pool; ArrayPool<GpuUInt128> gpuUInt128Pool = ThreadStaticPools.GpuUInt128Pool;
try try
@@ -639,7 +636,7 @@ internal static partial class PrimeOrderGpuHeuristics
gpuUInt128Pool.Return(rentedResults!, clearArray: false); gpuUInt128Pool.Return(rentedResults!, clearArray: false);
} }
lease.Dispose(); GpuPrimeWorkLimiter.Release();
} }
} }
+2 -3
View File
@@ -795,7 +795,6 @@ public class MersenneDivisorCycles
// Prepare output file with header // Prepare output file with header
ulong start = 3UL; ulong start = 3UL;
var locker = new object();
var pool = ThreadStaticPools.UlongPool; var pool = ThreadStaticPools.UlongPool;
ulong batchSizeUL = (ulong)batchSize, d, end; ulong batchSizeUL = (ulong)batchSize, d, end;
@@ -804,7 +803,7 @@ public class MersenneDivisorCycles
divisors = pool.Rent(count); divisors = pool.Rent(count);
outCycles = pool.Rent(count); outCycles = pool.Rent(count);
var lease = GpuKernelPool.Rent(); GpuPrimeWorkLimiter.Acquire();
var accelerator = SharedGpuContext.Accelerator; var accelerator = SharedGpuContext.Accelerator;
var stream = accelerator.CreateStream(); var stream = accelerator.CreateStream();
var loaded = accelerator.LoadAutoGroupedStreamKernel< var loaded = accelerator.LoadAutoGroupedStreamKernel<
@@ -898,7 +897,7 @@ public class MersenneDivisorCycles
pool.Return(divisors, clearArray: false); pool.Return(divisors, clearArray: false);
pool.Return(outCycles, clearArray: false); pool.Return(outCycles, clearArray: false);
lease.Dispose(); GpuPrimeWorkLimiter.Release();
} }
+3 -3
View File
@@ -147,10 +147,10 @@ public sealed class MersenneNumberTester(
UInt128[] qs = qsBuffer; UInt128[] qs = qsBuffer;
var gpuLease = GpuKernelPool.Rent(); GpuPrimeWorkLimiter.Acquire();
var accelerator = SharedGpuContext.Accelerator; var accelerator = SharedGpuContext.Accelerator;
var stream = accelerator.CreateStream(); var stream = accelerator.CreateStream();
var orderKernel = GpuKernelLease.OrderKernel!; var orderKernel = GpuKernelPool.Kernels.Order!;
// Guard long-running kernels by chunking the warm-up across batches. // Guard long-running kernels by chunking the warm-up across batches.
// Reuse the same ScanBatchSize knob used for scanning. // Reuse the same ScanBatchSize knob used for scanning.
@@ -197,7 +197,7 @@ public sealed class MersenneNumberTester(
} }
stream.Dispose(); stream.Dispose();
gpuLease.Dispose(); GpuPrimeWorkLimiter.Release();
uInt128Pool.Return(qs); uInt128Pool.Return(qs);
} }
@@ -19,7 +19,7 @@ internal static partial class PrimeOrderCalculator
Span<int> exponentBuffer = exponentBufferArray.AsSpan(0, GpuSmallPrimeFactorSlots); Span<int> exponentBuffer = exponentBufferArray.AsSpan(0, GpuSmallPrimeFactorSlots);
remaining = value; remaining = value;
var lease = GpuKernelPool.Rent(); GpuPrimeWorkLimiter.Acquire();
Accelerator accelerator = SharedGpuContext.Accelerator; Accelerator accelerator = SharedGpuContext.Accelerator;
var stream = accelerator.CreateStream(); var stream = accelerator.CreateStream();
KernelContainer kernels = GpuKernelPool.Kernels; KernelContainer kernels = GpuKernelPool.Kernels;
@@ -31,7 +31,7 @@ internal static partial class PrimeOrderCalculator
if (kernels.SmallPrimeFactorsPrimes is null) throw new NullReferenceException($"{nameof(kernels.SmallPrimeFactorsPrimes)} is null"); if (kernels.SmallPrimeFactorsPrimes is null) throw new NullReferenceException($"{nameof(kernels.SmallPrimeFactorsPrimes)} is null");
if (kernels.SmallPrimeFactorsSquares is null) throw new NullReferenceException($"{nameof(kernels.SmallPrimeFactorsSquares)} is null"); if (kernels.SmallPrimeFactorsSquares is null) throw new NullReferenceException($"{nameof(kernels.SmallPrimeFactorsSquares)} is null");
var kernel = GpuKernelLease.SmallPrimeFactorKernel; var kernel = GpuKernelPool.Kernels.SmallPrimeFactor!;
ArrayView1D<int, Stride1D.Dense> smallPrimeFactorCountSlotView = scratch.SmallPrimeFactorCountSlot.View; ArrayView1D<int, Stride1D.Dense> smallPrimeFactorCountSlotView = scratch.SmallPrimeFactorCountSlot.View;
ArrayView1D<ulong, Stride1D.Dense> smallPrimeFactorRemainingSlotView = scratch.SmallPrimeFactorRemainingSlot.View; ArrayView1D<ulong, Stride1D.Dense> smallPrimeFactorRemainingSlotView = scratch.SmallPrimeFactorRemainingSlot.View;
@@ -78,7 +78,7 @@ internal static partial class PrimeOrderCalculator
} }
GpuScratchBufferPool.Return(scratch); GpuScratchBufferPool.Return(scratch);
lease.Dispose(); GpuPrimeWorkLimiter.Release();
return true; return true;
} }
@@ -101,7 +101,7 @@ internal static partial class PrimeOrderCalculator
factorSpan[i] = factors[i].Value; factorSpan[i] = factors[i].Value;
} }
GpuKernelLease lease = GpuKernelPool.Rent(); GpuPrimeWorkLimiter.Acquire();
Accelerator accelerator = SharedGpuContext.Accelerator; Accelerator accelerator = SharedGpuContext.Accelerator;
var stream = accelerator.CreateStream(); var stream = accelerator.CreateStream();
@@ -111,7 +111,7 @@ internal static partial class PrimeOrderCalculator
ArrayView1D<ulong, Stride1D.Dense> specialMaxFactorsView = scratch.SpecialMaxFactors.View; ArrayView1D<ulong, Stride1D.Dense> specialMaxFactorsView = scratch.SpecialMaxFactors.View;
specialMaxFactorsView.SubView(0, factorCount).CopyFromCPU(stream, ref MemoryMarshal.GetReference(factorSpan), factorCount); specialMaxFactorsView.SubView(0, factorCount).CopyFromCPU(stream, ref MemoryMarshal.GetReference(factorSpan), factorCount);
var kernel = GpuKernelLease.SpecialMaxKernel; var kernel = GpuKernelPool.Kernels.SpecialMax!;
ArrayView1D<ulong, Stride1D.Dense> specialMaxResultView = scratch.SpecialMaxResult.View; ArrayView1D<ulong, Stride1D.Dense> specialMaxResultView = scratch.SpecialMaxResult.View;
@@ -134,7 +134,7 @@ internal static partial class PrimeOrderCalculator
// stream.Synchronize(); // stream.Synchronize();
// return result != 0; // return result != 0;
GpuScratchBufferPool.Return(scratch); GpuScratchBufferPool.Return(scratch);
lease.Dispose(); GpuPrimeWorkLimiter.Release();
return result[0] != 0; return result[0] != 0;
} }
} }
+2 -2
View File
@@ -461,7 +461,7 @@ public static partial class ULongExtensions
{ {
ulong result = 0UL; ulong result = 0UL;
GpuKernelLease lease = GpuKernelPool.Rent(); GpuPrimeWorkLimiter.Acquire();
Accelerator accelerator = SharedGpuContext.Accelerator; Accelerator accelerator = SharedGpuContext.Accelerator;
AcceleratorStream stream = accelerator.CreateStream(); AcceleratorStream stream = accelerator.CreateStream();
@@ -484,7 +484,7 @@ public static partial class ULongExtensions
stream.Dispose(); stream.Dispose();
resultBuffer.Dispose(); resultBuffer.Dispose();
exponentBuffer.Dispose(); exponentBuffer.Dispose();
lease.Dispose(); GpuPrimeWorkLimiter.Release();
return result; return result;
} }