From ab6a416de2fd719ed0da19a5cc22aa4d8006b6bb 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] 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)]