Inline legacy GPU sieve and update heuristic fallbacks

This commit is contained in:
Marek Lemańczyk committed 2025-10-26 17:06:13 +01:00
1 parent a7fc34e37a
commit 55554d9565
5 files changed
+10 -14

No files matched your search

@@ -86,7 +86,7 @@ public class PrimeTesterBenchmarks
for (int i = 0; i < values.Length; i++) for (int i = 0; i < values.Length; i++)
{ {
if (PrimeTester.IsPrimeGpuFallback(values[i], cancellationToken)) if (PrimeTester.Exclusive.IsPrimeGpu(values[i], cancellationToken))
{ {
primeCount++; primeCount++;
} }
+2 -2
View File
@@ -102,7 +102,7 @@ public sealed class HeuristicPrimeTester
{ {
if (!EnableHeuristicPrimeTesting) if (!EnableHeuristicPrimeTesting)
{ {
return PrimeTester.IsPrimeGpuFallback(n, ct); return PrimeTester.Exclusive.IsPrimeGpu(n, ct);
} }
return IsPrimeGpu(n); return IsPrimeGpu(n);
@@ -119,7 +119,7 @@ public sealed class HeuristicPrimeTester
{ {
if (!EnableHeuristicPrimeTesting) if (!EnableHeuristicPrimeTesting)
{ {
return PrimeTester.IsPrimeGpuFallback(n, CancellationToken.None); return PrimeTester.Exclusive.IsPrimeGpu(n, CancellationToken.None);
} }
return HeuristicIsPrimeGpuCore(n, sqrtLimit, nMod10); return HeuristicIsPrimeGpuCore(n, sqrtLimit, nMod10);
+5 -9
View File
@@ -72,18 +72,14 @@ public sealed class PrimeTester
return Exclusive.IsPrimeGpu(n, CancellationToken.None); return Exclusive.IsPrimeGpu(n, CancellationToken.None);
} }
public static bool IsPrimeGpu(ulong n, ulong limit, byte nMod10) // Legacy overload retained for heuristic toggles; tests and benchmarks now call the thread-local instance directly.
{ // public static bool IsPrimeGpu(ulong n, ulong limit, byte nMod10)
return Exclusive.IsPrimeGpu(n, CancellationToken.None); // {
} // return Exclusive.IsPrimeGpu(n, CancellationToken.None);
// }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsPrimeGpu(ulong n, CancellationToken ct) public bool IsPrimeGpu(ulong n, CancellationToken ct)
{
return IsPrimeGpuFallback(n, ct);
}
public static bool IsPrimeGpuFallback(ulong n, CancellationToken ct)
{ {
bool forceCpu = GpuContextPool.ForceCpu; bool forceCpu = GpuContextPool.ForceCpu;
Span<ulong> one = stackalloc ulong[1]; Span<ulong> one = stackalloc ulong[1];
+1 -1
View File
@@ -140,7 +140,7 @@ The plan incorporates the updated divisor-class heuristics (Groups A/B, wheels,
* [done] Streamed GPU batches from the same enumerator and Montgomery preparation data, removing the filtered-divisor scratch arrays while continuing to feed the existing kernel and hit bookkeeping. * [done] Streamed GPU batches from the same enumerator and Montgomery preparation data, removing the filtered-divisor scratch arrays while continuing to feed the existing kernel and hit bookkeeping.
9. [done] **Route legacy prime checks through heuristics** 9. [done] **Route legacy prime checks through heuristics**
* [done] Introduce `HeuristicPrimeTester` so heuristic CPU/GPU checks live alongside the legacy `PrimeTester` implementations while callers opt in explicitly. * [done] Introduce `HeuristicPrimeTester` so heuristic CPU/GPU checks live alongside the legacy `PrimeTester` implementations while callers opt in explicitly.
* [done] Maintain legacy paths for benchmarking via `PrimeTester.IsPrimeInternal`, `PrimeTester.IsPrimeGpuFallback`, and the global `HeuristicPrimeTester.EnableHeuristicPrimeTesting` switch so benchmark harnesses can compare behaviours. * [done] Maintain legacy paths for benchmarking via `PrimeTester.IsPrimeInternal`, `PrimeTester.Exclusive.IsPrimeGpu`, and the global `HeuristicPrimeTester.EnableHeuristicPrimeTesting` switch so benchmark harnesses can compare behaviours.
10. **Validation and benchmarking** 10. **Validation and benchmarking**
* Create targeted unit tests covering boundary cases for Group A/B transitions, wheel enumerations, residue deltas, and the stop-on-hit behaviour. * Create targeted unit tests covering boundary cases for Group A/B transitions, wheel enumerations, residue deltas, and the stop-on-hit behaviour.
* Expand GPU test harnesses to assert equivalence between CPU/GPU heuristics on sampled composite and prime inputs and to verify the residue-stream contract with by-divisor sessions. * Expand GPU test harnesses to assert equivalence between CPU/GPU heuristics on sampled composite and prime inputs and to verify the residue-stream contract with by-divisor sessions.
+1 -1
View File
@@ -62,7 +62,7 @@ No other endings are allowed or reordered.
## Default Prime Tester Entry Points ## Default Prime Tester Entry Points
* `PrimeTester.IsPrime` and `PrimeTester.IsPrimeGpu` continue to run the legacy trial-division and GPU sieve paths. Use `HeuristicPrimeTester` when the heuristic CPU/GPU flow is required. * `PrimeTester.IsPrime` and `PrimeTester.IsPrimeGpu` continue to run the legacy trial-division and GPU sieve paths. Use `HeuristicPrimeTester` when the heuristic CPU/GPU flow is required.
* Toggle `HeuristicPrimeTester.EnableHeuristicPrimeTesting` or fall back to `PrimeTester.IsPrimeInternal` / `PrimeTester.IsPrimeGpuFallback` to control heuristic rollouts in test harnesses. * Toggle `HeuristicPrimeTester.EnableHeuristicPrimeTesting` or fall back to `PrimeTester.IsPrimeInternal` / `PrimeTester.Exclusive.IsPrimeGpu` to control heuristic rollouts in test harnesses.
* Benchmark harnesses can compare behaviours by calling the legacy helpers on `PrimeTester` alongside the heuristic equivalents on `HeuristicPrimeTester`. * Benchmark harnesses can compare behaviours by calling the legacy helpers on `PrimeTester` alongside the heuristic equivalents on `HeuristicPrimeTester`.
## Integration with the By-Divisor Pipeline ## Integration with the By-Divisor Pipeline