From e5dc68886542400049ee1accfb98f22a6b97df28 Mon Sep 17 00:00:00 2001 From: mlemanczyk Date: Sun, 12 Oct 2025 05:38:33 +0200 Subject: [PATCH] Locking temporarily disabled for testing --- PerfectNumbers.Core/Gpu/GpuKernelPool.cs | 64 ++++++++++++------------ 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/PerfectNumbers.Core/Gpu/GpuKernelPool.cs b/PerfectNumbers.Core/Gpu/GpuKernelPool.cs index d9a1ac0..f65d521 100644 --- a/PerfectNumbers.Core/Gpu/GpuKernelPool.cs +++ b/PerfectNumbers.Core/Gpu/GpuKernelPool.cs @@ -110,15 +110,15 @@ public ref struct GpuKernelLease(IDisposable limiter, GpuContextLease gpu, Kerne get { var accel = Accelerator; // avoid capturing 'this' in lambda - lock (_gpu!.Value.KernelInitLock) - { + // lock (_gpu!.Value.KernelInitLock) + // { return KernelContainer.InitOnce(ref _kernels.Order, () => { var loaded = accel.LoadAutoGroupedStreamKernel, ArrayView>(OrderKernelScan); var kernel = KernelUtil.GetKernel(loaded); return kernel.CreateLauncherDelegate, ArrayView>>(); }); - } + // } } } @@ -127,15 +127,15 @@ public ref struct GpuKernelLease(IDisposable limiter, GpuContextLease gpu, Kerne get { var accel = Accelerator; - lock (_gpu!.Value.KernelInitLock) - { + // lock (_gpu!.Value.KernelInitLock) + // { return KernelContainer.InitOnce(ref _kernels.Pow2Mod, () => { var loaded = accel.LoadAutoGroupedStreamKernel, ArrayView1D, ArrayView1D, ArrayView1D, ArrayView1D, ArrayView1D>(Pow2ModKernelScan); var kernel = KernelUtil.GetKernel(loaded); return kernel.CreateLauncherDelegate, ArrayView1D, ArrayView1D, ArrayView1D, ArrayView1D, ArrayView1D>>(); }); - } + // } } } @@ -144,15 +144,15 @@ public ref struct GpuKernelLease(IDisposable limiter, GpuContextLease gpu, Kerne get { var accel = Accelerator; - lock (_gpu!.Value.KernelInitLock) - { + // lock (_gpu!.Value.KernelInitLock) + // { return KernelContainer.InitOnce(ref _kernels.Incremental, () => { var loaded = accel.LoadAutoGroupedStreamKernel, ArrayView1D>(IncrementalKernelScan); var kernel = KernelUtil.GetKernel(loaded); return kernel.CreateLauncherDelegate, ArrayView1D>>(); }); - } + // } } } @@ -161,15 +161,15 @@ public ref struct GpuKernelLease(IDisposable limiter, GpuContextLease gpu, Kerne get { var accel = Accelerator; - lock (_gpu!.Value.KernelInitLock) - { + // lock (_gpu!.Value.KernelInitLock) + // { return KernelContainer.InitOnce(ref _kernels.IncrementalOrder, () => { var loaded = accel.LoadAutoGroupedStreamKernel, ArrayView1D>(IncrementalOrderKernelScan); var kernel = KernelUtil.GetKernel(loaded); return kernel.CreateLauncherDelegate, ArrayView1D>>(); }); - } + // } } } @@ -178,15 +178,15 @@ public ref struct GpuKernelLease(IDisposable limiter, GpuContextLease gpu, Kerne get { var accel = Accelerator; - lock (_gpu!.Value.KernelInitLock) - { + // lock (_gpu!.Value.KernelInitLock) + // { return KernelContainer.InitOnce(ref _kernels.Pow2ModOrder, () => { var loaded = accel.LoadAutoGroupedStreamKernel, ArrayView1D>(Pow2ModOrderKernelScan); var kernel = KernelUtil.GetKernel(loaded); return kernel.CreateLauncherDelegate, ArrayView1D>>(); }); - } + // } } } @@ -195,15 +195,15 @@ public ref struct GpuKernelLease(IDisposable limiter, GpuContextLease gpu, Kerne get { var accel = Accelerator; - lock (_gpu!.Value.KernelInitLock) - { + // lock (_gpu!.Value.KernelInitLock) + // { return KernelContainer.InitOnce(ref _kernels.MontgomeryOddPowers, () => { var loaded = accel.LoadAutoGroupedStreamKernel, ArrayView1D, ArrayView1D, int>(MontgomeryOddPowerKernelScan); var kernel = KernelUtil.GetKernel(loaded); return kernel.CreateLauncherDelegate, ArrayView1D, ArrayView1D, int>>(); }); - } + // } } } @@ -739,18 +739,18 @@ public ref struct GpuKernelLease(IDisposable limiter, GpuContextLease gpu, Kerne public ExecutionScope(object? sync) { _lock = sync; - if (_lock is not null) - { - Monitor.Enter(_lock); - } + // if (_lock is not null) + // { + // Monitor.Enter(_lock); + // } } public void Dispose() { - if (_lock is not null) - { - Monitor.Exit(_lock); - } + // if (_lock is not null) + // { + // Monitor.Exit(_lock); + // } } } } @@ -777,8 +777,8 @@ public class GpuKernelPool } // Ensure single upload per accelerator even if multiple threads race here. - lock (kernels) // TODO: Remove this lock by pre-uploading the immutable small-cycle snapshot during initialization; once no mutation happens at runtime, the pool must expose a simple reference without synchronization. - { + // lock (kernels) // TODO: Remove this lock by pre-uploading the immutable small-cycle snapshot during initialization; once no mutation happens at runtime, the pool must expose a simple reference without synchronization. + // { if (kernels.SmallCycles is { } existing) { return existing.View; @@ -789,7 +789,7 @@ public class GpuKernelPool device.View.CopyFromCPU(host); kernels.SmallCycles = device; return device.View; - } + // } } public static ResiduePrimeViews EnsureSmallPrimesOnDevice(Accelerator accelerator) @@ -803,8 +803,8 @@ public class GpuKernelPool return new ResiduePrimeViews(lastOne.View, lastSeven.View, lastOnePow2.View, lastSevenPow2.View); } - lock (kernels) // TODO: Inline these small-prime uploads into startup initialization alongside the small-cycle snapshot so we can drop runtime locking and keep the GPU pool free of synchronization. - { + // lock (kernels) // TODO: Inline these small-prime uploads into startup initialization alongside the small-cycle snapshot so we can drop runtime locking and keep the GPU pool free of synchronization. + // { if (kernels.SmallPrimesLastOne is { } existingLastOne && kernels.SmallPrimesLastSeven is { } existingLastSeven && kernels.SmallPrimesPow2LastOne is { } existingLastOnePow2 && @@ -833,7 +833,7 @@ public class GpuKernelPool kernels.SmallPrimesPow2LastSeven = deviceLastSevenPow2; return new ResiduePrimeViews(deviceLastOne.View, deviceLastSeven.View, deviceLastOnePow2.View, deviceLastSevenPow2.View); - } + // } } public static GpuKernelLease GetKernel(bool useGpuOrder)