From 85c6eaece7615dd85aafedc52cfb333d537b8ef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Lema=C5=84czyk?= Date: Sat, 4 Oct 2025 22:40:02 +0200 Subject: [PATCH] Persist GPU capability snapshot without volatile --- .../Gpu/PrimeOrderGpuHeuristics.cs | 16 +++++----------- todo.txt | 2 ++ 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/PerfectNumbers.Core/Gpu/PrimeOrderGpuHeuristics.cs b/PerfectNumbers.Core/Gpu/PrimeOrderGpuHeuristics.cs index 21583db..9138697 100644 --- a/PerfectNumbers.Core/Gpu/PrimeOrderGpuHeuristics.cs +++ b/PerfectNumbers.Core/Gpu/PrimeOrderGpuHeuristics.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Concurrent; using System.Numerics; -using System.Threading; namespace PerfectNumbers.Core.Gpu; @@ -15,23 +14,18 @@ internal enum GpuPow2ModStatus internal static class PrimeOrderGpuHeuristics { private static readonly ConcurrentDictionary OverflowedPrimes = new(); - private static int s_modulusBitLimit = PrimeOrderGpuCapability.Default.ModulusBits; - private static int s_exponentBitLimit = PrimeOrderGpuCapability.Default.ExponentBits; + private static PrimeOrderGpuCapability s_capability = PrimeOrderGpuCapability.Default; internal static void OverrideCapabilitiesForTesting(PrimeOrderGpuCapability capability) { - Volatile.Write(ref s_modulusBitLimit, capability.ModulusBits); - Volatile.Write(ref s_exponentBitLimit, capability.ExponentBits); + s_capability = capability; } internal static void ResetCapabilitiesForTesting() { - OverrideCapabilitiesForTesting(PrimeOrderGpuCapability.Default); + s_capability = PrimeOrderGpuCapability.Default; } - private static PrimeOrderGpuCapability Capabilities - => new(Volatile.Read(ref s_modulusBitLimit), Volatile.Read(ref s_exponentBitLimit)); - internal static void MarkOverflow(ulong prime) => OverflowedPrimes[prime] = 0; internal static void ClearOverflow(ulong prime) => OverflowedPrimes.TryRemove(prime, out _); @@ -51,7 +45,7 @@ internal static class PrimeOrderGpuHeuristics return GpuPow2ModStatus.Overflow; } - PrimeOrderGpuCapability capability = Capabilities; + PrimeOrderGpuCapability capability = s_capability; if (!SupportsPrime(prime, capability)) { @@ -92,7 +86,7 @@ internal static class PrimeOrderGpuHeuristics return GpuPow2ModStatus.Overflow; } - PrimeOrderGpuCapability capability = Capabilities; + PrimeOrderGpuCapability capability = s_capability; if (!SupportsPrime(prime, capability)) { diff --git a/todo.txt b/todo.txt index 5f09bfc..c898b2f 100644 --- a/todo.txt +++ b/todo.txt @@ -1,2 +1,4 @@ +- Inline the overflow tracking helpers in PrimeOrderGpuHeuristics so call sites can cache the dictionary locally and reduce delegate indirection. +- Move the ulong bit-length helper into ULongExtensions and update PrimeOrderGpuHeuristics (and any other callers) to reuse it. - Implement the GPU heuristics for PrimeOrderCalculator using ILGPU. Reuse the numeric type compatibility probe results to select between EInteger, ERational, GpuUInt128, or any new GPU-specific big-integer representations, guarantee parity with the CPU heuristic algorithm (including 128-bit primes), prefer the shift-first multiply/shift variant to stay within device ranges, and wire the calculator through the accelerator path. - Extend the GPU numeric type probe to cover proposed custom types such as GpuUInt256 and GpuUInt512 once their definitions are available so we can plan the wide-order heuristics on the device.