GpuKernelPool.GetOrAddKernels removed in --mersenne=bydivisor mode. PartialFactorResult.WithAdditionalPrime will now modify the current instance instead of creating new copy for better performance. SmallPrimeFactorViews renamed to SmallPrimeFactorGpuTables for consistency with other static tables.

This commit is contained in:
mlemanczyk committed 2025-11-17 23:33:59 +01:00
1 parent 2fc74cf98d
commit 342eff2726
12 files changed
+119 -189

No files matched your search

-66
View File
@@ -57,16 +57,6 @@ public class GpuKernelPool
InitPow2ModOrderKernelScan(accelerator, kernels);
}
if (kernelType.HasFlag(KernelType.EvaluateSpecialMaxCandidatesKernel))
{
InitEvaluateSpecialMaxCandidatesKernel(accelerator, kernels);
}
if (kernelType.HasFlag(KernelType.SmallPrimeFactorKernelScan))
{
InitSmallPrimeFactorKernelScan(accelerator, kernels);
}
PreloadStaticTables(accelerator, kernels, stream, kernelType);
pool[acceleratorIndex] = kernels;
@@ -132,25 +122,6 @@ public class GpuKernelPool
return kernel.CreateLauncherDelegate<Action<AcceleratorStream, Index1D, ulong, GpuUInt128, GpuUInt128, byte, ulong, ResidueAutomatonArgs, ArrayView<int>, ArrayView1D<ulong, Stride1D.Dense>>>();
});
}
static void InitEvaluateSpecialMaxCandidatesKernel(Accelerator accelerator, KernelContainer kernels)
{
InitOnce(ref kernels.SpecialMax, () =>
{
var loaded = accelerator.LoadAutoGroupedStreamKernel<Index1D, ulong, ArrayView1D<ulong, Stride1D.Dense>, int, MontgomeryDivisorData, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>>(PrimeOrderGpuHeuristics.EvaluateSpecialMaxCandidatesKernel);
var kernel = KernelUtil.GetKernel(loaded);
return kernel.CreateLauncherDelegate<Action<AcceleratorStream, Index1D, ulong, ArrayView1D<ulong, Stride1D.Dense>, int, MontgomeryDivisorData, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>>>();
});
}
static void InitSmallPrimeFactorKernelScan(Accelerator accelerator, KernelContainer kernels)
{
var loaded = accelerator.LoadAutoGroupedStreamKernel<Index1D, ulong, uint, ArrayView1D<uint, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>, int, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<int, Stride1D.Dense>, ArrayView1D<int, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>>(SmallPrimeFactorKernels.SmallPrimeFactorKernelScan);
kernels.SmallPrimeFactor = KernelUtil.GetKernel(loaded);
}
}
internal static void PreloadStaticTables(Accelerator accelerator, KernelContainer kernels, AcceleratorStream stream, KernelType kernelType)
@@ -164,11 +135,6 @@ public class GpuKernelPool
{
EnsureSmallPrimesOnDevice(accelerator, kernels, stream);
}
if (kernelType.HasFlag(KernelType.SmallPrimeFactorKernelScan))
{
EnsureSmallPrimeFactorTables(accelerator, kernels, stream);
}
}
public static ArrayView1D<ulong, Stride1D.Dense> GetSmallCyclesOnDevice(KernelContainer kernels) => kernels.SmallCycles!;
@@ -247,38 +213,6 @@ public class GpuKernelPool
kernels.SmallPrimesPow2LastSeven = deviceLastSevenPow2;
}
public static SmallPrimeFactorViews GetSmallPrimeFactorTables(KernelContainer kernels) => new(
kernels.SmallPrimeFactorsPrimes!,
kernels.SmallPrimeFactorsSquares!
);
private static void EnsureSmallPrimeFactorTables(Accelerator accelerator, KernelContainer kernels, AcceleratorStream stream)
{
if (kernels.SmallPrimeFactorsPrimes is { } && kernels.SmallPrimeFactorsSquares is { })
{
return;
}
var hostPrimes = PrimesGenerator.SmallPrimes;
var hostSquares = PrimesGenerator.SmallPrimesPow2;
int length = hostPrimes.Length;
MemoryBuffer1D<uint, Stride1D.Dense>? devicePrimes;
MemoryBuffer1D<ulong, Stride1D.Dense>? deviceSquares;
// lock (accelerator)
{
devicePrimes = accelerator.Allocate1D<uint>(length);
deviceSquares = accelerator.Allocate1D<ulong>(length);
}
devicePrimes.View.CopyFromCPU(stream, hostPrimes);
deviceSquares.View.CopyFromCPU(stream, hostSquares);
kernels.SmallPrimeFactorsPrimes = devicePrimes;
kernels.SmallPrimeFactorsSquares = deviceSquares;
}
/// <summary>
/// Runs a GPU action with an acquired accelerator and stream.
/// </summary>