Refine GPU prime tester leasing
This commit is contained in:
1 parent
6395b06b19
commit
08499fdda1
2 files changed
+20
-50
No files matched your search
@@ -48,7 +48,7 @@ internal static class PrimeTesterKernels
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
int length = XMath.Min((int)primes.Length, (int)primeSquares.Length);
|
int length = (int)primes.Length;
|
||||||
for (int i = 0; i < length; i++)
|
for (int i = 0; i < length; i++)
|
||||||
{
|
{
|
||||||
ulong prime = primes[i];
|
ulong prime = primes[i];
|
||||||
|
|||||||
@@ -110,7 +110,6 @@ public sealed class PrimeTester
|
|||||||
var gpu = PrimeTesterGpuContextPool.Rent(1);
|
var gpu = PrimeTesterGpuContextPool.Rent(1);
|
||||||
var state = gpu.State;
|
var state = gpu.State;
|
||||||
var accelerator = gpu.Accelerator;
|
var accelerator = gpu.Accelerator;
|
||||||
gpu.EnsureCapacity(1);
|
|
||||||
|
|
||||||
ulong value = n;
|
ulong value = n;
|
||||||
byte flag = 0;
|
byte flag = 0;
|
||||||
@@ -211,10 +210,7 @@ public sealed class PrimeTester
|
|||||||
|
|
||||||
private KernelState? _kernelState;
|
private KernelState? _kernelState;
|
||||||
|
|
||||||
internal MemoryBuffer1D<ulong, Stride1D.Dense>? InputBuffer;
|
|
||||||
internal MemoryBuffer1D<byte, Stride1D.Dense>? OutputBuffer;
|
|
||||||
internal int BufferCapacity;
|
|
||||||
|
|
||||||
public PooledContext()
|
public PooledContext()
|
||||||
{
|
{
|
||||||
Context = Context.CreateDefault();
|
Context = Context.CreateDefault();
|
||||||
@@ -237,36 +233,8 @@ public sealed class PrimeTester
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void EnsureBufferCapacity(int minCapacity)
|
|
||||||
{
|
|
||||||
if (minCapacity < 1)
|
|
||||||
{
|
|
||||||
minCapacity = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
var input = InputBuffer;
|
|
||||||
var output = OutputBuffer;
|
|
||||||
|
|
||||||
if (BufferCapacity >= minCapacity && input is not null && output is not null && !input.IsDisposed && !output.IsDisposed)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
input?.Dispose();
|
|
||||||
output?.Dispose();
|
|
||||||
|
|
||||||
BufferCapacity = minCapacity;
|
|
||||||
InputBuffer = Accelerator.Allocate1D<ulong>(BufferCapacity);
|
|
||||||
OutputBuffer = Accelerator.Allocate1D<byte>(BufferCapacity);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
InputBuffer?.Dispose();
|
|
||||||
OutputBuffer?.Dispose();
|
|
||||||
InputBuffer = null;
|
|
||||||
OutputBuffer = null;
|
|
||||||
BufferCapacity = 0;
|
|
||||||
ClearGpuCaches(Accelerator);
|
ClearGpuCaches(Accelerator);
|
||||||
_kernelState = null;
|
_kernelState = null;
|
||||||
Accelerator.Dispose();
|
Accelerator.Dispose();
|
||||||
@@ -276,7 +244,7 @@ public sealed class PrimeTester
|
|||||||
|
|
||||||
private static readonly ConcurrentQueue<PooledContext> Pool = new();
|
private static readonly ConcurrentQueue<PooledContext> Pool = new();
|
||||||
|
|
||||||
internal static PrimeTesterGpuContextLease Rent(int minBufferCapacity = 0)
|
internal static PrimeTesterGpuContextLease Rent(int minBufferCapacity = 1)
|
||||||
{
|
{
|
||||||
if (Pool.TryDequeue(out var ctx))
|
if (Pool.TryDequeue(out var ctx))
|
||||||
{
|
{
|
||||||
@@ -316,18 +284,18 @@ public sealed class PrimeTester
|
|||||||
|
|
||||||
internal PrimeTesterGpuContextLease(PooledContext ctx, int minBufferCapacity)
|
internal PrimeTesterGpuContextLease(PooledContext ctx, int minBufferCapacity)
|
||||||
{
|
{
|
||||||
if (minBufferCapacity < 1)
|
// The pool never requests a zero-length buffer, so the old guard stays commented out to document the invariant.
|
||||||
{
|
// if (minBufferCapacity < 1)
|
||||||
minBufferCapacity = 1;
|
// {
|
||||||
}
|
// minBufferCapacity = 1;
|
||||||
|
// }
|
||||||
|
|
||||||
_ctx = ctx;
|
_ctx = ctx;
|
||||||
Accelerator = ctx.Accelerator;
|
Accelerator = ctx.Accelerator;
|
||||||
State = ctx.KernelState;
|
State = ctx.KernelState;
|
||||||
ctx.EnsureBufferCapacity(minBufferCapacity);
|
Input = Accelerator.Allocate1D<ulong>(minBufferCapacity);
|
||||||
Input = ctx.InputBuffer ?? throw new InvalidOperationException("GPU context input buffer is not initialized.");
|
Output = Accelerator.Allocate1D<byte>(minBufferCapacity);
|
||||||
Output = ctx.OutputBuffer ?? throw new InvalidOperationException("GPU context output buffer is not initialized.");
|
BufferCapacity = minBufferCapacity;
|
||||||
BufferCapacity = ctx.BufferCapacity;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void EnsureCapacity(int minCapacity)
|
public void EnsureCapacity(int minCapacity)
|
||||||
@@ -337,17 +305,19 @@ public sealed class PrimeTester
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_ctx.EnsureBufferCapacity(minCapacity);
|
Input.Dispose();
|
||||||
Input = _ctx.InputBuffer ?? throw new InvalidOperationException("GPU context input buffer is not initialized.");
|
Output.Dispose();
|
||||||
Output = _ctx.OutputBuffer ?? throw new InvalidOperationException("GPU context output buffer is not initialized.");
|
|
||||||
BufferCapacity = _ctx.BufferCapacity;
|
Input = Accelerator.Allocate1D<ulong>(minCapacity);
|
||||||
|
Output = Accelerator.Allocate1D<byte>(minCapacity);
|
||||||
|
BufferCapacity = minCapacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
BufferCapacity = _ctx.BufferCapacity;
|
Input.Dispose();
|
||||||
Input = _ctx.InputBuffer ?? throw new InvalidOperationException("GPU context input buffer is not initialized.");
|
Output.Dispose();
|
||||||
Output = _ctx.OutputBuffer ?? throw new InvalidOperationException("GPU context output buffer is not initialized.");
|
BufferCapacity = 0;
|
||||||
Return(_ctx);
|
Return(_ctx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user