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:
1 parent
2fc74cf98d
commit
342eff2726
12 files changed
+119
-189
No files matched your search
@@ -67,8 +67,10 @@ public sealed class PrimeOrderCalculatorAccelerator
|
||||
AcceleratorStreamPool.WarmUp(i);
|
||||
// Don't take this from the pool as quick uploads of data to the accelerator consumes much of GPU's memory and throws.
|
||||
AcceleratorStream stream = accelerator.CreateStream();
|
||||
// TODO: Review which tables are needed when final execution path is defined.
|
||||
LastDigitGpuTables.WarmUp(i, stream);
|
||||
HeuristicGpuCombinedTables.WarmUp(i, stream);
|
||||
// SmallPrimeFactorGpuTables.WarmUp(i, stream);
|
||||
// SharedHeuristicGpuTables.EnsureStaticTables(accelerator, stream);
|
||||
// _ = GpuKernelPool.GetOrAddKernels(accelerator, stream, KernelType.None);
|
||||
// KernelContainer kernels = GpuKernelPool.GetOrAddKernels(accelerator, stream);
|
||||
@@ -110,6 +112,7 @@ public sealed class PrimeOrderCalculatorAccelerator
|
||||
public readonly MemoryBuffer1D<ulong, Stride1D.Dense> SpecialMaxResult;
|
||||
|
||||
public readonly HeuristicGpuCombinedDivisorTables DivisorTables;
|
||||
public readonly SmallPrimeFactorGpuTables SmallPrimeFactorTables;
|
||||
|
||||
public readonly Kernel SmallPrimeSieveKernel;
|
||||
public readonly Kernel CheckFactorsKernel;
|
||||
@@ -118,6 +121,8 @@ public sealed class PrimeOrderCalculatorAccelerator
|
||||
public readonly Kernel KeepMontgomeryKernel;
|
||||
public readonly Kernel PollardRhoKernel;
|
||||
public readonly Kernel Pow2ModWideKernel;
|
||||
public readonly Kernel SmallPrimeFactorKernel;
|
||||
public readonly Kernel SpecialMaxKernel;
|
||||
|
||||
public Kernel SharesFactorKernel;
|
||||
|
||||
@@ -203,6 +208,7 @@ public sealed class PrimeOrderCalculatorAccelerator
|
||||
|
||||
var heuristicSharedTables = HeuristicGpuCombinedTables.GetStaticTables(acceleratorIndex);
|
||||
DivisorTables = heuristicSharedTables.CreateHeuristicDivisorTables();
|
||||
SmallPrimeFactorTables = SmallPrimeFactorGpuTables.GetStaticTables(acceleratorIndex);
|
||||
|
||||
var checkFactorsKernel = accelerator.LoadStreamKernel<int, ulong, ArrayView1D<KeyValuePair<ulong, int>, Stride1D.Dense>, MontgomeryDivisorData, ArrayView1D<ulong, Stride1D.Dense>>(PrimeOrderGpuHeuristics.CheckFactorsKernel);
|
||||
CheckFactorsKernel = KernelUtil.GetKernel(checkFactorsKernel);
|
||||
@@ -223,6 +229,10 @@ public sealed class PrimeOrderCalculatorAccelerator
|
||||
accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView1D<GpuUInt128, Stride1D.Dense>, GpuUInt128, ArrayView1D<GpuUInt128, Stride1D.Dense>>(PrimeOrderGpuHeuristics.Pow2ModKernelWide));
|
||||
|
||||
SharesFactorKernel = KernelUtil.GetKernel(accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<ulong>, ArrayView<byte>>(PrimeTesterKernels.SharesFactorKernel));
|
||||
|
||||
SpecialMaxKernel = KernelUtil.GetKernel(accelerator.LoadAutoGroupedStreamKernel<Index1D, ulong, ArrayView1D<ulong, Stride1D.Dense>, int, MontgomeryDivisorData, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>>(PrimeOrderGpuHeuristics.EvaluateSpecialMaxCandidatesKernel));
|
||||
|
||||
SmallPrimeFactorKernel = KernelUtil.GetKernel(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));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -10,7 +10,5 @@ public enum KernelType
|
||||
OrderKernelScan,
|
||||
IncrementalKernelScan,
|
||||
IncrementalOrderKernelScan,
|
||||
Pow2ModOrderKernelScan,
|
||||
EvaluateSpecialMaxCandidatesKernel,
|
||||
SmallPrimeFactorKernelScan
|
||||
Pow2ModOrderKernelScan
|
||||
}
|
||||
@@ -502,8 +502,6 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester : IMersenneNumberDiv
|
||||
|
||||
public sealed class DivisorScanSession : IMersenneNumberDivisorByDivisorTester.IDivisorScanSession
|
||||
{
|
||||
private static readonly Accelerator[] _accelerators = AcceleratorPool.Shared.Accelerators;
|
||||
|
||||
private readonly MersenneNumberDivisorByDivisorGpuTester _owner;
|
||||
private readonly PrimeOrderCalculatorAccelerator _primeTesterAccelerator;
|
||||
private readonly Accelerator _accelerator;
|
||||
|
||||
@@ -33,9 +33,6 @@ internal static partial class PrimeOrderGpuHeuristics
|
||||
[ThreadStatic]
|
||||
private static Kernel[]? _orderKernel;
|
||||
|
||||
[ThreadStatic]
|
||||
private static SmallPrimeDeviceCache[]? _smallPrimeDeviceCache;
|
||||
|
||||
public readonly struct OrderKernelConfig(ulong previousOrder, byte hasPreviousOrder, uint smallFactorLimit, int maxPowChecks, int mode)
|
||||
{
|
||||
public readonly ulong PreviousOrder = previousOrder;
|
||||
@@ -69,13 +66,6 @@ internal static partial class PrimeOrderGpuHeuristics
|
||||
public readonly ArrayView1D<byte, Stride1D.Dense> Status = status;
|
||||
}
|
||||
|
||||
private sealed class SmallPrimeDeviceCache
|
||||
{
|
||||
public MemoryBuffer1D<uint, Stride1D.Dense>? Primes;
|
||||
public MemoryBuffer1D<ulong, Stride1D.Dense>? Squares;
|
||||
public int Count;
|
||||
}
|
||||
|
||||
private const int WideStackThreshold = 12;
|
||||
private static PrimeOrderGpuCapability s_capability = PrimeOrderGpuCapability.Default;
|
||||
|
||||
@@ -100,36 +90,6 @@ internal static partial class PrimeOrderGpuHeuristics
|
||||
s_capability = PrimeOrderGpuCapability.Default;
|
||||
}
|
||||
|
||||
private static SmallPrimeDeviceCache GetSmallPrimeDeviceCache(int acceleratorIndex, AcceleratorStream stream)
|
||||
{
|
||||
var pool = _smallPrimeDeviceCache ??= [];
|
||||
if (pool[acceleratorIndex] is {} cached)
|
||||
{
|
||||
return cached;
|
||||
}
|
||||
|
||||
uint[] primes = PrimesGenerator.SmallPrimes;
|
||||
ulong[] squares = PrimesGenerator.SmallPrimesPow2;
|
||||
MemoryBuffer1D<uint, Stride1D.Dense>? primeBuffer;
|
||||
MemoryBuffer1D<ulong, Stride1D.Dense>? squareBuffer;
|
||||
|
||||
var accelerator = _accelerators[acceleratorIndex];
|
||||
primeBuffer = accelerator.Allocate1D<uint>(primes.Length);
|
||||
squareBuffer = accelerator.Allocate1D<ulong>(squares.Length);
|
||||
|
||||
primeBuffer.View.CopyFromCPU(stream, primes);
|
||||
squareBuffer.View.CopyFromCPU(stream, squares);
|
||||
cached = new SmallPrimeDeviceCache
|
||||
{
|
||||
Primes = primeBuffer,
|
||||
Squares = squareBuffer,
|
||||
Count = primes.Length,
|
||||
};
|
||||
|
||||
pool[acceleratorIndex] = cached;
|
||||
return cached;
|
||||
}
|
||||
|
||||
private static Kernel GetPartialFactorKernel(int acceleratorIndex)
|
||||
{
|
||||
var pool = _partialFactorKernel ??= [];
|
||||
@@ -212,7 +172,7 @@ internal static partial class PrimeOrderGpuHeuristics
|
||||
Accelerator accelerator = _accelerators[acceleratorIndex];
|
||||
AcceleratorStream stream = accelerator.CreateStream();
|
||||
|
||||
SmallPrimeDeviceCache cache = GetSmallPrimeDeviceCache(acceleratorIndex, stream);
|
||||
SmallPrimeFactorGpuTables cache = gpu.SmallPrimeFactorTables;
|
||||
|
||||
// TODO: We should create / reallocate these buffer only once or if the new required length is bigger than capacity.
|
||||
MemoryBuffer1D<ulong, Stride1D.Dense>? factorBuffer = accelerator.Allocate1D<ulong>(primeTargets.Length);
|
||||
@@ -363,6 +323,7 @@ internal static partial class PrimeOrderGpuHeuristics
|
||||
}
|
||||
|
||||
internal static bool TryCalculateOrder(
|
||||
PrimeOrderCalculatorAccelerator gpu,
|
||||
ulong prime,
|
||||
ulong? previousOrder,
|
||||
PrimeOrderCalculator.PrimeOrderSearchConfig config,
|
||||
@@ -375,7 +336,7 @@ internal static partial class PrimeOrderGpuHeuristics
|
||||
int acceleratorIndex = AcceleratorPool.Shared.Rent();
|
||||
Accelerator accelerator = _accelerators[acceleratorIndex];
|
||||
AcceleratorStream stream = AcceleratorStreamPool.Rent(acceleratorIndex);
|
||||
SmallPrimeDeviceCache cache = GetSmallPrimeDeviceCache(acceleratorIndex, stream);
|
||||
SmallPrimeFactorGpuTables cache = gpu.SmallPrimeFactorTables;
|
||||
|
||||
// TODO: These buffers should be created reallocated once and assigned to an accelerator. Callers should use their own
|
||||
// thread static cache to prevent other threads from using taking accelerators with pre-allocated buffers if they don't
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using ILGPU;
|
||||
using ILGPU.Runtime;
|
||||
|
||||
namespace PerfectNumbers.Core.Gpu;
|
||||
|
||||
public readonly struct SmallPrimeFactorGpuTables
|
||||
{
|
||||
private static readonly Accelerator[] _accelerators = AcceleratorPool.Shared.Accelerators;
|
||||
private static readonly SmallPrimeFactorGpuTables[] _sharedTables = new SmallPrimeFactorGpuTables[PerfectNumberConstants.RollingAccelerators];
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal static SmallPrimeFactorGpuTables GetStaticTables(int acceleratorIndex) => _sharedTables[acceleratorIndex];
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal static void WarmUp(int acceleratorIndex, AcceleratorStream stream) => _sharedTables[acceleratorIndex] = new SmallPrimeFactorGpuTables(_accelerators[acceleratorIndex], stream);
|
||||
|
||||
public readonly MemoryBuffer1D<uint, Stride1D.Dense> Primes;
|
||||
public readonly MemoryBuffer1D<ulong, Stride1D.Dense> Squares;
|
||||
public readonly int Count;
|
||||
|
||||
public SmallPrimeFactorGpuTables(Accelerator accelerator, AcceleratorStream stream)
|
||||
{
|
||||
var hostPrimes = PrimesGenerator.SmallPrimes;
|
||||
var hostSquares = PrimesGenerator.SmallPrimesPow2;
|
||||
|
||||
MemoryBuffer1D<uint, Stride1D.Dense>? devicePrimes = accelerator.Allocate1D(stream, hostPrimes);
|
||||
MemoryBuffer1D<ulong, Stride1D.Dense>? deviceSquares = accelerator.Allocate1D(stream, hostSquares);
|
||||
|
||||
Primes = devicePrimes;
|
||||
Squares = deviceSquares;
|
||||
Count = hostPrimes.Length;
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using ILGPU;
|
||||
using ILGPU.Runtime;
|
||||
|
||||
namespace PerfectNumbers.Core.Gpu;
|
||||
|
||||
public readonly struct SmallPrimeFactorViews(
|
||||
MemoryBuffer1D<uint, Stride1D.Dense> primes,
|
||||
MemoryBuffer1D<ulong, Stride1D.Dense> squares)
|
||||
{
|
||||
public readonly MemoryBuffer1D<uint, Stride1D.Dense> Primes = primes;
|
||||
public readonly MemoryBuffer1D<ulong, Stride1D.Dense> Squares = squares;
|
||||
public readonly int Count = (int)primes.Length;
|
||||
|
||||
public ArrayView1D<uint, Stride1D.Dense> PrimesView
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
return Primes.View;
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayView1D<ulong, Stride1D.Dense> SquaresView
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
return Squares.View;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user