From 66ca9699296fb44986a0aecd30ad19236eebe59f Mon Sep 17 00:00:00 2001 From: mlemanczyk Date: Tue, 18 Nov 2025 03:43:52 +0100 Subject: [PATCH] Naming improvements and small cleanup --- ...ables.cs => HeuristicCombinedGpuTables.cs} | 10 +++++----- ...Tables.cs => HeuristicGroupABGpuTables.cs} | 10 +++++----- .../HeuristicGroupABPrimeTesterAccelerator.cs | 12 ++++++------ .../PrimeOrderCalculatorAccelerator.cs | 19 ++++++++++++------- ...Tables.cs => HeuristicCombinedGpuViews.cs} | 2 +- ...rTables.cs => HeuristicGroupABGpuViews.cs} | 2 +- .../Gpu/Kernels/PrimeTesterKernels.cs | 4 ++-- .../Gpu/SmallPrimeFactorGpuTables.cs | 2 +- .../HeuristicCombinedPrimeTester.cs | 2 +- PerfectNumbers.Core/HeuristicPrimeTester.cs | 2 +- 10 files changed, 35 insertions(+), 30 deletions(-) rename PerfectNumbers.Core/Gpu/Accelerators/{HeuristicGpuCombinedTables.cs => HeuristicCombinedGpuTables.cs} (89%) rename PerfectNumbers.Core/Gpu/Accelerators/{HeuristicGpuGroupABTables.cs => HeuristicGroupABGpuTables.cs} (89%) rename PerfectNumbers.Core/Gpu/{HeuristicGpuCombinedDivisorTables.cs => HeuristicCombinedGpuViews.cs} (97%) rename PerfectNumbers.Core/Gpu/{HeuristicGpuGroupABDivisorTables.cs => HeuristicGroupABGpuViews.cs} (98%) diff --git a/PerfectNumbers.Core/Gpu/Accelerators/HeuristicGpuCombinedTables.cs b/PerfectNumbers.Core/Gpu/Accelerators/HeuristicCombinedGpuTables.cs similarity index 89% rename from PerfectNumbers.Core/Gpu/Accelerators/HeuristicGpuCombinedTables.cs rename to PerfectNumbers.Core/Gpu/Accelerators/HeuristicCombinedGpuTables.cs index 8ba3400..349c8cd 100644 --- a/PerfectNumbers.Core/Gpu/Accelerators/HeuristicGpuCombinedTables.cs +++ b/PerfectNumbers.Core/Gpu/Accelerators/HeuristicCombinedGpuTables.cs @@ -3,17 +3,17 @@ using ILGPU.Runtime; namespace PerfectNumbers.Core.Gpu.Accelerators; -internal sealed class HeuristicGpuCombinedTables +internal readonly struct HeuristicCombinedGpuTables { private static readonly Accelerator[] _accelerators = AcceleratorPool.Shared.Accelerators; - private static readonly HeuristicGpuCombinedTables[] _sharedTables = new HeuristicGpuCombinedTables[PerfectNumberConstants.RollingAccelerators]; + private static readonly HeuristicCombinedGpuTables[] _sharedTables = new HeuristicCombinedGpuTables[PerfectNumberConstants.RollingAccelerators]; internal static void WarmUp(int acceleratorIndex, AcceleratorStream stream) => _sharedTables[acceleratorIndex] = new(_accelerators[acceleratorIndex], stream); - internal static HeuristicGpuCombinedTables GetStaticTables(int acceleratorIndex) => _sharedTables[acceleratorIndex]; + internal static HeuristicCombinedGpuTables GetStaticTables(int acceleratorIndex) => _sharedTables[acceleratorIndex]; - internal HeuristicGpuCombinedTables(Accelerator accelerator, AcceleratorStream stream) + internal HeuristicCombinedGpuTables(Accelerator accelerator, AcceleratorStream stream) { var combinedEnding1 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding1; HeuristicCombinedDivisorsEnding1 = accelerator.CopySpanToDevice(stream, combinedEnding1); @@ -45,7 +45,7 @@ internal sealed class HeuristicGpuCombinedTables internal readonly MemoryBuffer1D HeuristicCombinedDivisorsEnding9; internal readonly MemoryBuffer1D HeuristicCombinedDivisorSquaresEnding9; - internal HeuristicGpuCombinedDivisorTables CreateHeuristicDivisorTables() => new( + internal HeuristicCombinedGpuViews CreateViews() => new( HeuristicCombinedDivisorsEnding1.View, HeuristicCombinedDivisorsEnding3.View, HeuristicCombinedDivisorsEnding7.View, diff --git a/PerfectNumbers.Core/Gpu/Accelerators/HeuristicGpuGroupABTables.cs b/PerfectNumbers.Core/Gpu/Accelerators/HeuristicGroupABGpuTables.cs similarity index 89% rename from PerfectNumbers.Core/Gpu/Accelerators/HeuristicGpuGroupABTables.cs rename to PerfectNumbers.Core/Gpu/Accelerators/HeuristicGroupABGpuTables.cs index 11ed95f..3ca4872 100644 --- a/PerfectNumbers.Core/Gpu/Accelerators/HeuristicGpuGroupABTables.cs +++ b/PerfectNumbers.Core/Gpu/Accelerators/HeuristicGroupABGpuTables.cs @@ -3,15 +3,15 @@ using ILGPU.Runtime; namespace PerfectNumbers.Core.Gpu.Accelerators; -internal sealed class HeuristicGpuGroupABTables +internal sealed class HeuristicGroupABGpuTables { private static readonly Accelerator[] _accelerators = AcceleratorPool.Shared.Accelerators; - private static readonly HeuristicGpuGroupABTables[] _sharedTables = new HeuristicGpuGroupABTables[PerfectNumberConstants.RollingAccelerators]; + private static readonly HeuristicGroupABGpuTables[] _sharedTables = new HeuristicGroupABGpuTables[PerfectNumberConstants.RollingAccelerators]; private static readonly SemaphoreSlim[] _locks = [..Enumerable.Range(1, PerfectNumberConstants.RollingAccelerators).Select(_ => new SemaphoreSlim(1))]; - internal static HeuristicGpuGroupABTables EnsureStaticTables(int acceleratorIndex, AcceleratorStream stream) + internal static HeuristicGroupABGpuTables EnsureStaticTables(int acceleratorIndex, AcceleratorStream stream) { var @lock = _locks[acceleratorIndex]; @lock.Wait(); @@ -28,7 +28,7 @@ internal sealed class HeuristicGpuGroupABTables return existing; } - internal HeuristicGpuGroupABTables(Accelerator accelerator, AcceleratorStream stream) + internal HeuristicGroupABGpuTables(Accelerator accelerator, AcceleratorStream stream) { var heuristicGroupA = HeuristicPrimeSieves.GroupADivisorsStorage; HeuristicGroupADivisors = accelerator.CopySpanToDevice(stream, heuristicGroupA); @@ -55,7 +55,7 @@ internal sealed class HeuristicGpuGroupABTables internal readonly MemoryBuffer1D HeuristicGroupBDivisorsEnding9; internal readonly MemoryBuffer1D HeuristicGroupBDivisorSquaresEnding9; - internal HeuristicGpuGroupABDivisorTables CreateHeuristicDivisorTables() => new( + internal HeuristicGroupABGpuViews CreateViews() => new( HeuristicGroupADivisors.View, HeuristicGroupADivisorSquares.View, HeuristicGroupBDivisorsEnding1.View, diff --git a/PerfectNumbers.Core/Gpu/Accelerators/HeuristicGroupABPrimeTesterAccelerator.cs b/PerfectNumbers.Core/Gpu/Accelerators/HeuristicGroupABPrimeTesterAccelerator.cs index ec284a4..262422b 100644 --- a/PerfectNumbers.Core/Gpu/Accelerators/HeuristicGroupABPrimeTesterAccelerator.cs +++ b/PerfectNumbers.Core/Gpu/Accelerators/HeuristicGroupABPrimeTesterAccelerator.cs @@ -19,7 +19,7 @@ internal sealed class HeuristicGroupABPrimeTesterAccelerator Console.WriteLine($"Preparing heuristic accelerator {i}..."); var accelerator = accelerators[i]; AcceleratorStream stream = accelerator.CreateStream(); - _ = HeuristicGpuGroupABTables.EnsureStaticTables(i, stream); + _ = HeuristicGroupABGpuTables.EnsureStaticTables(i, stream); // _ = GpuKernelPool.GetOrAddKernels(accelerator, stream, KernelType.None); // KernelContainer kernels = GpuKernelPool.GetOrAddKernels(accelerator, stream); // GpuStaticTableInitializer.EnsureStaticTables(accelerator, kernels, stream); @@ -75,7 +75,7 @@ internal sealed class HeuristicGroupABPrimeTesterAccelerator #endregion - public readonly HeuristicGpuGroupABDivisorTables DivisorTables; + public readonly HeuristicGroupABGpuViews DivisorTables; internal readonly Context Context; public readonly Accelerator Accelerator; @@ -102,7 +102,7 @@ internal sealed class HeuristicGroupABPrimeTesterAccelerator public MemoryBuffer1D? HeuristicFlag = null!; public int BufferCapacity; - private HeuristicGpuGroupABTables _sharedTables; + private HeuristicGroupABGpuTables _sharedTables; public MemoryBuffer1D HeuristicGroupADivisors => _sharedTables.HeuristicGroupADivisors; public MemoryBuffer1D HeuristicGroupADivisorSquares => _sharedTables.HeuristicGroupADivisorSquares; public MemoryBuffer1D HeuristicGroupBDivisorsEnding1 => _sharedTables.HeuristicGroupBDivisorsEnding1; @@ -125,14 +125,14 @@ internal sealed class HeuristicGroupABPrimeTesterAccelerator Accelerator = accelerator; EnsureCapacity(minBufferCapacity); - HeuristicTrialDivisionKernel = KernelUtil.GetKernel(accelerator.LoadAutoGroupedStreamKernel, ulong, ulong, HeuristicGpuDivisorTableKind, HeuristicGpuGroupABDivisorTables>(PrimeTesterKernels.HeuristicTrialGroupABDivisionKernel)); + HeuristicTrialDivisionKernel = KernelUtil.GetKernel(accelerator.LoadAutoGroupedStreamKernel, ulong, ulong, HeuristicGpuDivisorTableKind, HeuristicGroupABGpuViews>(PrimeTesterKernels.HeuristicTrialGroupABDivisionKernel)); AcceleratorStream stream = AcceleratorStreamPool.Rent(acceleratorIndex); // GpuStaticTableInitializer.EnsureStaticTables(accelerator, kernels, stream); - _sharedTables = HeuristicGpuGroupABTables.EnsureStaticTables(acceleratorIndex, stream); - DivisorTables = _sharedTables.CreateHeuristicDivisorTables(); + _sharedTables = HeuristicGroupABGpuTables.EnsureStaticTables(acceleratorIndex, stream); + DivisorTables = _sharedTables.CreateViews(); stream.Synchronize(); AcceleratorStreamPool.Return(acceleratorIndex, stream); diff --git a/PerfectNumbers.Core/Gpu/Accelerators/PrimeOrderCalculatorAccelerator.cs b/PerfectNumbers.Core/Gpu/Accelerators/PrimeOrderCalculatorAccelerator.cs index fe2b841..f569523 100644 --- a/PerfectNumbers.Core/Gpu/Accelerators/PrimeOrderCalculatorAccelerator.cs +++ b/PerfectNumbers.Core/Gpu/Accelerators/PrimeOrderCalculatorAccelerator.cs @@ -69,7 +69,7 @@ public sealed class PrimeOrderCalculatorAccelerator AcceleratorStream stream = accelerator.CreateStream(); // TODO: Review which tables are needed when final execution path is defined. LastDigitGpuTables.WarmUp(i, stream); - HeuristicGpuCombinedTables.WarmUp(i, stream); + HeuristicCombinedGpuTables.WarmUp(i, stream); // SmallPrimeFactorGpuTables.WarmUp(i, stream); // SharedHeuristicGpuTables.EnsureStaticTables(accelerator, stream); // _ = GpuKernelPool.GetOrAddKernels(accelerator, stream, KernelType.None); @@ -111,8 +111,9 @@ public sealed class PrimeOrderCalculatorAccelerator public readonly MemoryBuffer1D SmallPrimeFactorCountSlot; public readonly MemoryBuffer1D SpecialMaxResult; - public readonly HeuristicGpuCombinedDivisorTables DivisorTables; - public readonly SmallPrimeFactorGpuTables SmallPrimeFactorTables; + public readonly HeuristicCombinedGpuViews DivisorTables; + public readonly MemoryBuffer1D SmallPrimeFactorPrimes; + public readonly MemoryBuffer1D SmallPrimeFactorSquares; public readonly Kernel CheckFactorsKernel; public readonly Kernel ConvertToStandardKernel; @@ -217,9 +218,13 @@ public sealed class PrimeOrderCalculatorAccelerator DevicePrimesPow2LastThree = lastDigitSharedTables.DevicePrimesPow2LastThree; DevicePrimesPow2LastNine = lastDigitSharedTables.DevicePrimesPow2LastNine; - var heuristicSharedTables = HeuristicGpuCombinedTables.GetStaticTables(acceleratorIndex); - DivisorTables = heuristicSharedTables.CreateHeuristicDivisorTables(); - SmallPrimeFactorTables = SmallPrimeFactorGpuTables.GetStaticTables(acceleratorIndex); + var heuristicSharedTables = HeuristicCombinedGpuTables.GetStaticTables(acceleratorIndex); + + DivisorTables = heuristicSharedTables.CreateViews(); + + var smallPrimeFactorTables = SmallPrimeFactorGpuTables.GetStaticTables(acceleratorIndex); + SmallPrimeFactorPrimes = smallPrimeFactorTables.Primes; + SmallPrimeFactorSquares = smallPrimeFactorTables.Squares; var checkFactorsKernel = accelerator.LoadStreamKernel, Stride1D.Dense>, MontgomeryDivisorData, ArrayView1D>(PrimeOrderGpuHeuristics.CheckFactorsKernel); CheckFactorsKernel = KernelUtil.GetKernel(checkFactorsKernel); @@ -227,7 +232,7 @@ public sealed class PrimeOrderCalculatorAccelerator var convertKernel = accelerator.LoadAutoGroupedStreamKernel, MontgomeryDivisorData, ArrayView1D>(Pow2MontgomeryKernels.Pow2MontgomeryKernelConvertToStandard); ConvertToStandardKernel = KernelUtil.GetKernel(convertKernel); - HeuristicCombinedTrialDivisionKernel = KernelUtil.GetKernel(accelerator.LoadAutoGroupedStreamKernel, ulong, ulong, HeuristicGpuCombinedDivisorTables>(PrimeTesterKernels.HeuristicTrialCombinedDivisionKernel)); + HeuristicCombinedTrialDivisionKernel = KernelUtil.GetKernel(accelerator.LoadAutoGroupedStreamKernel, ulong, ulong, HeuristicCombinedGpuViews>(PrimeTesterKernels.HeuristicTrialCombinedDivisionKernel)); var keepKernel = accelerator.LoadAutoGroupedStreamKernel, MontgomeryDivisorData, ArrayView1D>(Pow2MontgomeryKernels.Pow2MontgomeryKernelKeepMontgomery); KeepMontgomeryKernel = KernelUtil.GetKernel(keepKernel); diff --git a/PerfectNumbers.Core/Gpu/HeuristicGpuCombinedDivisorTables.cs b/PerfectNumbers.Core/Gpu/HeuristicCombinedGpuViews.cs similarity index 97% rename from PerfectNumbers.Core/Gpu/HeuristicGpuCombinedDivisorTables.cs rename to PerfectNumbers.Core/Gpu/HeuristicCombinedGpuViews.cs index d8f0f3c..1a8f86d 100644 --- a/PerfectNumbers.Core/Gpu/HeuristicGpuCombinedDivisorTables.cs +++ b/PerfectNumbers.Core/Gpu/HeuristicCombinedGpuViews.cs @@ -4,7 +4,7 @@ using ILGPU.Runtime; namespace PerfectNumbers.Core.Gpu; -public readonly struct HeuristicGpuCombinedDivisorTables( +public readonly struct HeuristicCombinedGpuViews( ArrayView1D combinedDivisorsEnding1, ArrayView1D combinedDivisorsEnding3, ArrayView1D combinedDivisorsEnding7, diff --git a/PerfectNumbers.Core/Gpu/HeuristicGpuGroupABDivisorTables.cs b/PerfectNumbers.Core/Gpu/HeuristicGroupABGpuViews.cs similarity index 98% rename from PerfectNumbers.Core/Gpu/HeuristicGpuGroupABDivisorTables.cs rename to PerfectNumbers.Core/Gpu/HeuristicGroupABGpuViews.cs index de01098..b325f34 100644 --- a/PerfectNumbers.Core/Gpu/HeuristicGpuGroupABDivisorTables.cs +++ b/PerfectNumbers.Core/Gpu/HeuristicGroupABGpuViews.cs @@ -4,7 +4,7 @@ using ILGPU.Runtime; namespace PerfectNumbers.Core.Gpu; -public readonly struct HeuristicGpuGroupABDivisorTables( +public readonly struct HeuristicGroupABGpuViews( ArrayView1D groupADivisors, ArrayView1D groupADivisorSquares, ArrayView1D groupBDivisorsEnding1, diff --git a/PerfectNumbers.Core/Gpu/Kernels/PrimeTesterKernels.cs b/PerfectNumbers.Core/Gpu/Kernels/PrimeTesterKernels.cs index 1547b8b..718ca41 100644 --- a/PerfectNumbers.Core/Gpu/Kernels/PrimeTesterKernels.cs +++ b/PerfectNumbers.Core/Gpu/Kernels/PrimeTesterKernels.cs @@ -83,7 +83,7 @@ internal static class PrimeTesterKernels ArrayView resultFlag, ulong n, ulong maxDivisorSquare, - HeuristicGpuCombinedDivisorTables tables) + HeuristicCombinedGpuViews tables) { // byte nMod10 = (byte)(n % 10UL); ArrayView1D divisors = tables.SelectDivisors(nMod10); @@ -116,7 +116,7 @@ internal static class PrimeTesterKernels ulong n, ulong maxDivisorSquare, HeuristicGpuDivisorTableKind tableKind, - HeuristicGpuGroupABDivisorTables tables) + HeuristicGroupABGpuViews tables) { ArrayView1D divisors = tables.SelectDivisors(tableKind); ArrayView1D divisorSquares = tables.SelectDivisorSquares(tableKind); diff --git a/PerfectNumbers.Core/Gpu/SmallPrimeFactorGpuTables.cs b/PerfectNumbers.Core/Gpu/SmallPrimeFactorGpuTables.cs index 50ffa17..db66daf 100644 --- a/PerfectNumbers.Core/Gpu/SmallPrimeFactorGpuTables.cs +++ b/PerfectNumbers.Core/Gpu/SmallPrimeFactorGpuTables.cs @@ -3,8 +3,8 @@ using ILGPU; using ILGPU.Runtime; namespace PerfectNumbers.Core.Gpu; - public readonly struct SmallPrimeFactorGpuTables + { private static readonly Accelerator[] _accelerators = AcceleratorPool.Shared.Accelerators; private static readonly SmallPrimeFactorGpuTables[] _sharedTables = new SmallPrimeFactorGpuTables[PerfectNumberConstants.RollingAccelerators]; diff --git a/PerfectNumbers.Core/HeuristicCombinedPrimeTester.cs b/PerfectNumbers.Core/HeuristicCombinedPrimeTester.cs index f6c83f5..ec84e5a 100644 --- a/PerfectNumbers.Core/HeuristicCombinedPrimeTester.cs +++ b/PerfectNumbers.Core/HeuristicCombinedPrimeTester.cs @@ -246,7 +246,7 @@ public sealed class HeuristicCombinedPrimeTester int compositeFlag = 0; flagView1D.CopyFromCPU(stream, ref compositeFlag, 1); - var kernelLauncher = kernel.CreateLauncherDelegate, ulong, ulong, HeuristicGpuCombinedDivisorTables>>(); + var kernelLauncher = kernel.CreateLauncherDelegate, ulong, ulong, HeuristicCombinedGpuViews>>(); kernelLauncher( stream, 1, diff --git a/PerfectNumbers.Core/HeuristicPrimeTester.cs b/PerfectNumbers.Core/HeuristicPrimeTester.cs index 4ee1288..e9b15bd 100644 --- a/PerfectNumbers.Core/HeuristicPrimeTester.cs +++ b/PerfectNumbers.Core/HeuristicPrimeTester.cs @@ -261,7 +261,7 @@ public sealed class HeuristicPrimeTester // HeuristicGpuDivisorTableKind tableKind, // HeuristicGpuDivisorTables tables) - var kernelLauncher = kernel.CreateLauncherDelegate, ulong, ulong, HeuristicGpuDivisorTableKind, HeuristicGpuCombinedDivisorTables>>(); + var kernelLauncher = kernel.CreateLauncherDelegate, ulong, ulong, HeuristicGpuDivisorTableKind, HeuristicCombinedGpuViews>>(); kernelLauncher( stream, 1,