diff --git a/PerfectNumbers.Core/Gpu/GpuContextPool.cs b/PerfectNumbers.Core/Gpu/GpuContextPool.cs index eb9d26c..ae11bda 100644 --- a/PerfectNumbers.Core/Gpu/GpuContextPool.cs +++ b/PerfectNumbers.Core/Gpu/GpuContextPool.cs @@ -13,14 +13,13 @@ public static class GpuContextPool { public readonly Context Context; public readonly Accelerator Accelerator; - public readonly KernelContainer Kernels; public PooledContext(KernelContainer kernels) { Context = SharedGpuContext.Context; Accelerator = SharedGpuContext.Accelerator; - Kernels = kernels; GpuStaticTableInitializer.EnsureStaticTables(kernels, Accelerator); + kernels.Dispose(); // NOTE: Avoid loading/compiling any kernel here to prevent implicit // CL stream/queue creation during accelerator construction. // Some OpenCL drivers are fragile when a queue is created immediately @@ -35,7 +34,6 @@ public static class GpuContextPool // Clear any per-accelerator cached resources to avoid releasing // after the accelerator is destroyed. PrimeTester.ClearGpuCaches(Accelerator); - Kernels.Dispose(); // These resources are shared between GPU leases // Accelerator.Dispose(); @@ -55,9 +53,7 @@ public static class GpuContextPool } // Create a new accelerator when the pool does not have one available. - KernelContainer kernels = GpuKernelPool.GetKernels(); - var context = new PooledContext(kernels); - return new GpuContextLease(context); + return new GpuContextLease(new PooledContext(GpuKernelPool.GetKernels())); } @@ -115,8 +111,6 @@ public static class GpuContextPool public Context Context => _ctx!.Context; public Accelerator Accelerator => _ctx!.Accelerator; - public KernelContainer Kernels => _ctx!.Kernels; - public void Dispose() { if (_disposed) diff --git a/PerfectNumbers.Core/Gpu/GpuKernelLease.cs b/PerfectNumbers.Core/Gpu/GpuKernelLease.cs index fb48461..1db24b1 100644 --- a/PerfectNumbers.Core/Gpu/GpuKernelLease.cs +++ b/PerfectNumbers.Core/Gpu/GpuKernelLease.cs @@ -12,22 +12,23 @@ public sealed class GpuKernelLease private static readonly ConcurrentQueue Pool = new(); private GpuContextLease? _gpu; - public KernelContainer Kernels = null!; + public KernelContainer Kernels; private AcceleratorStream? _stream; - private GpuKernelLease() + private GpuKernelLease(KernelContainer kernels) { + Kernels = kernels; } - internal static GpuKernelLease Rent(GpuContextLease gpu) + internal static GpuKernelLease Rent(GpuContextLease gpu, KernelContainer kernels) { if (!Pool.TryDequeue(out var lease)) { - lease = new GpuKernelLease(); + lease = new GpuKernelLease(kernels); } lease._gpu = gpu; - lease.Kernels = gpu.Kernels; + lease.Kernels = kernels; lease._stream = null; return lease; } @@ -155,9 +156,9 @@ public sealed class GpuKernelLease _gpu?.Dispose(); _gpu = null; - Kernels = null!; - GpuPrimeWorkLimiter.Release(); + Kernels?.Dispose(); + GpuPrimeWorkLimiter.Release(); Pool.Enqueue(this); } diff --git a/PerfectNumbers.Core/Gpu/GpuKernelPool.cs b/PerfectNumbers.Core/Gpu/GpuKernelPool.cs index e445f6f..365b4d6 100644 --- a/PerfectNumbers.Core/Gpu/GpuKernelPool.cs +++ b/PerfectNumbers.Core/Gpu/GpuKernelPool.cs @@ -299,11 +299,10 @@ public class GpuKernelPool public static GpuKernelLease GetKernel(bool useGpuOrder) { - _ = useGpuOrder; - GpuPrimeWorkLimiter.Acquire(); var gpu = Rent(); - return GpuKernelLease.Rent(gpu); + var kernels = GetKernels(); + return GpuKernelLease.Rent(gpu, kernels); } ///