PooledContext removal - continuation. GpuKernelLease prepared for removal.

This commit is contained in:
mlemanczyk committed 2025-11-07 14:13:11 +01:00
1 parent f4e365194d
commit 0410fd2c91
20 files changed
+192 -267

No files matched your search

@@ -352,11 +352,11 @@ public class GpuKernelCompilationTests
var lease = GpuKernelPool.Rent();
try
{
_ = lease.OrderKernel;
_ = lease.IncrementalKernel;
_ = lease.Pow2ModKernel;
_ = lease.IncrementalOrderKernel;
_ = lease.Pow2ModOrderKernel;
_ = GpuKernelLease.OrderKernel;
_ = GpuKernelLease.IncrementalKernel;
_ = GpuKernelLease.Pow2ModKernel;
_ = GpuKernelLease.IncrementalOrderKernel;
_ = GpuKernelLease.Pow2ModOrderKernel;
}
finally
{
@@ -366,11 +366,11 @@ public class GpuKernelCompilationTests
var orderLease = GpuKernelPool.Rent();
try
{
_ = orderLease.OrderKernel;
_ = orderLease.IncrementalKernel;
_ = orderLease.Pow2ModKernel;
_ = orderLease.IncrementalOrderKernel;
_ = orderLease.Pow2ModOrderKernel;
_ = GpuKernelLease.OrderKernel;
_ = GpuKernelLease.IncrementalKernel;
_ = GpuKernelLease.Pow2ModKernel;
_ = GpuKernelLease.IncrementalOrderKernel;
_ = GpuKernelLease.Pow2ModOrderKernel;
}
finally
{
@@ -150,39 +150,31 @@ public class NttGpuMathTests
var modulus = new GpuUInt128(0UL, 17UL);
var primitiveRoot = new GpuUInt128(0UL, 3UL);
GpuContextLease gpu = GpuContextPool.Rent();
try
var accelerator = SharedGpuContext.Accelerator;
var stream = accelerator.CreateStream();
var method = typeof(NttGpuMath).GetMethod("GetSquareCache", BindingFlags.NonPublic | BindingFlags.Static);
method.Should().NotBeNull();
var cache = method!.Invoke(null, new object[] { accelerator, 8, modulus, primitiveRoot })!;
var cacheType = cache.GetType();
var twiddlesMont = (MemoryBuffer1D<GpuUInt128, Stride1D.Dense>)cacheType.GetProperty("TwiddlesMont")!.GetValue(cache)!;
var twiddlesInvMont = (MemoryBuffer1D<GpuUInt128, Stride1D.Dense>)cacheType.GetProperty("TwiddlesInvMont")!.GetValue(cache)!;
var r2 = (ulong)cacheType.GetProperty("MontR2Mod64")!.GetValue(cache)!;
var mod = (ulong)cacheType.GetProperty("ModulusLow")!.GetValue(cache)!;
int count = 7; // length 8 -> 7 twiddles
var forward = new GpuUInt128[count];
var inverse = new GpuUInt128[count];
twiddlesMont.CopyToCPU(stream, forward);
twiddlesInvMont.CopyToCPU(stream, inverse);
stream.Synchronize();
stream.Dispose();
for (int i = 0; i < count; i++)
{
var accelerator = gpu.Accelerator;
var stream = accelerator.CreateStream();
var method = typeof(NttGpuMath).GetMethod("GetSquareCache", BindingFlags.NonPublic | BindingFlags.Static);
method.Should().NotBeNull();
var cache = method!.Invoke(null, new object[] { accelerator, 8, modulus, primitiveRoot })!;
var cacheType = cache.GetType();
var twiddlesMont = (MemoryBuffer1D<GpuUInt128, Stride1D.Dense>)cacheType.GetProperty("TwiddlesMont")!.GetValue(cache)!;
var twiddlesInvMont = (MemoryBuffer1D<GpuUInt128, Stride1D.Dense>)cacheType.GetProperty("TwiddlesInvMont")!.GetValue(cache)!;
var r2 = (ulong)cacheType.GetProperty("MontR2Mod64")!.GetValue(cache)!;
var mod = (ulong)cacheType.GetProperty("ModulusLow")!.GetValue(cache)!;
int count = 7; // length 8 -> 7 twiddles
var forward = new GpuUInt128[count];
var inverse = new GpuUInt128[count];
twiddlesMont.CopyToCPU(stream, forward);
twiddlesInvMont.CopyToCPU(stream, inverse);
stream.Synchronize();
stream.Dispose();
for (int i = 0; i < count; i++)
{
ulong prod = (ulong)((((BigInteger)forward[i].Low) * inverse[i].Low) % mod);
prod.Should().Be(r2);
}
}
finally
{
gpu.Dispose();
ulong prod = (ulong)((((BigInteger)forward[i].Low) * inverse[i].Low) % mod);
prod.Should().Be(r2);
}
}
@@ -348,15 +340,14 @@ public class NttGpuMathTests
values[1] = new GpuUInt128(0UL, 2UL);
var modulus = new GpuUInt128(0UL, 17UL);
var primitiveRoot = new GpuUInt128(0UL, 3UL);
GpuContextLease gpu = GpuContextPool.Rent();
var stream = gpu.Accelerator.CreateStream();
var buffer = gpu.Accelerator.Allocate1D<GpuUInt128>(values.Length);
Accelerator accelerator = SharedGpuContext.Accelerator;
var stream = accelerator.CreateStream();
var buffer = accelerator.Allocate1D<GpuUInt128>(values.Length);
buffer.View.CopyFromCPU(stream, ref values[0], values.Length);
NttGpuMath.SquareDevice(gpu.Accelerator, stream, buffer.View, modulus, primitiveRoot);
NttGpuMath.SquareDevice(accelerator, stream, buffer.View, modulus, primitiveRoot);
stream.Synchronize();
buffer.Dispose();
stream.Dispose();
gpu.Dispose();
buffer.Dispose();
// Verify that the square cache now holds entries for the accelerator.
var field = typeof(NttGpuMath).GetField("SquareCache", BindingFlags.NonPublic | BindingFlags.Static)!;
@@ -364,8 +355,6 @@ public class NttGpuMathTests
int count = (int)cacheObj.GetType().GetProperty("Count")!.GetValue(cacheObj)!;
count.Should().BeGreaterThan(0);
GpuContextPool.DisposeAll();
count = (int)cacheObj.GetType().GetProperty("Count")!.GetValue(cacheObj)!;
count.Should().Be(0);
}
@@ -33,8 +33,7 @@ public class NttMontgomeryTests
NttGpuMath.Square(cpu, modulus, primitiveRoot);
// Device execution
GpuContextLease context = GpuContextPool.Rent();
var accelerator = context.Accelerator;
var accelerator = SharedGpuContext.Accelerator;
var stream = accelerator.CreateStream();
using var buffer = accelerator.Allocate1D<GpuUInt128>(gpu.Length);
buffer.View.CopyFromCPU(stream, ref gpu[0], gpu.Length);
@@ -48,7 +47,6 @@ public class NttMontgomeryTests
}
stream.Dispose();
context.Dispose();
}
[Fact]
@@ -10,37 +10,23 @@ public class PrimeTesterGpuTests
[Fact]
public void IsPrimeGpu_accepts_known_primes()
{
try
{
var tester = new PrimeTester();
ulong[] primes = [31UL, 61UL, 89UL, 107UL, 127UL, 521UL];
foreach (ulong prime in primes)
{
PrimeTester.IsPrimeGpu(prime).Should().BeTrue();
}
}
finally
{
GpuContextPool.DisposeAll();
}
var tester = new PrimeTester();
ulong[] primes = [31UL, 61UL, 89UL, 107UL, 127UL, 521UL];
foreach (ulong prime in primes)
{
PrimeTester.IsPrimeGpu(prime).Should().BeTrue();
}
}
[Fact]
public void IsPrimeGpu_rejects_composites()
{
try
{
var tester = new PrimeTester();
ulong[] composites = [33UL, 39UL, 51UL, 77UL, 91UL];
foreach (ulong composite in composites)
{
PrimeTester.IsPrimeGpu(composite).Should().BeFalse();
}
}
finally
{
GpuContextPool.DisposeAll();
}
var tester = new PrimeTester();
ulong[] composites = [33UL, 39UL, 51UL, 77UL, 91UL];
foreach (ulong composite in composites)
{
PrimeTester.IsPrimeGpu(composite).Should().BeFalse();
}
}
}