From 2281d9535e446a89a546c38bebbc9be083447b72 Mon Sep 17 00:00:00 2001 From: mlemanczyk Date: Sun, 2 Nov 2025 11:22:14 +0100 Subject: [PATCH] Revert "Merge pull request #251 from mlemanczyk/codex/refactor-prime-checking-on-gpu-n5t2pm" This reverts commit 936ce239dbc7375b19bcd162d49ef972b918dac1, reversing changes made to e14b77e5049ed47b70d8a03937d82dc6895379b9. --- EvenPerfectBitScanner/Program.cs | 15 +- .../Gpu/GpuContextPoolTests.cs | 144 +++--- .../Gpu/GpuKernelCompilationTests.cs | 34 +- .../Gpu/LucasLehmerPrimeTesterTests.cs | 6 +- .../Gpu/PrimeTesterGpuTests.cs | 4 + .../GpuKernelPoolTests.cs | 36 +- .../HeuristicCombinedPrimeTesterTests.cs | 88 ++-- .../PrimeTester/PrimeTesterIsPrimeTests.cs | 11 +- .../Gpu/DivisorGeneratorGpuCache.cs | 142 ------ PerfectNumbers.Core/Gpu/GpuContextPool.cs | 137 ++--- PerfectNumbers.Core/Gpu/GpuKernelPool.cs | 2 +- .../Gpu/HeuristicGpuDivisorTables.cs | 146 ------ .../Gpu/Kernels/PrimeTesterKernels.cs | 46 +- ...MersenneNumberDivisorByDivisorGpuTester.cs | 4 +- .../Gpu/MersenneNumberDivisorGpuTester.cs | 2 +- .../Gpu/MersenneNumberLucasLehmerGpuTester.cs | 16 +- .../Gpu/MersenneNumberResidueGpuTester.cs | 2 +- .../HeuristicCombinedPrimeTester.cs | 191 ++++--- PerfectNumbers.Core/HeuristicPrimeSieves.cs | 39 +- PerfectNumbers.Core/HeuristicPrimeTester.cs | 217 +++++--- .../PrimeOrderCalculator.Cpu.cs | 266 +++++++--- PerfectNumbers.Core/PrimeTester.cs | 482 ++++-------------- PerfectNumbers.Core/ThreadStaticPools.cs | 37 ++ 23 files changed, 880 insertions(+), 1187 deletions(-) delete mode 100644 PerfectNumbers.Core/Gpu/DivisorGeneratorGpuCache.cs delete mode 100644 PerfectNumbers.Core/Gpu/HeuristicGpuDivisorTables.cs diff --git a/EvenPerfectBitScanner/Program.cs b/EvenPerfectBitScanner/Program.cs index b55bce1..bf755d4 100644 --- a/EvenPerfectBitScanner/Program.cs +++ b/EvenPerfectBitScanner/Program.cs @@ -28,8 +28,6 @@ internal static class Program [ThreadStatic] private static bool _lastCompositeP; - private static bool _runPrimesOnCpu; - private static void Main(string[] args) { try @@ -51,21 +49,18 @@ internal static class Program NttGpuMath.GpuTransformBackend = _cliArguments.NttBackend; NttGpuMath.ReductionMode = _cliArguments.ModReductionMode; - _runPrimesOnCpu = _cliArguments.ForcePrimeKernelsOnCpu; + GpuContextPool.ForceCpu = _cliArguments.ForcePrimeKernelsOnCpu; ulong currentP = _cliArguments.StartPrime; 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; @@ -119,7 +114,7 @@ internal static class Program // Apply GPU prime sieve runtime configuration GpuPrimeWorkLimiter.SetLimit(gpuPrimeThreads); - PrimeTester.GpuBatchSize = gpuPrimeBatch; + PrimeTester.GpuBatchSize = Math.Max(1, gpuPrimeBatch); MersenneDivisorCycles mersenneDivisorCycles = new(); if (!File.Exists(cyclesPath) || continueCyclesGeneration) @@ -261,7 +256,7 @@ internal static class Program orderWarmupLimitOverride ?? 5_000_000UL, NttGpuMath.ReductionMode, mersenneOnGpu ? "gpu" : "cpu", - _runPrimesOnCpu ? "cpu" : "gpu", + GpuContextPool.ForceCpu ? "cpu" : "gpu", orderOnGpu ? "gpu" : "cpu"); if (!string.IsNullOrEmpty(_cliArguments.ResultsPrefix)) @@ -626,7 +621,7 @@ internal static class Program // If primes-device=gpu, route p primality through GPU-assisted sieve with deterministic MR validation. if (!_cliArguments.UseByDivisor) { - if (!(_runPrimesOnCpu + if (!(GpuContextPool.ForceCpu ? PrimeTester.IsPrime(p) : PrimeTesters.Value!.IsPrimeGpu(p))) { diff --git a/PerfectNumbers.Core.Tests/Gpu/GpuContextPoolTests.cs b/PerfectNumbers.Core.Tests/Gpu/GpuContextPoolTests.cs index 8558b82..3909631 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 92cb06b..b33320f 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 4be4955..7fd6cf1 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().IsPrime(p, true).Should().Be(expected); + new MersenneNumberLucasLehmerGpuTester().IsMersennePrime(p).Should().Be(expected); } [Fact] @@ -26,7 +26,7 @@ public class LucasLehmerPrimeTesterTests const ulong exponent = 128UL; var tester = new MersenneNumberLucasLehmerGpuTester(); tester.WarmUpNttParameters(exponent); - tester.IsPrime(exponent, true).Should().BeFalse(); + tester.IsMersennePrime(exponent).Should().BeFalse(); } [Fact] @@ -49,7 +49,7 @@ public class LucasLehmerPrimeTesterTests { var tester = new MersenneNumberLucasLehmerGpuTester(); tester.WarmUpNttParameters(exponent); - tester.IsPrime(exponent, true).Should().BeFalse(); + tester.IsMersennePrime(exponent).Should().BeFalse(); } [Theory] diff --git a/PerfectNumbers.Core.Tests/Gpu/PrimeTesterGpuTests.cs b/PerfectNumbers.Core.Tests/Gpu/PrimeTesterGpuTests.cs index 9ba37b0..6213b07 100644 --- a/PerfectNumbers.Core.Tests/Gpu/PrimeTesterGpuTests.cs +++ b/PerfectNumbers.Core.Tests/Gpu/PrimeTesterGpuTests.cs @@ -10,6 +10,7 @@ public class PrimeTesterGpuTests [Fact] public void IsPrimeGpu_accepts_known_primes() { + GpuContextPool.ForceCpu = false; try { var tester = new PrimeTester(); @@ -21,6 +22,7 @@ public class PrimeTesterGpuTests } finally { + GpuContextPool.ForceCpu = false; GpuContextPool.DisposeAll(); } } @@ -28,6 +30,7 @@ public class PrimeTesterGpuTests [Fact] public void IsPrimeGpu_rejects_composites() { + GpuContextPool.ForceCpu = false; try { var tester = new PrimeTester(); @@ -39,6 +42,7 @@ public class PrimeTesterGpuTests } finally { + GpuContextPool.ForceCpu = false; GpuContextPool.DisposeAll(); } } diff --git a/PerfectNumbers.Core.Tests/GpuKernelPoolTests.cs b/PerfectNumbers.Core.Tests/GpuKernelPoolTests.cs index ea766f7..c2bdd5b 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/HeuristicCombinedPrimeTesterTests.cs b/PerfectNumbers.Core.Tests/HeuristicCombinedPrimeTesterTests.cs index 92e6e26..d3d2051 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(3UL); - combined[1].Should().Be(7UL); - combined[2].Should().Be(11UL); - combined[3].Should().Be(13UL); + combined[0].Should().Be(3U); + combined[1].Should().Be(7U); + combined[2].Should().Be(11U); + combined[3].Should().Be(13U); int index = 4; const int segmentsToCheck = 6; for (int segment = 0; segment < segmentsToCheck; segment++) { - ulong groupAValue = combined[index++]; - groupAValue.Should().BeGreaterThan(13UL); - (groupAValue % 10UL).Should().Be(3UL); + uint groupAValue = combined[index++]; + groupAValue.Should().BeGreaterThan(13U); + (groupAValue % 10U).Should().Be(3U); - ulong groupBValue = combined[index++]; - groupBValue.Should().BeGreaterThan(13UL); - (groupBValue % 10UL).Should().NotBe(3UL); + uint groupBValue = combined[index++]; + groupBValue.Should().BeGreaterThan(13U); + (groupBValue % 10U).Should().NotBe(3U); } } @@ -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(3UL); - combined[1].Should().Be(7UL); - combined[2].Should().Be(11UL); - combined[3].Should().Be(13UL); + combined[0].Should().Be(3U); + combined[1].Should().Be(7U); + combined[2].Should().Be(11U); + combined[3].Should().Be(13U); int index = 4; const int segmentsToCheck = 5; @@ -111,14 +111,14 @@ public class HeuristicCombinedPrimeTesterTests { for (int i = 0; i < 2; i++) { - ulong groupAValue = combined[index++]; - groupAValue.Should().BeGreaterThan(13UL); - (groupAValue % 10UL).Should().Be(3UL); + uint groupAValue = combined[index++]; + groupAValue.Should().BeGreaterThan(13U); + (groupAValue % 10U).Should().Be(3U); } - ulong groupBValue = combined[index++]; - groupBValue.Should().BeGreaterThan(13UL); - (groupBValue % 10UL).Should().NotBe(3UL); + uint groupBValue = combined[index++]; + groupBValue.Should().BeGreaterThan(13U); + (groupBValue % 10U).Should().NotBe(3U); } } @@ -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(3UL); - combined[1].Should().Be(7UL); - combined[2].Should().Be(11UL); - combined[3].Should().Be(13UL); + combined[0].Should().Be(3U); + combined[1].Should().Be(7U); + combined[2].Should().Be(11U); + combined[3].Should().Be(13U); int index = 4; const int segmentsToCheck = 4; @@ -146,16 +146,16 @@ public class HeuristicCombinedPrimeTesterTests { for (int i = 0; i < 3; i++) { - ulong groupAValue = combined[index++]; - groupAValue.Should().BeGreaterThan(13UL); - (groupAValue % 10UL).Should().Be(3UL); + uint groupAValue = combined[index++]; + groupAValue.Should().BeGreaterThan(13U); + (groupAValue % 10U).Should().Be(3U); } for (int i = 0; i < 2; i++) { - ulong groupBValue = combined[index++]; - groupBValue.Should().BeGreaterThan(13UL); - (groupBValue % 10UL).Should().NotBe(3UL); + uint groupBValue = combined[index++]; + groupBValue.Should().BeGreaterThan(13U); + (groupBValue % 10U).Should().NotBe(3U); } } } @@ -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(3UL); - combined[1].Should().Be(7UL); - combined[2].Should().Be(11UL); - combined[3].Should().Be(13UL); + combined[0].Should().Be(3U); + combined[1].Should().Be(7U); + combined[2].Should().Be(11U); + combined[3].Should().Be(13U); int index = 4; const int segmentsToCheck = 4; @@ -184,14 +184,14 @@ public class HeuristicCombinedPrimeTesterTests { for (int i = 0; i < 3; i++) { - ulong groupAValue = combined[index++]; - groupAValue.Should().BeGreaterThan(13UL); - (groupAValue % 10UL).Should().Be(3UL); + uint groupAValue = combined[index++]; + groupAValue.Should().BeGreaterThan(13U); + (groupAValue % 10U).Should().Be(3U); } - ulong groupBValue = combined[index++]; - groupBValue.Should().BeGreaterThan(13UL); - (groupBValue % 10UL).Should().NotBe(3UL); + uint groupBValue = combined[index++]; + groupBValue.Should().BeGreaterThan(13U); + (groupBValue % 10U).Should().NotBe(3U); } } diff --git a/PerfectNumbers.Core.Tests/PrimeTester/PrimeTesterIsPrimeTests.cs b/PerfectNumbers.Core.Tests/PrimeTester/PrimeTesterIsPrimeTests.cs index d1efe98..f7dd5b7 100644 --- a/PerfectNumbers.Core.Tests/PrimeTester/PrimeTesterIsPrimeTests.cs +++ b/PerfectNumbers.Core.Tests/PrimeTester/PrimeTesterIsPrimeTests.cs @@ -37,6 +37,7 @@ 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); @@ -47,14 +48,20 @@ public class PrimeTesterIsPrimeTests public void IsPrimeGpu_falls_back_to_cpu_when_forced() { var tester = new PrimeTester(); - PrimeTester.IsPrime(137UL).Should().BeTrue(); - PrimeTester.IsPrime(341UL).Should().BeFalse(); + GpuContextPool.ForceCpu = true; + + tester.IsPrimeGpu(137UL).Should().BeTrue(); + + tester.IsPrimeGpu(341UL).Should().BeFalse(); + + GpuContextPool.ForceCpu = false; } [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/DivisorGeneratorGpuCache.cs b/PerfectNumbers.Core/Gpu/DivisorGeneratorGpuCache.cs deleted file mode 100644 index 9236270..0000000 --- a/PerfectNumbers.Core/Gpu/DivisorGeneratorGpuCache.cs +++ /dev/null @@ -1,142 +0,0 @@ -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 e74a27d..4943be7 100644 --- a/PerfectNumbers.Core/Gpu/GpuContextPool.cs +++ b/PerfectNumbers.Core/Gpu/GpuContextPool.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Concurrent; using ILGPU; using ILGPU.Runtime; @@ -7,19 +6,24 @@ 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.) - internal sealed class PooledContext - { - public Context Context { get; } - public Accelerator Accelerator { get; } + 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.) + public static bool ForceCpu { get; set; } = false; - public PooledContext() + 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) { Context = Context.CreateDefault(); - Accelerator = Context.GetPreferredDevice(false).CreateAccelerator(Context); - DivisorGeneratorGpuCache.WarmUpByDivisorTables(Accelerator); + Accelerator = Context.GetPreferredDevice(preferCpu).CreateAccelerator(Context); + IsCpu = Accelerator.AcceleratorType == AcceleratorType.CPU; // 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 @@ -28,12 +32,12 @@ 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) // Clear any per-accelerator cached resources to avoid releasing // after the accelerator is destroyed. - DivisorGeneratorGpuCache.Clear(Accelerator); PrimeTester.ClearGpuCaches(Accelerator); Accelerator.Dispose(); @@ -41,77 +45,96 @@ public static class GpuContextPool } } + private static readonly ConcurrentQueue CpuPool = new(); private static readonly ConcurrentQueue GpuPool = new(); - private static int WarmedGpuContextCount; - // Allows callers to choose CPU/GPU per use-case, decoupled from ForceCpu. public static GpuContextLease Rent() { - if (GpuPool.TryDequeue(out var gpu)) + return RentPreferred(ForceCpu); + } + + // Allows callers to choose CPU/GPU per use-case, decoupled from ForceCpu. + public static GpuContextLease RentPreferred(bool preferCpu) + { + if (PoolingEnabled) { - return new GpuContextLease(gpu); + if (preferCpu && CpuPool.TryDequeue(out var cpu)) + { + return new GpuContextLease(cpu); + } + + if (!preferCpu && 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()); - } - - public static void WarmUpPool(int threadCount) - { - if (threadCount <= WarmedGpuContextCount) - { - return; - } - - int toCreate = threadCount - WarmedGpuContextCount; - var contexts = new PooledContext[toCreate]; - for (int i = 0; i < toCreate; i++) - { - contexts[i] = new PooledContext(); - } - - for (int i = 0; i < toCreate; i++) - { - GpuPool.Enqueue(contexts[i]); - } - - WarmedGpuContextCount = threadCount; + return new GpuContextLease(new PooledContext(preferCpu)); } public static void DisposeAll() { + if (!PoolingEnabled) + { + return; + } + + while (CpuPool.TryDequeue(out var cpu)) + { + cpu.Dispose(); + } + while (GpuPool.TryDequeue(out var gpu)) { gpu.Dispose(); } - DivisorGeneratorGpuCache.DisposeAll(); PrimeTester.DisposeGpuContexts(); - WarmedGpuContextCount = 0; } private static void Return(PooledContext ctx) { - ctx.Accelerator.Synchronize(); - GpuPool.Enqueue(ctx); - } - - public readonly struct GpuContextLease - { - private readonly PooledContext _ctx; - - internal GpuContextLease(PooledContext ctx) + if (PoolingEnabled) { - _ctx = ctx; + ctx.Accelerator.Synchronize(); + if (ctx.IsCpu) + { + CpuPool.Enqueue(ctx); + } + else + { + GpuPool.Enqueue(ctx); + } } - public readonly Context Context => _ctx.Context; - public readonly Accelerator Accelerator => _ctx.Accelerator; + else + { + ctx.Dispose(); + } + } + + public struct GpuContextLease + { + private readonly PooledContext _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 void Dispose() - { Return(_ctx); - } - } + } + } } diff --git a/PerfectNumbers.Core/Gpu/GpuKernelPool.cs b/PerfectNumbers.Core/Gpu/GpuKernelPool.cs index 0257fc9..fb8a934 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 = Rent(); + var gpu = RentPreferred(preferCpu: !useGpuOrder); var accelerator = gpu.Accelerator; var kernels = GetKernels(accelerator); return GpuKernelLease.Rent(limiter, gpu, kernels); diff --git a/PerfectNumbers.Core/Gpu/HeuristicGpuDivisorTables.cs b/PerfectNumbers.Core/Gpu/HeuristicGpuDivisorTables.cs deleted file mode 100644 index d295417..0000000 --- a/PerfectNumbers.Core/Gpu/HeuristicGpuDivisorTables.cs +++ /dev/null @@ -1,146 +0,0 @@ -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 d6842e0..ac4bd39 100644 --- a/PerfectNumbers.Core/Gpu/Kernels/PrimeTesterKernels.cs +++ b/PerfectNumbers.Core/Gpu/Kernels/PrimeTesterKernels.cs @@ -1,19 +1,9 @@ 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. @@ -61,13 +51,13 @@ 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 (n % prime == 0UL) { result = 0; @@ -78,31 +68,19 @@ internal static class PrimeTesterKernels results[index] = result; } - public static void HeuristicTrialDivisionKernel( - Index1D index, - ArrayView resultFlag, - ulong n, - ulong maxDivisorSquare, - HeuristicGpuDivisorTableKind tableKind, - HeuristicGpuDivisorTables tables) + public static void HeuristicTrialDivisionKernel(Index1D index, ArrayView divisors, ulong n, ArrayView resultFlag) { - byte nMod10 = (byte)(n % 10UL); - ArrayView1D divisors = tables.SelectDivisors(tableKind, nMod10); - ArrayView1D divisorSquares = tables.SelectDivisorSquares(tableKind, nMod10); + ulong divisor = divisors[index]; + if (divisor <= 1UL) + { + return; + } - int divisorLength = (int)divisorSquares.Length; - int threadIndex = index; - bool skipThread = threadIndex >= divisorLength; - ulong divisorSquare = skipThread ? 0UL : divisorSquares[threadIndex]; - skipThread = skipThread | (divisorSquare > maxDivisorSquare); - - ulong divisor = skipThread ? 1UL : divisors[threadIndex]; - ulong remainder = skipThread ? 1UL : n % divisor; - bool hasFactor = !skipThread & (remainder == 0UL); - if (hasFactor) - { - resultFlag[0] = 1; - } + ulong residue = n % divisor; + if (residue == 0UL) + { + resultFlag[0] = 1; + } } public static void SharesFactorKernel(Index1D index, ArrayView numbers, ArrayView results) diff --git a/PerfectNumbers.Core/Gpu/MersenneNumberDivisorByDivisorGpuTester.cs b/PerfectNumbers.Core/Gpu/MersenneNumberDivisorByDivisorGpuTester.cs index 25391b0..27a066a 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.Rent(); + _lease = GpuContextPool.RentPreferred(preferCpu: false); _accelerator = _lease.Accelerator; _capacity = Math.Max(1, owner._gpuBatchSize); } @@ -657,7 +657,7 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester : IMersenneNumberDiv return lease; } - return GpuContextPool.Rent(); + return GpuContextPool.RentPreferred(preferCpu: false); } private void ReturnAccelerator(GpuContextPool.GpuContextLease lease) => _acceleratorPool.Add(lease); diff --git a/PerfectNumbers.Core/Gpu/MersenneNumberDivisorGpuTester.cs b/PerfectNumbers.Core/Gpu/MersenneNumberDivisorGpuTester.cs index eaffd13..93b52b3 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.Rent(); + var gpu = GpuContextPool.RentPreferred(preferCpu: false); 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 228e13b..4a118c1 100644 --- a/PerfectNumbers.Core/Gpu/MersenneNumberLucasLehmerGpuTester.cs +++ b/PerfectNumbers.Core/Gpu/MersenneNumberLucasLehmerGpuTester.cs @@ -38,6 +38,16 @@ 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: @@ -68,7 +78,7 @@ public class MersenneNumberLucasLehmerGpuTester } else { - var gpu = GpuContextPool.Rent(); + var gpu = GpuContextPool.RentPreferred(preferCpu: !runOnGpu); 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. @@ -100,7 +110,7 @@ public class MersenneNumberLucasLehmerGpuTester } } - var gpu = GpuContextPool.Rent(); + var gpu = GpuContextPool.RentPreferred(preferCpu: false); var accelerator = gpu.Accelerator; var kernel = GetBatchKernel(accelerator); @@ -152,7 +162,7 @@ public class MersenneNumberLucasLehmerGpuTester private bool IsMersennePrimeNtt(ulong exponent, GpuUInt128 nttMod, GpuUInt128 primitiveRoot, bool runOnGpu) { - var gpu = GpuContextPool.Rent(); + var gpu = GpuContextPool.RentPreferred(preferCpu: !runOnGpu); 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 8a97c87..759d575 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.Rent(); + return GpuContextPool.RentPreferred(preferCpu: !_useGpuOrder); } private void ReturnAccelerator(GpuContextPool.GpuContextLease lease) => _acceleratorPool.Add(lease); diff --git a/PerfectNumbers.Core/HeuristicCombinedPrimeTester.cs b/PerfectNumbers.Core/HeuristicCombinedPrimeTester.cs index 0413fd9..24d52a7 100644 --- a/PerfectNumbers.Core/HeuristicCombinedPrimeTester.cs +++ b/PerfectNumbers.Core/HeuristicCombinedPrimeTester.cs @@ -1,7 +1,6 @@ using Open.Numeric.Primes; -using System; -using ILGPU.Runtime; using System.Runtime.CompilerServices; +using ILGPU.Runtime; using PerfectNumbers.Core.Gpu; namespace PerfectNumbers.Core; @@ -41,48 +40,32 @@ 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 ulong[] CombinedDivisorsEnding1TwoAOneB; - private static readonly ulong[] CombinedDivisorsEnding3TwoAOneB; - private static readonly ulong[] CombinedDivisorsEnding7TwoAOneB; - private static readonly ulong[] CombinedDivisorsEnding9TwoAOneB; + private static readonly uint[] CombinedDivisorsEnding1TwoAOneB; + private static readonly uint[] CombinedDivisorsEnding3TwoAOneB; + private static readonly uint[] CombinedDivisorsEnding7TwoAOneB; + private static readonly uint[] CombinedDivisorsEnding9TwoAOneB; - private static readonly ulong[] CombinedDivisorsEnding1OneAOneB; - private static readonly ulong[] CombinedDivisorsEnding3OneAOneB; - private static readonly ulong[] CombinedDivisorsEnding7OneAOneB; - private static readonly ulong[] CombinedDivisorsEnding9OneAOneB; + private static readonly uint[] CombinedDivisorsEnding1OneAOneB; + private static readonly uint[] CombinedDivisorsEnding3OneAOneB; + private static readonly uint[] CombinedDivisorsEnding7OneAOneB; + private static readonly uint[] CombinedDivisorsEnding9OneAOneB; - private static readonly ulong[] CombinedDivisorsEnding1ThreeAOneB; - private static readonly ulong[] CombinedDivisorsEnding3ThreeAOneB; - private static readonly ulong[] CombinedDivisorsEnding7ThreeAOneB; - private static readonly ulong[] CombinedDivisorsEnding9ThreeAOneB; + private static readonly uint[] CombinedDivisorsEnding1ThreeAOneB; + private static readonly uint[] CombinedDivisorsEnding3ThreeAOneB; + private static readonly uint[] CombinedDivisorsEnding7ThreeAOneB; + private static readonly uint[] CombinedDivisorsEnding9ThreeAOneB; - private static readonly ulong[] CombinedDivisorsEnding1ThreeATwoB; - private static readonly ulong[] CombinedDivisorsEnding3ThreeATwoB; - private static readonly ulong[] CombinedDivisorsEnding7ThreeATwoB; - private static readonly ulong[] CombinedDivisorsEnding9ThreeATwoB; + private static readonly uint[] CombinedDivisorsEnding1ThreeATwoB; + private static readonly uint[] CombinedDivisorsEnding3ThreeATwoB; + private static readonly uint[] CombinedDivisorsEnding7ThreeATwoB; + private static readonly uint[] CombinedDivisorsEnding9ThreeATwoB; - private static readonly ulong[] CombinedDivisorsEnding1; - private static readonly ulong[] CombinedDivisorsEnding3; - private static readonly ulong[] CombinedDivisorsEnding7; - private static readonly ulong[] CombinedDivisorsEnding9; + private static readonly uint[] CombinedDivisorsEnding1; + private static readonly uint[] CombinedDivisorsEnding3; + private static readonly uint[] CombinedDivisorsEnding7; + private static readonly uint[] CombinedDivisorsEnding9; private static readonly ulong[] CombinedDivisorsEnding1TwoAOneBSquares; private static readonly ulong[] CombinedDivisorsEnding3TwoAOneBSquares; @@ -190,7 +173,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; @@ -222,40 +205,78 @@ public sealed class HeuristicCombinedPrimeTester _ => 256, }; - - 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; - - 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; - } + private static bool HeuristicTrialDivisionGpuDetectsDivisor(ulong n, ulong maxDivisorSquare, byte nMod10) + { + int batchCapacity = GetBatchSize(n); - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static bool EvaluateWithOpenNumericFallback(ulong n) + 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) { return Prime.Numbers.IsPrime(n); } @@ -264,7 +285,7 @@ public sealed class HeuristicCombinedPrimeTester return n; } - private static ReadOnlySpan GetCombinedDivisors(byte nMod10) + private static ReadOnlySpan GetCombinedDivisors(byte nMod10) { return nMod10 switch { @@ -288,7 +309,7 @@ public sealed class HeuristicCombinedPrimeTester }; } - internal static ReadOnlySpan GetCombinedDivisors(byte nMod10, CombinedDivisorPattern pattern) + internal static ReadOnlySpan GetCombinedDivisors(byte nMod10, CombinedDivisorPattern pattern) { return pattern switch { @@ -368,17 +389,17 @@ public sealed class HeuristicCombinedPrimeTester }; } - private static ulong[] BuildCombinedDivisors(byte nMod10, CombinedDivisorPattern pattern, out ulong[] squares) + private static uint[] 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(groupA[groupAIndex]); + combined.Add((uint)groupA[groupAIndex]); groupAIndex++; } @@ -414,7 +435,7 @@ public sealed class HeuristicCombinedPrimeTester int addedA = 0; while (addedA < groupAInterleaveCount && groupAIndex < groupA.Length) { - combined.Add(groupA[groupAIndex]); + combined.Add((uint)groupA[groupAIndex]); groupAIndex++; addedA++; } @@ -428,7 +449,7 @@ public sealed class HeuristicCombinedPrimeTester } } - ulong[] result = [.. combined]; + uint[] result = [.. combined]; ulong[] resultSquares = new ulong[result.Length]; for (int i = 0; i < result.Length; i++) { @@ -453,12 +474,12 @@ public sealed class HeuristicCombinedPrimeTester return index; } - private static bool IsGroupAPrefixValue(ulong value) => value is 3UL or 7UL or 11UL or 13UL; + private static bool IsGroupAPrefixValue(uint value) => value is 3U or 7U or 11U or 13U; - private static HeuristicDivisorGroup ResolveGroup(ulong value) => value switch + private static HeuristicDivisorGroup ResolveGroup(uint value) => value switch { - 3UL or 7UL or 11UL or 13UL => HeuristicDivisorGroup.GroupAConstant, - _ when value % 10UL == 3UL => HeuristicDivisorGroup.GroupAWheel, + 3U or 7U or 11U or 13U => HeuristicDivisorGroup.GroupAConstant, + _ when value % 10U == 3U => HeuristicDivisorGroup.GroupAWheel, _ => HeuristicDivisorGroup.GroupB, }; @@ -557,7 +578,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; @@ -582,7 +603,7 @@ public sealed class HeuristicCombinedPrimeTester break; } - ulong entry = combinedDivisors[currentIndex]; + uint entry = combinedDivisors[currentIndex]; index = currentIndex + 1; HeuristicDivisorGroup group = ResolveGroup(entry); diff --git a/PerfectNumbers.Core/HeuristicPrimeSieves.cs b/PerfectNumbers.Core/HeuristicPrimeSieves.cs index 3fb19ba..9bc0acc 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 ulong[] GroupADivisorsStorage; + private static readonly int[] GroupADivisorsStorage; private static readonly ulong[] GroupADivisorSquaresStorage; - private static readonly ulong[] GroupBDivisorsEnding1Storage; + private static readonly int[] GroupBDivisorsEnding1Storage; private static readonly ulong[] GroupBDivisorSquaresEnding1Storage; - private static readonly ulong[] GroupBDivisorsEnding3Storage; + private static readonly int[] GroupBDivisorsEnding3Storage; private static readonly ulong[] GroupBDivisorSquaresEnding3Storage; - private static readonly ulong[] GroupBDivisorsEnding7Storage; + private static readonly int[] GroupBDivisorsEnding7Storage; private static readonly ulong[] GroupBDivisorSquaresEnding7Storage; - private static readonly ulong[] GroupBDivisorsEnding9Storage; + private static readonly int[] GroupBDivisorsEnding9Storage; private static readonly ulong[] GroupBDivisorSquaresEnding9Storage; static HeuristicPrimeSieves() @@ -46,46 +46,45 @@ 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 (ulong[] divisors, ulong[] squares) BuildGroupADivisors() + private static (int[] divisors, ulong[] squares) BuildGroupADivisors() { - var divisors = new ulong[SieveLength]; + var divisors = new int[SieveLength]; var squares = new ulong[SieveLength]; int index = 0; for (; index < GroupAConstants.Length && index < divisors.Length; index++) { - ulong value = (ulong)GroupAConstants[index]; + int value = GroupAConstants[index]; divisors[index] = value; - squares[index] = value * value; + squares[index] = (ulong)value * (ulong)value; } int candidate = 23; int incrementIndex = 0; while (index < divisors.Length) { - ulong value = (ulong)candidate; - divisors[index] = value; - squares[index] = value * value; + divisors[index] = candidate; + squares[index] = (ulong)candidate * (ulong)candidate; candidate += GroupAIncrementPattern[incrementIndex]; incrementIndex ^= 1; @@ -95,9 +94,9 @@ public static class HeuristicPrimeSieves return (divisors, squares); } - private static (ulong[] divisors, ulong[] squares) BuildGroupBResidueSieve(ReadOnlySpan residues) + private static (int[] divisors, ulong[] squares) BuildGroupBResidueSieve(ReadOnlySpan residues) { - var divisors = new ulong[SieveLength]; + var divisors = new int[SieveLength]; var squares = new ulong[SieveLength]; int index = 0; @@ -113,7 +112,7 @@ public static class HeuristicPrimeSieves continue; } - divisors[index] = candidate; + divisors[index] = (int)candidate; squares[index] = candidate * candidate; index++; } diff --git a/PerfectNumbers.Core/HeuristicPrimeTester.cs b/PerfectNumbers.Core/HeuristicPrimeTester.cs index a60227b..930de3d 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,78 +237,149 @@ public sealed class HeuristicPrimeTester return true; } - - 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; - - 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; - - 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; - } + private bool HeuristicTrialDivisionGpuDetectsDivisor(ulong n, ulong maxDivisorSquare, byte nMod10) + { + int batchCapacity = HeuristicCombinedPrimeTester.GetBatchSize(n); + int interleaveBatchSize = batchCapacity; + var limiter = GpuPrimeWorkLimiter.Acquire(); + var gpu = PrimeTester.PrimeTesterGpuContextPool.Rent(batchCapacity); + var accelerator = gpu.Accelerator; + var kernel = gpu.HeuristicTrialDivisionKernel; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static bool EvaluateWithOpenNumericFallback(ulong n) + 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) { return Prime.Numbers.IsPrime(n); } @@ -327,7 +398,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; @@ -510,7 +581,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; @@ -538,7 +609,7 @@ public sealed class HeuristicPrimeTester } int currentIndex = groupAIndex; - ulong divisor = groupADivisors[currentIndex]; + ulong divisor = (ulong)groupADivisors[currentIndex]; groupAIndex++; HeuristicDivisorGroup group = currentIndex < GroupAConstantCount diff --git a/PerfectNumbers.Core/PrimeOrderCalculator.Cpu.cs b/PerfectNumbers.Core/PrimeOrderCalculator.Cpu.cs index 702039e..3f5773e 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,15 +1083,41 @@ internal static partial class PrimeOrderCalculator if (!gpuFactored) { - PopulateSmallPrimeFactorsCpu( - value, - limit, - ref primeSlots, - ref exponentSlots, - ref primeSlotArray, - ref exponentSlotArray, - out factorCount, - out remaining); + // 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; + } } @@ -1131,7 +1157,7 @@ internal static partial class PrimeOrderCalculator if (isPrime) { - AddFactorToCollector(ref primeSlots, ref exponentSlots, ref primeSlotArray, ref exponentSlotArray, ref factorCount, composite, 1); + AddFactorToCollector(ref useDictionary, ref counts, primeSlots, exponentSlots, ref factorCount, composite, 1); continue; } @@ -1203,7 +1229,7 @@ internal static partial class PrimeOrderCalculator if (entry.IsPrime) { - AddFactorToCollector(ref primeSlots, ref exponentSlots, ref primeSlotArray, ref exponentSlotArray, ref factorCount, composite, 1); + AddFactorToCollector(ref useDictionary, ref counts, primeSlots, exponentSlots, ref factorCount, composite, 1); } // composite is never smaller on the execution path // else if (composite > 1UL) @@ -1238,15 +1264,33 @@ internal static partial class PrimeOrderCalculator ArrayPool pool = ThreadStaticPools.FactorEntryPool; bool temp; - if (factorCount == 0) + if (useDictionary) { - if (cofactor == value) + temp = counts is null || counts.Count == 0; + if (temp && cofactor == value) { result = PartialFactorResult.Rent(null, cofactor, false, 0, cofactorIsPrime); goto ReturnResult; } - result = PartialFactorResult.Rent(null, cofactor, cofactor == 1UL, 0, cofactorIsPrime); + 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); goto ReturnResult; } @@ -1286,10 +1330,14 @@ internal static partial class PrimeOrderCalculator result = PartialFactorResult.Rent(array, cofactor, temp, arrayIndex, cofactorIsPrime); ReturnResult: - if (primeSlotArray is not null) + // ThreadStaticPools.UlongPool.Return(primeSlotsArray); + // ThreadStaticPools.IntPool.Return(exponentSlotsArray); + + + if (counts is not null) { - ThreadStaticPools.UlongPool.Return(primeSlotArray, clearArray: false); - ThreadStaticPools.IntPool.Return(exponentSlotArray!, clearArray: false); + counts.Clear(); + ThreadStaticPools.ReturnUlongIntDictionary(counts); } if (compositeStack is not null) @@ -1303,28 +1351,35 @@ internal static partial class PrimeOrderCalculator return result; } - 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) + + private static bool TryPopulateSmallPrimeFactorsCpu( + ulong value, + uint limit, + Span primeTargets, + Span exponentTargets, + out int factorCount, + out ulong remaining) { factorCount = 0; - ulong remainingLocal = value; + remaining = value; - if (value <= 1UL) - { - remaining = remainingLocal; - return; - } + // 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; + // } 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++) @@ -1343,13 +1398,13 @@ internal static partial class PrimeOrderCalculator break; } - ulong primeValue = primeCandidate; // primeCandidate will never equal 0 in production code // if (primeCandidate == 0UL) // { // continue; // } + ulong primeValue = primeCandidate; if ((remainingLocal % primeValue) != 0UL) { continue; @@ -1361,13 +1416,10 @@ internal static partial class PrimeOrderCalculator continue; } - EnsureCollectorCapacity( - ref primeTargets, - ref exponentTargets, - ref primeArray, - ref exponentArray, - factorCount, - factorCount + 1); + if (factorCount >= capacity) + { + throw new InvalidOperationException($"Capacity is smaller than factor count"); + } primeTargets[factorCount] = primeValue; exponentTargets[factorCount] = exponent; @@ -1375,6 +1427,7 @@ internal static partial class PrimeOrderCalculator } remaining = remainingLocal; + return true; } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -1442,6 +1495,46 @@ 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) @@ -1564,18 +1657,11 @@ internal static partial class PrimeOrderCalculator } } - private static void AddFactorToCollector( - ref Span primes, - ref Span exponents, - ref ulong[]? primeArray, - ref int[]? exponentArray, - ref int count, - ulong prime, - int exponent) + private static bool TryAppendFactor(Span primes, Span exponents, ref int count, ulong prime, int exponent) { if (exponent <= 0) { - return; + return true; } for (int i = 0; i < count; i++) @@ -1583,60 +1669,70 @@ internal static partial class PrimeOrderCalculator if (primes[i] == prime) { exponents[i] += exponent; - return; + return true; } } - EnsureCollectorCapacity( - ref primes, - ref exponents, - ref primeArray, - ref exponentArray, - count, - count + 1); + if (count >= primes.Length) + { + return false; + } primes[count] = prime; exponents[count] = exponent; count++; + return true; } - private static void EnsureCollectorCapacity( - ref Span primes, - ref Span exponents, - ref ulong[]? primeArray, - ref int[]? exponentArray, - int count, - int requiredCapacity) + private static void AddFactorToCollector( + ref bool useDictionary, + ref Dictionary? counts, + Span primes, + Span exponents, + ref int count, + ulong prime, + int exponent) { - if (requiredCapacity <= primes.Length) + if (exponent <= 0) { return; } - int newCapacity = primes.Length << 1; - if (newCapacity < requiredCapacity) + if (useDictionary) { - newCapacity = requiredCapacity; + AddFactor(counts!, prime, exponent); + return; } - 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) + if (TryAppendFactor(primes, exponents, ref count, prime, exponent)) { - ThreadStaticPools.UlongPool.Return(primeArray, clearArray: false); - ThreadStaticPools.IntPool.Return(exponentArray!, clearArray: false); + return; } - primeArray = newPrimeArray; - exponentArray = newExponentArray; - primes = newPrimeSpan; - exponents = newExponentSpan; + 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++) + { + ulong prime = primes[i]; + int exponent = exponents[i]; + if (prime == 0UL || exponent == 0) + { + continue; + } + + target[prime] = exponent; + } } private static ulong CalculateByFactorizationCpu(ulong prime, in MontgomeryDivisorData divisorData) diff --git a/PerfectNumbers.Core/PrimeTester.cs b/PerfectNumbers.Core/PrimeTester.cs index 9a8fc90..0b46256 100644 --- a/PerfectNumbers.Core/PrimeTester.cs +++ b/PerfectNumbers.Core/PrimeTester.cs @@ -1,7 +1,5 @@ -using System; using System.Buffers; using System.Collections.Concurrent; -using System.Collections.Generic; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -95,86 +93,53 @@ public sealed class PrimeTester [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool IsPrimeGpu(ulong n) { - // bool belowGpuRange = n < 31UL; + bool forceCpu = GpuContextPool.ForceCpu; + bool belowGpuRange = n < 31UL; - // if (belowGpuRange) - // { + if (forceCpu || 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; - private static readonly object GpuWarmUpLock = new(); - private static int WarmedGpuLeaseCount; - - public static void WarmUpGpuKernels(int threadCount) - { - int target = threadCount >> 2; - if (target == 0) - { - target = threadCount; - } - - 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) { @@ -229,43 +194,6 @@ 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) - { - if (tables is null) - { - return; - } - - lock (SharedTablesLock) - { - tables.Release(); - } - } internal static PrimeTesterGpuContextLease Rent(int minBufferCapacity = 1) { @@ -294,16 +222,6 @@ public sealed class PrimeTester { lease.DisposeResources(); } - - lock (SharedTablesLock) - { - foreach (var entry in SharedTables.Values) - { - entry.Dispose(); - } - - SharedTables.Clear(); - } } internal static void Clear(Accelerator accelerator) @@ -335,95 +253,101 @@ 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, ulong, HeuristicGpuDivisorTableKind, HeuristicGpuDivisorTables> HeuristicTrialDivisionKernel; - public MemoryBuffer1D Input = null!; - public MemoryBuffer1D Output = null!; - public MemoryBuffer1D HeuristicFlag = null!; + 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 int BufferCapacity; - - 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; + public ulong[] DivisorArray; 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, ulong, HeuristicGpuDivisorTableKind, HeuristicGpuDivisorTables>(PrimeTesterKernels.HeuristicTrialDivisionKernel); + HeuristicTrialDivisionKernel = Accelerator.LoadAutoGroupedStreamKernel, ulong, ArrayView>(PrimeTesterKernels.HeuristicTrialDivisionKernel); - _sharedTables = RentSharedTables(Accelerator); + var primesDefault = DivisorGenerator.SmallPrimes; + DevicePrimesDefault = Accelerator.Allocate1D(primesDefault.Length); + DevicePrimesDefault.View.CopyFromCPU(primesDefault); - _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); + 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); - EnsureCapacity(minBufferCapacity); _returned = false; } public void EnsureCapacity(int minCapacity) { - if (minCapacity <= BufferCapacity) + int required = minCapacity; + if (required <= BufferCapacity) { return; } - Input?.Dispose(); - Output?.Dispose(); - HeuristicFlag?.Dispose(); - Input = Accelerator.Allocate1D(minCapacity); - Output = Accelerator.Allocate1D(minCapacity); - HeuristicFlag = Accelerator.Allocate1D(minCapacity); - BufferCapacity = minCapacity; + 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; } internal void ResetReturnFlag() @@ -444,174 +368,9 @@ public sealed class PrimeTester internal void DisposeResources() { - 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 void Release() - { - if (_referenceCount > 0) - { - _referenceCount--; - } - } - - 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; - } - - internal void Dispose() - { + Input.Dispose(); + Output.Dispose(); + HeuristicFlag.Dispose(); DevicePrimesDefault.Dispose(); DevicePrimesLastOne.Dispose(); DevicePrimesLastSeven.Dispose(); @@ -622,26 +381,11 @@ public sealed class PrimeTester DevicePrimesPow2LastSeven.Dispose(); DevicePrimesPow2LastThree.Dispose(); DevicePrimesPow2LastNine.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(); + Accelerator.Dispose(); + AcceleratorContext.Dispose(); } } } - // Expose cache clearing for accelerator disposal coordination public static void ClearGpuCaches(Accelerator accelerator) { @@ -651,10 +395,6 @@ public sealed class PrimeTester internal static void DisposeGpuContexts() { PrimeTesterGpuContextPool.DisposeAll(); - lock (GpuWarmUpLock) - { - WarmedGpuLeaseCount = 0; - } } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/PerfectNumbers.Core/ThreadStaticPools.cs b/PerfectNumbers.Core/ThreadStaticPools.cs index 3d5ca9b..b019cf8 100644 --- a/PerfectNumbers.Core/ThreadStaticPools.cs +++ b/PerfectNumbers.Core/ThreadStaticPools.cs @@ -260,6 +260,43 @@ 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;