IsPrime with cancellation token removed.
This commit is contained in:
1 parent
f9504a6a19
commit
fc5700f82c
11 files changed
+39
-66
No files matched your search
@@ -29,8 +29,7 @@ public class PrimeTesterBenchmarks
|
||||
return;
|
||||
}
|
||||
|
||||
CancellationToken cancellationToken = CancellationToken.None;
|
||||
_ = _heuristicTester.IsPrimeCpu(_candidates[0], cancellationToken);
|
||||
_ = _heuristicTester.IsPrimeCpu(_candidates[0]);
|
||||
_ = _heuristicTester.IsPrimeGpu(_candidates[0]);
|
||||
_ = Prime.Numbers.IsPrime(_candidates[0]);
|
||||
}
|
||||
@@ -40,12 +39,11 @@ public class PrimeTesterBenchmarks
|
||||
{
|
||||
HeuristicPrimeTester tester = _heuristicTester;
|
||||
ulong[] values = _candidates;
|
||||
CancellationToken cancellationToken = CancellationToken.None;
|
||||
int primeCount = 0;
|
||||
|
||||
for (int i = 0; i < values.Length; i++)
|
||||
{
|
||||
if (tester.IsPrimeCpu(values[i], cancellationToken))
|
||||
if (tester.IsPrimeCpu(values[i]))
|
||||
{
|
||||
primeCount++;
|
||||
}
|
||||
@@ -75,7 +73,6 @@ public class PrimeTesterBenchmarks
|
||||
[Benchmark]
|
||||
public int HeuristicCombinedGpu()
|
||||
{
|
||||
HeuristicCombinedPrimeTester tester = _heuristicCombinedTester;
|
||||
ulong[] values = _candidates;
|
||||
int primeCount = 0;
|
||||
|
||||
@@ -93,7 +90,6 @@ public class PrimeTesterBenchmarks
|
||||
[Benchmark]
|
||||
public int HeuristicCombinedCpu()
|
||||
{
|
||||
HeuristicCombinedPrimeTester tester = _heuristicCombinedTester;
|
||||
ulong[] values = _candidates;
|
||||
int primeCount = 0;
|
||||
|
||||
@@ -115,12 +111,11 @@ public class PrimeTesterBenchmarks
|
||||
{
|
||||
PrimeTester tester = _nonHeuristicTester;
|
||||
ulong[] values = _candidates;
|
||||
CancellationToken cancellationToken = CancellationToken.None;
|
||||
int primeCount = 0;
|
||||
|
||||
for (int i = 0; i < values.Length; i++)
|
||||
{
|
||||
if (tester.IsPrimeGpu(values[i], cancellationToken))
|
||||
if (tester.IsPrimeGpu(values[i]))
|
||||
{
|
||||
primeCount++;
|
||||
}
|
||||
@@ -133,12 +128,11 @@ public class PrimeTesterBenchmarks
|
||||
public int NonHeuristicCpu()
|
||||
{
|
||||
ulong[] values = _candidates;
|
||||
CancellationToken cancellationToken = CancellationToken.None;
|
||||
int primeCount = 0;
|
||||
|
||||
for (int i = 0; i < values.Length; i++)
|
||||
{
|
||||
if (PrimeTester.IsPrimeCpu(values[i], cancellationToken))
|
||||
if (PrimeTester.IsPrime(values[i]))
|
||||
{
|
||||
primeCount++;
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ public class ProgramTests
|
||||
[InlineData(81UL, false)]
|
||||
public void IsPrime_identifies_primes_correctly(ulong n, bool expected)
|
||||
{
|
||||
PrimeTester.IsPrime(n, CancellationToken.None).Should().Be(expected);
|
||||
PrimeTester.IsPrime(n).Should().Be(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
@@ -622,8 +622,8 @@ internal static class Program
|
||||
if (!_cliArguments.UseByDivisor)
|
||||
{
|
||||
if (!(GpuContextPool.ForceCpu
|
||||
? PrimeTester.IsPrime(p, CancellationToken.None)
|
||||
: PrimeTesters.Value!.IsPrimeGpu(p, CancellationToken.None)))
|
||||
? PrimeTester.IsPrime(p)
|
||||
: PrimeTesters.Value!.IsPrimeGpu(p)))
|
||||
{
|
||||
_lastCompositeP = true;
|
||||
return false;
|
||||
|
||||
@@ -17,7 +17,7 @@ public class PrimeTesterGpuTests
|
||||
ulong[] primes = [31UL, 61UL, 89UL, 107UL, 127UL, 521UL];
|
||||
foreach (ulong prime in primes)
|
||||
{
|
||||
tester.IsPrimeGpu(prime, CancellationToken.None).Should().BeTrue();
|
||||
tester.IsPrimeGpu(prime).Should().BeTrue();
|
||||
}
|
||||
}
|
||||
finally
|
||||
@@ -37,7 +37,7 @@ public class PrimeTesterGpuTests
|
||||
ulong[] composites = [33UL, 39UL, 51UL, 77UL, 91UL];
|
||||
foreach (ulong composite in composites)
|
||||
{
|
||||
tester.IsPrimeGpu(composite, CancellationToken.None).Should().BeFalse();
|
||||
tester.IsPrimeGpu(composite).Should().BeFalse();
|
||||
}
|
||||
}
|
||||
finally
|
||||
|
||||
@@ -26,7 +26,7 @@ public class PrimeTesterIsPrimeTests
|
||||
{
|
||||
var tester = new PrimeTester();
|
||||
|
||||
PrimeTester.IsPrime(n, CancellationToken.None).Should().Be(expected);
|
||||
PrimeTester.IsPrime(n).Should().Be(expected);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
@@ -40,7 +40,7 @@ public class PrimeTesterIsPrimeTests
|
||||
GpuContextPool.ForceCpu = false;
|
||||
var tester = new PrimeTester();
|
||||
|
||||
tester.IsPrimeGpu(n, CancellationToken.None).Should().Be(expected);
|
||||
tester.IsPrimeGpu(n).Should().Be(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -50,9 +50,9 @@ public class PrimeTesterIsPrimeTests
|
||||
var tester = new PrimeTester();
|
||||
GpuContextPool.ForceCpu = true;
|
||||
|
||||
tester.IsPrimeGpu(137UL, CancellationToken.None).Should().BeTrue();
|
||||
tester.IsPrimeGpu(137UL).Should().BeTrue();
|
||||
|
||||
tester.IsPrimeGpu(341UL, CancellationToken.None).Should().BeFalse();
|
||||
tester.IsPrimeGpu(341UL).Should().BeFalse();
|
||||
|
||||
GpuContextPool.ForceCpu = false;
|
||||
}
|
||||
@@ -71,7 +71,7 @@ public class PrimeTesterIsPrimeTests
|
||||
var testers = new ThreadLocal<PrimeTester>(() => new PrimeTester(), trackAllValues: true);
|
||||
Parallel.For(0, primes.Length, i =>
|
||||
{
|
||||
results[i] = testers.Value!.IsPrimeGpu(primes[i], CancellationToken.None);
|
||||
results[i] = testers.Value!.IsPrimeGpu(primes[i]);
|
||||
});
|
||||
|
||||
results.Should().AllBeEquivalentTo(true);
|
||||
@@ -80,7 +80,7 @@ public class PrimeTesterIsPrimeTests
|
||||
{
|
||||
foreach (ulong prime in primes)
|
||||
{
|
||||
testers.Value!.IsPrimeGpu(prime, CancellationToken.None).Should().BeTrue();
|
||||
testers.Value!.IsPrimeGpu(prime).Should().BeTrue();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -77,10 +77,8 @@ public sealed class HeuristicPrimeTester
|
||||
|
||||
public static HeuristicPrimeTester Exclusive => _tester ??= new();
|
||||
|
||||
public bool IsPrimeCpu(ulong n, CancellationToken ct)
|
||||
public bool IsPrimeCpu(ulong n)
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
|
||||
byte nMod10 = (byte)n.Mod10();
|
||||
ulong maxDivisorSquare = ComputeHeuristicDivisorSquareLimit(n);
|
||||
|
||||
@@ -93,11 +91,6 @@ public sealed class HeuristicPrimeTester
|
||||
return HeuristicTrialDivisionCpu(n, maxDivisorSquare, nMod10, includeGroupB);
|
||||
}
|
||||
|
||||
public bool IsPrimeCpu(ulong n)
|
||||
{
|
||||
return IsPrimeCpu(n, CancellationToken.None);
|
||||
}
|
||||
|
||||
public bool IsPrimeGpu(ulong n)
|
||||
{
|
||||
byte nMod10 = (byte)n.Mod10();
|
||||
|
||||
@@ -411,8 +411,8 @@ public class MersenneDivisorCycles
|
||||
// Atomic.Add(ref _tryFactorIntoCountsInternalHits, 1UL);
|
||||
// Console.WriteLine($"MersenneDivisorCycles.TryFactorIntoCountsInternal hits {Volatile.Read(ref _tryFactorIntoCountsInternalHits)}");
|
||||
|
||||
HeuristicPrimeTester primeTester = _primeTester ??= new();
|
||||
if (primeTester.IsPrimeGpu(remaining))
|
||||
// HeuristicPrimeTester primeTester = _primeTester ??= new();
|
||||
if (PrimeTester.IsPrime(remaining))
|
||||
// if (PrimeTester.IsPrimeCpu(remaining, CancellationToken.None))
|
||||
{
|
||||
AddFactor(counts, remaining);
|
||||
@@ -917,8 +917,8 @@ public class MersenneDivisorCycles
|
||||
return CalculateCycleLengthFallback(divisor);
|
||||
}
|
||||
|
||||
[ThreadStatic]
|
||||
private static HeuristicPrimeTester? _primeTester;
|
||||
// [ThreadStatic]
|
||||
// private static HeuristicPrimeTester? _primeTester;
|
||||
|
||||
internal static bool TryCalculateCycleLengthHeuristic(ulong divisor, in MontgomeryDivisorData divisorData, out ulong cycleLength, bool skipPrimeOrderHeuristic = false)
|
||||
{
|
||||
@@ -940,10 +940,10 @@ public class MersenneDivisorCycles
|
||||
// Console.WriteLine($"MersenneDivisorCycles.CalculateCycleLengthFallback hits {Volatile.Read(ref _tryCalculateCycleLengthHeuristicHits)}");
|
||||
// }
|
||||
|
||||
HeuristicPrimeTester primeTester = _primeTester ??= new();
|
||||
// HeuristicPrimeTester primeTester = _primeTester ??= new();
|
||||
// if (!skipPrimeOrderHeuristic && HeuristicPrimeTester.Exclusive.IsPrimeCpu(divisor, CancellationToken.None))
|
||||
if (!skipPrimeOrderHeuristic && primeTester.IsPrimeGpu(divisor))
|
||||
// if (!skipPrimeOrderHeuristic && PrimeTester.IsPrimeCpu(divisor, CancellationToken.None))
|
||||
// if (!skipPrimeOrderHeuristic && primeTester.IsPrimeGpu(divisor))
|
||||
if (!skipPrimeOrderHeuristic && PrimeTester.IsPrime(divisor))
|
||||
{
|
||||
ulong computedOrder = PrimeOrderCalculator.Calculate(
|
||||
divisor,
|
||||
|
||||
@@ -34,7 +34,7 @@ public static class MersennePrimeFactorTester
|
||||
// Atomic.Add(ref _isPrimeFactorHits, 1UL);
|
||||
// Console.WriteLine($"MersennePrimeFactorTester.IsPrimeFactor hits {Volatile.Read(ref _isPrimeFactorHits)}");
|
||||
|
||||
if (!PrimeTester.IsPrimeCpu(q, ct))
|
||||
if (!PrimeTester.IsPrime(q))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -247,7 +247,7 @@ public static class MersennePrimeFactorTester
|
||||
// Console.WriteLine($"MersennePrimeFactorTester.Factor64 hits {Volatile.Read(ref _factor64Hits)}");
|
||||
|
||||
// if (HeuristicPrimeTester.Exclusive.IsPrime(m, ct))
|
||||
if (PrimeTester.IsPrimeCpu(m, ct))
|
||||
if (PrimeTester.IsPrime(m))
|
||||
{
|
||||
dict[m] = 1;
|
||||
continue;
|
||||
|
||||
@@ -1079,7 +1079,7 @@ internal static partial class PrimeOrderCalculator
|
||||
PartialFactorResult result;
|
||||
PrimeTester primeTester = PrimeTester.Exclusive;
|
||||
CancellationToken ct = CancellationToken.None;
|
||||
HeuristicPrimeTester? tester = _tester ??= new();
|
||||
// HeuristicPrimeTester? tester = _tester ??= new();
|
||||
|
||||
if (!gpuFactored)
|
||||
{
|
||||
@@ -1152,7 +1152,7 @@ internal static partial class PrimeOrderCalculator
|
||||
// Console.WriteLine($"Partial factor hits {Volatile.Read(ref _partialFactorHits)}");
|
||||
|
||||
// bool isPrime = PrimeTester.IsPrimeCpu(composite, CancellationToken.None);
|
||||
bool isPrime = tester.IsPrimeGpu(composite);
|
||||
bool isPrime = PrimeTester.IsPrime(composite);
|
||||
// bool isPrime = Open.Numeric.Primes.Prime.Numbers.IsPrime(composite);
|
||||
|
||||
if (isPrime)
|
||||
@@ -1220,7 +1220,7 @@ internal static partial class PrimeOrderCalculator
|
||||
// Console.WriteLine($"Partial factor pending hits {Volatile.Read(ref _partialFactorPendingHits)}");
|
||||
|
||||
// bool isPrime = PrimeTester.IsPrimeCpu(composite, CancellationToken.None);
|
||||
bool isPrime = tester.IsPrimeGpu(composite);
|
||||
bool isPrime = PrimeTester.IsPrime(composite);
|
||||
// bool isPrime = Open.Numeric.Primes.Prime.Numbers.IsPrime(composite);
|
||||
|
||||
entry = entry.WithPrimality(isPrime);
|
||||
@@ -1257,7 +1257,7 @@ internal static partial class PrimeOrderCalculator
|
||||
// Atomic.Add(ref _partialFactorCofactorHits, 1UL);
|
||||
// Console.WriteLine($"Partial factor cofactor hits {Volatile.Read(ref _partialFactorCofactorHits)}");
|
||||
|
||||
cofactorIsPrime = tester.IsPrimeGpu(cofactor);
|
||||
cofactorIsPrime = PrimeTester.IsPrime(cofactor);
|
||||
// cofactorIsPrime = PrimeTester.IsPrimeGpu(cofactor);
|
||||
// cofactorIsPrime = Open.Numeric.Primes.Prime.Numbers.IsPrime(cofactor);
|
||||
}
|
||||
@@ -1782,8 +1782,8 @@ internal static partial class PrimeOrderCalculator
|
||||
|
||||
// private static ulong _factorCompletelyHits;
|
||||
// private static ulong _factorCompletelyPollardRhoHits;
|
||||
[ThreadStatic]
|
||||
private static HeuristicPrimeTester? _tester;
|
||||
// [ThreadStatic]
|
||||
// private static HeuristicPrimeTester? _tester;
|
||||
|
||||
private static void FactorCompletely(ulong value, Dictionary<ulong, int> counts, bool knownComposite)
|
||||
{
|
||||
@@ -1794,10 +1794,10 @@ internal static partial class PrimeOrderCalculator
|
||||
|
||||
// Atomic.Add(ref _factorCompletelyHits, 1UL);
|
||||
// Console.WriteLine($"Factor completely hits {Volatile.Read(ref _factorCompletelyHits)}");
|
||||
HeuristicPrimeTester tester = _tester ??= new();
|
||||
// HeuristicPrimeTester tester = _tester ??= new();
|
||||
|
||||
// bool isPrime = primeTester.HeuristicIsPrimeGpu(value);
|
||||
bool isPrime = tester.IsPrimeGpu(value);
|
||||
bool isPrime = PrimeTester.IsPrime(value);
|
||||
// bool isPrime = PrimeTester.IsPrimeGpu(value);
|
||||
// bool isPrime = Open.Numeric.Primes.Prime.Numbers.IsPrime(value);
|
||||
if (!knownComposite && isPrime)
|
||||
@@ -1813,7 +1813,7 @@ internal static partial class PrimeOrderCalculator
|
||||
// Console.WriteLine($"Factor completely after PollardRho hits {Volatile.Read(ref _factorCompletelyPollardRhoHits)}");
|
||||
|
||||
// isPrime = primeTester.HeuristicIsPrimeGpu(factor);
|
||||
isPrime = tester.IsPrimeGpu(factor);
|
||||
isPrime = PrimeTester.IsPrime(factor);
|
||||
// isPrime = PrimeTester.IsPrimeGpu(factor);
|
||||
// isPrime = Open.Numeric.Primes.Prime.Numbers.IsPrime(factor);
|
||||
if (isPrime)
|
||||
@@ -1850,7 +1850,7 @@ internal static partial class PrimeOrderCalculator
|
||||
// Atomic.Add(ref _partialFactorCofactorHits, 1UL);
|
||||
// Console.WriteLine($"Partial factor cofactor hits {Volatile.Read(ref _partialFactorCofactorHits)}");
|
||||
|
||||
isPrime = tester.IsPrimeGpu(quotient);
|
||||
isPrime = PrimeTester.IsPrime(quotient);
|
||||
// isPrime = PrimeTester.IsPrimeGpu(quotient);
|
||||
// isPrime = Open.Numeric.Primes.Prime.Numbers.IsPrime(quotient);
|
||||
}
|
||||
|
||||
@@ -950,7 +950,7 @@ internal static partial class PrimeOrderCalculator
|
||||
if (value <= ulong.MaxValue)
|
||||
{
|
||||
// return HeuristicPrimeTester.Exclusive.IsPrimeCpu((ulong)value, CancellationToken.None);
|
||||
return PrimeTester.IsPrimeCpu((ulong)value, CancellationToken.None);
|
||||
return PrimeTester.IsPrime((ulong)value);
|
||||
}
|
||||
|
||||
if ((value & UInt128.One) == UInt128.Zero)
|
||||
|
||||
@@ -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;
|
||||
@@ -22,7 +20,7 @@ public sealed class PrimeTester
|
||||
|
||||
public static PrimeTester Exclusive => _tester ??= new();
|
||||
|
||||
public static bool IsPrime(ulong n, CancellationToken ct)
|
||||
public static bool IsPrime(ulong n)
|
||||
{
|
||||
if (n <= 1UL)
|
||||
{
|
||||
@@ -92,20 +90,15 @@ public sealed class PrimeTester
|
||||
return result;
|
||||
}
|
||||
|
||||
public static bool IsPrimeGpu(ulong n)
|
||||
{
|
||||
return Exclusive.IsPrimeGpu(n, CancellationToken.None);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool IsPrimeGpu(ulong n, CancellationToken ct)
|
||||
public bool IsPrimeGpu(ulong n)
|
||||
{
|
||||
bool forceCpu = GpuContextPool.ForceCpu;
|
||||
bool belowGpuRange = n < 31UL;
|
||||
|
||||
if (forceCpu || belowGpuRange)
|
||||
{
|
||||
return IsPrimeCpu(n, ct);
|
||||
return IsPrime(n);
|
||||
}
|
||||
|
||||
var limiter = GpuPrimeWorkLimiter.Acquire();
|
||||
@@ -145,13 +138,6 @@ public sealed class PrimeTester
|
||||
return flag != 0;
|
||||
}
|
||||
|
||||
public static bool IsPrimeCpu(ulong n, CancellationToken ct)
|
||||
{
|
||||
// Preserve the legacy entry point for callers that bypass the thread-local caches.
|
||||
return IsPrime(n, ct);
|
||||
}
|
||||
|
||||
|
||||
public static int GpuBatchSize { get; set; } = 262_144;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
|
||||
Reference in new issue
Block a user