Fix odd power table updates in GPU heuristics

This commit is contained in:
Marek Lemańczyk committed 2025-10-05 16:55:27 +02:00
1 parent ce1355c749
commit 20120f5bc0
11 files changed
+1865 -141

No files matched your search

@@ -0,0 +1,418 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using FluentAssertions;
using ILGPU;
using ILGPU.Runtime;
using PerfectNumbers.Core;
using PerfectNumbers.Core.Gpu;
using Xunit;
namespace PerfectNumbers.Core.Tests.Gpu;
[Collection("GpuNtt")]
public class GpuKernelCompilationTests
{
private static readonly MethodInfo DivisorCycleCacheLoadKernelMethod =
typeof(DivisorCycleCache).GetMethod("LoadKernel", BindingFlags.NonPublic | BindingFlags.Static)
?? throw new InvalidOperationException("DivisorCycleCache.LoadKernel not found.");
private static readonly MethodInfo NttGetBitReverseKernelMethod =
typeof(NttGpuMath).GetMethod("GetBitReverseKernel", BindingFlags.NonPublic | BindingFlags.Static)
?? throw new InvalidOperationException("NttGpuMath.GetBitReverseKernel not found.");
private static readonly MethodInfo NttGetMulKernelMethod =
typeof(NttGpuMath).GetMethod("GetMulKernel", BindingFlags.NonPublic | BindingFlags.Static)
?? throw new InvalidOperationException("NttGpuMath.GetMulKernel not found.");
private static readonly MethodInfo NttGetStageKernelMethod =
typeof(NttGpuMath).GetMethod("GetStageKernel", BindingFlags.NonPublic | BindingFlags.Static)
?? throw new InvalidOperationException("NttGpuMath.GetStageKernel not found.");
private static readonly MethodInfo NttGetScaleKernelMethod =
typeof(NttGpuMath).GetMethod("GetScaleKernel", BindingFlags.NonPublic | BindingFlags.Static)
?? throw new InvalidOperationException("NttGpuMath.GetScaleKernel not found.");
private static readonly MethodInfo NttGetStageMontKernelMethod =
typeof(NttGpuMath).GetMethod("GetStageMontKernel", BindingFlags.NonPublic | BindingFlags.Static)
?? throw new InvalidOperationException("NttGpuMath.GetStageMontKernel not found.");
private static readonly MethodInfo NttGetStageBarrettKernelMethod =
typeof(NttGpuMath).GetMethod("GetStageBarrett128Kernel", BindingFlags.NonPublic | BindingFlags.Static)
?? throw new InvalidOperationException("NttGpuMath.GetStageBarrett128Kernel not found.");
private static readonly MethodInfo NttGetScaleBarrettKernelMethod =
typeof(NttGpuMath).GetMethod("GetScaleBarrett128Kernel", BindingFlags.NonPublic | BindingFlags.Static)
?? throw new InvalidOperationException("NttGpuMath.GetScaleBarrett128Kernel not found.");
private static readonly MethodInfo NttGetSquareBarrettKernelMethod =
typeof(NttGpuMath).GetMethod("GetSquareBarrett128Kernel", BindingFlags.NonPublic | BindingFlags.Static)
?? throw new InvalidOperationException("NttGpuMath.GetSquareBarrett128Kernel not found.");
private static readonly MethodInfo NttGetToMont64KernelMethod =
typeof(NttGpuMath).GetMethod("GetToMont64Kernel", BindingFlags.NonPublic | BindingFlags.Static)
?? throw new InvalidOperationException("NttGpuMath.GetToMont64Kernel not found.");
private static readonly MethodInfo NttGetFromMont64KernelMethod =
typeof(NttGpuMath).GetMethod("GetFromMont64Kernel", BindingFlags.NonPublic | BindingFlags.Static)
?? throw new InvalidOperationException("NttGpuMath.GetFromMont64Kernel not found.");
private static readonly MethodInfo NttGetSquareMont64KernelMethod =
typeof(NttGpuMath).GetMethod("GetSquareMont64Kernel", BindingFlags.NonPublic | BindingFlags.Static)
?? throw new InvalidOperationException("NttGpuMath.GetSquareMont64Kernel not found.");
private static readonly MethodInfo NttGetScaleMont64KernelMethod =
typeof(NttGpuMath).GetMethod("GetScaleMont64Kernel", BindingFlags.NonPublic | BindingFlags.Static)
?? throw new InvalidOperationException("NttGpuMath.GetScaleMont64Kernel not found.");
private static readonly MethodInfo NttGetForwardKernelMethod =
typeof(NttGpuMath).GetMethod("GetForwardKernel", BindingFlags.NonPublic | BindingFlags.Static)
?? throw new InvalidOperationException("NttGpuMath.GetForwardKernel not found.");
private static readonly MethodInfo NttGetInverseKernelMethod =
typeof(NttGpuMath).GetMethod("GetInverseKernel", BindingFlags.NonPublic | BindingFlags.Static)
?? throw new InvalidOperationException("NttGpuMath.GetInverseKernel not found.");
private static readonly MethodInfo DivisorByDivisorGetKernelMethod =
typeof(MersenneNumberDivisorByDivisorGpuTester).GetMethod("GetKernel", BindingFlags.NonPublic | BindingFlags.Instance)
?? throw new InvalidOperationException("MersenneNumberDivisorByDivisorGpuTester.GetKernel not found.");
private static readonly MethodInfo DivisorByDivisorGetExponentKernelMethod =
typeof(MersenneNumberDivisorByDivisorGpuTester).GetMethod("GetKernelByPrimeExponent", BindingFlags.NonPublic | BindingFlags.Instance)
?? throw new InvalidOperationException("MersenneNumberDivisorByDivisorGpuTester.GetKernelByPrimeExponent not found.");
private static readonly MethodInfo DivisorByDivisorGetRemainderDeltaKernelMethod =
typeof(MersenneNumberDivisorByDivisorGpuTester).GetMethod("GetRemainderDeltaKernel", BindingFlags.NonPublic | BindingFlags.Instance)
?? throw new InvalidOperationException("MersenneNumberDivisorByDivisorGpuTester.GetRemainderDeltaKernel not found.");
private static readonly MethodInfo DivisorByDivisorGetRemainderScanKernelMethod =
typeof(MersenneNumberDivisorByDivisorGpuTester).GetMethod("GetRemainderScanKernel", BindingFlags.NonPublic | BindingFlags.Instance)
?? throw new InvalidOperationException("MersenneNumberDivisorByDivisorGpuTester.GetRemainderScanKernel not found.");
private static readonly MethodInfo DivisorTesterGetKernelMethod =
typeof(MersenneNumberDivisorGpuTester).GetMethod("GetKernel", BindingFlags.NonPublic | BindingFlags.Instance)
?? throw new InvalidOperationException("MersenneNumberDivisorGpuTester.GetKernel not found.");
private static readonly MethodInfo LucasLehmerGetKernelMethod =
typeof(MersenneNumberLucasLehmerGpuTester).GetMethod("GetKernel", BindingFlags.NonPublic | BindingFlags.Instance)
?? throw new InvalidOperationException("MersenneNumberLucasLehmerGpuTester.GetKernel not found.");
private static readonly MethodInfo LucasLehmerGetAddSmallKernelMethod =
typeof(MersenneNumberLucasLehmerGpuTester).GetMethod("GetAddSmallKernel", BindingFlags.NonPublic | BindingFlags.Instance)
?? throw new InvalidOperationException("MersenneNumberLucasLehmerGpuTester.GetAddSmallKernel not found.");
private static readonly MethodInfo LucasLehmerGetSubSmallKernelMethod =
typeof(MersenneNumberLucasLehmerGpuTester).GetMethod("GetSubSmallKernel", BindingFlags.NonPublic | BindingFlags.Instance)
?? throw new InvalidOperationException("MersenneNumberLucasLehmerGpuTester.GetSubSmallKernel not found.");
private static readonly MethodInfo LucasLehmerGetReduceKernelMethod =
typeof(MersenneNumberLucasLehmerGpuTester).GetMethod("GetReduceKernel", BindingFlags.NonPublic | BindingFlags.Instance)
?? throw new InvalidOperationException("MersenneNumberLucasLehmerGpuTester.GetReduceKernel not found.");
private static readonly MethodInfo LucasLehmerGetIsZeroKernelMethod =
typeof(MersenneNumberLucasLehmerGpuTester).GetMethod("GetIsZeroKernel", BindingFlags.NonPublic | BindingFlags.Instance)
?? throw new InvalidOperationException("MersenneNumberLucasLehmerGpuTester.GetIsZeroKernel not found.");
private static readonly MethodInfo LucasLehmerGetBatchKernelMethod =
typeof(MersenneNumberLucasLehmerGpuTester).GetMethod("GetBatchKernel", BindingFlags.NonPublic | BindingFlags.Instance)
?? throw new InvalidOperationException("MersenneNumberLucasLehmerGpuTester.GetBatchKernel not found.");
private static readonly MethodInfo PrimeOrderGetPartialFactorKernelMethod =
typeof(PrimeOrderGpuHeuristics).GetMethod("GetPartialFactorKernel", BindingFlags.NonPublic | BindingFlags.Static)
?? throw new InvalidOperationException("PrimeOrderGpuHeuristics.GetPartialFactorKernel not found.");
private static readonly MethodInfo PrimeOrderGetOrderKernelMethod =
typeof(PrimeOrderGpuHeuristics).GetMethod("GetOrderKernel", BindingFlags.NonPublic | BindingFlags.Static)
?? throw new InvalidOperationException("PrimeOrderGpuHeuristics.GetOrderKernel not found.");
private static readonly MethodInfo PrimeOrderGetPow2ModKernelMethod =
typeof(PrimeOrderGpuHeuristics).GetMethod("GetPow2ModKernel", BindingFlags.NonPublic | BindingFlags.Static)
?? throw new InvalidOperationException("PrimeOrderGpuHeuristics.GetPow2ModKernel not found.");
private static readonly MethodInfo PrimeOrderGetPow2ModWideKernelMethod =
typeof(PrimeOrderGpuHeuristics).GetMethod("GetPow2ModWideKernel", BindingFlags.NonPublic | BindingFlags.Static)
?? throw new InvalidOperationException("PrimeOrderGpuHeuristics.GetPow2ModWideKernel not found.");
private static readonly Action<Index1D, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>> MersenneDivisorCyclesKernel =
(Action<Index1D, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>>)Delegate.CreateDelegate(
typeof(Action<Index1D, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>>),
typeof(MersenneDivisorCycles).GetMethod("GpuDivisorCycleKernel", BindingFlags.NonPublic | BindingFlags.Static)
?? throw new InvalidOperationException("MersenneDivisorCycles.GpuDivisorCycleKernel not found."));
private static readonly Action<Index1D, ArrayView<ulong>, ArrayView<uint>, ArrayView<byte>> PrimeTesterSmallPrimeKernel =
(Action<Index1D, ArrayView<ulong>, ArrayView<uint>, ArrayView<byte>>)Delegate.CreateDelegate(
typeof(Action<Index1D, ArrayView<ulong>, ArrayView<uint>, ArrayView<byte>>),
typeof(PrimeTester).GetMethod("SmallPrimeSieveKernel", BindingFlags.NonPublic | BindingFlags.Static)
?? throw new InvalidOperationException("PrimeTester.SmallPrimeSieveKernel not found."));
private static readonly Action<Index1D, ArrayView<ulong>, ArrayView<byte>> PrimeTesterSharesFactorKernel =
(Action<Index1D, ArrayView<ulong>, ArrayView<byte>>)Delegate.CreateDelegate(
typeof(Action<Index1D, ArrayView<ulong>, ArrayView<byte>>),
typeof(PrimeTester).GetMethod("SharesFactorKernel", BindingFlags.NonPublic | BindingFlags.Static)
?? throw new InvalidOperationException("PrimeTester.SharesFactorKernel not found."));
public static IEnumerable<object[]> KernelLoaders()
{
yield return Loader("DivisorCycleCache.GpuAdvanceDivisorCyclesKernel", CompileDivisorCycleCacheKernel);
yield return Loader("NttGpuMath.BitReverseKernel", CompileNttBitReverseKernel);
yield return Loader("NttGpuMath.MulKernel", CompileNttMulKernel);
yield return Loader("NttGpuMath.StageKernel", CompileNttStageKernel);
yield return Loader("NttGpuMath.ScaleKernel", CompileNttScaleKernel);
yield return Loader("NttGpuMath.StageMontKernel", CompileNttStageMontKernel);
yield return Loader("NttGpuMath.StageBarrett128Kernel", CompileNttStageBarrettKernel);
yield return Loader("NttGpuMath.ScaleBarrett128Kernel", CompileNttScaleBarrettKernel);
yield return Loader("NttGpuMath.SquareBarrett128Kernel", CompileNttSquareBarrettKernel);
yield return Loader("NttGpuMath.ToMont64Kernel", CompileNttToMont64Kernel);
yield return Loader("NttGpuMath.FromMont64Kernel", CompileNttFromMont64Kernel);
yield return Loader("NttGpuMath.SquareMont64Kernel", CompileNttSquareMont64Kernel);
yield return Loader("NttGpuMath.ScaleMont64Kernel", CompileNttScaleMont64Kernel);
yield return Loader("NttGpuMath.ForwardKernel", CompileNttForwardKernel);
yield return Loader("NttGpuMath.InverseKernel", CompileNttInverseKernel);
yield return Loader("MersenneNumberDivisorByDivisorGpuTester.CheckKernel", CompileDivisorByDivisorCheckKernel);
yield return Loader("MersenneNumberDivisorByDivisorGpuTester.ComputeMontgomeryExponentKernel", CompileDivisorByDivisorExponentKernel);
yield return Loader("MersenneNumberDivisorByDivisorGpuTester.ComputeRemainderDeltasKernel", CompileDivisorByDivisorRemainderDeltaKernel);
yield return Loader("MersenneNumberDivisorByDivisorGpuTester.AccumulateRemaindersKernel", CompileDivisorByDivisorRemainderScanKernel);
yield return Loader("MersenneNumberDivisorGpuTester.Kernel", CompileDivisorTesterKernel);
yield return Loader("MersenneNumberLucasLehmerGpuTester.Kernel", CompileLucasLehmerKernel);
yield return Loader("MersenneNumberLucasLehmerGpuTester.AddSmallKernel", CompileLucasLehmerAddSmallKernel);
yield return Loader("MersenneNumberLucasLehmerGpuTester.SubtractSmallKernel", CompileLucasLehmerSubSmallKernel);
yield return Loader("MersenneNumberLucasLehmerGpuTester.ReduceModMersenneKernel", CompileLucasLehmerReduceKernel);
yield return Loader("MersenneNumberLucasLehmerGpuTester.IsZeroKernel", CompileLucasLehmerIsZeroKernel);
yield return Loader("MersenneNumberLucasLehmerGpuTester.KernelBatch", CompileLucasLehmerBatchKernel);
yield return Loader("PrimeOrderGpuHeuristics.PartialFactorKernel", CompilePrimeOrderPartialFactorKernel);
yield return Loader("PrimeOrderGpuHeuristics.CalculateOrderKernel", CompilePrimeOrderKernel);
yield return Loader("PrimeOrderGpuHeuristics.Pow2ModKernel", CompilePrimeOrderPow2ModKernel);
yield return Loader("PrimeOrderGpuHeuristics.Pow2ModKernelWide", CompilePrimeOrderPow2ModWideKernel);
yield return Loader("MersenneDivisorCycles.GpuDivisorCycleKernel", CompileMersenneDivisorCyclesKernel);
yield return Loader("PrimeTester.SmallPrimeSieveKernel", CompilePrimeTesterSmallPrimeKernel);
yield return Loader("PrimeTester.SharesFactorKernel", CompilePrimeTesterSharesFactorKernel);
yield return Loader("GpuKernelPool.KernelLeaseKernels", CompileGpuKernelPoolKernels);
}
private static object[] Loader(string name, Action<Accelerator> loader)
{
return new object[] { name, loader };
}
[Theory]
[Trait("Category", "Fast")]
[MemberData(nameof(KernelLoaders))]
public void Kernel_compiles_without_internal_compiler_errors(string name, Action<Accelerator> compile)
{
using var lease = GpuContextPool.RentPreferred(preferCpu: true);
var accelerator = lease.Accelerator;
Action action = () => compile(accelerator);
action.Should().NotThrow($"kernel {name} should compile");
}
private static void CompileDivisorCycleCacheKernel(Accelerator accelerator)
{
_ = DivisorCycleCacheLoadKernelMethod.Invoke(null, new object[] { accelerator });
}
private static void CompileNttBitReverseKernel(Accelerator accelerator)
{
_ = NttGetBitReverseKernelMethod.Invoke(null, new object[] { accelerator });
}
private static void CompileNttMulKernel(Accelerator accelerator)
{
_ = NttGetMulKernelMethod.Invoke(null, new object[] { accelerator });
}
private static void CompileNttStageKernel(Accelerator accelerator)
{
_ = NttGetStageKernelMethod.Invoke(null, new object[] { accelerator });
}
private static void CompileNttScaleKernel(Accelerator accelerator)
{
_ = NttGetScaleKernelMethod.Invoke(null, new object[] { accelerator });
}
private static void CompileNttStageMontKernel(Accelerator accelerator)
{
_ = NttGetStageMontKernelMethod.Invoke(null, new object[] { accelerator });
}
private static void CompileNttStageBarrettKernel(Accelerator accelerator)
{
_ = NttGetStageBarrettKernelMethod.Invoke(null, new object[] { accelerator });
}
private static void CompileNttScaleBarrettKernel(Accelerator accelerator)
{
_ = NttGetScaleBarrettKernelMethod.Invoke(null, new object[] { accelerator });
}
private static void CompileNttSquareBarrettKernel(Accelerator accelerator)
{
_ = NttGetSquareBarrettKernelMethod.Invoke(null, new object[] { accelerator });
}
private static void CompileNttToMont64Kernel(Accelerator accelerator)
{
_ = NttGetToMont64KernelMethod.Invoke(null, new object[] { accelerator });
}
private static void CompileNttFromMont64Kernel(Accelerator accelerator)
{
_ = NttGetFromMont64KernelMethod.Invoke(null, new object[] { accelerator });
}
private static void CompileNttSquareMont64Kernel(Accelerator accelerator)
{
_ = NttGetSquareMont64KernelMethod.Invoke(null, new object[] { accelerator });
}
private static void CompileNttScaleMont64Kernel(Accelerator accelerator)
{
_ = NttGetScaleMont64KernelMethod.Invoke(null, new object[] { accelerator });
}
private static void CompileNttForwardKernel(Accelerator accelerator)
{
_ = NttGetForwardKernelMethod.Invoke(null, new object[] { accelerator });
}
private static void CompileNttInverseKernel(Accelerator accelerator)
{
_ = NttGetInverseKernelMethod.Invoke(null, new object[] { accelerator });
}
private static void CompileDivisorByDivisorCheckKernel(Accelerator accelerator)
{
var tester = new MersenneNumberDivisorByDivisorGpuTester();
_ = DivisorByDivisorGetKernelMethod.Invoke(tester, new object[] { accelerator });
}
private static void CompileDivisorByDivisorExponentKernel(Accelerator accelerator)
{
var tester = new MersenneNumberDivisorByDivisorGpuTester();
_ = DivisorByDivisorGetExponentKernelMethod.Invoke(tester, new object[] { accelerator });
}
private static void CompileDivisorByDivisorRemainderDeltaKernel(Accelerator accelerator)
{
var tester = new MersenneNumberDivisorByDivisorGpuTester();
_ = DivisorByDivisorGetRemainderDeltaKernelMethod.Invoke(tester, new object[] { accelerator });
}
private static void CompileDivisorByDivisorRemainderScanKernel(Accelerator accelerator)
{
var tester = new MersenneNumberDivisorByDivisorGpuTester();
_ = DivisorByDivisorGetRemainderScanKernelMethod.Invoke(tester, new object[] { accelerator });
}
private static void CompileDivisorTesterKernel(Accelerator accelerator)
{
var tester = new MersenneNumberDivisorGpuTester();
_ = DivisorTesterGetKernelMethod.Invoke(tester, new object[] { accelerator });
}
private static void CompileLucasLehmerKernel(Accelerator accelerator)
{
var tester = new MersenneNumberLucasLehmerGpuTester();
_ = LucasLehmerGetKernelMethod.Invoke(tester, new object[] { accelerator });
}
private static void CompileLucasLehmerAddSmallKernel(Accelerator accelerator)
{
var tester = new MersenneNumberLucasLehmerGpuTester();
_ = LucasLehmerGetAddSmallKernelMethod.Invoke(tester, new object[] { accelerator });
}
private static void CompileLucasLehmerSubSmallKernel(Accelerator accelerator)
{
var tester = new MersenneNumberLucasLehmerGpuTester();
_ = LucasLehmerGetSubSmallKernelMethod.Invoke(tester, new object[] { accelerator });
}
private static void CompileLucasLehmerReduceKernel(Accelerator accelerator)
{
var tester = new MersenneNumberLucasLehmerGpuTester();
_ = LucasLehmerGetReduceKernelMethod.Invoke(tester, new object[] { accelerator });
}
private static void CompileLucasLehmerIsZeroKernel(Accelerator accelerator)
{
var tester = new MersenneNumberLucasLehmerGpuTester();
_ = LucasLehmerGetIsZeroKernelMethod.Invoke(tester, new object[] { accelerator });
}
private static void CompileLucasLehmerBatchKernel(Accelerator accelerator)
{
var tester = new MersenneNumberLucasLehmerGpuTester();
_ = LucasLehmerGetBatchKernelMethod.Invoke(tester, new object[] { accelerator });
}
private static void CompilePrimeOrderPartialFactorKernel(Accelerator accelerator)
{
_ = PrimeOrderGetPartialFactorKernelMethod.Invoke(null, new object[] { accelerator });
}
private static void CompilePrimeOrderKernel(Accelerator accelerator)
{
_ = PrimeOrderGetOrderKernelMethod.Invoke(null, new object[] { accelerator });
}
private static void CompilePrimeOrderPow2ModKernel(Accelerator accelerator)
{
_ = PrimeOrderGetPow2ModKernelMethod.Invoke(null, new object[] { accelerator });
}
private static void CompilePrimeOrderPow2ModWideKernel(Accelerator accelerator)
{
_ = PrimeOrderGetPow2ModWideKernelMethod.Invoke(null, new object[] { accelerator });
}
private static void CompileMersenneDivisorCyclesKernel(Accelerator accelerator)
{
var kernel = accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>>(MersenneDivisorCyclesKernel);
_ = kernel;
}
private static void CompilePrimeTesterSmallPrimeKernel(Accelerator accelerator)
{
var kernel = accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<ulong>, ArrayView<uint>, ArrayView<byte>>(PrimeTesterSmallPrimeKernel);
_ = kernel;
}
private static void CompilePrimeTesterSharesFactorKernel(Accelerator accelerator)
{
var kernel = accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<ulong>, ArrayView<byte>>(PrimeTesterSharesFactorKernel);
_ = kernel;
}
private static void CompileGpuKernelPoolKernels(Accelerator accelerator)
{
_ = accelerator;
var lease = GpuKernelPool.GetKernel(useGpuOrder: false);
try
{
_ = lease.OrderKernel;
_ = lease.IncrementalKernel;
_ = lease.Pow2ModKernel;
_ = lease.IncrementalOrderKernel;
_ = lease.Pow2ModOrderKernel;
}
finally
{
lease.Dispose();
}
var orderLease = GpuKernelPool.GetKernel(useGpuOrder: true);
try
{
_ = orderLease.OrderKernel;
_ = orderLease.IncrementalKernel;
_ = orderLease.Pow2ModKernel;
_ = orderLease.IncrementalOrderKernel;
_ = orderLease.Pow2ModOrderKernel;
}
finally
{
orderLease.Dispose();
}
}
}
@@ -11,10 +11,12 @@ namespace PerfectNumbers.Core.Tests;
public class MersenneNumberDivisorGpuTesterTests
{
[Theory]
[InlineData(3UL, 7UL, true)]
[InlineData(11UL, 23UL, true)]
[InlineData(7UL, 35UL, false)]
[InlineData(13UL, 23UL, false)]
[InlineData(33UL, 7UL, true)]
[InlineData(35UL, 31UL, true)]
[InlineData(37UL, 223UL, true)]
[InlineData(33UL, 13UL, false)]
[InlineData(35UL, 73UL, false)]
[InlineData(37UL, 227UL, false)]
[Trait("Category", "Fast")]
public void IsDivisible_returns_expected(ulong exponent, ulong divisor, bool expected)
{
@@ -41,7 +43,7 @@ public class MersenneNumberDivisorGpuTesterTests
.GetField("_divisorCandidates", BindingFlags.NonPublic | BindingFlags.Static)!
.SetValue(null, Array.Empty<(ulong, uint)>());
tester.IsPrime(11UL, UInt128.Zero, 0UL, out bool divisorsExhausted).Should().BeTrue();
tester.IsPrime(31UL, UInt128.Zero, 0UL, out bool divisorsExhausted).Should().BeTrue();
divisorsExhausted.Should().BeFalse();
}
@@ -50,7 +52,7 @@ public class MersenneNumberDivisorGpuTesterTests
public void IsPrime_sets_divisorsExhausted_true_when_divisible_by_specified_divisor()
{
var tester = new MersenneNumberDivisorGpuTester();
tester.IsPrime(3UL, 7UL, 0UL, out bool divisorsExhausted).Should().BeFalse();
tester.IsPrime(35UL, 31UL, 0UL, out bool divisorsExhausted).Should().BeFalse();
divisorsExhausted.Should().BeTrue();
}
@@ -59,7 +61,7 @@ public class MersenneNumberDivisorGpuTesterTests
public void IsPrime_accepts_large_search_limits()
{
var tester = new MersenneNumberDivisorGpuTester();
tester.IsPrime(3UL, 7UL, ulong.MaxValue, out bool exhausted).Should().BeFalse();
tester.IsPrime(35UL, 31UL, ulong.MaxValue, out bool exhausted).Should().BeFalse();
exhausted.Should().BeTrue();
}
@@ -68,15 +70,15 @@ public class MersenneNumberDivisorGpuTesterTests
public void ByDivisor_tester_tracks_divisors_across_primes()
{
var tester = new MersenneNumberDivisorByDivisorGpuTester();
tester.ConfigureFromMaxPrime(11UL);
tester.ConfigureFromMaxPrime(43UL);
tester.IsPrime(5UL, out bool exhausted).Should().BeTrue();
tester.IsPrime(31UL, out bool exhausted).Should().BeTrue();
exhausted.Should().BeTrue();
tester.IsPrime(7UL, out exhausted).Should().BeTrue();
tester.IsPrime(37UL, out exhausted).Should().BeFalse();
exhausted.Should().BeTrue();
tester.IsPrime(11UL, out exhausted).Should().BeFalse();
tester.IsPrime(43UL, out exhausted).Should().BeFalse();
exhausted.Should().BeTrue();
}
@@ -85,7 +87,7 @@ public class MersenneNumberDivisorGpuTesterTests
public void ByDivisor_tester_requires_configuration()
{
var tester = new MersenneNumberDivisorByDivisorGpuTester();
Action act = () => tester.IsPrime(5UL, out _);
Action act = () => tester.IsPrime(31UL, out _);
act.Should().Throw<InvalidOperationException>();
}
@@ -94,16 +96,30 @@ public class MersenneNumberDivisorGpuTesterTests
public void ByDivisor_tester_respects_filter_based_limit()
{
var tester = new MersenneNumberDivisorByDivisorGpuTester();
tester.ConfigureFromMaxPrime(5UL);
tester.ConfigureFromMaxPrime(41UL);
tester.IsPrime(5UL, out _).Should().BeTrue();
tester.IsPrime(7UL, out bool exhausted).Should().BeTrue();
tester.IsPrime(31UL, out _).Should().BeTrue();
tester.IsPrime(37UL, out bool exhausted).Should().BeFalse();
exhausted.Should().BeTrue();
tester.IsPrime(11UL, out bool divisorsExhausted).Should().BeTrue();
tester.IsPrime(41UL, out bool divisorsExhausted).Should().BeFalse();
divisorsExhausted.Should().BeTrue();
}
[Theory]
[InlineData(71UL, false)]
[InlineData(89UL, true)]
[InlineData(127UL, true)]
[Trait("Category", "Fast")]
public void ByDivisor_tester_matches_incremental_expectations(ulong exponent, bool expectedPrime)
{
var tester = new MersenneNumberDivisorByDivisorGpuTester();
tester.ConfigureFromMaxPrime(exponent);
tester.IsPrime(exponent, out bool exhausted).Should().Be(expectedPrime);
exhausted.Should().BeTrue();
}
[Fact]
[Trait("Category", "Fast")]
public void ByDivisor_gpu_tester_uses_cycle_remainders_per_divisor()
@@ -113,9 +129,9 @@ public class MersenneNumberDivisorGpuTesterTests
GpuBatchSize = 8,
};
tester.ConfigureFromMaxPrime(13UL);
tester.ConfigureFromMaxPrime(43UL);
tester.IsPrime(11UL, out bool divisorsExhausted).Should().BeFalse();
tester.IsPrime(41UL, out bool divisorsExhausted).Should().BeFalse();
divisorsExhausted.Should().BeTrue();
}
@@ -124,20 +140,20 @@ public class MersenneNumberDivisorGpuTesterTests
public void ByDivisor_session_checks_divisors_across_primes()
{
var tester = new MersenneNumberDivisorByDivisorGpuTester();
tester.ConfigureFromMaxPrime(13UL);
tester.ConfigureFromMaxPrime(43UL);
using var session = tester.CreateDivisorSession();
ulong[] primes = { 5UL, 7UL, 11UL, 13UL };
ulong[] primes = { 31UL, 37UL, 41UL, 43UL };
byte[] hits = new byte[primes.Length];
ulong cycle23 = MersenneDivisorCycles.CalculateCycleLength(23UL);
session.CheckDivisor(23UL, cycle23, primes, hits);
hits.Should().ContainInOrder(new byte[] { 0, 0, 1, 0 });
ulong cycle223 = MersenneDivisorCycles.CalculateCycleLength(223UL);
session.CheckDivisor(223UL, cycle223, primes, hits);
hits.Should().ContainInOrder(new byte[] { 0, 1, 0, 0 });
Array.Fill(hits, (byte)0);
ulong cycle31 = MersenneDivisorCycles.CalculateCycleLength(31UL);
session.CheckDivisor(31UL, cycle31, primes, hits);
hits.Should().ContainInOrder(new byte[] { 1, 0, 0, 0 });
ulong cycle13367 = MersenneDivisorCycles.CalculateCycleLength(13367UL);
session.CheckDivisor(13367UL, cycle13367, primes, hits);
hits.Should().ContainInOrder(new byte[] { 0, 0, 1, 0 });
}
[Fact]
@@ -149,16 +165,16 @@ public class MersenneNumberDivisorGpuTesterTests
GpuBatchSize = 2,
};
tester.ConfigureFromMaxPrime(17UL);
tester.ConfigureFromMaxPrime(47UL);
using var session = tester.CreateDivisorSession();
ulong[] primes = { 5UL, 7UL, 11UL, 13UL, 17UL };
ulong[] primes = { 31UL, 37UL, 41UL, 43UL, 47UL };
byte[] hits = new byte[primes.Length];
ulong cycle23 = MersenneDivisorCycles.CalculateCycleLength(23UL);
session.CheckDivisor(23UL, cycle23, primes, hits);
ulong cycle223 = MersenneDivisorCycles.CalculateCycleLength(223UL);
session.CheckDivisor(223UL, cycle223, primes, hits);
hits.Should().ContainInOrder(new byte[] { 0, 0, 1, 0, 0 });
hits.Should().ContainInOrder(new byte[] { 0, 1, 0, 0, 0 });
}
[Fact]
@@ -170,16 +186,16 @@ public class MersenneNumberDivisorGpuTesterTests
BatchSize = 2,
};
tester.ConfigureFromMaxPrime(17UL);
tester.ConfigureFromMaxPrime(47UL);
using var session = tester.CreateDivisorSession();
ulong[] primes = { 5UL, 7UL, 11UL, 13UL, 17UL };
ulong[] primes = { 31UL, 37UL, 41UL, 43UL, 47UL };
byte[] hits = new byte[primes.Length];
ulong cycle23 = MersenneDivisorCycles.CalculateCycleLength(23UL);
session.CheckDivisor(23UL, cycle23, primes, hits);
ulong cycle223 = MersenneDivisorCycles.CalculateCycleLength(223UL);
session.CheckDivisor(223UL, cycle223, primes, hits);
hits.Should().ContainInOrder(new byte[] { 0, 0, 1, 0, 0 });
hits.Should().ContainInOrder(new byte[] { 0, 1, 0, 0, 0 });
}
}
@@ -9,7 +9,7 @@ public class MersenneNumberIncrementalGpuTesterTests
{
private static bool LastDigitIsSeven(ulong exponent) => (exponent & 3UL) == 3UL;
[Theory]
[Theory(Skip = "Disabled until the GPU incremental cycle heuristics are ported.")]
[InlineData(GpuKernelType.Pow2Mod, false)]
[InlineData(GpuKernelType.Incremental, false)]
[InlineData(GpuKernelType.Pow2Mod, true)]
@@ -20,7 +20,8 @@ public class MersenneNumberIncrementalGpuTesterTests
if (type == GpuKernelType.Pow2Mod)
{
RunCase(tester, 11UL, 1UL, expectedPrime: false);
// Mirrored by the divisor GPU coverage to ensure the failing cases stay validated.
RunCase(tester, 37UL, 1UL, expectedPrime: false);
RunCase(tester, 71UL, 341_860UL, expectedPrime: false);
}