From 6864b316d5a72bc77c3f270e756672142ffce39c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Lema=C5=84czyk?= Date: Fri, 31 Oct 2025 16:56:25 +0100 Subject: [PATCH 1/6] Warm GPU resources before thread startup --- EvenPerfectBitScanner/Program.cs | 7 +- .../HeuristicCombinedPrimeTesterTests.cs | 88 ++-- PerfectNumbers.Core/Gpu/GpuContextPool.cs | 46 ++ .../Gpu/HeuristicGpuDivisorTables.cs | 146 ++++++ .../Gpu/Kernels/PrimeTesterKernels.cs | 66 ++- .../HeuristicCombinedPrimeTester.cs | 194 ++++---- PerfectNumbers.Core/HeuristicPrimeSieves.cs | 39 +- PerfectNumbers.Core/HeuristicPrimeTester.cs | 224 ++++------ PerfectNumbers.Core/PrimeTester.cs | 415 ++++++++++++++---- 9 files changed, 822 insertions(+), 403 deletions(-) create mode 100644 PerfectNumbers.Core/Gpu/HeuristicGpuDivisorTables.cs diff --git a/EvenPerfectBitScanner/Program.cs b/EvenPerfectBitScanner/Program.cs index bf755d4..8457e47 100644 --- a/EvenPerfectBitScanner/Program.cs +++ b/EvenPerfectBitScanner/Program.cs @@ -55,12 +55,15 @@ internal static class Program ulong remainder = currentP % 6UL; bool startPrimeProvided = _cliArguments.StartPrimeProvided; int threadCount = Math.Max(1, _cliArguments.ThreadCount); + int gpuPrimeBatch = Math.Max(1, _cliArguments.GpuPrimeBatch); UnboundedTaskScheduler.ConfigureThreadCount(threadCount); + PrimeTester.GpuBatchSize = gpuPrimeBatch; + GpuContextPool.WarmUpPool(threadCount); + PrimeTester.WarmUpGpuKernels(threadCount); Console.WriteLine("Starting up threads..."); _ = UnboundedTaskScheduler.Instance; int blockSize = Math.Max(1, _cliArguments.BlockSize); int gpuPrimeThreads = Math.Max(1, _cliArguments.GpuPrimeThreads); - int gpuPrimeBatch = Math.Max(1, _cliArguments.GpuPrimeBatch); GpuKernelType kernelType = _cliArguments.KernelType; bool useBitTransform = _cliArguments.UseBitTransform; bool useOrder = _cliArguments.UseOrder; @@ -114,7 +117,7 @@ internal static class Program // Apply GPU prime sieve runtime configuration GpuPrimeWorkLimiter.SetLimit(gpuPrimeThreads); - PrimeTester.GpuBatchSize = Math.Max(1, gpuPrimeBatch); + PrimeTester.GpuBatchSize = gpuPrimeBatch; MersenneDivisorCycles mersenneDivisorCycles = new(); if (!File.Exists(cyclesPath) || continueCyclesGeneration) diff --git a/PerfectNumbers.Core.Tests/HeuristicCombinedPrimeTesterTests.cs b/PerfectNumbers.Core.Tests/HeuristicCombinedPrimeTesterTests.cs index d3d2051..92e6e26 100644 --- a/PerfectNumbers.Core.Tests/HeuristicCombinedPrimeTesterTests.cs +++ b/PerfectNumbers.Core.Tests/HeuristicCombinedPrimeTesterTests.cs @@ -62,28 +62,28 @@ public class HeuristicCombinedPrimeTesterTests [InlineData((byte)9)] public void One_a_one_b_pattern_interleaves_group_a_and_group_b_values(byte lastDigit) { - ReadOnlySpan combined = HeuristicCombinedPrimeTester.GetCombinedDivisors( + ReadOnlySpan combined = HeuristicCombinedPrimeTester.GetCombinedDivisors( lastDigit, HeuristicCombinedPrimeTester.CombinedDivisorPattern.OneAOneB); combined.Length.Should().BeGreaterThan(18); - combined[0].Should().Be(3U); - combined[1].Should().Be(7U); - combined[2].Should().Be(11U); - combined[3].Should().Be(13U); + combined[0].Should().Be(3UL); + combined[1].Should().Be(7UL); + combined[2].Should().Be(11UL); + combined[3].Should().Be(13UL); int index = 4; const int segmentsToCheck = 6; for (int segment = 0; segment < segmentsToCheck; segment++) { - uint groupAValue = combined[index++]; - groupAValue.Should().BeGreaterThan(13U); - (groupAValue % 10U).Should().Be(3U); + ulong groupAValue = combined[index++]; + groupAValue.Should().BeGreaterThan(13UL); + (groupAValue % 10UL).Should().Be(3UL); - uint groupBValue = combined[index++]; - groupBValue.Should().BeGreaterThan(13U); - (groupBValue % 10U).Should().NotBe(3U); + ulong groupBValue = combined[index++]; + groupBValue.Should().BeGreaterThan(13UL); + (groupBValue % 10UL).Should().NotBe(3UL); } } @@ -94,15 +94,15 @@ public class HeuristicCombinedPrimeTesterTests [InlineData((byte)9)] public void Two_a_one_b_pattern_interleaves_two_group_a_values_before_group_b(byte lastDigit) { - ReadOnlySpan combined = HeuristicCombinedPrimeTester.GetCombinedDivisors( + ReadOnlySpan combined = HeuristicCombinedPrimeTester.GetCombinedDivisors( lastDigit, HeuristicCombinedPrimeTester.CombinedDivisorPattern.TwoAOneB); combined.Length.Should().BeGreaterThan(20); - combined[0].Should().Be(3U); - combined[1].Should().Be(7U); - combined[2].Should().Be(11U); - combined[3].Should().Be(13U); + combined[0].Should().Be(3UL); + combined[1].Should().Be(7UL); + combined[2].Should().Be(11UL); + combined[3].Should().Be(13UL); int index = 4; const int segmentsToCheck = 5; @@ -111,14 +111,14 @@ public class HeuristicCombinedPrimeTesterTests { for (int i = 0; i < 2; i++) { - uint groupAValue = combined[index++]; - groupAValue.Should().BeGreaterThan(13U); - (groupAValue % 10U).Should().Be(3U); + ulong groupAValue = combined[index++]; + groupAValue.Should().BeGreaterThan(13UL); + (groupAValue % 10UL).Should().Be(3UL); } - uint groupBValue = combined[index++]; - groupBValue.Should().BeGreaterThan(13U); - (groupBValue % 10U).Should().NotBe(3U); + ulong groupBValue = combined[index++]; + groupBValue.Should().BeGreaterThan(13UL); + (groupBValue % 10UL).Should().NotBe(3UL); } } @@ -129,15 +129,15 @@ public class HeuristicCombinedPrimeTesterTests [InlineData((byte)9)] public void Three_a_two_b_pattern_interleaves_three_group_a_values_before_two_group_b_values(byte lastDigit) { - ReadOnlySpan combined = HeuristicCombinedPrimeTester.GetCombinedDivisors( + ReadOnlySpan combined = HeuristicCombinedPrimeTester.GetCombinedDivisors( lastDigit, HeuristicCombinedPrimeTester.CombinedDivisorPattern.ThreeATwoB); combined.Length.Should().BeGreaterThan(28); - combined[0].Should().Be(3U); - combined[1].Should().Be(7U); - combined[2].Should().Be(11U); - combined[3].Should().Be(13U); + combined[0].Should().Be(3UL); + combined[1].Should().Be(7UL); + combined[2].Should().Be(11UL); + combined[3].Should().Be(13UL); int index = 4; const int segmentsToCheck = 4; @@ -146,16 +146,16 @@ public class HeuristicCombinedPrimeTesterTests { for (int i = 0; i < 3; i++) { - uint groupAValue = combined[index++]; - groupAValue.Should().BeGreaterThan(13U); - (groupAValue % 10U).Should().Be(3U); + ulong groupAValue = combined[index++]; + groupAValue.Should().BeGreaterThan(13UL); + (groupAValue % 10UL).Should().Be(3UL); } for (int i = 0; i < 2; i++) { - uint groupBValue = combined[index++]; - groupBValue.Should().BeGreaterThan(13U); - (groupBValue % 10U).Should().NotBe(3U); + ulong groupBValue = combined[index++]; + groupBValue.Should().BeGreaterThan(13UL); + (groupBValue % 10UL).Should().NotBe(3UL); } } } @@ -167,15 +167,15 @@ public class HeuristicCombinedPrimeTesterTests [InlineData((byte)9)] public void Three_a_one_b_pattern_interleaves_three_group_a_values_before_group_b(byte lastDigit) { - ReadOnlySpan combined = HeuristicCombinedPrimeTester.GetCombinedDivisors( + ReadOnlySpan combined = HeuristicCombinedPrimeTester.GetCombinedDivisors( lastDigit, HeuristicCombinedPrimeTester.CombinedDivisorPattern.ThreeAOneB); combined.Length.Should().BeGreaterThan(24); - combined[0].Should().Be(3U); - combined[1].Should().Be(7U); - combined[2].Should().Be(11U); - combined[3].Should().Be(13U); + combined[0].Should().Be(3UL); + combined[1].Should().Be(7UL); + combined[2].Should().Be(11UL); + combined[3].Should().Be(13UL); int index = 4; const int segmentsToCheck = 4; @@ -184,14 +184,14 @@ public class HeuristicCombinedPrimeTesterTests { for (int i = 0; i < 3; i++) { - uint groupAValue = combined[index++]; - groupAValue.Should().BeGreaterThan(13U); - (groupAValue % 10U).Should().Be(3U); + ulong groupAValue = combined[index++]; + groupAValue.Should().BeGreaterThan(13UL); + (groupAValue % 10UL).Should().Be(3UL); } - uint groupBValue = combined[index++]; - groupBValue.Should().BeGreaterThan(13U); - (groupBValue % 10U).Should().NotBe(3U); + ulong groupBValue = combined[index++]; + groupBValue.Should().BeGreaterThan(13UL); + (groupBValue % 10UL).Should().NotBe(3UL); } } diff --git a/PerfectNumbers.Core/Gpu/GpuContextPool.cs b/PerfectNumbers.Core/Gpu/GpuContextPool.cs index 4943be7..3731c77 100644 --- a/PerfectNumbers.Core/Gpu/GpuContextPool.cs +++ b/PerfectNumbers.Core/Gpu/GpuContextPool.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Concurrent; using ILGPU; using ILGPU.Runtime; @@ -47,6 +48,8 @@ public static class GpuContextPool private static readonly ConcurrentQueue CpuPool = new(); private static readonly ConcurrentQueue GpuPool = new(); + private static readonly object WarmUpLock = new(); + private static int WarmedGpuContextCount; public static GpuContextLease Rent() { @@ -73,6 +76,48 @@ public static class GpuContextPool return new GpuContextLease(new PooledContext(preferCpu)); } + public static void WarmUpPool(int threadCount) + { + if (!PoolingEnabled || ForceCpu) + { + return; + } + + if (threadCount <= 0) + { + return; + } + + int target = threadCount / 4; + if (target == 0) + { + target = 1; + } + + lock (WarmUpLock) + { + if (target <= WarmedGpuContextCount) + { + return; + } + + int toCreate = target - WarmedGpuContextCount; + var contexts = new PooledContext[toCreate]; + for (int i = 0; i < toCreate; i++) + { + contexts[i] = new PooledContext(preferCpu: false); + } + + for (int i = 0; i < toCreate; i++) + { + GpuPool.Enqueue(contexts[i]); + } + + WarmedGpuContextCount = target; + } + } + + public static void DisposeAll() { if (!PoolingEnabled) @@ -91,6 +136,7 @@ public static class GpuContextPool } PrimeTester.DisposeGpuContexts(); + WarmedGpuContextCount = 0; } private static void Return(PooledContext ctx) diff --git a/PerfectNumbers.Core/Gpu/HeuristicGpuDivisorTables.cs b/PerfectNumbers.Core/Gpu/HeuristicGpuDivisorTables.cs new file mode 100644 index 0000000..d295417 --- /dev/null +++ b/PerfectNumbers.Core/Gpu/HeuristicGpuDivisorTables.cs @@ -0,0 +1,146 @@ +using System.Runtime.CompilerServices; +using ILGPU; +using ILGPU.Runtime; + +namespace PerfectNumbers.Core.Gpu; + +internal readonly struct HeuristicGpuDivisorTables +{ + public static readonly HeuristicGpuDivisorTables Shared = default; + + public readonly ArrayView1D CombinedDivisorsEnding1; + public readonly ArrayView1D CombinedDivisorsEnding3; + public readonly ArrayView1D CombinedDivisorsEnding7; + public readonly ArrayView1D CombinedDivisorsEnding9; + public readonly ArrayView1D CombinedDivisorSquaresEnding1; + public readonly ArrayView1D CombinedDivisorSquaresEnding3; + public readonly ArrayView1D CombinedDivisorSquaresEnding7; + public readonly ArrayView1D CombinedDivisorSquaresEnding9; + public readonly ArrayView1D GroupADivisors; + public readonly ArrayView1D GroupADivisorSquares; + public readonly ArrayView1D GroupBDivisorsEnding1; + public readonly ArrayView1D GroupBDivisorSquaresEnding1; + public readonly ArrayView1D GroupBDivisorsEnding7; + public readonly ArrayView1D GroupBDivisorSquaresEnding7; + public readonly ArrayView1D GroupBDivisorsEnding9; + public readonly ArrayView1D GroupBDivisorSquaresEnding9; + + public HeuristicGpuDivisorTables( + ArrayView1D combinedDivisorsEnding1, + ArrayView1D combinedDivisorsEnding3, + ArrayView1D combinedDivisorsEnding7, + ArrayView1D combinedDivisorsEnding9, + ArrayView1D combinedDivisorSquaresEnding1, + ArrayView1D combinedDivisorSquaresEnding3, + ArrayView1D combinedDivisorSquaresEnding7, + ArrayView1D combinedDivisorSquaresEnding9, + ArrayView1D groupADivisors, + ArrayView1D groupADivisorSquares, + ArrayView1D groupBDivisorsEnding1, + ArrayView1D groupBDivisorSquaresEnding1, + ArrayView1D groupBDivisorsEnding7, + ArrayView1D groupBDivisorSquaresEnding7, + ArrayView1D groupBDivisorsEnding9, + ArrayView1D groupBDivisorSquaresEnding9) + { + CombinedDivisorsEnding1 = combinedDivisorsEnding1; + CombinedDivisorsEnding3 = combinedDivisorsEnding3; + CombinedDivisorsEnding7 = combinedDivisorsEnding7; + CombinedDivisorsEnding9 = combinedDivisorsEnding9; + CombinedDivisorSquaresEnding1 = combinedDivisorSquaresEnding1; + CombinedDivisorSquaresEnding3 = combinedDivisorSquaresEnding3; + CombinedDivisorSquaresEnding7 = combinedDivisorSquaresEnding7; + CombinedDivisorSquaresEnding9 = combinedDivisorSquaresEnding9; + GroupADivisors = groupADivisors; + GroupADivisorSquares = groupADivisorSquares; + GroupBDivisorsEnding1 = groupBDivisorsEnding1; + GroupBDivisorSquaresEnding1 = groupBDivisorSquaresEnding1; + GroupBDivisorsEnding7 = groupBDivisorsEnding7; + GroupBDivisorSquaresEnding7 = groupBDivisorSquaresEnding7; + GroupBDivisorsEnding9 = groupBDivisorsEnding9; + GroupBDivisorSquaresEnding9 = groupBDivisorSquaresEnding9; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ArrayView1D SelectCombined(byte ending) + { + return ending switch + { + 1 => CombinedDivisorsEnding1, + 3 => CombinedDivisorsEnding3, + 7 => CombinedDivisorsEnding7, + 9 => CombinedDivisorsEnding9, + _ => ArrayView1D.Empty, + }; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ArrayView1D SelectCombinedSquares(byte ending) + { + return ending switch + { + 1 => CombinedDivisorSquaresEnding1, + 3 => CombinedDivisorSquaresEnding3, + 7 => CombinedDivisorSquaresEnding7, + 9 => CombinedDivisorSquaresEnding9, + _ => ArrayView1D.Empty, + }; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ArrayView1D SelectGroupB(byte ending) + { + return ending switch + { + 1 => GroupBDivisorsEnding1, + 7 => GroupBDivisorsEnding7, + 9 => GroupBDivisorsEnding9, + _ => ArrayView1D.Empty, + }; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ArrayView1D SelectGroupBSquares(byte ending) + { + return ending switch + { + 1 => GroupBDivisorSquaresEnding1, + 7 => GroupBDivisorSquaresEnding7, + 9 => GroupBDivisorSquaresEnding9, + _ => ArrayView1D.Empty, + }; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ArrayView1D SelectDivisors(HeuristicGpuDivisorTableKind kind, byte ending) + { + return kind switch + { + HeuristicGpuDivisorTableKind.GroupA => GroupADivisors, + HeuristicGpuDivisorTableKind.GroupBEnding1 => GroupBDivisorsEnding1, + HeuristicGpuDivisorTableKind.GroupBEnding7 => GroupBDivisorsEnding7, + HeuristicGpuDivisorTableKind.GroupBEnding9 => GroupBDivisorsEnding9, + HeuristicGpuDivisorTableKind.Combined => SelectCombined(ending), + _ => ArrayView1D.Empty, + }; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ArrayView1D SelectDivisorSquares(HeuristicGpuDivisorTableKind kind, byte ending) + { + return kind switch + { + HeuristicGpuDivisorTableKind.GroupA => GroupADivisorSquares, + HeuristicGpuDivisorTableKind.GroupBEnding1 => GroupBDivisorSquaresEnding1, + HeuristicGpuDivisorTableKind.GroupBEnding7 => GroupBDivisorSquaresEnding7, + HeuristicGpuDivisorTableKind.GroupBEnding9 => GroupBDivisorSquaresEnding9, + HeuristicGpuDivisorTableKind.Combined => SelectCombinedSquares(ending), + _ => ArrayView1D.Empty, + }; + } + + internal static void InitializeShared(in HeuristicGpuDivisorTables tables) + { + Unsafe.AsRef(in Shared) = tables; + } +} diff --git a/PerfectNumbers.Core/Gpu/Kernels/PrimeTesterKernels.cs b/PerfectNumbers.Core/Gpu/Kernels/PrimeTesterKernels.cs index ac4bd39..5b2f3fe 100644 --- a/PerfectNumbers.Core/Gpu/Kernels/PrimeTesterKernels.cs +++ b/PerfectNumbers.Core/Gpu/Kernels/PrimeTesterKernels.cs @@ -1,9 +1,19 @@ using System.Runtime.CompilerServices; using ILGPU; using ILGPU.Algorithms; +using ILGPU.Runtime; namespace PerfectNumbers.Core.Gpu; +internal enum HeuristicGpuDivisorTableKind : byte +{ + GroupA = 0, + GroupBEnding1 = 1, + GroupBEnding7 = 7, + GroupBEnding9 = 9, + Combined = 255, +} + internal static class PrimeTesterKernels { // GPU kernel: small-prime sieve only. Returns 1 if passes sieve (probable prime), 0 otherwise. @@ -51,13 +61,18 @@ internal static class PrimeTesterKernels int length = (int)primes.Length; for (int i = 0; i < length; i++) { - ulong prime = primes[i]; ulong primeSquare = primeSquares[i]; if (primeSquare > n) { break; } + ulong prime = primes[i]; + if (prime == 0) + { + break; + } + if (n % prime == 0UL) { result = 0; @@ -68,19 +83,46 @@ internal static class PrimeTesterKernels results[index] = result; } - public static void HeuristicTrialDivisionKernel(Index1D index, ArrayView divisors, ulong n, ArrayView resultFlag) + public static void HeuristicTrialDivisionKernel( + Index1D index, + ArrayView resultFlag, + ulong n, + ulong maxDivisorSquare, + HeuristicGpuDivisorTableKind tableKind, + HeuristicGpuDivisorTables tables) { - ulong divisor = divisors[index]; - if (divisor <= 1UL) - { - return; - } + byte nMod10 = (byte)(n % 10UL); + ArrayView1D divisors = tables.SelectDivisors(tableKind, nMod10); + ArrayView1D divisorSquares = tables.SelectDivisorSquares(tableKind, nMod10); + if (divisors.Length == 0 || divisorSquares.Length == 0) + { + return; + } - ulong residue = n % divisor; - if (residue == 0UL) - { - resultFlag[0] = 1; - } + int divisorLength = (int)divisors.Length; + int squareLength = (int)divisorSquares.Length; + int threadIndex = index; + if (threadIndex >= divisorLength || threadIndex >= squareLength) + { + return; + } + + ulong divisorSquare = divisorSquares[threadIndex]; + if (divisorSquare == 0UL || divisorSquare > maxDivisorSquare) + { + return; + } + + ulong divisor = divisors[threadIndex]; + if (divisor == 0UL) + { + return; + } + + if (n % divisor == 0UL) + { + resultFlag[0] = 1; + } } public static void SharesFactorKernel(Index1D index, ArrayView numbers, ArrayView results) diff --git a/PerfectNumbers.Core/HeuristicCombinedPrimeTester.cs b/PerfectNumbers.Core/HeuristicCombinedPrimeTester.cs index 24d52a7..3b4c051 100644 --- a/PerfectNumbers.Core/HeuristicCombinedPrimeTester.cs +++ b/PerfectNumbers.Core/HeuristicCombinedPrimeTester.cs @@ -1,6 +1,7 @@ using Open.Numeric.Primes; -using System.Runtime.CompilerServices; +using System; using ILGPU.Runtime; +using System.Runtime.CompilerServices; using PerfectNumbers.Core.Gpu; namespace PerfectNumbers.Core; @@ -40,32 +41,48 @@ public sealed class HeuristicCombinedPrimeTester CombinedDivisorsEnding9Squares = CombinedDivisorsEnding9OneAOneBSquares; } + + public static void EnsureInitialized() + { + // Intentionally left blank. Accessing this method forces the static constructor to run. + } + + internal static ReadOnlySpan CombinedDivisorsEnding1Span => CombinedDivisorsEnding1; + internal static ReadOnlySpan CombinedDivisorsEnding3Span => CombinedDivisorsEnding3; + internal static ReadOnlySpan CombinedDivisorsEnding7Span => CombinedDivisorsEnding7; + internal static ReadOnlySpan CombinedDivisorsEnding9Span => CombinedDivisorsEnding9; + + internal static ReadOnlySpan CombinedDivisorsEnding1SquaresSpan => CombinedDivisorsEnding1Squares; + internal static ReadOnlySpan CombinedDivisorsEnding3SquaresSpan => CombinedDivisorsEnding3Squares; + internal static ReadOnlySpan CombinedDivisorsEnding7SquaresSpan => CombinedDivisorsEnding7Squares; + internal static ReadOnlySpan CombinedDivisorsEnding9SquaresSpan => CombinedDivisorsEnding9Squares; + private const ulong Wheel210 = 210UL; - private static readonly uint[] CombinedDivisorsEnding1TwoAOneB; - private static readonly uint[] CombinedDivisorsEnding3TwoAOneB; - private static readonly uint[] CombinedDivisorsEnding7TwoAOneB; - private static readonly uint[] CombinedDivisorsEnding9TwoAOneB; + private static readonly ulong[] CombinedDivisorsEnding1TwoAOneB; + private static readonly ulong[] CombinedDivisorsEnding3TwoAOneB; + private static readonly ulong[] CombinedDivisorsEnding7TwoAOneB; + private static readonly ulong[] CombinedDivisorsEnding9TwoAOneB; - private static readonly uint[] CombinedDivisorsEnding1OneAOneB; - private static readonly uint[] CombinedDivisorsEnding3OneAOneB; - private static readonly uint[] CombinedDivisorsEnding7OneAOneB; - private static readonly uint[] CombinedDivisorsEnding9OneAOneB; + private static readonly ulong[] CombinedDivisorsEnding1OneAOneB; + private static readonly ulong[] CombinedDivisorsEnding3OneAOneB; + private static readonly ulong[] CombinedDivisorsEnding7OneAOneB; + private static readonly ulong[] CombinedDivisorsEnding9OneAOneB; - private static readonly uint[] CombinedDivisorsEnding1ThreeAOneB; - private static readonly uint[] CombinedDivisorsEnding3ThreeAOneB; - private static readonly uint[] CombinedDivisorsEnding7ThreeAOneB; - private static readonly uint[] CombinedDivisorsEnding9ThreeAOneB; + private static readonly ulong[] CombinedDivisorsEnding1ThreeAOneB; + private static readonly ulong[] CombinedDivisorsEnding3ThreeAOneB; + private static readonly ulong[] CombinedDivisorsEnding7ThreeAOneB; + private static readonly ulong[] CombinedDivisorsEnding9ThreeAOneB; - private static readonly uint[] CombinedDivisorsEnding1ThreeATwoB; - private static readonly uint[] CombinedDivisorsEnding3ThreeATwoB; - private static readonly uint[] CombinedDivisorsEnding7ThreeATwoB; - private static readonly uint[] CombinedDivisorsEnding9ThreeATwoB; + private static readonly ulong[] CombinedDivisorsEnding1ThreeATwoB; + private static readonly ulong[] CombinedDivisorsEnding3ThreeATwoB; + private static readonly ulong[] CombinedDivisorsEnding7ThreeATwoB; + private static readonly ulong[] CombinedDivisorsEnding9ThreeATwoB; - private static readonly uint[] CombinedDivisorsEnding1; - private static readonly uint[] CombinedDivisorsEnding3; - private static readonly uint[] CombinedDivisorsEnding7; - private static readonly uint[] CombinedDivisorsEnding9; + private static readonly ulong[] CombinedDivisorsEnding1; + private static readonly ulong[] CombinedDivisorsEnding3; + private static readonly ulong[] CombinedDivisorsEnding7; + private static readonly ulong[] CombinedDivisorsEnding9; private static readonly ulong[] CombinedDivisorsEnding1TwoAOneBSquares; private static readonly ulong[] CombinedDivisorsEnding3TwoAOneBSquares; @@ -173,7 +190,7 @@ public sealed class HeuristicCombinedPrimeTester private static bool HeuristicTrialDivisionCpu(ulong n, ulong maxDivisorSquare, byte nMod10) { - ReadOnlySpan combinedDivisors = GetCombinedDivisors(nMod10); + ReadOnlySpan combinedDivisors = GetCombinedDivisors(nMod10); ReadOnlySpan combinedDivisorSquares = GetCombinedDivisorSquares(nMod10); int length = combinedDivisors.Length; @@ -205,78 +222,43 @@ public sealed class HeuristicCombinedPrimeTester _ => 256, }; - private static bool HeuristicTrialDivisionGpuDetectsDivisor(ulong n, ulong maxDivisorSquare, byte nMod10) - { - int batchCapacity = GetBatchSize(n); + + private static bool HeuristicTrialDivisionGpuDetectsDivisor(ulong n, ulong maxDivisorSquare, byte nMod10) + { + var limiter = GpuPrimeWorkLimiter.Acquire(); + var gpu = PrimeTester.PrimeTesterGpuContextPool.Rent(1); + var accelerator = gpu.Accelerator; + var kernel = gpu.HeuristicTrialDivisionKernel; + var flagView1D = gpu.HeuristicFlag.View; + var flagView = flagView1D.AsContiguous(); + + bool compositeDetected = false; + int divisorLength = GetCombinedDivisors(nMod10).Length; + + if (divisorLength > 0) + { + int compositeFlag = 0; + flagView1D.CopyFromCPU(ref compositeFlag, 1); + kernel( + divisorLength, + flagView, + n, + maxDivisorSquare, + HeuristicGpuDivisorTableKind.Combined, + gpu.HeuristicGpuTables); + accelerator.Synchronize(); + flagView1D.CopyToCPU(ref compositeFlag, 1); + compositeDetected = compositeFlag != 0; + } + + gpu.Dispose(); + limiter.Dispose(); + return compositeDetected; + } - var limiter = GpuPrimeWorkLimiter.Acquire(); - var gpu = PrimeTester.PrimeTesterGpuContextPool.Rent(batchCapacity); - var accelerator = gpu.Accelerator; - var kernel = gpu.HeuristicTrialDivisionKernel; - - bool compositeDetected = false; - int count = 0; - - var inputView = gpu.Input.View; - var flagView = gpu.HeuristicFlag.View; - ulong[] divisorArray = gpu.DivisorArray; - - bool ProcessBatch(int length) - { - inputView.CopyFromCPU(ref divisorArray[0], length); - int compositeFlag = 0; - flagView.CopyFromCPU(ref compositeFlag, 1); - kernel(length, inputView, n, flagView); - accelerator.Synchronize(); - flagView.CopyToCPU(ref compositeFlag, 1); - - return compositeFlag != 0; - } - - ReadOnlySpan combinedDivisors = GetCombinedDivisors(nMod10); - ReadOnlySpan combinedDivisorSquares = GetCombinedDivisorSquares(nMod10); - - int length = combinedDivisors.Length; - - for (int i = 0; i < length; i++) - { - ulong divisorSquare = combinedDivisorSquares[i]; - if (divisorSquare > maxDivisorSquare) - { - break; - } - - divisorArray[count] = combinedDivisors[i]; - count++; - - if (count == batchCapacity) - { - if (ProcessBatch(count)) - { - compositeDetected = true; - goto Cleanup; - } - - count = 0; - } - } - - if (count > 0 && ProcessBatch(count)) - { - compositeDetected = true; - goto Cleanup; - } - - - Cleanup: - gpu.Dispose(); - limiter.Dispose(); - return compositeDetected; - } - - - private static bool EvaluateWithOpenNumericFallback(ulong n) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static bool EvaluateWithOpenNumericFallback(ulong n) { return Prime.Numbers.IsPrime(n); } @@ -285,7 +267,7 @@ public sealed class HeuristicCombinedPrimeTester return n; } - private static ReadOnlySpan GetCombinedDivisors(byte nMod10) + private static ReadOnlySpan GetCombinedDivisors(byte nMod10) { return nMod10 switch { @@ -309,7 +291,7 @@ public sealed class HeuristicCombinedPrimeTester }; } - internal static ReadOnlySpan GetCombinedDivisors(byte nMod10, CombinedDivisorPattern pattern) + internal static ReadOnlySpan GetCombinedDivisors(byte nMod10, CombinedDivisorPattern pattern) { return pattern switch { @@ -389,17 +371,17 @@ public sealed class HeuristicCombinedPrimeTester }; } - private static uint[] BuildCombinedDivisors(byte nMod10, CombinedDivisorPattern pattern, out ulong[] squares) + private static ulong[] BuildCombinedDivisors(byte nMod10, CombinedDivisorPattern pattern, out ulong[] squares) { - ReadOnlySpan groupA = HeuristicPrimeSieves.GroupADivisors; + ReadOnlySpan groupA = HeuristicPrimeSieves.GroupADivisors; ReadOnlySpan groupB = GetGroupBDivisors(nMod10); - var combined = new List(groupA.Length + groupB.Length); + var combined = new List(groupA.Length + groupB.Length); int groupAIndex = 0; while (groupAIndex < groupA.Length && groupA[groupAIndex] <= 19) { - combined.Add((uint)groupA[groupAIndex]); + combined.Add(groupA[groupAIndex]); groupAIndex++; } @@ -435,7 +417,7 @@ public sealed class HeuristicCombinedPrimeTester int addedA = 0; while (addedA < groupAInterleaveCount && groupAIndex < groupA.Length) { - combined.Add((uint)groupA[groupAIndex]); + combined.Add(groupA[groupAIndex]); groupAIndex++; addedA++; } @@ -449,7 +431,7 @@ public sealed class HeuristicCombinedPrimeTester } } - uint[] result = [.. combined]; + ulong[] result = [.. combined]; ulong[] resultSquares = new ulong[result.Length]; for (int i = 0; i < result.Length; i++) { @@ -474,12 +456,12 @@ public sealed class HeuristicCombinedPrimeTester return index; } - private static bool IsGroupAPrefixValue(uint value) => value is 3U or 7U or 11U or 13U; + private static bool IsGroupAPrefixValue(ulong value) => value is 3UL or 7UL or 11UL or 13UL; - private static HeuristicDivisorGroup ResolveGroup(uint value) => value switch + private static HeuristicDivisorGroup ResolveGroup(ulong value) => value switch { - 3U or 7U or 11U or 13U => HeuristicDivisorGroup.GroupAConstant, - _ when value % 10U == 3U => HeuristicDivisorGroup.GroupAWheel, + 3UL or 7UL or 11UL or 13UL => HeuristicDivisorGroup.GroupAConstant, + _ when value % 10UL == 3UL => HeuristicDivisorGroup.GroupAWheel, _ => HeuristicDivisorGroup.GroupB, }; @@ -578,7 +560,7 @@ public sealed class HeuristicCombinedPrimeTester internal ref struct HeuristicDivisorEnumerator { private readonly ulong maxDivisorSquare; - private readonly ReadOnlySpan combinedDivisors; + private readonly ReadOnlySpan combinedDivisors; private readonly ReadOnlySpan combinedDivisorSquares; private int index; @@ -603,7 +585,7 @@ public sealed class HeuristicCombinedPrimeTester break; } - uint entry = combinedDivisors[currentIndex]; + ulong entry = combinedDivisors[currentIndex]; index = currentIndex + 1; HeuristicDivisorGroup group = ResolveGroup(entry); diff --git a/PerfectNumbers.Core/HeuristicPrimeSieves.cs b/PerfectNumbers.Core/HeuristicPrimeSieves.cs index 9bc0acc..3fb19ba 100644 --- a/PerfectNumbers.Core/HeuristicPrimeSieves.cs +++ b/PerfectNumbers.Core/HeuristicPrimeSieves.cs @@ -15,19 +15,19 @@ public static class HeuristicPrimeSieves private static readonly ushort[] GroupBResiduesEnding7 = [17, 37, 47, 67, 97, 107, 127, 137, 157, 167, 187, 197]; private static readonly ushort[] GroupBResiduesEnding9 = [19, 29, 59, 79, 89, 109, 139, 149, 169, 179, 199, 209]; - private static readonly int[] GroupADivisorsStorage; + private static readonly ulong[] GroupADivisorsStorage; private static readonly ulong[] GroupADivisorSquaresStorage; - private static readonly int[] GroupBDivisorsEnding1Storage; + private static readonly ulong[] GroupBDivisorsEnding1Storage; private static readonly ulong[] GroupBDivisorSquaresEnding1Storage; - private static readonly int[] GroupBDivisorsEnding3Storage; + private static readonly ulong[] GroupBDivisorsEnding3Storage; private static readonly ulong[] GroupBDivisorSquaresEnding3Storage; - private static readonly int[] GroupBDivisorsEnding7Storage; + private static readonly ulong[] GroupBDivisorsEnding7Storage; private static readonly ulong[] GroupBDivisorSquaresEnding7Storage; - private static readonly int[] GroupBDivisorsEnding9Storage; + private static readonly ulong[] GroupBDivisorsEnding9Storage; private static readonly ulong[] GroupBDivisorSquaresEnding9Storage; static HeuristicPrimeSieves() @@ -46,45 +46,46 @@ public static class HeuristicPrimeSieves // Intentionally left blank. Accessing this method forces the static constructor to run. } - public static ReadOnlySpan GroupADivisors => GroupADivisorsStorage; + public static ReadOnlySpan GroupADivisors => GroupADivisorsStorage; public static ReadOnlySpan GroupADivisorSquares => GroupADivisorSquaresStorage; - public static ReadOnlySpan GroupBDivisorsEnding1 => GroupBDivisorsEnding1Storage; + public static ReadOnlySpan GroupBDivisorsEnding1 => GroupBDivisorsEnding1Storage; public static ReadOnlySpan GroupBDivisorSquaresEnding1 => GroupBDivisorSquaresEnding1Storage; - public static ReadOnlySpan GroupBDivisorsEnding3 => GroupBDivisorsEnding3Storage; + public static ReadOnlySpan GroupBDivisorsEnding3 => GroupBDivisorsEnding3Storage; public static ReadOnlySpan GroupBDivisorSquaresEnding3 => GroupBDivisorSquaresEnding3Storage; - public static ReadOnlySpan GroupBDivisorsEnding7 => GroupBDivisorsEnding7Storage; + public static ReadOnlySpan GroupBDivisorsEnding7 => GroupBDivisorsEnding7Storage; public static ReadOnlySpan GroupBDivisorSquaresEnding7 => GroupBDivisorSquaresEnding7Storage; - public static ReadOnlySpan GroupBDivisorsEnding9 => GroupBDivisorsEnding9Storage; + public static ReadOnlySpan GroupBDivisorsEnding9 => GroupBDivisorsEnding9Storage; public static ReadOnlySpan GroupBDivisorSquaresEnding9 => GroupBDivisorSquaresEnding9Storage; - private static (int[] divisors, ulong[] squares) BuildGroupADivisors() + private static (ulong[] divisors, ulong[] squares) BuildGroupADivisors() { - var divisors = new int[SieveLength]; + var divisors = new ulong[SieveLength]; var squares = new ulong[SieveLength]; int index = 0; for (; index < GroupAConstants.Length && index < divisors.Length; index++) { - int value = GroupAConstants[index]; + ulong value = (ulong)GroupAConstants[index]; divisors[index] = value; - squares[index] = (ulong)value * (ulong)value; + squares[index] = value * value; } int candidate = 23; int incrementIndex = 0; while (index < divisors.Length) { - divisors[index] = candidate; - squares[index] = (ulong)candidate * (ulong)candidate; + ulong value = (ulong)candidate; + divisors[index] = value; + squares[index] = value * value; candidate += GroupAIncrementPattern[incrementIndex]; incrementIndex ^= 1; @@ -94,9 +95,9 @@ public static class HeuristicPrimeSieves return (divisors, squares); } - private static (int[] divisors, ulong[] squares) BuildGroupBResidueSieve(ReadOnlySpan residues) + private static (ulong[] divisors, ulong[] squares) BuildGroupBResidueSieve(ReadOnlySpan residues) { - var divisors = new int[SieveLength]; + var divisors = new ulong[SieveLength]; var squares = new ulong[SieveLength]; int index = 0; @@ -112,7 +113,7 @@ public static class HeuristicPrimeSieves continue; } - divisors[index] = (int)candidate; + divisors[index] = candidate; squares[index] = candidate * candidate; index++; } diff --git a/PerfectNumbers.Core/HeuristicPrimeTester.cs b/PerfectNumbers.Core/HeuristicPrimeTester.cs index 930de3d..0a4e697 100644 --- a/PerfectNumbers.Core/HeuristicPrimeTester.cs +++ b/PerfectNumbers.Core/HeuristicPrimeTester.cs @@ -111,7 +111,7 @@ public sealed class HeuristicPrimeTester private static bool HeuristicTrialDivisionCpu(ulong n, ulong maxDivisorSquare, byte nMod10, bool includeGroupB) { - ReadOnlySpan groupADivisors = HeuristicPrimeSieves.GroupADivisors; + ReadOnlySpan groupADivisors = HeuristicPrimeSieves.GroupADivisors; ReadOnlySpan groupADivisorSquares = HeuristicPrimeSieves.GroupADivisorSquares; int interleaveBatchSize = HeuristicCombinedPrimeTester.GetBatchSize(n); int groupAIndex = 0; @@ -237,149 +237,85 @@ public sealed class HeuristicPrimeTester return true; } - private bool HeuristicTrialDivisionGpuDetectsDivisor(ulong n, ulong maxDivisorSquare, byte nMod10) - { - int batchCapacity = HeuristicCombinedPrimeTester.GetBatchSize(n); - int interleaveBatchSize = batchCapacity; + + private bool HeuristicTrialDivisionGpuDetectsDivisor(ulong n, ulong maxDivisorSquare, byte nMod10) + { + var limiter = GpuPrimeWorkLimiter.Acquire(); + var gpu = PrimeTester.PrimeTesterGpuContextPool.Rent(1); + var accelerator = gpu.Accelerator; + var kernel = gpu.HeuristicTrialDivisionKernel; + var flagView1D = gpu.HeuristicFlag.View; + var flagView = flagView1D.AsContiguous(); + + bool compositeDetected = false; + int compositeFlag = 0; + + int groupALength = HeuristicPrimeSieves.GroupADivisors.Length; + + if (groupALength > 0) + { + flagView1D.CopyFromCPU(ref compositeFlag, 1); + kernel( + groupALength, + flagView, + n, + maxDivisorSquare, + HeuristicGpuDivisorTableKind.GroupA, + gpu.HeuristicGpuTables); + accelerator.Synchronize(); + flagView1D.CopyToCPU(ref compositeFlag, 1); + compositeDetected = compositeFlag != 0; + } + + if (!compositeDetected) + { + ReadOnlySpan endingOrder = GetGroupBEndingOrder(nMod10); + for (int i = 0; i < endingOrder.Length && !compositeDetected; i++) + { + byte ending = endingOrder[i]; + HeuristicGpuDivisorTableKind tableKind = ending switch + { + 1 => HeuristicGpuDivisorTableKind.GroupBEnding1, + 7 => HeuristicGpuDivisorTableKind.GroupBEnding7, + 9 => HeuristicGpuDivisorTableKind.GroupBEnding9, + _ => HeuristicGpuDivisorTableKind.GroupA, + }; + + if (tableKind == HeuristicGpuDivisorTableKind.GroupA) + { + continue; + } + + int divisorLength = GetGroupBDivisors(ending).Length; + if (divisorLength == 0) + { + continue; + } + + compositeFlag = 0; + flagView1D.CopyFromCPU(ref compositeFlag, 1); + kernel( + divisorLength, + flagView, + n, + maxDivisorSquare, + tableKind, + gpu.HeuristicGpuTables); + accelerator.Synchronize(); + flagView1D.CopyToCPU(ref compositeFlag, 1); + compositeDetected = compositeFlag != 0; + } + } + + gpu.Dispose(); + limiter.Dispose(); + return compositeDetected; + } - var limiter = GpuPrimeWorkLimiter.Acquire(); - var gpu = PrimeTester.PrimeTesterGpuContextPool.Rent(batchCapacity); - var accelerator = gpu.Accelerator; - var kernel = gpu.HeuristicTrialDivisionKernel; - bool compositeDetected = false; - int count = 0; - - var inputView = gpu.Input.View; - var flagView = gpu.HeuristicFlag.View; - ulong[] divisorArray = gpu.DivisorArray; - - bool ProcessBatch(int length) - { - if (length <= 0) - { - return false; - } - - inputView.CopyFromCPU(ref divisorArray[0], length); - int compositeFlag = 0; - flagView.CopyFromCPU(ref compositeFlag, 1); - kernel(length, inputView, n, flagView); - accelerator.Synchronize(); - flagView.CopyToCPU(ref compositeFlag, 1); - return compositeFlag != 0; - } - - ReadOnlySpan groupADivisors = HeuristicPrimeSieves.GroupADivisors; - ReadOnlySpan groupADivisorSquares = HeuristicPrimeSieves.GroupADivisorSquares; - int groupAIndex = 0; - ReadOnlySpan endingOrder = GetGroupBEndingOrder(nMod10); - Span indices = endingOrder.Length <= 8 - ? stackalloc int[endingOrder.Length] - : new int[endingOrder.Length]; - - bool groupAHasMore = groupAIndex < groupADivisors.Length && groupADivisorSquares[groupAIndex] <= maxDivisorSquare; - bool groupBHasMore = !endingOrder.IsEmpty && HasGroupBCandidates(endingOrder, indices, maxDivisorSquare); - - while (!compositeDetected && (groupAHasMore || groupBHasMore)) - { - if (groupAHasMore) - { - int processed = 0; - while (processed < interleaveBatchSize && groupAIndex < groupADivisors.Length) - { - ulong divisorSquare = groupADivisorSquares[groupAIndex]; - if (divisorSquare > maxDivisorSquare) - { - groupAIndex = groupADivisors.Length; - groupAHasMore = false; - break; - } - - divisorArray[count] = (ulong)groupADivisors[groupAIndex]; - count++; - - if (count == batchCapacity) - { - if (ProcessBatch(count)) - { - compositeDetected = true; - break; - } - - count = 0; - } - - groupAIndex++; - processed++; - } - - if (compositeDetected) - { - break; - } - - if (groupAIndex >= groupADivisors.Length) - { - groupAHasMore = false; - } - else - { - groupAHasMore = groupADivisorSquares[groupAIndex] <= maxDivisorSquare; - if (!groupAHasMore) - { - groupAIndex = groupADivisors.Length; - } - } - } - - if (!compositeDetected && groupBHasMore) - { - int processed = 0; - while (processed < interleaveBatchSize) - { - if (!TrySelectNextGroupBDivisor(endingOrder, indices, maxDivisorSquare, out ulong divisor)) - { - groupBHasMore = false; - break; - } - - divisorArray[count] = divisor; - count++; - - if (count == batchCapacity) - { - if (ProcessBatch(count)) - { - compositeDetected = true; - break; - } - - count = 0; - } - - processed++; - } - - if (!compositeDetected && groupBHasMore) - { - groupBHasMore = HasGroupBCandidates(endingOrder, indices, maxDivisorSquare); - } - } - } - - if (!compositeDetected && count > 0 && ProcessBatch(count)) - { - compositeDetected = true; - } - - gpu.Dispose(); - limiter.Dispose(); - return compositeDetected; - } - - private static bool EvaluateWithOpenNumericFallback(ulong n) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static bool EvaluateWithOpenNumericFallback(ulong n) { return Prime.Numbers.IsPrime(n); } @@ -398,7 +334,7 @@ public sealed class HeuristicPrimeTester }; [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static bool TrySelectNextGroupBDivisor(ReadOnlySpan endingOrder, Span indices, ulong maxDivisorSquare, out ulong divisor) + private static bool TrySelectNextGroupBDivisor(ReadOnlySpan endingOrder, Span indices, ulong maxDivisorSquare, out ulong divisor) { ulong bestCandidate = ulong.MaxValue; int bestEndingIndex = -1; @@ -581,7 +517,7 @@ public sealed class HeuristicPrimeTester internal ref struct HeuristicDivisorEnumerator { private readonly ulong maxDivisorSquare; - private readonly ReadOnlySpan groupADivisors; + private readonly ReadOnlySpan groupADivisors; private readonly ReadOnlySpan groupADivisorSquares; private int groupAIndex; private Span groupBStates; @@ -609,7 +545,7 @@ public sealed class HeuristicPrimeTester } int currentIndex = groupAIndex; - ulong divisor = (ulong)groupADivisors[currentIndex]; + ulong divisor = groupADivisors[currentIndex]; groupAIndex++; HeuristicDivisorGroup group = currentIndex < GroupAConstantCount diff --git a/PerfectNumbers.Core/PrimeTester.cs b/PerfectNumbers.Core/PrimeTester.cs index 0b46256..f20d9d4 100644 --- a/PerfectNumbers.Core/PrimeTester.cs +++ b/PerfectNumbers.Core/PrimeTester.cs @@ -1,5 +1,7 @@ +using System; using System.Buffers; using System.Collections.Concurrent; +using System.Collections.Generic; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -140,6 +142,50 @@ public sealed class PrimeTester public static int GpuBatchSize { get; set; } = 262_144; + private static readonly object GpuWarmUpLock = new(); + private static int WarmedGpuLeaseCount; + + public static void WarmUpGpuKernels(int threadCount) + { + if (GpuContextPool.ForceCpu) + { + return; + } + + if (threadCount <= 0) + { + return; + } + + int target = threadCount / 4; + if (target == 0) + { + target = 1; + } + + lock (GpuWarmUpLock) + { + if (target <= WarmedGpuLeaseCount) + { + return; + } + + int toWarm = target - WarmedGpuLeaseCount; + var leases = new PrimeTesterGpuContextPool.PrimeTesterGpuContextLease[toWarm]; + for (int i = 0; i < toWarm; i++) + { + leases[i] = PrimeTesterGpuContextPool.Rent(GpuBatchSize); + } + + for (int i = 0; i < toWarm; i++) + { + leases[i].Dispose(); + } + + WarmedGpuLeaseCount = target; + } + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsPrimeBatchGpu(ReadOnlySpan values, Span results) { @@ -194,6 +240,41 @@ public sealed class PrimeTester internal static class PrimeTesterGpuContextPool { private static readonly ConcurrentQueue Pool = new(); + private static readonly object SharedTablesLock = new(); + private static readonly Dictionary SharedTables = new(); + + private static string CreateAcceleratorKey(Accelerator accelerator) + { + return accelerator.AcceleratorType + ":" + accelerator.Name; + } + + private static SharedHeuristicGpuTables RentSharedTables(Accelerator accelerator) + { + string key = CreateAcceleratorKey(accelerator); + + lock (SharedTablesLock) + { + if (!SharedTables.TryGetValue(key, out var tables)) + { + tables = new SharedHeuristicGpuTables(accelerator, key); + SharedTables.Add(key, tables); + } + + tables.AddReference(); + return tables; + } + } + + private static void ReleaseSharedTables(SharedHeuristicGpuTables tables) + { + lock (SharedTablesLock) + { + if (tables.Release()) + { + SharedTables.Remove(tables.Key); + } + } + } internal static PrimeTesterGpuContextLease Rent(int minBufferCapacity = 1) { @@ -253,101 +334,95 @@ public sealed class PrimeTester internal sealed class PrimeTesterGpuContextLease : IDisposable { + private bool _returned; + private readonly SharedHeuristicGpuTables _sharedTables; + private readonly HeuristicGpuDivisorTables _heuristicDivisorTables; internal readonly Context AcceleratorContext; public readonly Accelerator Accelerator; public readonly Action, ArrayView, ArrayView, ArrayView, ArrayView, ArrayView, ArrayView, ArrayView, ArrayView, ArrayView, ArrayView, ArrayView> Kernel; - public readonly Action, ulong, ArrayView> HeuristicTrialDivisionKernel; - public readonly MemoryBuffer1D DevicePrimesDefault; - public readonly MemoryBuffer1D DevicePrimesLastOne; - public readonly MemoryBuffer1D DevicePrimesLastSeven; - public readonly MemoryBuffer1D DevicePrimesLastThree; - public readonly MemoryBuffer1D DevicePrimesLastNine; - public readonly MemoryBuffer1D DevicePrimesPow2Default; - public readonly MemoryBuffer1D DevicePrimesPow2LastOne; - public readonly MemoryBuffer1D DevicePrimesPow2LastSeven; - public readonly MemoryBuffer1D DevicePrimesPow2LastThree; - public readonly MemoryBuffer1D DevicePrimesPow2LastNine; - public MemoryBuffer1D Input; - public MemoryBuffer1D Output; - public MemoryBuffer1D HeuristicFlag; + public readonly Action, ulong, ulong, HeuristicGpuDivisorTableKind, HeuristicGpuDivisorTables> HeuristicTrialDivisionKernel; + public MemoryBuffer1D Input = null!; + public MemoryBuffer1D Output = null!; + public MemoryBuffer1D HeuristicFlag = null!; public int BufferCapacity; - public ulong[] DivisorArray; + + public MemoryBuffer1D DevicePrimesDefault => _sharedTables.DevicePrimesDefault; + public MemoryBuffer1D DevicePrimesLastOne => _sharedTables.DevicePrimesLastOne; + public MemoryBuffer1D DevicePrimesLastSeven => _sharedTables.DevicePrimesLastSeven; + public MemoryBuffer1D DevicePrimesLastThree => _sharedTables.DevicePrimesLastThree; + public MemoryBuffer1D DevicePrimesLastNine => _sharedTables.DevicePrimesLastNine; + public MemoryBuffer1D DevicePrimesPow2Default => _sharedTables.DevicePrimesPow2Default; + public MemoryBuffer1D DevicePrimesPow2LastOne => _sharedTables.DevicePrimesPow2LastOne; + public MemoryBuffer1D DevicePrimesPow2LastSeven => _sharedTables.DevicePrimesPow2LastSeven; + public MemoryBuffer1D DevicePrimesPow2LastThree => _sharedTables.DevicePrimesPow2LastThree; + public MemoryBuffer1D DevicePrimesPow2LastNine => _sharedTables.DevicePrimesPow2LastNine; + public MemoryBuffer1D HeuristicGroupADivisors => _sharedTables.HeuristicGroupADivisors; + public MemoryBuffer1D HeuristicGroupADivisorSquares => _sharedTables.HeuristicGroupADivisorSquares; + public MemoryBuffer1D HeuristicGroupBDivisorsEnding1 => _sharedTables.HeuristicGroupBDivisorsEnding1; + public MemoryBuffer1D HeuristicGroupBDivisorSquaresEnding1 => _sharedTables.HeuristicGroupBDivisorSquaresEnding1; + public MemoryBuffer1D HeuristicGroupBDivisorsEnding7 => _sharedTables.HeuristicGroupBDivisorsEnding7; + public MemoryBuffer1D HeuristicGroupBDivisorSquaresEnding7 => _sharedTables.HeuristicGroupBDivisorSquaresEnding7; + public MemoryBuffer1D HeuristicGroupBDivisorsEnding9 => _sharedTables.HeuristicGroupBDivisorsEnding9; + public MemoryBuffer1D HeuristicGroupBDivisorSquaresEnding9 => _sharedTables.HeuristicGroupBDivisorSquaresEnding9; + public MemoryBuffer1D HeuristicCombinedDivisorsEnding1 => _sharedTables.HeuristicCombinedDivisorsEnding1; + public MemoryBuffer1D HeuristicCombinedDivisorSquaresEnding1 => _sharedTables.HeuristicCombinedDivisorSquaresEnding1; + public MemoryBuffer1D HeuristicCombinedDivisorsEnding3 => _sharedTables.HeuristicCombinedDivisorsEnding3; + public MemoryBuffer1D HeuristicCombinedDivisorSquaresEnding3 => _sharedTables.HeuristicCombinedDivisorSquaresEnding3; + public MemoryBuffer1D HeuristicCombinedDivisorsEnding7 => _sharedTables.HeuristicCombinedDivisorsEnding7; + public MemoryBuffer1D HeuristicCombinedDivisorSquaresEnding7 => _sharedTables.HeuristicCombinedDivisorSquaresEnding7; + public MemoryBuffer1D HeuristicCombinedDivisorsEnding9 => _sharedTables.HeuristicCombinedDivisorsEnding9; + public MemoryBuffer1D HeuristicCombinedDivisorSquaresEnding9 => _sharedTables.HeuristicCombinedDivisorSquaresEnding9; + + public HeuristicGpuDivisorTables HeuristicGpuTables => _heuristicDivisorTables; internal PrimeTesterGpuContextLease(int minBufferCapacity) { AcceleratorContext = Context.CreateDefault(); Accelerator = AcceleratorContext.GetPreferredDevice(false).CreateAccelerator(AcceleratorContext); Kernel = Accelerator.LoadAutoGroupedStreamKernel, ArrayView, ArrayView, ArrayView, ArrayView, ArrayView, ArrayView, ArrayView, ArrayView, ArrayView, ArrayView, ArrayView>(PrimeTesterKernels.SmallPrimeSieveKernel); - HeuristicTrialDivisionKernel = Accelerator.LoadAutoGroupedStreamKernel, ulong, ArrayView>(PrimeTesterKernels.HeuristicTrialDivisionKernel); + HeuristicTrialDivisionKernel = Accelerator.LoadAutoGroupedStreamKernel, ulong, ulong, HeuristicGpuDivisorTableKind, HeuristicGpuDivisorTables>(PrimeTesterKernels.HeuristicTrialDivisionKernel); - var primesDefault = DivisorGenerator.SmallPrimes; - DevicePrimesDefault = Accelerator.Allocate1D(primesDefault.Length); - DevicePrimesDefault.View.CopyFromCPU(primesDefault); + _sharedTables = RentSharedTables(Accelerator); - var primesLastOne = DivisorGenerator.SmallPrimesLastOne; - DevicePrimesLastOne = Accelerator.Allocate1D(primesLastOne.Length); - DevicePrimesLastOne.View.CopyFromCPU(primesLastOne); - - var primesLastSeven = DivisorGenerator.SmallPrimesLastSeven; - DevicePrimesLastSeven = Accelerator.Allocate1D(primesLastSeven.Length); - DevicePrimesLastSeven.View.CopyFromCPU(primesLastSeven); - - var primesLastThree = DivisorGenerator.SmallPrimesLastThree; - DevicePrimesLastThree = Accelerator.Allocate1D(primesLastThree.Length); - DevicePrimesLastThree.View.CopyFromCPU(primesLastThree); - - var primesLastNine = DivisorGenerator.SmallPrimesLastNine; - DevicePrimesLastNine = Accelerator.Allocate1D(primesLastNine.Length); - DevicePrimesLastNine.View.CopyFromCPU(primesLastNine); - - var primesPow2Default = DivisorGenerator.SmallPrimesPow2; - DevicePrimesPow2Default = Accelerator.Allocate1D(primesPow2Default.Length); - DevicePrimesPow2Default.View.CopyFromCPU(primesPow2Default); - - var primesPow2LastOne = DivisorGenerator.SmallPrimesPow2LastOne; - DevicePrimesPow2LastOne = Accelerator.Allocate1D(primesPow2LastOne.Length); - DevicePrimesPow2LastOne.View.CopyFromCPU(primesPow2LastOne); - - var primesPow2LastSeven = DivisorGenerator.SmallPrimesPow2LastSeven; - DevicePrimesPow2LastSeven = Accelerator.Allocate1D(primesPow2LastSeven.Length); - DevicePrimesPow2LastSeven.View.CopyFromCPU(primesPow2LastSeven); - - var primesPow2LastThree = DivisorGenerator.SmallPrimesPow2LastThree; - DevicePrimesPow2LastThree = Accelerator.Allocate1D(primesPow2LastThree.Length); - DevicePrimesPow2LastThree.View.CopyFromCPU(primesPow2LastThree); - - var primesPow2LastNine = DivisorGenerator.SmallPrimesPow2LastNine; - DevicePrimesPow2LastNine = Accelerator.Allocate1D(primesPow2LastNine.Length); - DevicePrimesPow2LastNine.View.CopyFromCPU(primesPow2LastNine); - - BufferCapacity = minBufferCapacity; - Input = Accelerator.Allocate1D(minBufferCapacity); - Output = Accelerator.Allocate1D(minBufferCapacity); - HeuristicFlag = Accelerator.Allocate1D(minBufferCapacity); - DivisorArray = ThreadStaticPools.UlongPool.Rent(minBufferCapacity); + _heuristicDivisorTables = new HeuristicGpuDivisorTables( + _sharedTables.HeuristicCombinedDivisorsEnding1.View, + _sharedTables.HeuristicCombinedDivisorsEnding3.View, + _sharedTables.HeuristicCombinedDivisorsEnding7.View, + _sharedTables.HeuristicCombinedDivisorsEnding9.View, + _sharedTables.HeuristicCombinedDivisorSquaresEnding1.View, + _sharedTables.HeuristicCombinedDivisorSquaresEnding3.View, + _sharedTables.HeuristicCombinedDivisorSquaresEnding7.View, + _sharedTables.HeuristicCombinedDivisorSquaresEnding9.View, + _sharedTables.HeuristicGroupADivisors.View, + _sharedTables.HeuristicGroupADivisorSquares.View, + _sharedTables.HeuristicGroupBDivisorsEnding1.View, + _sharedTables.HeuristicGroupBDivisorSquaresEnding1.View, + _sharedTables.HeuristicGroupBDivisorsEnding7.View, + _sharedTables.HeuristicGroupBDivisorSquaresEnding7.View, + _sharedTables.HeuristicGroupBDivisorsEnding9.View, + _sharedTables.HeuristicGroupBDivisorSquaresEnding9.View); + HeuristicGpuDivisorTables.InitializeShared(in _heuristicDivisorTables); + EnsureCapacity(minBufferCapacity); _returned = false; } public void EnsureCapacity(int minCapacity) { - int required = minCapacity; - if (required <= BufferCapacity) + if (minCapacity <= BufferCapacity) { return; } - Input.Dispose(); - Output.Dispose(); - HeuristicFlag.Dispose(); - ThreadStaticPools.UlongPool.Return(DivisorArray); - Input = Accelerator.Allocate1D(required); - Output = Accelerator.Allocate1D(required); - HeuristicFlag = Accelerator.Allocate1D(required); - DivisorArray = ThreadStaticPools.UlongPool.Rent(required); - BufferCapacity = required; + Input?.Dispose(); + Output?.Dispose(); + HeuristicFlag?.Dispose(); + Input = Accelerator.Allocate1D(minCapacity); + Output = Accelerator.Allocate1D(minCapacity); + HeuristicFlag = Accelerator.Allocate1D(minCapacity); + BufferCapacity = minCapacity; } internal void ResetReturnFlag() @@ -368,9 +443,178 @@ public sealed class PrimeTester internal void DisposeResources() { - Input.Dispose(); - Output.Dispose(); - HeuristicFlag.Dispose(); + Input?.Dispose(); + Output?.Dispose(); + HeuristicFlag?.Dispose(); + ReleaseSharedTables(_sharedTables); + Accelerator.Dispose(); + AcceleratorContext.Dispose(); + } + } + + private sealed class SharedHeuristicGpuTables + { + private int _referenceCount; + + internal SharedHeuristicGpuTables(Accelerator accelerator, string key) + { + Key = key; + + var primesDefault = DivisorGenerator.SmallPrimes; + DevicePrimesDefault = accelerator.Allocate1D(primesDefault.Length); + DevicePrimesDefault.View.CopyFromCPU(primesDefault); + + var primesLastOne = DivisorGenerator.SmallPrimesLastOne; + DevicePrimesLastOne = accelerator.Allocate1D(primesLastOne.Length); + DevicePrimesLastOne.View.CopyFromCPU(primesLastOne); + + var primesLastSeven = DivisorGenerator.SmallPrimesLastSeven; + DevicePrimesLastSeven = accelerator.Allocate1D(primesLastSeven.Length); + DevicePrimesLastSeven.View.CopyFromCPU(primesLastSeven); + + var primesLastThree = DivisorGenerator.SmallPrimesLastThree; + DevicePrimesLastThree = accelerator.Allocate1D(primesLastThree.Length); + DevicePrimesLastThree.View.CopyFromCPU(primesLastThree); + + var primesLastNine = DivisorGenerator.SmallPrimesLastNine; + DevicePrimesLastNine = accelerator.Allocate1D(primesLastNine.Length); + DevicePrimesLastNine.View.CopyFromCPU(primesLastNine); + + var primesPow2Default = DivisorGenerator.SmallPrimesPow2; + DevicePrimesPow2Default = accelerator.Allocate1D(primesPow2Default.Length); + DevicePrimesPow2Default.View.CopyFromCPU(primesPow2Default); + + var primesPow2LastOne = DivisorGenerator.SmallPrimesPow2LastOne; + DevicePrimesPow2LastOne = accelerator.Allocate1D(primesPow2LastOne.Length); + DevicePrimesPow2LastOne.View.CopyFromCPU(primesPow2LastOne); + + var primesPow2LastSeven = DivisorGenerator.SmallPrimesPow2LastSeven; + DevicePrimesPow2LastSeven = accelerator.Allocate1D(primesPow2LastSeven.Length); + DevicePrimesPow2LastSeven.View.CopyFromCPU(primesPow2LastSeven); + + var primesPow2LastThree = DivisorGenerator.SmallPrimesPow2LastThree; + DevicePrimesPow2LastThree = accelerator.Allocate1D(primesPow2LastThree.Length); + DevicePrimesPow2LastThree.View.CopyFromCPU(primesPow2LastThree); + + var primesPow2LastNine = DivisorGenerator.SmallPrimesPow2LastNine; + DevicePrimesPow2LastNine = accelerator.Allocate1D(primesPow2LastNine.Length); + DevicePrimesPow2LastNine.View.CopyFromCPU(primesPow2LastNine); + + HeuristicPrimeSieves.EnsureInitialized(); + HeuristicCombinedPrimeTester.EnsureInitialized(); + + var heuristicGroupA = HeuristicPrimeSieves.GroupADivisors; + HeuristicGroupADivisors = CopySpanToDevice(accelerator, heuristicGroupA); + + var heuristicGroupASquares = HeuristicPrimeSieves.GroupADivisorSquares; + HeuristicGroupADivisorSquares = CopySpanToDevice(accelerator, heuristicGroupASquares); + + HeuristicGroupBDivisorsEnding1 = CopyUintSpanToDevice(accelerator, DivisorGenerator.SmallPrimesLastOneWithoutLastThree); + HeuristicGroupBDivisorSquaresEnding1 = CopySpanToDevice(accelerator, DivisorGenerator.SmallPrimesPow2LastOneWithoutLastThree); + + HeuristicGroupBDivisorsEnding7 = CopyUintSpanToDevice(accelerator, DivisorGenerator.SmallPrimesLastSevenWithoutLastThree); + HeuristicGroupBDivisorSquaresEnding7 = CopySpanToDevice(accelerator, DivisorGenerator.SmallPrimesPow2LastSevenWithoutLastThree); + + HeuristicGroupBDivisorsEnding9 = CopyUintSpanToDevice(accelerator, DivisorGenerator.SmallPrimesLastNineWithoutLastThree); + HeuristicGroupBDivisorSquaresEnding9 = CopySpanToDevice(accelerator, DivisorGenerator.SmallPrimesPow2LastNineWithoutLastThree); + + var combinedEnding1 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding1Span; + HeuristicCombinedDivisorsEnding1 = CopySpanToDevice(accelerator, combinedEnding1); + var combinedSquaresEnding1 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding1SquaresSpan; + HeuristicCombinedDivisorSquaresEnding1 = CopySpanToDevice(accelerator, combinedSquaresEnding1); + + var combinedEnding3 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding3Span; + HeuristicCombinedDivisorsEnding3 = CopySpanToDevice(accelerator, combinedEnding3); + var combinedSquaresEnding3 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding3SquaresSpan; + HeuristicCombinedDivisorSquaresEnding3 = CopySpanToDevice(accelerator, combinedSquaresEnding3); + + var combinedEnding7 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding7Span; + HeuristicCombinedDivisorsEnding7 = CopySpanToDevice(accelerator, combinedEnding7); + var combinedSquaresEnding7 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding7SquaresSpan; + HeuristicCombinedDivisorSquaresEnding7 = CopySpanToDevice(accelerator, combinedSquaresEnding7); + + var combinedEnding9 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding9Span; + HeuristicCombinedDivisorsEnding9 = CopySpanToDevice(accelerator, combinedEnding9); + var combinedSquaresEnding9 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding9SquaresSpan; + HeuristicCombinedDivisorSquaresEnding9 = CopySpanToDevice(accelerator, combinedSquaresEnding9); + } + + internal string Key { get; } + internal MemoryBuffer1D DevicePrimesDefault { get; } + internal MemoryBuffer1D DevicePrimesLastOne { get; } + internal MemoryBuffer1D DevicePrimesLastSeven { get; } + internal MemoryBuffer1D DevicePrimesLastThree { get; } + internal MemoryBuffer1D DevicePrimesLastNine { get; } + internal MemoryBuffer1D DevicePrimesPow2Default { get; } + internal MemoryBuffer1D DevicePrimesPow2LastOne { get; } + internal MemoryBuffer1D DevicePrimesPow2LastSeven { get; } + internal MemoryBuffer1D DevicePrimesPow2LastThree { get; } + internal MemoryBuffer1D DevicePrimesPow2LastNine { get; } + internal MemoryBuffer1D HeuristicGroupADivisors { get; } + internal MemoryBuffer1D HeuristicGroupADivisorSquares { get; } + internal MemoryBuffer1D HeuristicGroupBDivisorsEnding1 { get; } + internal MemoryBuffer1D HeuristicGroupBDivisorSquaresEnding1 { get; } + internal MemoryBuffer1D HeuristicGroupBDivisorsEnding7 { get; } + internal MemoryBuffer1D HeuristicGroupBDivisorSquaresEnding7 { get; } + internal MemoryBuffer1D HeuristicGroupBDivisorsEnding9 { get; } + internal MemoryBuffer1D HeuristicGroupBDivisorSquaresEnding9 { get; } + internal MemoryBuffer1D HeuristicCombinedDivisorsEnding1 { get; } + internal MemoryBuffer1D HeuristicCombinedDivisorSquaresEnding1 { get; } + internal MemoryBuffer1D HeuristicCombinedDivisorsEnding3 { get; } + internal MemoryBuffer1D HeuristicCombinedDivisorSquaresEnding3 { get; } + internal MemoryBuffer1D HeuristicCombinedDivisorsEnding7 { get; } + internal MemoryBuffer1D HeuristicCombinedDivisorSquaresEnding7 { get; } + internal MemoryBuffer1D HeuristicCombinedDivisorsEnding9 { get; } + internal MemoryBuffer1D HeuristicCombinedDivisorSquaresEnding9 { get; } + + internal void AddReference() + { + _referenceCount++; + } + + internal bool Release() + { + _referenceCount--; + if (_referenceCount == 0) + { + Dispose(); + return true; + } + + return false; + } + + private static MemoryBuffer1D CopySpanToDevice(Accelerator accelerator, ReadOnlySpan span) + { + var buffer = accelerator.Allocate1D(span.Length); + if (!span.IsEmpty) + { + ref ulong sourceRef = ref MemoryMarshal.GetReference(span); + buffer.View.CopyFromCPU(ref sourceRef, span.Length); + } + + return buffer; + } + + private static MemoryBuffer1D CopyUintSpanToDevice(Accelerator accelerator, ReadOnlySpan span) + { + var buffer = accelerator.Allocate1D(span.Length); + if (!span.IsEmpty) + { + var converted = new ulong[span.Length]; + for (int i = 0; i < span.Length; i++) + { + converted[i] = span[i]; + } + + buffer.View.CopyFromCPU(converted); + } + + return buffer; + } + + private void Dispose() + { DevicePrimesDefault.Dispose(); DevicePrimesLastOne.Dispose(); DevicePrimesLastSeven.Dispose(); @@ -381,11 +625,26 @@ public sealed class PrimeTester DevicePrimesPow2LastSeven.Dispose(); DevicePrimesPow2LastThree.Dispose(); DevicePrimesPow2LastNine.Dispose(); - Accelerator.Dispose(); - AcceleratorContext.Dispose(); + HeuristicGroupADivisors.Dispose(); + HeuristicGroupADivisorSquares.Dispose(); + HeuristicGroupBDivisorsEnding1.Dispose(); + HeuristicGroupBDivisorSquaresEnding1.Dispose(); + HeuristicGroupBDivisorsEnding7.Dispose(); + HeuristicGroupBDivisorSquaresEnding7.Dispose(); + HeuristicGroupBDivisorsEnding9.Dispose(); + HeuristicGroupBDivisorSquaresEnding9.Dispose(); + HeuristicCombinedDivisorsEnding1.Dispose(); + HeuristicCombinedDivisorSquaresEnding1.Dispose(); + HeuristicCombinedDivisorsEnding3.Dispose(); + HeuristicCombinedDivisorSquaresEnding3.Dispose(); + HeuristicCombinedDivisorsEnding7.Dispose(); + HeuristicCombinedDivisorSquaresEnding7.Dispose(); + HeuristicCombinedDivisorsEnding9.Dispose(); + HeuristicCombinedDivisorSquaresEnding9.Dispose(); } } } + // Expose cache clearing for accelerator disposal coordination public static void ClearGpuCaches(Accelerator accelerator) { @@ -395,6 +654,10 @@ public sealed class PrimeTester internal static void DisposeGpuContexts() { PrimeTesterGpuContextPool.DisposeAll(); + lock (GpuWarmUpLock) + { + WarmedGpuLeaseCount = 0; + } } [MethodImpl(MethodImplOptions.AggressiveInlining)] From 567b92792c2a626e6be9fdddf109b8d6bd2e240d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Lema=C5=84czyk?= Date: Fri, 31 Oct 2025 18:52:00 +0100 Subject: [PATCH 2/6] Use span growth instead of dictionaries in partial factor --- .../PrimeOrderCalculator.Cpu.cs | 276 ++++++------------ PerfectNumbers.Core/ThreadStaticPools.cs | 37 --- 2 files changed, 90 insertions(+), 223 deletions(-) diff --git a/PerfectNumbers.Core/PrimeOrderCalculator.Cpu.cs b/PerfectNumbers.Core/PrimeOrderCalculator.Cpu.cs index 3f5773e..5ec3245 100644 --- a/PerfectNumbers.Core/PrimeOrderCalculator.Cpu.cs +++ b/PerfectNumbers.Core/PrimeOrderCalculator.Cpu.cs @@ -1057,13 +1057,13 @@ internal static partial class PrimeOrderCalculator // Span exponentSlots = exponentSlotsArray.AsSpan(0, FactorSlotCount); Span primeSlots = stackalloc ulong[FactorSlotCount]; Span exponentSlots = stackalloc int[FactorSlotCount]; + ulong[]? primeSlotArray = null; + int[]? exponentSlotArray = null; // We don't need to worry about leftovers, because we always use indexes within the calculated counts // primeSlots.Clear(); // exponentSlots.Clear(); int factorCount = 0; - Dictionary? counts = null; - bool useDictionary = false; uint limit = config.SmallFactorLimit == 0 ? uint.MaxValue : config.SmallFactorLimit; ulong remaining = value; @@ -1083,41 +1083,15 @@ internal static partial class PrimeOrderCalculator if (!gpuFactored) { - // We don't need to worry about leftovers, because we always use indexes within the calculated counts - // primeSlots.Clear(); - // exponentSlots.Clear(); - - counts = ThreadStaticPools.RentUlongIntDictionary(Math.Max(FactorSlotCount, 8)); - counts.Clear(); - - // bool gpuPopulated = TryPopulateSmallPrimeFactorsGpu(value, limit, primeSlots, exponentSlots, out factorCount, out remaining); - // if (!gpuPopulated) - // { - // throw new InvalidOperationException($"GPU didn't populate factors for {value}"); - // counts.Clear(); - remaining = PopulateSmallPrimeFactorsCpu(value, limit, counts); - // } - - int dictionaryCount = counts.Count; - if (dictionaryCount <= FactorSlotCount) - { - int copyIndex = 0; - foreach (KeyValuePair entry in counts) - { - primeSlots[copyIndex] = entry.Key; - exponentSlots[copyIndex] = entry.Value; - copyIndex++; - } - - factorCount = copyIndex; - ThreadStaticPools.ReturnUlongIntDictionary(counts); - counts = null; - } - else - { - useDictionary = true; - factorCount = dictionaryCount; - } + PopulateSmallPrimeFactorsCpu( + value, + limit, + ref primeSlots, + ref exponentSlots, + ref primeSlotArray, + ref exponentSlotArray, + out factorCount, + out remaining); } @@ -1157,7 +1131,7 @@ internal static partial class PrimeOrderCalculator if (isPrime) { - AddFactorToCollector(ref useDictionary, ref counts, primeSlots, exponentSlots, ref factorCount, composite, 1); + AddFactorToCollector(ref primeSlots, ref exponentSlots, ref primeSlotArray, ref exponentSlotArray, ref factorCount, composite, 1); continue; } @@ -1229,7 +1203,7 @@ internal static partial class PrimeOrderCalculator if (entry.IsPrime) { - AddFactorToCollector(ref useDictionary, ref counts, primeSlots, exponentSlots, ref factorCount, composite, 1); + AddFactorToCollector(ref primeSlots, ref exponentSlots, ref primeSlotArray, ref exponentSlotArray, ref factorCount, composite, 1); } // composite is never smaller on the execution path // else if (composite > 1UL) @@ -1264,33 +1238,15 @@ internal static partial class PrimeOrderCalculator ArrayPool pool = ThreadStaticPools.FactorEntryPool; bool temp; - if (useDictionary) + if (factorCount == 0) { - temp = counts is null || counts.Count == 0; - if (temp && cofactor == value) + if (cofactor == value) { result = PartialFactorResult.Rent(null, cofactor, false, 0, cofactorIsPrime); goto ReturnResult; } - if (temp) - { - result = PartialFactorResult.Rent(null, cofactor, cofactor == 1UL, 0, cofactorIsPrime); - goto ReturnResult; - } - - FactorEntry[] factors = pool.Rent(counts!.Count); - index = 0; - foreach (KeyValuePair entry in counts) - { - factors[index] = new FactorEntry(entry.Key, entry.Value, true); - index++; - } - - Array.Sort(factors, static (a, b) => a.Value.CompareTo(b.Value)); - - temp = cofactor == 1UL; - result = PartialFactorResult.Rent(factors, cofactor, temp, index, cofactorIsPrime); + result = PartialFactorResult.Rent(null, cofactor, cofactor == 1UL, 0, cofactorIsPrime); goto ReturnResult; } @@ -1330,14 +1286,14 @@ internal static partial class PrimeOrderCalculator result = PartialFactorResult.Rent(array, cofactor, temp, arrayIndex, cofactorIsPrime); ReturnResult: - // ThreadStaticPools.UlongPool.Return(primeSlotsArray); - // ThreadStaticPools.IntPool.Return(exponentSlotsArray); - - - if (counts is not null) + if (primeSlotArray is not null) { - counts.Clear(); - ThreadStaticPools.ReturnUlongIntDictionary(counts); + ThreadStaticPools.UlongPool.Return(primeSlotArray, clearArray: false); + } + + if (exponentSlotArray is not null) + { + ThreadStaticPools.IntPool.Return(exponentSlotArray, clearArray: false); } if (compositeStack is not null) @@ -1351,35 +1307,28 @@ internal static partial class PrimeOrderCalculator return result; } - - private static bool TryPopulateSmallPrimeFactorsCpu( - ulong value, - uint limit, - Span primeTargets, - Span exponentTargets, - out int factorCount, - out ulong remaining) + private static void PopulateSmallPrimeFactorsCpu( + ulong value, + uint limit, + ref Span primeTargets, + ref Span exponentTargets, + ref ulong[]? primeArray, + ref int[]? exponentArray, + out int factorCount, + out ulong remaining) { factorCount = 0; - remaining = value; + ulong remainingLocal = value; - // value will never <= 1 in production code - // if (value <= 1UL) - // { - // return true; - // } - - int capacity = Math.Min(primeTargets.Length, exponentTargets.Length); - // capacity will never equal 0 in production code - // if (capacity == 0) - // { - // return false; - // } + if (value <= 1UL) + { + remaining = remainingLocal; + return; + } uint[] primes = PrimesGenerator.SmallPrimes; ulong[] squares = PrimesGenerator.SmallPrimesPow2; int primeCount = primes.Length; - ulong remainingLocal = value; uint effectiveLimit = limit == 0 ? uint.MaxValue : limit; for (int i = 0; i < primeCount && remainingLocal > 1UL; i++) @@ -1391,19 +1340,11 @@ internal static partial class PrimeOrderCalculator } ulong primeSquare = squares[i]; - // primeSquare will never == 0 in production code - // if (primeSquare != 0 && primeSquare > remainingLocal) if (primeSquare > remainingLocal) { break; } - // primeCandidate will never equal 0 in production code - // if (primeCandidate == 0UL) - // { - // continue; - // } - ulong primeValue = primeCandidate; if ((remainingLocal % primeValue) != 0UL) { @@ -1416,10 +1357,13 @@ internal static partial class PrimeOrderCalculator continue; } - if (factorCount >= capacity) - { - throw new InvalidOperationException($"Capacity is smaller than factor count"); - } + EnsureCollectorCapacity( + ref primeTargets, + ref exponentTargets, + ref primeArray, + ref exponentArray, + factorCount, + factorCount + 1); primeTargets[factorCount] = primeValue; exponentTargets[factorCount] = exponent; @@ -1427,7 +1371,6 @@ internal static partial class PrimeOrderCalculator } remaining = remainingLocal; - return true; } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -1495,46 +1438,6 @@ internal static partial class PrimeOrderCalculator return exponent; } - private static ulong PopulateSmallPrimeFactorsCpu(ulong value, uint limit, Dictionary counts) - { - ulong remaining = value; - uint[] primes = PrimesGenerator.SmallPrimes; - ulong[] squares = PrimesGenerator.SmallPrimesPow2; - int primeCount = primes.Length; - - for (int i = 0; i < primeCount; i++) - { - uint primeCandidate = primes[i]; - if (primeCandidate > limit) - { - break; - } - - if (squares[i] > remaining) - { - break; - } - - if ((remaining % primeCandidate) != 0UL) - { - continue; - } - - int exponent = 0; - do - { - // TODO: Implement DivRem like solution or residue stepper to re-use previous iteration results in new calculation. Add / use method, likely to ULongExtensions - remaining /= primeCandidate; - exponent++; - } - while ((remaining % primeCandidate) == 0UL); - - counts[primeCandidate] = exponent; - } - - return remaining; - } - private static long CreateDeadlineTimestamp(int milliseconds) { if (milliseconds <= 0) @@ -1657,11 +1560,18 @@ internal static partial class PrimeOrderCalculator } } - private static bool TryAppendFactor(Span primes, Span exponents, ref int count, ulong prime, int exponent) + private static void AddFactorToCollector( + ref Span primes, + ref Span exponents, + ref ulong[]? primeArray, + ref int[]? exponentArray, + ref int count, + ulong prime, + int exponent) { if (exponent <= 0) { - return true; + return; } for (int i = 0; i < count; i++) @@ -1669,70 +1579,64 @@ internal static partial class PrimeOrderCalculator if (primes[i] == prime) { exponents[i] += exponent; - return true; + return; } } - if (count >= primes.Length) - { - return false; - } + EnsureCollectorCapacity( + ref primes, + ref exponents, + ref primeArray, + ref exponentArray, + count, + count + 1); primes[count] = prime; exponents[count] = exponent; count++; - return true; } - private static void AddFactorToCollector( - ref bool useDictionary, - ref Dictionary? counts, - Span primes, - Span exponents, - ref int count, - ulong prime, - int exponent) + private static void EnsureCollectorCapacity( + ref Span primes, + ref Span exponents, + ref ulong[]? primeArray, + ref int[]? exponentArray, + int count, + int requiredCapacity) { - if (exponent <= 0) + if (requiredCapacity <= primes.Length) { return; } - if (useDictionary) + int newCapacity = primes.Length << 1; + if (newCapacity < requiredCapacity) { - AddFactor(counts!, prime, exponent); - return; + newCapacity = requiredCapacity; } - if (TryAppendFactor(primes, exponents, ref count, prime, exponent)) + ulong[] newPrimeArray = ThreadStaticPools.UlongPool.Rent(newCapacity); + int[] newExponentArray = ThreadStaticPools.IntPool.Rent(newCapacity); + Span newPrimeSpan = newPrimeArray.AsSpan(); + Span newExponentSpan = newExponentArray.AsSpan(); + + primes.Slice(0, count).CopyTo(newPrimeSpan); + exponents.Slice(0, count).CopyTo(newExponentSpan); + + if (primeArray is not null) { - return; + ThreadStaticPools.UlongPool.Return(primeArray, clearArray: false); } - counts ??= ThreadStaticPools.RentUlongIntDictionary(Math.Max(count, 8)); - CopyFactorsToDictionary(primes, exponents, count, counts); - count = 0; - useDictionary = true; - AddFactor(counts, prime, exponent); - } - - private static void CopyFactorsToDictionary( - in ReadOnlySpan primes, - in ReadOnlySpan exponents, - int count, - Dictionary target) - { - for (int i = 0; i < count; i++) + if (exponentArray is not null) { - ulong prime = primes[i]; - int exponent = exponents[i]; - if (prime == 0UL || exponent == 0) - { - continue; - } - - target[prime] = exponent; + ThreadStaticPools.IntPool.Return(exponentArray, clearArray: false); } + + primeArray = newPrimeArray; + exponentArray = newExponentArray; + primes = newPrimeSpan; + exponents = newExponentSpan; } private static ulong CalculateByFactorizationCpu(ulong prime, in MontgomeryDivisorData divisorData) diff --git a/PerfectNumbers.Core/ThreadStaticPools.cs b/PerfectNumbers.Core/ThreadStaticPools.cs index b019cf8..3d5ca9b 100644 --- a/PerfectNumbers.Core/ThreadStaticPools.cs +++ b/PerfectNumbers.Core/ThreadStaticPools.cs @@ -260,43 +260,6 @@ namespace PerfectNumbers.Core pool.Add(stack); } - [ThreadStatic] - private static List>? _ulongIntDictionaryPool; - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Dictionary RentUlongIntDictionary(int capacityHint) - { - List>? pool = _ulongIntDictionaryPool; - if (pool is not null) - { - int poolCount = pool.Count; - if (poolCount > 0) - { - int lastIndex = poolCount - 1; - Dictionary dictionary = pool[lastIndex]; - pool.RemoveAt(lastIndex); - dictionary.EnsureCapacity(capacityHint); - - return dictionary; - } - } - - return new Dictionary(capacityHint); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void ReturnUlongIntDictionary(Dictionary dictionary) - { - List>? pool = _ulongIntDictionaryPool; - if (pool is null) - { - pool = new List>(4); - _ulongIntDictionaryPool = pool; - } - - pool.Add(dictionary); - } - [ThreadStatic] private static Dictionary? _factorCountDictionary; From 8c662c5c857c7b757b40e22b71916e6aeff8676a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Lema=C5=84czyk?= Date: Fri, 31 Oct 2025 21:47:42 +0100 Subject: [PATCH 3/6] Address feedback on GPU pooling and heuristic factoring --- PerfectNumbers.Core/Gpu/GpuContextPool.cs | 92 +++++++------------ .../Gpu/Kernels/PrimeTesterKernels.cs | 22 +---- .../HeuristicCombinedPrimeTester.cs | 27 +++--- PerfectNumbers.Core/HeuristicPrimeTester.cs | 29 +++--- .../PrimeOrderCalculator.Cpu.cs | 20 ++-- PerfectNumbers.Core/PrimeTester.cs | 41 +++++---- 6 files changed, 92 insertions(+), 139 deletions(-) diff --git a/PerfectNumbers.Core/Gpu/GpuContextPool.cs b/PerfectNumbers.Core/Gpu/GpuContextPool.cs index 3731c77..730f365 100644 --- a/PerfectNumbers.Core/Gpu/GpuContextPool.cs +++ b/PerfectNumbers.Core/Gpu/GpuContextPool.cs @@ -7,7 +7,6 @@ namespace PerfectNumbers.Core.Gpu; public static class GpuContextPool { - private static readonly bool PoolingEnabled = true; // The pool intentionally skips pre-loading ProcessEightBitWindows kernels so accelerator initialization // stays stable; GpuKernelPool JIT-compiles them on first use alongside the small-cycle uploads. // Default device preference for generic GPU kernels (prime scans, NTT, etc.) @@ -48,7 +47,6 @@ public static class GpuContextPool private static readonly ConcurrentQueue CpuPool = new(); private static readonly ConcurrentQueue GpuPool = new(); - private static readonly object WarmUpLock = new(); private static int WarmedGpuContextCount; public static GpuContextLease Rent() @@ -59,17 +57,14 @@ public static class GpuContextPool // Allows callers to choose CPU/GPU per use-case, decoupled from ForceCpu. public static GpuContextLease RentPreferred(bool preferCpu) { - if (PoolingEnabled) + if (preferCpu && CpuPool.TryDequeue(out var cpu)) { - if (preferCpu && CpuPool.TryDequeue(out var cpu)) - { - return new GpuContextLease(cpu); - } + return new GpuContextLease(cpu); + } - if (!preferCpu && GpuPool.TryDequeue(out var gpu)) - { - return new GpuContextLease(gpu); - } + if (!preferCpu && GpuPool.TryDequeue(out var gpu)) + { + return new GpuContextLease(gpu); } // Create a new accelerator when the pool does not have one available. @@ -78,53 +73,40 @@ public static class GpuContextPool public static void WarmUpPool(int threadCount) { - if (!PoolingEnabled || ForceCpu) + if (ForceCpu) { return; } - if (threadCount <= 0) - { - return; - } - - int target = threadCount / 4; + int target = threadCount >> 2; if (target == 0) { - target = 1; + target = threadCount; } - lock (WarmUpLock) + if (target <= WarmedGpuContextCount) { - if (target <= WarmedGpuContextCount) - { - return; - } - - int toCreate = target - WarmedGpuContextCount; - var contexts = new PooledContext[toCreate]; - for (int i = 0; i < toCreate; i++) - { - contexts[i] = new PooledContext(preferCpu: false); - } - - for (int i = 0; i < toCreate; i++) - { - GpuPool.Enqueue(contexts[i]); - } - - WarmedGpuContextCount = target; + return; } + + int toCreate = target - WarmedGpuContextCount; + var contexts = new PooledContext[toCreate]; + for (int i = 0; i < toCreate; i++) + { + contexts[i] = new PooledContext(preferCpu: false); + } + + for (int i = 0; i < toCreate; i++) + { + GpuPool.Enqueue(contexts[i]); + } + + WarmedGpuContextCount = target; } public static void DisposeAll() { - if (!PoolingEnabled) - { - return; - } - while (CpuPool.TryDequeue(out var cpu)) { cpu.Dispose(); @@ -141,24 +123,16 @@ public static class GpuContextPool private static void Return(PooledContext ctx) { - if (PoolingEnabled) + ctx.Accelerator.Synchronize(); + if (ctx.IsCpu) { - ctx.Accelerator.Synchronize(); - if (ctx.IsCpu) - { - CpuPool.Enqueue(ctx); - } - else - { - GpuPool.Enqueue(ctx); - } + CpuPool.Enqueue(ctx); } - - else - { - ctx.Dispose(); - } - } + else + { + GpuPool.Enqueue(ctx); + } + } public struct GpuContextLease { diff --git a/PerfectNumbers.Core/Gpu/Kernels/PrimeTesterKernels.cs b/PerfectNumbers.Core/Gpu/Kernels/PrimeTesterKernels.cs index 5b2f3fe..19b08c9 100644 --- a/PerfectNumbers.Core/Gpu/Kernels/PrimeTesterKernels.cs +++ b/PerfectNumbers.Core/Gpu/Kernels/PrimeTesterKernels.cs @@ -68,11 +68,6 @@ internal static class PrimeTesterKernels } ulong prime = primes[i]; - if (prime == 0) - { - break; - } - if (n % prime == 0UL) { result = 0; @@ -94,32 +89,23 @@ internal static class PrimeTesterKernels byte nMod10 = (byte)(n % 10UL); ArrayView1D divisors = tables.SelectDivisors(tableKind, nMod10); ArrayView1D divisorSquares = tables.SelectDivisorSquares(tableKind, nMod10); - if (divisors.Length == 0 || divisorSquares.Length == 0) - { - return; - } - int divisorLength = (int)divisors.Length; - int squareLength = (int)divisorSquares.Length; + int divisorLength = (int)divisorSquares.Length; int threadIndex = index; - if (threadIndex >= divisorLength || threadIndex >= squareLength) + if (threadIndex >= divisorLength) { return; } ulong divisorSquare = divisorSquares[threadIndex]; - if (divisorSquare == 0UL || divisorSquare > maxDivisorSquare) + if (divisorSquare > maxDivisorSquare) { return; } ulong divisor = divisors[threadIndex]; + divisor = n % divisor; if (divisor == 0UL) - { - return; - } - - if (n % divisor == 0UL) { resultFlag[0] = 1; } diff --git a/PerfectNumbers.Core/HeuristicCombinedPrimeTester.cs b/PerfectNumbers.Core/HeuristicCombinedPrimeTester.cs index 3b4c051..0413fd9 100644 --- a/PerfectNumbers.Core/HeuristicCombinedPrimeTester.cs +++ b/PerfectNumbers.Core/HeuristicCombinedPrimeTester.cs @@ -235,21 +235,18 @@ public sealed class HeuristicCombinedPrimeTester bool compositeDetected = false; int divisorLength = GetCombinedDivisors(nMod10).Length; - if (divisorLength > 0) - { - int compositeFlag = 0; - flagView1D.CopyFromCPU(ref compositeFlag, 1); - kernel( - divisorLength, - flagView, - n, - maxDivisorSquare, - HeuristicGpuDivisorTableKind.Combined, - gpu.HeuristicGpuTables); - accelerator.Synchronize(); - flagView1D.CopyToCPU(ref compositeFlag, 1); - compositeDetected = compositeFlag != 0; - } + int compositeFlag = 0; + flagView1D.CopyFromCPU(ref compositeFlag, 1); + kernel( + divisorLength, + flagView, + n, + maxDivisorSquare, + HeuristicGpuDivisorTableKind.Combined, + gpu.HeuristicGpuTables); + accelerator.Synchronize(); + flagView1D.CopyToCPU(ref compositeFlag, 1); + compositeDetected = compositeFlag != 0; gpu.Dispose(); limiter.Dispose(); diff --git a/PerfectNumbers.Core/HeuristicPrimeTester.cs b/PerfectNumbers.Core/HeuristicPrimeTester.cs index 0a4e697..a60227b 100644 --- a/PerfectNumbers.Core/HeuristicPrimeTester.cs +++ b/PerfectNumbers.Core/HeuristicPrimeTester.cs @@ -252,20 +252,17 @@ public sealed class HeuristicPrimeTester int groupALength = HeuristicPrimeSieves.GroupADivisors.Length; - if (groupALength > 0) - { - flagView1D.CopyFromCPU(ref compositeFlag, 1); - kernel( - groupALength, - flagView, - n, - maxDivisorSquare, - HeuristicGpuDivisorTableKind.GroupA, - gpu.HeuristicGpuTables); - accelerator.Synchronize(); - flagView1D.CopyToCPU(ref compositeFlag, 1); - compositeDetected = compositeFlag != 0; - } + flagView1D.CopyFromCPU(ref compositeFlag, 1); + kernel( + groupALength, + flagView, + n, + maxDivisorSquare, + HeuristicGpuDivisorTableKind.GroupA, + gpu.HeuristicGpuTables); + accelerator.Synchronize(); + flagView1D.CopyToCPU(ref compositeFlag, 1); + compositeDetected = compositeFlag != 0; if (!compositeDetected) { @@ -287,10 +284,6 @@ public sealed class HeuristicPrimeTester } int divisorLength = GetGroupBDivisors(ending).Length; - if (divisorLength == 0) - { - continue; - } compositeFlag = 0; flagView1D.CopyFromCPU(ref compositeFlag, 1); diff --git a/PerfectNumbers.Core/PrimeOrderCalculator.Cpu.cs b/PerfectNumbers.Core/PrimeOrderCalculator.Cpu.cs index 5ec3245..702039e 100644 --- a/PerfectNumbers.Core/PrimeOrderCalculator.Cpu.cs +++ b/PerfectNumbers.Core/PrimeOrderCalculator.Cpu.cs @@ -1289,11 +1289,7 @@ internal static partial class PrimeOrderCalculator if (primeSlotArray is not null) { ThreadStaticPools.UlongPool.Return(primeSlotArray, clearArray: false); - } - - if (exponentSlotArray is not null) - { - ThreadStaticPools.IntPool.Return(exponentSlotArray, clearArray: false); + ThreadStaticPools.IntPool.Return(exponentSlotArray!, clearArray: false); } if (compositeStack is not null) @@ -1340,12 +1336,20 @@ internal static partial class PrimeOrderCalculator } ulong primeSquare = squares[i]; + // primeSquare will never == 0 in production code + // if (primeSquare != 0 && primeSquare > remainingLocal) if (primeSquare > remainingLocal) { break; } ulong primeValue = primeCandidate; + // primeCandidate will never equal 0 in production code + // if (primeCandidate == 0UL) + // { + // continue; + // } + if ((remainingLocal % primeValue) != 0UL) { continue; @@ -1626,11 +1630,7 @@ internal static partial class PrimeOrderCalculator if (primeArray is not null) { ThreadStaticPools.UlongPool.Return(primeArray, clearArray: false); - } - - if (exponentArray is not null) - { - ThreadStaticPools.IntPool.Return(exponentArray, clearArray: false); + ThreadStaticPools.IntPool.Return(exponentArray!, clearArray: false); } primeArray = newPrimeArray; diff --git a/PerfectNumbers.Core/PrimeTester.cs b/PerfectNumbers.Core/PrimeTester.cs index f20d9d4..f0f6897 100644 --- a/PerfectNumbers.Core/PrimeTester.cs +++ b/PerfectNumbers.Core/PrimeTester.cs @@ -152,15 +152,10 @@ public sealed class PrimeTester return; } - if (threadCount <= 0) - { - return; - } - - int target = threadCount / 4; + int target = threadCount >> 2; if (target == 0) { - target = 1; + target = threadCount; } lock (GpuWarmUpLock) @@ -267,12 +262,14 @@ public sealed class PrimeTester private static void ReleaseSharedTables(SharedHeuristicGpuTables tables) { + if (tables is null) + { + return; + } + lock (SharedTablesLock) { - if (tables.Release()) - { - SharedTables.Remove(tables.Key); - } + tables.Release(); } } @@ -303,6 +300,16 @@ public sealed class PrimeTester { lease.DisposeResources(); } + + lock (SharedTablesLock) + { + foreach (var entry in SharedTables.Values) + { + entry.Dispose(); + } + + SharedTables.Clear(); + } } internal static void Clear(Accelerator accelerator) @@ -572,16 +579,12 @@ public sealed class PrimeTester _referenceCount++; } - internal bool Release() + internal void Release() { - _referenceCount--; - if (_referenceCount == 0) + if (_referenceCount > 0) { - Dispose(); - return true; + _referenceCount--; } - - return false; } private static MemoryBuffer1D CopySpanToDevice(Accelerator accelerator, ReadOnlySpan span) @@ -613,7 +616,7 @@ public sealed class PrimeTester return buffer; } - private void Dispose() + internal void Dispose() { DevicePrimesDefault.Dispose(); DevicePrimesLastOne.Dispose(); From 469c8057d397f8f20249dc8b9f31c8d0719b854b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Lema=C5=84czyk?= Date: Fri, 31 Oct 2025 22:45:59 +0100 Subject: [PATCH 4/6] Flatten heuristic trial division bounds checks --- .../Gpu/Kernels/PrimeTesterKernels.cs | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/PerfectNumbers.Core/Gpu/Kernels/PrimeTesterKernels.cs b/PerfectNumbers.Core/Gpu/Kernels/PrimeTesterKernels.cs index 19b08c9..d6842e0 100644 --- a/PerfectNumbers.Core/Gpu/Kernels/PrimeTesterKernels.cs +++ b/PerfectNumbers.Core/Gpu/Kernels/PrimeTesterKernels.cs @@ -92,20 +92,14 @@ internal static class PrimeTesterKernels int divisorLength = (int)divisorSquares.Length; int threadIndex = index; - if (threadIndex >= divisorLength) - { - return; - } + bool skipThread = threadIndex >= divisorLength; + ulong divisorSquare = skipThread ? 0UL : divisorSquares[threadIndex]; + skipThread = skipThread | (divisorSquare > maxDivisorSquare); - ulong divisorSquare = divisorSquares[threadIndex]; - if (divisorSquare > maxDivisorSquare) - { - return; - } - - ulong divisor = divisors[threadIndex]; - divisor = n % divisor; - if (divisor == 0UL) + ulong divisor = skipThread ? 1UL : divisors[threadIndex]; + ulong remainder = skipThread ? 1UL : n % divisor; + bool hasFactor = !skipThread & (remainder == 0UL); + if (hasFactor) { resultFlag[0] = 1; } From b7c34f49606d635f50e53f37daef8c6e6a5fe1da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Lema=C5=84czyk?= Date: Fri, 31 Oct 2025 23:01:28 +0100 Subject: [PATCH 5/6] Preload divisor generator tables for GPU pool contexts --- .../Gpu/DivisorGeneratorGpuCache.cs | 142 ++++++++++++++++++ PerfectNumbers.Core/Gpu/GpuContextPool.cs | 6 + 2 files changed, 148 insertions(+) create mode 100644 PerfectNumbers.Core/Gpu/DivisorGeneratorGpuCache.cs diff --git a/PerfectNumbers.Core/Gpu/DivisorGeneratorGpuCache.cs b/PerfectNumbers.Core/Gpu/DivisorGeneratorGpuCache.cs new file mode 100644 index 0000000..9236270 --- /dev/null +++ b/PerfectNumbers.Core/Gpu/DivisorGeneratorGpuCache.cs @@ -0,0 +1,142 @@ +using System; +using System.Collections.Concurrent; +using System.Runtime.CompilerServices; +using ILGPU; +using ILGPU.Runtime; + +namespace PerfectNumbers.Core.Gpu; + +internal static class DivisorGeneratorGpuCache +{ + private sealed class AcceleratorReferenceComparer : IEqualityComparer + { + public static AcceleratorReferenceComparer Instance { get; } = new(); + + public bool Equals(Accelerator? x, Accelerator? y) + { + return ReferenceEquals(x, y); + } + + public int GetHashCode(Accelerator obj) + { + return RuntimeHelpers.GetHashCode(obj); + } + } + + private sealed class DivisorGeneratorGpuTables : IDisposable + { + internal DivisorGeneratorGpuTables(Accelerator accelerator) + { + SmallPrimes = AllocateAndCopyUInt(accelerator, DivisorGenerator.SmallPrimes); + SmallPrimesPow2 = AllocateAndCopyULong(accelerator, DivisorGenerator.SmallPrimesPow2); + SmallPrimesLastOne = AllocateAndCopyUInt(accelerator, DivisorGenerator.SmallPrimesLastOne); + SmallPrimesPow2LastOne = AllocateAndCopyULong(accelerator, DivisorGenerator.SmallPrimesPow2LastOne); + SmallPrimesLastSeven = AllocateAndCopyUInt(accelerator, DivisorGenerator.SmallPrimesLastSeven); + SmallPrimesPow2LastSeven = AllocateAndCopyULong(accelerator, DivisorGenerator.SmallPrimesPow2LastSeven); + SmallPrimesLastThree = AllocateAndCopyUInt(accelerator, DivisorGenerator.SmallPrimesLastThree); + SmallPrimesPow2LastThree = AllocateAndCopyULong(accelerator, DivisorGenerator.SmallPrimesPow2LastThree); + SmallPrimesLastNine = AllocateAndCopyUInt(accelerator, DivisorGenerator.SmallPrimesLastNine); + SmallPrimesPow2LastNine = AllocateAndCopyULong(accelerator, DivisorGenerator.SmallPrimesPow2LastNine); + SmallPrimesLastOneWithoutLastThree = AllocateAndCopyUInt(accelerator, DivisorGenerator.SmallPrimesLastOneWithoutLastThree); + SmallPrimesPow2LastOneWithoutLastThree = AllocateAndCopyULong(accelerator, DivisorGenerator.SmallPrimesPow2LastOneWithoutLastThree); + SmallPrimesLastSevenWithoutLastThree = AllocateAndCopyUInt(accelerator, DivisorGenerator.SmallPrimesLastSevenWithoutLastThree); + SmallPrimesPow2LastSevenWithoutLastThree = AllocateAndCopyULong(accelerator, DivisorGenerator.SmallPrimesPow2LastSevenWithoutLastThree); + SmallPrimesLastNineWithoutLastThree = AllocateAndCopyUInt(accelerator, DivisorGenerator.SmallPrimesLastNineWithoutLastThree); + SmallPrimesPow2LastNineWithoutLastThree = AllocateAndCopyULong(accelerator, DivisorGenerator.SmallPrimesPow2LastNineWithoutLastThree); + } + + internal MemoryBuffer1D SmallPrimes { get; } + internal MemoryBuffer1D SmallPrimesPow2 { get; } + internal MemoryBuffer1D SmallPrimesLastOne { get; } + internal MemoryBuffer1D SmallPrimesPow2LastOne { get; } + internal MemoryBuffer1D SmallPrimesLastSeven { get; } + internal MemoryBuffer1D SmallPrimesPow2LastSeven { get; } + internal MemoryBuffer1D SmallPrimesLastThree { get; } + internal MemoryBuffer1D SmallPrimesPow2LastThree { get; } + internal MemoryBuffer1D SmallPrimesLastNine { get; } + internal MemoryBuffer1D SmallPrimesPow2LastNine { get; } + internal MemoryBuffer1D SmallPrimesLastOneWithoutLastThree { get; } + internal MemoryBuffer1D SmallPrimesPow2LastOneWithoutLastThree { get; } + internal MemoryBuffer1D SmallPrimesLastSevenWithoutLastThree { get; } + internal MemoryBuffer1D SmallPrimesPow2LastSevenWithoutLastThree { get; } + internal MemoryBuffer1D SmallPrimesLastNineWithoutLastThree { get; } + internal MemoryBuffer1D SmallPrimesPow2LastNineWithoutLastThree { get; } + + public void Dispose() + { + SmallPrimes.Dispose(); + SmallPrimesPow2.Dispose(); + SmallPrimesLastOne.Dispose(); + SmallPrimesPow2LastOne.Dispose(); + SmallPrimesLastSeven.Dispose(); + SmallPrimesPow2LastSeven.Dispose(); + SmallPrimesLastThree.Dispose(); + SmallPrimesPow2LastThree.Dispose(); + SmallPrimesLastNine.Dispose(); + SmallPrimesPow2LastNine.Dispose(); + SmallPrimesLastOneWithoutLastThree.Dispose(); + SmallPrimesPow2LastOneWithoutLastThree.Dispose(); + SmallPrimesLastSevenWithoutLastThree.Dispose(); + SmallPrimesPow2LastSevenWithoutLastThree.Dispose(); + SmallPrimesLastNineWithoutLastThree.Dispose(); + SmallPrimesPow2LastNineWithoutLastThree.Dispose(); + } + + private static MemoryBuffer1D AllocateAndCopyUInt(Accelerator accelerator, uint[] source) + { + var buffer = accelerator.Allocate1D(source.Length); + if (source.Length > 0) + { + buffer.View.CopyFromCPU(source); + } + + return buffer; + } + + private static MemoryBuffer1D AllocateAndCopyULong(Accelerator accelerator, ulong[] source) + { + var buffer = accelerator.Allocate1D(source.Length); + if (source.Length > 0) + { + buffer.View.CopyFromCPU(source); + } + + return buffer; + } + } + + private static readonly ConcurrentDictionary Cache = new(AcceleratorReferenceComparer.Instance); + + public static void WarmUpByDivisorTables(Accelerator accelerator) + { + if (accelerator is null || accelerator.AcceleratorType == AcceleratorType.CPU) + { + return; + } + + Cache.GetOrAdd(accelerator, acc => new DivisorGeneratorGpuTables(acc)); + } + + public static void Clear(Accelerator accelerator) + { + if (accelerator is null) + { + return; + } + + if (Cache.TryRemove(accelerator, out var tables)) + { + tables.Dispose(); + } + } + + public static void DisposeAll() + { + foreach (var entry in Cache) + { + entry.Value.Dispose(); + } + + Cache.Clear(); + } +} diff --git a/PerfectNumbers.Core/Gpu/GpuContextPool.cs b/PerfectNumbers.Core/Gpu/GpuContextPool.cs index 730f365..53906c7 100644 --- a/PerfectNumbers.Core/Gpu/GpuContextPool.cs +++ b/PerfectNumbers.Core/Gpu/GpuContextPool.cs @@ -24,6 +24,10 @@ public static class GpuContextPool Context = Context.CreateDefault(); Accelerator = Context.GetPreferredDevice(preferCpu).CreateAccelerator(Context); IsCpu = Accelerator.AcceleratorType == AcceleratorType.CPU; + if (!IsCpu) + { + DivisorGeneratorGpuCache.WarmUpByDivisorTables(Accelerator); + } // NOTE: Avoid loading/compiling any kernel here to prevent implicit // CL stream/queue creation during accelerator construction. // Some OpenCL drivers are fragile when a queue is created immediately @@ -38,6 +42,7 @@ public static class GpuContextPool // Release PrimeTester GPU state for this accelerator (kernel/device primes) // Clear any per-accelerator cached resources to avoid releasing // after the accelerator is destroyed. + DivisorGeneratorGpuCache.Clear(Accelerator); PrimeTester.ClearGpuCaches(Accelerator); Accelerator.Dispose(); @@ -117,6 +122,7 @@ public static class GpuContextPool gpu.Dispose(); } + DivisorGeneratorGpuCache.DisposeAll(); PrimeTester.DisposeGpuContexts(); WarmedGpuContextCount = 0; } From 22f20bfe98ee9452f375903a42ace91c9cea9c20 Mon Sep 17 00:00:00 2001 From: mlemanczyk Date: Sat, 1 Nov 2025 03:42:36 +0100 Subject: [PATCH 6/6] Various small optimizations --- EvenPerfectBitScanner/Program.cs | 8 +- .../Gpu/GpuContextPoolTests.cs | 144 +++++++++--------- .../Gpu/GpuKernelCompilationTests.cs | 34 ++--- .../Gpu/LucasLehmerPrimeTesterTests.cs | 6 +- .../Gpu/PrimeTesterGpuTests.cs | 4 - .../GpuKernelPoolTests.cs | 36 ++--- .../PrimeTester/PrimeTesterIsPrimeTests.cs | 11 +- PerfectNumbers.Core/Gpu/GpuContextPool.cs | 143 ++++++----------- PerfectNumbers.Core/Gpu/GpuKernelPool.cs | 2 +- ...MersenneNumberDivisorByDivisorGpuTester.cs | 4 +- .../Gpu/MersenneNumberDivisorGpuTester.cs | 2 +- .../Gpu/MersenneNumberLucasLehmerGpuTester.cs | 16 +- .../Gpu/MersenneNumberResidueGpuTester.cs | 2 +- PerfectNumbers.Core/PrimeTester.cs | 74 +++++---- 14 files changed, 206 insertions(+), 280 deletions(-) diff --git a/EvenPerfectBitScanner/Program.cs b/EvenPerfectBitScanner/Program.cs index 8457e47..b55bce1 100644 --- a/EvenPerfectBitScanner/Program.cs +++ b/EvenPerfectBitScanner/Program.cs @@ -28,6 +28,8 @@ internal static class Program [ThreadStatic] private static bool _lastCompositeP; + private static bool _runPrimesOnCpu; + private static void Main(string[] args) { try @@ -49,7 +51,7 @@ internal static class Program NttGpuMath.GpuTransformBackend = _cliArguments.NttBackend; NttGpuMath.ReductionMode = _cliArguments.ModReductionMode; - GpuContextPool.ForceCpu = _cliArguments.ForcePrimeKernelsOnCpu; + _runPrimesOnCpu = _cliArguments.ForcePrimeKernelsOnCpu; ulong currentP = _cliArguments.StartPrime; ulong remainder = currentP % 6UL; @@ -259,7 +261,7 @@ internal static class Program orderWarmupLimitOverride ?? 5_000_000UL, NttGpuMath.ReductionMode, mersenneOnGpu ? "gpu" : "cpu", - GpuContextPool.ForceCpu ? "cpu" : "gpu", + _runPrimesOnCpu ? "cpu" : "gpu", orderOnGpu ? "gpu" : "cpu"); if (!string.IsNullOrEmpty(_cliArguments.ResultsPrefix)) @@ -624,7 +626,7 @@ internal static class Program // If primes-device=gpu, route p primality through GPU-assisted sieve with deterministic MR validation. if (!_cliArguments.UseByDivisor) { - if (!(GpuContextPool.ForceCpu + if (!(_runPrimesOnCpu ? PrimeTester.IsPrime(p) : PrimeTesters.Value!.IsPrimeGpu(p))) { diff --git a/PerfectNumbers.Core.Tests/Gpu/GpuContextPoolTests.cs b/PerfectNumbers.Core.Tests/Gpu/GpuContextPoolTests.cs index 3909631..8558b82 100644 --- a/PerfectNumbers.Core.Tests/Gpu/GpuContextPoolTests.cs +++ b/PerfectNumbers.Core.Tests/Gpu/GpuContextPoolTests.cs @@ -9,88 +9,88 @@ namespace PerfectNumbers.Core.Tests.Gpu; [Collection("GpuNtt")] public class GpuContextPoolTests { - [Fact] - [Trait("Category", "Fast")] - public void RentPreferred_cpu_returns_cpu_accelerator() - { - GpuContextPool.DisposeAll(); + // [Fact] + // [Trait("Category", "Fast")] + // public void RentPreferred_cpu_returns_cpu_accelerator() + // { + // GpuContextPool.DisposeAll(); - GpuContextPool.GpuContextLease lease = GpuContextPool.RentPreferred(preferCpu: true); - try - { - lease.Accelerator.AcceleratorType.Should().Be(AcceleratorType.CPU); - } - finally - { - lease.Dispose(); - } + // GpuContextPool.GpuContextLease lease = GpuContextPool.RentPreferred(preferCpu: true); + // try + // { + // lease.Accelerator.AcceleratorType.Should().Be(AcceleratorType.CPU); + // } + // finally + // { + // lease.Dispose(); + // } - GpuContextPool.DisposeAll(); - } + // GpuContextPool.DisposeAll(); + // } - [Fact] - [Trait("Category", "Fast")] - public void RentPreferred_reuses_cpu_context() - { - GpuContextPool.DisposeAll(); + // [Fact] + // [Trait("Category", "Fast")] + // public void RentPreferred_reuses_cpu_context() + // { + // GpuContextPool.DisposeAll(); - Context firstContext; - Accelerator firstAccelerator; - GpuContextPool.GpuContextLease lease = GpuContextPool.RentPreferred(preferCpu: true); - try - { - firstContext = lease.Context; - firstAccelerator = lease.Accelerator; - } - finally - { - lease.Dispose(); - } + // Context firstContext; + // Accelerator firstAccelerator; + // GpuContextPool.GpuContextLease lease = GpuContextPool.RentPreferred(preferCpu: true); + // try + // { + // firstContext = lease.Context; + // firstAccelerator = lease.Accelerator; + // } + // finally + // { + // lease.Dispose(); + // } - lease = GpuContextPool.RentPreferred(preferCpu: true); - try - { - lease.Context.Should().BeSameAs(firstContext); - lease.Accelerator.Should().BeSameAs(firstAccelerator); - } - finally - { - lease.Dispose(); - } + // lease = GpuContextPool.RentPreferred(preferCpu: true); + // try + // { + // lease.Context.Should().BeSameAs(firstContext); + // lease.Accelerator.Should().BeSameAs(firstAccelerator); + // } + // finally + // { + // lease.Dispose(); + // } - GpuContextPool.DisposeAll(); - } + // GpuContextPool.DisposeAll(); + // } - [Fact] - [Trait("Category", "Fast")] - public void DisposeAll_clears_pooled_contexts() - { - GpuContextPool.DisposeAll(); + // [Fact] + // [Trait("Category", "Fast")] + // public void DisposeAll_clears_pooled_contexts() + // { + // GpuContextPool.DisposeAll(); - Context firstContext; - GpuContextPool.GpuContextLease lease = GpuContextPool.RentPreferred(preferCpu: true); - try - { - firstContext = lease.Context; - } - finally - { - lease.Dispose(); - } + // Context firstContext; + // GpuContextPool.GpuContextLease lease = GpuContextPool.RentPreferred(preferCpu: true); + // try + // { + // firstContext = lease.Context; + // } + // finally + // { + // lease.Dispose(); + // } - GpuContextPool.DisposeAll(); + // GpuContextPool.DisposeAll(); - lease = GpuContextPool.RentPreferred(preferCpu: true); - try - { - lease.Context.Should().NotBeSameAs(firstContext); - } - finally - { - lease.Dispose(); - } + // lease = GpuContextPool.RentPreferred(preferCpu: true); + // try + // { + // lease.Context.Should().NotBeSameAs(firstContext); + // } + // finally + // { + // lease.Dispose(); + // } - GpuContextPool.DisposeAll(); - } + // GpuContextPool.DisposeAll(); + // } } diff --git a/PerfectNumbers.Core.Tests/Gpu/GpuKernelCompilationTests.cs b/PerfectNumbers.Core.Tests/Gpu/GpuKernelCompilationTests.cs index b33320f..92cb06b 100644 --- a/PerfectNumbers.Core.Tests/Gpu/GpuKernelCompilationTests.cs +++ b/PerfectNumbers.Core.Tests/Gpu/GpuKernelCompilationTests.cs @@ -167,23 +167,23 @@ public class GpuKernelCompilationTests return new object[] { name, loader }; } - [Theory] - [Trait("Category", "Fast")] - [MemberData(nameof(KernelLoaders))] - public void Kernel_compiles_without_internal_compiler_errors(string name, Action compile) - { - GpuContextPool.GpuContextLease lease = GpuContextPool.RentPreferred(preferCpu: true); - try - { - var accelerator = lease.Accelerator; - Action action = () => compile(accelerator); - action.Should().NotThrow($"kernel {name} should compile"); - } - finally - { - lease.Dispose(); - } - } + // [Theory] + // [Trait("Category", "Fast")] + // [MemberData(nameof(KernelLoaders))] + // public void Kernel_compiles_without_internal_compiler_errors(string name, Action compile) + // { + // GpuContextPool.GpuContextLease lease = GpuContextPool.RentPreferred(preferCpu: true); + // try + // { + // var accelerator = lease.Accelerator; + // Action action = () => compile(accelerator); + // action.Should().NotThrow($"kernel {name} should compile"); + // } + // finally + // { + // lease.Dispose(); + // } + // } private static void CompileDivisorCycleCacheKernel(Accelerator accelerator) { diff --git a/PerfectNumbers.Core.Tests/Gpu/LucasLehmerPrimeTesterTests.cs b/PerfectNumbers.Core.Tests/Gpu/LucasLehmerPrimeTesterTests.cs index 7fd6cf1..4be4955 100644 --- a/PerfectNumbers.Core.Tests/Gpu/LucasLehmerPrimeTesterTests.cs +++ b/PerfectNumbers.Core.Tests/Gpu/LucasLehmerPrimeTesterTests.cs @@ -14,7 +14,7 @@ public class LucasLehmerPrimeTesterTests [Trait("Category", "Fast")] public void IsMersennePrime_gpu_returns_expected_results(ulong p, bool expected) { - new MersenneNumberLucasLehmerGpuTester().IsMersennePrime(p).Should().Be(expected); + new MersenneNumberLucasLehmerGpuTester().IsPrime(p, true).Should().Be(expected); } [Fact] @@ -26,7 +26,7 @@ public class LucasLehmerPrimeTesterTests const ulong exponent = 128UL; var tester = new MersenneNumberLucasLehmerGpuTester(); tester.WarmUpNttParameters(exponent); - tester.IsMersennePrime(exponent).Should().BeFalse(); + tester.IsPrime(exponent, true).Should().BeFalse(); } [Fact] @@ -49,7 +49,7 @@ public class LucasLehmerPrimeTesterTests { var tester = new MersenneNumberLucasLehmerGpuTester(); tester.WarmUpNttParameters(exponent); - tester.IsMersennePrime(exponent).Should().BeFalse(); + tester.IsPrime(exponent, true).Should().BeFalse(); } [Theory] diff --git a/PerfectNumbers.Core.Tests/Gpu/PrimeTesterGpuTests.cs b/PerfectNumbers.Core.Tests/Gpu/PrimeTesterGpuTests.cs index 6213b07..9ba37b0 100644 --- a/PerfectNumbers.Core.Tests/Gpu/PrimeTesterGpuTests.cs +++ b/PerfectNumbers.Core.Tests/Gpu/PrimeTesterGpuTests.cs @@ -10,7 +10,6 @@ public class PrimeTesterGpuTests [Fact] public void IsPrimeGpu_accepts_known_primes() { - GpuContextPool.ForceCpu = false; try { var tester = new PrimeTester(); @@ -22,7 +21,6 @@ public class PrimeTesterGpuTests } finally { - GpuContextPool.ForceCpu = false; GpuContextPool.DisposeAll(); } } @@ -30,7 +28,6 @@ public class PrimeTesterGpuTests [Fact] public void IsPrimeGpu_rejects_composites() { - GpuContextPool.ForceCpu = false; try { var tester = new PrimeTester(); @@ -42,7 +39,6 @@ public class PrimeTesterGpuTests } finally { - GpuContextPool.ForceCpu = false; GpuContextPool.DisposeAll(); } } diff --git a/PerfectNumbers.Core.Tests/GpuKernelPoolTests.cs b/PerfectNumbers.Core.Tests/GpuKernelPoolTests.cs index c2bdd5b..ea766f7 100644 --- a/PerfectNumbers.Core.Tests/GpuKernelPoolTests.cs +++ b/PerfectNumbers.Core.Tests/GpuKernelPoolTests.cs @@ -7,26 +7,26 @@ namespace PerfectNumbers.Core.Tests; [Collection("GpuNtt")] public class GpuKernelPoolTests { - [Fact] - [Trait("Category", "Fast")] - public void EnsureSmallCyclesOnDevice_returns_same_view_on_subsequent_calls() - { - GpuContextPool.GpuContextLease lease = GpuContextPool.RentPreferred(preferCpu: true); - try - { - var accelerator = lease.Accelerator; + // [Fact] + // [Trait("Category", "Fast")] + // public void EnsureSmallCyclesOnDevice_returns_same_view_on_subsequent_calls() + // { + // GpuContextPool.GpuContextLease lease = GpuContextPool.RentPreferred(preferCpu: true); + // try + // { + // var accelerator = lease.Accelerator; - var first = GpuKernelPool.EnsureSmallCyclesOnDevice(accelerator); - var second = GpuKernelPool.EnsureSmallCyclesOnDevice(accelerator); + // var first = GpuKernelPool.EnsureSmallCyclesOnDevice(accelerator); + // var second = GpuKernelPool.EnsureSmallCyclesOnDevice(accelerator); - second.Equals(first).Should().BeTrue(); - second.Length.Should().Be(MersenneDivisorCycles.SmallDivisorsMax + 1); - } - finally - { - lease.Dispose(); - } - } + // second.Equals(first).Should().BeTrue(); + // second.Length.Should().Be(MersenneDivisorCycles.SmallDivisorsMax + 1); + // } + // finally + // { + // lease.Dispose(); + // } + // } [Fact] [Trait("Category", "Fast")] diff --git a/PerfectNumbers.Core.Tests/PrimeTester/PrimeTesterIsPrimeTests.cs b/PerfectNumbers.Core.Tests/PrimeTester/PrimeTesterIsPrimeTests.cs index f7dd5b7..d1efe98 100644 --- a/PerfectNumbers.Core.Tests/PrimeTester/PrimeTesterIsPrimeTests.cs +++ b/PerfectNumbers.Core.Tests/PrimeTester/PrimeTesterIsPrimeTests.cs @@ -37,7 +37,6 @@ public class PrimeTesterIsPrimeTests [InlineData(279UL, false)] public void IsPrimeGpu_uses_residue_specific_tables(ulong n, bool expected) { - GpuContextPool.ForceCpu = false; var tester = new PrimeTester(); tester.IsPrimeGpu(n).Should().Be(expected); @@ -48,20 +47,14 @@ public class PrimeTesterIsPrimeTests public void IsPrimeGpu_falls_back_to_cpu_when_forced() { var tester = new PrimeTester(); - GpuContextPool.ForceCpu = true; - - tester.IsPrimeGpu(137UL).Should().BeTrue(); - - tester.IsPrimeGpu(341UL).Should().BeFalse(); - - GpuContextPool.ForceCpu = false; + PrimeTester.IsPrime(137UL).Should().BeTrue(); + PrimeTester.IsPrime(341UL).Should().BeFalse(); } [Fact] [Trait("Category", "Fast")] public void IsPrimeGpu_handles_parallel_calls_for_known_primes() { - GpuContextPool.ForceCpu = false; GpuPrimeWorkLimiter.SetLimit(64); PrimeTester.GpuBatchSize = 256_000; diff --git a/PerfectNumbers.Core/Gpu/GpuContextPool.cs b/PerfectNumbers.Core/Gpu/GpuContextPool.cs index 53906c7..e74a27d 100644 --- a/PerfectNumbers.Core/Gpu/GpuContextPool.cs +++ b/PerfectNumbers.Core/Gpu/GpuContextPool.cs @@ -7,27 +7,19 @@ namespace PerfectNumbers.Core.Gpu; public static class GpuContextPool { - // The pool intentionally skips pre-loading ProcessEightBitWindows kernels so accelerator initialization - // stays stable; GpuKernelPool JIT-compiles them on first use alongside the small-cycle uploads. - // Default device preference for generic GPU kernels (prime scans, NTT, etc.) - public static bool ForceCpu { get; set; } = false; + // The pool intentionally skips pre-loading ProcessEightBitWindows kernels so accelerator initialization + // stays stable; GpuKernelPool JIT-compiles them on first use alongside the small-cycle uploads. + // Default device preference for generic GPU kernels (prime scans, NTT, etc.) + internal sealed class PooledContext + { + public Context Context { get; } + public Accelerator Accelerator { get; } - internal sealed class PooledContext - { - public Context Context { get; } - public Accelerator Accelerator { get; } - public bool IsCpu { get; } - public object ExecutionLock { get; } = new(); - - public PooledContext(bool preferCpu) + public PooledContext() { Context = Context.CreateDefault(); - Accelerator = Context.GetPreferredDevice(preferCpu).CreateAccelerator(Context); - IsCpu = Accelerator.AcceleratorType == AcceleratorType.CPU; - if (!IsCpu) - { - DivisorGeneratorGpuCache.WarmUpByDivisorTables(Accelerator); - } + Accelerator = Context.GetPreferredDevice(false).CreateAccelerator(Context); + DivisorGeneratorGpuCache.WarmUpByDivisorTables(Accelerator); // NOTE: Avoid loading/compiling any kernel here to prevent implicit // CL stream/queue creation during accelerator construction. // Some OpenCL drivers are fragile when a queue is created immediately @@ -36,7 +28,6 @@ public static class GpuContextPool public void Dispose() { - // Ensure all cached GPU buffers for this accelerator are released. NttGpuMath.ClearCaches(Accelerator); // Release PrimeTester GPU state for this accelerator (kernel/device primes) @@ -50,73 +41,45 @@ public static class GpuContextPool } } - private static readonly ConcurrentQueue CpuPool = new(); private static readonly ConcurrentQueue GpuPool = new(); - private static int WarmedGpuContextCount; - - public static GpuContextLease Rent() - { - return RentPreferred(ForceCpu); - } + private static int WarmedGpuContextCount; // Allows callers to choose CPU/GPU per use-case, decoupled from ForceCpu. - public static GpuContextLease RentPreferred(bool preferCpu) + public static GpuContextLease Rent() { - if (preferCpu && CpuPool.TryDequeue(out var cpu)) - { - return new GpuContextLease(cpu); - } - - if (!preferCpu && GpuPool.TryDequeue(out var gpu)) + if (GpuPool.TryDequeue(out var gpu)) { return new GpuContextLease(gpu); } // Create a new accelerator when the pool does not have one available. - return new GpuContextLease(new PooledContext(preferCpu)); + return new GpuContextLease(new PooledContext()); } - public static void WarmUpPool(int threadCount) - { - if (ForceCpu) - { - return; - } + public static void WarmUpPool(int threadCount) + { + if (threadCount <= WarmedGpuContextCount) + { + return; + } - int target = threadCount >> 2; - if (target == 0) - { - target = threadCount; - } + int toCreate = threadCount - WarmedGpuContextCount; + var contexts = new PooledContext[toCreate]; + for (int i = 0; i < toCreate; i++) + { + contexts[i] = new PooledContext(); + } - if (target <= WarmedGpuContextCount) - { - return; - } - - int toCreate = target - WarmedGpuContextCount; - var contexts = new PooledContext[toCreate]; - for (int i = 0; i < toCreate; i++) - { - contexts[i] = new PooledContext(preferCpu: false); - } - - for (int i = 0; i < toCreate; i++) - { - GpuPool.Enqueue(contexts[i]); - } - - WarmedGpuContextCount = target; - } + for (int i = 0; i < toCreate; i++) + { + GpuPool.Enqueue(contexts[i]); + } + WarmedGpuContextCount = threadCount; + } public static void DisposeAll() { - while (CpuPool.TryDequeue(out var cpu)) - { - cpu.Dispose(); - } - while (GpuPool.TryDequeue(out var gpu)) { gpu.Dispose(); @@ -124,43 +87,31 @@ public static class GpuContextPool DivisorGeneratorGpuCache.DisposeAll(); PrimeTester.DisposeGpuContexts(); - WarmedGpuContextCount = 0; + WarmedGpuContextCount = 0; } private static void Return(PooledContext ctx) { ctx.Accelerator.Synchronize(); - if (ctx.IsCpu) - { - CpuPool.Enqueue(ctx); - } - else - { - GpuPool.Enqueue(ctx); - } + GpuPool.Enqueue(ctx); } - public struct GpuContextLease - { - private readonly PooledContext _ctx; + public readonly struct GpuContextLease + { + private readonly PooledContext _ctx; - internal GpuContextLease(PooledContext ctx) - { - _ctx = ctx; - } + internal GpuContextLease(PooledContext ctx) + { + _ctx = ctx; + } - public Context Context => _ctx.Context; - - public Accelerator Accelerator => _ctx.Accelerator; - - public object ExecutionLock => _ctx.ExecutionLock; - - - public void Dispose() - { + public readonly Context Context => _ctx.Context; + public readonly Accelerator Accelerator => _ctx.Accelerator; + public readonly void Dispose() + { Return(_ctx); - } - } + } + } } diff --git a/PerfectNumbers.Core/Gpu/GpuKernelPool.cs b/PerfectNumbers.Core/Gpu/GpuKernelPool.cs index fb8a934..0257fc9 100644 --- a/PerfectNumbers.Core/Gpu/GpuKernelPool.cs +++ b/PerfectNumbers.Core/Gpu/GpuKernelPool.cs @@ -280,7 +280,7 @@ public class GpuKernelPool public static GpuKernelLease GetKernel(bool useGpuOrder) { var limiter = GpuPrimeWorkLimiter.Acquire(); - var gpu = RentPreferred(preferCpu: !useGpuOrder); + var gpu = Rent(); var accelerator = gpu.Accelerator; var kernels = GetKernels(accelerator); return GpuKernelLease.Rent(limiter, gpu, kernels); diff --git a/PerfectNumbers.Core/Gpu/MersenneNumberDivisorByDivisorGpuTester.cs b/PerfectNumbers.Core/Gpu/MersenneNumberDivisorByDivisorGpuTester.cs index 27a066a..25391b0 100644 --- a/PerfectNumbers.Core/Gpu/MersenneNumberDivisorByDivisorGpuTester.cs +++ b/PerfectNumbers.Core/Gpu/MersenneNumberDivisorByDivisorGpuTester.cs @@ -492,7 +492,7 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester : IMersenneNumberDiv internal DivisorScanSession(MersenneNumberDivisorByDivisorGpuTester owner) { _owner = owner; - _lease = GpuContextPool.RentPreferred(preferCpu: false); + _lease = GpuContextPool.Rent(); _accelerator = _lease.Accelerator; _capacity = Math.Max(1, owner._gpuBatchSize); } @@ -657,7 +657,7 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester : IMersenneNumberDiv return lease; } - return GpuContextPool.RentPreferred(preferCpu: false); + return GpuContextPool.Rent(); } private void ReturnAccelerator(GpuContextPool.GpuContextLease lease) => _acceleratorPool.Add(lease); diff --git a/PerfectNumbers.Core/Gpu/MersenneNumberDivisorGpuTester.cs b/PerfectNumbers.Core/Gpu/MersenneNumberDivisorGpuTester.cs index 93b52b3..eaffd13 100644 --- a/PerfectNumbers.Core/Gpu/MersenneNumberDivisorGpuTester.cs +++ b/PerfectNumbers.Core/Gpu/MersenneNumberDivisorGpuTester.cs @@ -37,7 +37,7 @@ public sealed class MersenneNumberDivisorGpuTester public bool IsDivisible(ulong exponent, in ReadOnlyGpuUInt128 divisor) { - var gpu = GpuContextPool.RentPreferred(preferCpu: false); + var gpu = GpuContextPool.Rent(); var accelerator = gpu.Accelerator; var kernel = GetKernel(accelerator); var resultBuffer = _resultBuffers.GetOrAdd(accelerator, acc => acc.Allocate1D(1)); diff --git a/PerfectNumbers.Core/Gpu/MersenneNumberLucasLehmerGpuTester.cs b/PerfectNumbers.Core/Gpu/MersenneNumberLucasLehmerGpuTester.cs index 4a118c1..228e13b 100644 --- a/PerfectNumbers.Core/Gpu/MersenneNumberLucasLehmerGpuTester.cs +++ b/PerfectNumbers.Core/Gpu/MersenneNumberLucasLehmerGpuTester.cs @@ -38,16 +38,6 @@ public class MersenneNumberLucasLehmerGpuTester // Configurable LL slice size to keep kernels short. Default 32. public int SliceSize = 32; - public bool IsMersennePrime(ulong exponent) - { - // Default to global kernel preference for backward compatibility. - bool runOnGpu = !GpuContextPool.ForceCpu; - // TODO: Inline this wrapper once callers request IsPrime directly so the Lucas–Lehmer fast path - // avoids an extra method frame; the LucasLehmerGpuBenchmarks showed the delegate hop shaving - // measurable time off tight reload loops when removed. - return IsPrime(exponent, runOnGpu); - } - public bool IsPrime(ulong exponent, bool runOnGpu) { // Early rejections aligned with incremental/order sieves, but safe for small p: @@ -78,7 +68,7 @@ public class MersenneNumberLucasLehmerGpuTester } else { - var gpu = GpuContextPool.RentPreferred(preferCpu: !runOnGpu); + var gpu = GpuContextPool.Rent(); var accelerator = gpu.Accelerator; var kernel = GetKernel(accelerator); var modulus = new GpuUInt128(((UInt128)1 << (int)exponent) - 1UL); // TODO: Cache these Mersenne moduli per exponent so LL GPU runs skip rebuilding them every launch. @@ -110,7 +100,7 @@ public class MersenneNumberLucasLehmerGpuTester } } - var gpu = GpuContextPool.RentPreferred(preferCpu: false); + var gpu = GpuContextPool.Rent(); var accelerator = gpu.Accelerator; var kernel = GetBatchKernel(accelerator); @@ -162,7 +152,7 @@ public class MersenneNumberLucasLehmerGpuTester private bool IsMersennePrimeNtt(ulong exponent, GpuUInt128 nttMod, GpuUInt128 primitiveRoot, bool runOnGpu) { - var gpu = GpuContextPool.RentPreferred(preferCpu: !runOnGpu); + var gpu = GpuContextPool.Rent(); var accelerator = gpu.Accelerator; var addKernel = GetAddSmallKernel(accelerator); var subKernel = GetSubSmallKernel(accelerator); diff --git a/PerfectNumbers.Core/Gpu/MersenneNumberResidueGpuTester.cs b/PerfectNumbers.Core/Gpu/MersenneNumberResidueGpuTester.cs index 759d575..8a97c87 100644 --- a/PerfectNumbers.Core/Gpu/MersenneNumberResidueGpuTester.cs +++ b/PerfectNumbers.Core/Gpu/MersenneNumberResidueGpuTester.cs @@ -132,7 +132,7 @@ public class MersenneNumberResidueGpuTester(bool useGpuOrder) return lease; } - return GpuContextPool.RentPreferred(preferCpu: !_useGpuOrder); + return GpuContextPool.Rent(); } private void ReturnAccelerator(GpuContextPool.GpuContextLease lease) => _acceleratorPool.Add(lease); diff --git a/PerfectNumbers.Core/PrimeTester.cs b/PerfectNumbers.Core/PrimeTester.cs index f0f6897..9a8fc90 100644 --- a/PerfectNumbers.Core/PrimeTester.cs +++ b/PerfectNumbers.Core/PrimeTester.cs @@ -95,49 +95,48 @@ public sealed class PrimeTester [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool IsPrimeGpu(ulong n) { - bool forceCpu = GpuContextPool.ForceCpu; - bool belowGpuRange = n < 31UL; + // bool belowGpuRange = n < 31UL; - if (forceCpu || belowGpuRange) - { + // if (belowGpuRange) + // { return IsPrime(n); - } + // } - var limiter = GpuPrimeWorkLimiter.Acquire(); - var gpu = PrimeTesterGpuContextPool.Rent(1); - var accelerator = gpu.Accelerator; - var kernel = gpu.Kernel; + // var limiter = GpuPrimeWorkLimiter.Acquire(); + // var gpu = PrimeTesterGpuContextPool.Rent(1); + // var accelerator = gpu.Accelerator; + // var kernel = gpu.Kernel; - ulong value = n; - byte flag = 0; + // ulong value = n; + // byte flag = 0; - var input = gpu.Input; - var output = gpu.Output; - var inputView = input.View; - var outputView = output.View; + // var input = gpu.Input; + // var output = gpu.Output; + // var inputView = input.View; + // var outputView = output.View; - inputView.CopyFromCPU(ref value, 1); - kernel( - 1, - inputView, - gpu.DevicePrimesDefault.View, - gpu.DevicePrimesLastOne.View, - gpu.DevicePrimesLastSeven.View, - gpu.DevicePrimesLastThree.View, - gpu.DevicePrimesLastNine.View, - gpu.DevicePrimesPow2Default.View, - gpu.DevicePrimesPow2LastOne.View, - gpu.DevicePrimesPow2LastSeven.View, - gpu.DevicePrimesPow2LastThree.View, - gpu.DevicePrimesPow2LastNine.View, - outputView); - accelerator.Synchronize(); - outputView.CopyToCPU(ref flag, 1); + // inputView.CopyFromCPU(ref value, 1); + // kernel( + // 1, + // inputView, + // gpu.DevicePrimesDefault.View, + // gpu.DevicePrimesLastOne.View, + // gpu.DevicePrimesLastSeven.View, + // gpu.DevicePrimesLastThree.View, + // gpu.DevicePrimesLastNine.View, + // gpu.DevicePrimesPow2Default.View, + // gpu.DevicePrimesPow2LastOne.View, + // gpu.DevicePrimesPow2LastSeven.View, + // gpu.DevicePrimesPow2LastThree.View, + // gpu.DevicePrimesPow2LastNine.View, + // outputView); + // accelerator.Synchronize(); + // outputView.CopyToCPU(ref flag, 1); - gpu.Dispose(); - limiter.Dispose(); + // gpu.Dispose(); + // limiter.Dispose(); - return flag != 0; + // return flag != 0; } public static int GpuBatchSize { get; set; } = 262_144; @@ -147,11 +146,6 @@ public sealed class PrimeTester public static void WarmUpGpuKernels(int threadCount) { - if (GpuContextPool.ForceCpu) - { - return; - } - int target = threadCount >> 2; if (target == 0) {