Tie GPU scratch buffers to context leases

This commit is contained in:
Marek Lemańczyk committed 2025-11-04 00:26:38 +01:00
1 parent 4c4d4243fb
commit dbde6337ea
3 files changed
+18 -12

No files matched your search

+8 -2
View File
@@ -13,13 +13,14 @@ 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
@@ -34,6 +35,7 @@ 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();
@@ -53,7 +55,9 @@ public static class GpuContextPool
}
// Create a new accelerator when the pool does not have one available.
return new GpuContextLease(new PooledContext(GpuKernelPool.GetKernels()));
KernelContainer kernels = GpuKernelPool.GetKernels();
var context = new PooledContext(kernels);
return new GpuContextLease(context);
}
@@ -111,6 +115,8 @@ public static class GpuContextPool
public Context Context => _ctx!.Context;
public Accelerator Accelerator => _ctx!.Accelerator;
public KernelContainer Kernels => _ctx!.Kernels;
public void Dispose()
{
if (_disposed)
+7 -8
View File
@@ -12,23 +12,22 @@ public sealed class GpuKernelLease
private static readonly ConcurrentQueue<GpuKernelLease> Pool = new();
private GpuContextLease? _gpu;
public KernelContainer Kernels;
public KernelContainer Kernels = null!;
private AcceleratorStream? _stream;
private GpuKernelLease(KernelContainer kernels)
private GpuKernelLease()
{
Kernels = kernels;
}
internal static GpuKernelLease Rent(GpuContextLease gpu, KernelContainer kernels)
internal static GpuKernelLease Rent(GpuContextLease gpu)
{
if (!Pool.TryDequeue(out var lease))
{
lease = new GpuKernelLease(kernels);
lease = new GpuKernelLease();
}
lease._gpu = gpu;
lease.Kernels = kernels;
lease.Kernels = gpu.Kernels;
lease._stream = null;
return lease;
}
@@ -156,9 +155,9 @@ public sealed class GpuKernelLease
_gpu?.Dispose();
_gpu = null;
Kernels = null!;
Kernels?.Dispose();
GpuPrimeWorkLimiter.Release();
GpuPrimeWorkLimiter.Release();
Pool.Enqueue(this);
}
+3 -2
View File
@@ -299,10 +299,11 @@ public class GpuKernelPool
public static GpuKernelLease GetKernel(bool useGpuOrder)
{
_ = useGpuOrder;
GpuPrimeWorkLimiter.Acquire();
var gpu = Rent();
var kernels = GetKernels();
return GpuKernelLease.Rent(gpu, kernels);
return GpuKernelLease.Rent(gpu);
}
/// <summary>