PrimeOrderCalculatorAccelerator.Rent is now created outside of the loops and re-used for all calculations.

This commit is contained in:
mlemanczyk committed 2025-11-24 22:51:53 +01:00
1 parent 57a50e2a37
commit b9ca95bf8c
53 files changed
+4639 -4229

No files matched your search

@@ -1,4 +1,5 @@
using FluentAssertions;
using PerfectNumbers.Core.Gpu.Accelerators;
using Xunit;
namespace PerfectNumbers.Core.Tests;
@@ -6,20 +7,28 @@ namespace PerfectNumbers.Core.Tests;
[Trait("Category", "Fast")]
public class PrimeTesterGcdTests
{
[Theory]
[InlineData(81UL, true)]
[InlineData(101UL, false)]
public void SharesFactorWithMaxExponent_detects_gcd(ulong n, bool expected)
{
PrimeTester.SharesFactorWithMaxExponent(n).Should().Be(expected);
}
[Theory]
[InlineData(81UL, true)]
[InlineData(101UL, false)]
public void SharesFactorWithMaxExponent_detects_gcd(ulong n, bool expected)
{
PrimeTester.SharesFactorWithMaxExponent(n).Should().Be(expected);
}
[Fact]
public void SharesFactorWithMaxExponentBatch_filters_values()
{
ulong[] values = [81UL, 101UL];
Span<byte> results = stackalloc byte[values.Length];
PrimeTester.SharesFactorWithMaxExponentBatch(values, results);
results.ToArray().Should().Equal(new byte[] { 1, 0 });
}
[Fact]
public void SharesFactorWithMaxExponentBatch_filters_values()
{
ulong[] values = [81UL, 101UL];
Span<byte> results = stackalloc byte[values.Length];
var gpu = PrimeOrderCalculatorAccelerator.Rent(1);
try
{
PrimeTester.SharesFactorWithMaxExponentBatch(gpu, values, results);
results.ToArray().Should().Equal(new byte[] { 1, 0 });
}
finally
{
PrimeOrderCalculatorAccelerator.Return(gpu);
}
}
}
@@ -1,81 +1,98 @@
using FluentAssertions;
using PerfectNumbers.Core.Gpu;
using PerfectNumbers.Core.Gpu.Accelerators;
using Xunit;
namespace PerfectNumbers.Core.Tests;
public class PrimeTesterIsPrimeTests
{
[Theory]
[Trait("Category", "Fast")]
// EvenPerfectBitScanner never routes the tiny values anymore; keep coverage focused on the admissible range handled by production scans so the guards in PrimeTester stay commented out without breaking validation.
[InlineData(7UL, true)]
[InlineData(9UL, false)]
[InlineData(11UL, true)]
[InlineData(13UL, true)]
[InlineData(21UL, false)]
[InlineData(97UL, true)]
[InlineData(133UL, false)]
[InlineData(137UL, true)]
[InlineData(143UL, false)]
[InlineData(199UL, true)]
[InlineData(209UL, false)]
[InlineData(403UL, false)]
[InlineData(341UL, false)]
public void IsPrime_returns_expected_results(ulong n, bool expected)
{
var tester = new PrimeTester();
[Theory]
[Trait("Category", "Fast")]
// EvenPerfectBitScanner never routes the tiny values anymore; keep coverage focused on the admissible range handled by production scans so the guards in PrimeTester stay commented out without breaking validation.
[InlineData(7UL, true)]
[InlineData(9UL, false)]
[InlineData(11UL, true)]
[InlineData(13UL, true)]
[InlineData(21UL, false)]
[InlineData(97UL, true)]
[InlineData(133UL, false)]
[InlineData(137UL, true)]
[InlineData(143UL, false)]
[InlineData(199UL, true)]
[InlineData(209UL, false)]
[InlineData(403UL, false)]
[InlineData(341UL, false)]
public void IsPrime_returns_expected_results(ulong n, bool expected)
{
var tester = new PrimeTester();
PrimeTester.IsPrime(n).Should().Be(expected);
}
}
[Theory]
[Trait("Category", "Fast")]
[InlineData(139UL, true)]
[InlineData(189UL, false)]
[InlineData(193UL, true)]
[InlineData(279UL, false)]
public void IsPrimeGpu_uses_residue_specific_tables(ulong n, bool expected)
{
var tester = new PrimeTester();
[Theory]
[Trait("Category", "Fast")]
[InlineData(139UL, true)]
[InlineData(189UL, false)]
[InlineData(193UL, true)]
[InlineData(279UL, false)]
public void IsPrimeGpu_uses_residue_specific_tables(ulong n, bool expected)
{
var tester = new PrimeTester();
PrimeTester.IsPrimeGpu(n).Should().Be(expected);
}
var gpu = PrimeOrderCalculatorAccelerator.Rent(1);
try
{
PrimeTester.IsPrimeGpu(gpu, n).Should().Be(expected);
}
finally
{
PrimeOrderCalculatorAccelerator.Return(gpu);
}
}
[Fact]
[Trait("Category", "Fast")]
public void IsPrimeGpu_falls_back_to_cpu_when_forced()
{
var tester = new PrimeTester();
PrimeTester.IsPrime(137UL).Should().BeTrue();
PrimeTester.IsPrime(341UL).Should().BeFalse();
}
[Fact]
[Trait("Category", "Fast")]
public void IsPrimeGpu_falls_back_to_cpu_when_forced()
{
var tester = new PrimeTester();
PrimeTester.IsPrime(137UL).Should().BeTrue();
PrimeTester.IsPrime(341UL).Should().BeFalse();
}
[Fact]
[Trait("Category", "Fast")]
public void IsPrimeGpu_handles_parallel_calls_for_known_primes()
{
GpuPrimeWorkLimiter.SetLimit(64);
PrimeTester.GpuBatchSize = 256_000;
[Fact]
[Trait("Category", "Fast")]
public void IsPrimeGpu_handles_parallel_calls_for_known_primes()
{
GpuPrimeWorkLimiter.SetLimit(64);
PrimeTester.GpuBatchSize = 256_000;
ulong[] primes = [107UL, 127UL, 521UL, 607UL];
var results = new bool[primes.Length];
ulong[] primes = [107UL, 127UL, 521UL, 607UL];
var results = new bool[primes.Length];
var testers = new ThreadLocal<PrimeTester>(() => new PrimeTester(), trackAllValues: true);
Parallel.For(0, primes.Length, i =>
{
results[i] = PrimeTester.IsPrimeGpu(primes[i]);
});
var testers = new ThreadLocal<PrimeTester>(() => new PrimeTester(), trackAllValues: true);
var gpu = PrimeOrderCalculatorAccelerator.Rent(1);
try
{
Parallel.For(0, primes.Length, i =>
{
results[i] = PrimeTester.IsPrimeGpu(gpu, primes[i]);
});
results.Should().AllBeEquivalentTo(true);
results.Should().AllBeEquivalentTo(true);
Parallel.For(0, Environment.ProcessorCount * 16, _ =>
{
foreach (ulong prime in primes)
{
PrimeTester.IsPrimeGpu(prime).Should().BeTrue();
}
});
}
Parallel.For(0, Environment.ProcessorCount * 16, _ =>
{
foreach (ulong prime in primes)
{
PrimeTester.IsPrimeGpu(gpu, prime).Should().BeTrue();
}
});
}
finally
{
PrimeOrderCalculatorAccelerator.Return(gpu);
}
}
}