Restore per-lease kernel containers

This commit is contained in:
Marek Lemańczyk committed 2025-11-04 00:46:59 +01:00
1 parent dbde6337ea
commit 5a17fa64d5
3 files changed
+11 -17

No files matched your search

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