GPU contexts and accelerators made shared to prevent application crashes with high no. of threads.
This commit is contained in:
1 parent
b33a3c8e75
commit
0e7bbae132
2 files changed
+27
-17
No files matched your search
@@ -12,13 +12,17 @@ public static class GpuContextPool
|
|||||||
// Default device preference for generic GPU kernels (prime scans, NTT, etc.)
|
// Default device preference for generic GPU kernels (prime scans, NTT, etc.)
|
||||||
internal sealed class PooledContext
|
internal sealed class PooledContext
|
||||||
{
|
{
|
||||||
public Context Context { get; }
|
public readonly Context Context;
|
||||||
public Accelerator Accelerator { get; }
|
public readonly Accelerator Accelerator;
|
||||||
|
|
||||||
|
private static readonly Context _sharedContext = Context.CreateDefault();
|
||||||
|
private static readonly Device _sharedDevice = _sharedContext.GetPreferredDevice(false);
|
||||||
|
private static readonly Accelerator _sharedAccelerator = _sharedDevice.CreateAccelerator(_sharedContext);
|
||||||
|
|
||||||
public PooledContext()
|
public PooledContext()
|
||||||
{
|
{
|
||||||
Context = Context.CreateDefault();
|
Context = _sharedContext;
|
||||||
Accelerator = Context.GetPreferredDevice(false).CreateAccelerator(Context);
|
Accelerator = _sharedAccelerator;
|
||||||
// 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
|
||||||
@@ -34,8 +38,9 @@ public static class GpuContextPool
|
|||||||
// after the accelerator is destroyed.
|
// after the accelerator is destroyed.
|
||||||
PrimeTester.ClearGpuCaches(Accelerator);
|
PrimeTester.ClearGpuCaches(Accelerator);
|
||||||
|
|
||||||
Accelerator.Dispose();
|
// These resources are shared between GPU leases
|
||||||
Context.Dispose();
|
// Accelerator.Dispose();
|
||||||
|
// Context.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,15 +67,15 @@ public static class GpuContextPool
|
|||||||
}
|
}
|
||||||
|
|
||||||
int toCreate = threadCount - WarmedGpuContextCount;
|
int toCreate = threadCount - WarmedGpuContextCount;
|
||||||
|
Console.WriteLine($"Warming up GPU context pool to {toCreate} contexts...");
|
||||||
var contexts = new PooledContext[toCreate];
|
var contexts = new PooledContext[toCreate];
|
||||||
|
var pool = GpuPool;
|
||||||
|
PooledContext context;
|
||||||
for (int i = 0; i < toCreate; i++)
|
for (int i = 0; i < toCreate; i++)
|
||||||
{
|
{
|
||||||
contexts[i] = new PooledContext();
|
context = new PooledContext();
|
||||||
}
|
contexts[i] = context;
|
||||||
|
pool.Enqueue(context);
|
||||||
for (int i = 0; i < toCreate; i++)
|
|
||||||
{
|
|
||||||
GpuPool.Enqueue(contexts[i]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WarmedGpuContextCount = threadCount;
|
WarmedGpuContextCount = threadCount;
|
||||||
|
|||||||
@@ -378,10 +378,14 @@ public sealed class PrimeTester
|
|||||||
|
|
||||||
public HeuristicGpuDivisorTables HeuristicGpuTables => _heuristicDivisorTables;
|
public HeuristicGpuDivisorTables HeuristicGpuTables => _heuristicDivisorTables;
|
||||||
|
|
||||||
|
private static readonly Context _sharedContext = Context.CreateDefault();
|
||||||
|
private static readonly Device _sharedDevice = _sharedContext.GetPreferredDevice(false);
|
||||||
|
private static readonly Accelerator _sharedAccelerator = _sharedDevice.CreateAccelerator(_sharedContext);
|
||||||
|
|
||||||
internal PrimeTesterGpuContextLease(int minBufferCapacity)
|
internal PrimeTesterGpuContextLease(int minBufferCapacity)
|
||||||
{
|
{
|
||||||
AcceleratorContext = Context.CreateDefault();
|
AcceleratorContext = _sharedContext;// Context.CreateDefault();
|
||||||
Accelerator = AcceleratorContext.GetPreferredDevice(false).CreateAccelerator(AcceleratorContext);
|
Accelerator = _sharedAccelerator; //AcceleratorContext.GetPreferredDevice(false).CreateAccelerator(AcceleratorContext);
|
||||||
Kernel = Accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<ulong>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<byte>>(PrimeTesterKernels.SmallPrimeSieveKernel);
|
Kernel = Accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<ulong>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<byte>>(PrimeTesterKernels.SmallPrimeSieveKernel);
|
||||||
HeuristicTrialDivisionKernel = Accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<int>, ulong, ulong, HeuristicGpuDivisorTableKind, HeuristicGpuDivisorTables>(PrimeTesterKernels.HeuristicTrialDivisionKernel);
|
HeuristicTrialDivisionKernel = Accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<int>, ulong, ulong, HeuristicGpuDivisorTableKind, HeuristicGpuDivisorTables>(PrimeTesterKernels.HeuristicTrialDivisionKernel);
|
||||||
|
|
||||||
@@ -439,7 +443,7 @@ public sealed class PrimeTester
|
|||||||
}
|
}
|
||||||
|
|
||||||
_returned = true;
|
_returned = true;
|
||||||
PrimeTesterGpuContextPool.Return(this);
|
Return(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void DisposeResources()
|
internal void DisposeResources()
|
||||||
@@ -448,8 +452,9 @@ public sealed class PrimeTester
|
|||||||
Output?.Dispose();
|
Output?.Dispose();
|
||||||
HeuristicFlag?.Dispose();
|
HeuristicFlag?.Dispose();
|
||||||
ReleaseSharedTables(_sharedTables);
|
ReleaseSharedTables(_sharedTables);
|
||||||
Accelerator.Dispose();
|
// These resources are shared between GPU leases
|
||||||
AcceleratorContext.Dispose();
|
// Accelerator.Dispose();
|
||||||
|
// AcceleratorContext.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user