diff --git a/PerfectNumbers.Core/Gpu/GpuKernelPool.cs b/PerfectNumbers.Core/Gpu/GpuKernelPool.cs index b00ab89..a1ddcbe 100644 --- a/PerfectNumbers.Core/Gpu/GpuKernelPool.cs +++ b/PerfectNumbers.Core/Gpu/GpuKernelPool.cs @@ -5,97 +5,6 @@ using static PerfectNumbers.Core.Gpu.GpuContextPool; namespace PerfectNumbers.Core.Gpu; -public readonly struct ResidueAutomatonArgs -{ - public readonly ulong Q0M10; - public readonly ulong Step10; - public readonly ulong Q0M8; - public readonly ulong Step8; - public readonly ulong Q0M3; - public readonly ulong Step3; - public readonly ulong Q0M5; - public readonly ulong Step5; - - public ResidueAutomatonArgs(ulong q0m10, ulong step10, ulong q0m8, ulong step8, ulong q0m3, ulong step3, ulong q0m5, ulong step5) - { - Q0M10 = q0m10; Step10 = step10; Q0M8 = q0m8; Step8 = step8; Q0M3 = q0m3; Step3 = step3; Q0M5 = q0m5; Step5 = step5; - } -} - - -public sealed class KernelContainer : IDisposable -{ - // Serializes first-time initialization of kernels/buffers per accelerator. - public Action, ArrayView>? Order; - public Action, ArrayView1D>? Incremental; - public Action, ArrayView1D, - ArrayView1D, ArrayView1D, - ArrayView1D, ArrayView1D>? Pow2Mod; - public Action, ArrayView1D>? IncrementalOrder; - public Action, ArrayView1D>? Pow2ModOrder; - public Action, ArrayView1D, int, ArrayView1D, ArrayView1D, ArrayView1D, ArrayView1D>? SmallPrimeFactor; - public Action, int, MontgomeryDivisorData, ArrayView1D, ArrayView1D>? SpecialMax; - - // Optional device buffer with small divisor cycles (<= 4M). Index = divisor, value = cycle length. - public MemoryBuffer1D? SmallCycles; - - public MemoryBuffer1D? SmallPrimesLastOne; - public MemoryBuffer1D? SmallPrimesLastSeven; - public MemoryBuffer1D? SmallPrimesPow2LastOne; - public MemoryBuffer1D? SmallPrimesPow2LastSeven; - - public MemoryBuffer1D? SmallPrimeFactorsPrimes; - public MemoryBuffer1D? SmallPrimeFactorsSquares; - - public void Dispose() - { - // Order = null; - // Incremental = null; - // Pow2Mod = null; - // IncrementalOrder = null; - // Pow2ModOrder = null; - // SmallPrimeFactor = null; - // SpecialMax = null; - - // SmallCycles?.Dispose(); - // SmallPrimesLastOne?.Dispose(); - // SmallPrimesLastSeven?.Dispose(); - // SmallPrimesPow2LastOne?.Dispose(); - // SmallPrimesPow2LastSeven?.Dispose(); - // SmallPrimeFactorsPrimes?.Dispose(); - // SmallPrimeFactorsSquares?.Dispose(); - } -} - -public readonly struct ResiduePrimeViews( - ArrayView1D lastOne, - ArrayView1D lastSeven, - ArrayView1D lastOnePow2, - ArrayView1D lastSevenPow2) -{ - public readonly ArrayView1D LastOne = lastOne; - public readonly ArrayView1D LastSeven = lastSeven; - public readonly ArrayView1D LastOnePow2 = lastOnePow2; - public readonly ArrayView1D LastSevenPow2 = lastSevenPow2; -} - -public readonly struct SmallPrimeFactorViews( - MemoryBuffer1D primes, - MemoryBuffer1D squares, - int count) -{ - public readonly MemoryBuffer1D Primes = primes; - public readonly MemoryBuffer1D Squares = squares; - public readonly int Count = count; - - public ArrayView1D PrimesView => Primes.View; - public ArrayView1D SquaresView => Squares.View; -} - public class GpuKernelPool { private static readonly ConcurrentDictionary _kernels = new(); diff --git a/PerfectNumbers.Core/Gpu/KernelContainer.cs b/PerfectNumbers.Core/Gpu/KernelContainer.cs new file mode 100644 index 0000000..6feda20 --- /dev/null +++ b/PerfectNumbers.Core/Gpu/KernelContainer.cs @@ -0,0 +1,52 @@ +using ILGPU; +using ILGPU.Runtime; + +namespace PerfectNumbers.Core.Gpu; + +public sealed class KernelContainer +{ + // Serializes first-time initialization of kernels/buffers per accelerator. + public Action, ArrayView>? Order; + public Action, ArrayView1D>? Incremental; + public Action, ArrayView1D, + ArrayView1D, ArrayView1D, + ArrayView1D, ArrayView1D>? Pow2Mod; + public Action, ArrayView1D>? IncrementalOrder; + public Action, ArrayView1D>? Pow2ModOrder; + public Action, ArrayView1D, int, ArrayView1D, ArrayView1D, ArrayView1D, ArrayView1D>? SmallPrimeFactor; + public Action, int, MontgomeryDivisorData, ArrayView1D, ArrayView1D>? SpecialMax; + + // Optional device buffer with small divisor cycles (<= 4M). Index = divisor, value = cycle length. + public MemoryBuffer1D? SmallCycles; + + public MemoryBuffer1D? SmallPrimesLastOne; + public MemoryBuffer1D? SmallPrimesLastSeven; + public MemoryBuffer1D? SmallPrimesPow2LastOne; + public MemoryBuffer1D? SmallPrimesPow2LastSeven; + + public MemoryBuffer1D? SmallPrimeFactorsPrimes; + public MemoryBuffer1D? SmallPrimeFactorsSquares; + + public void Dispose() + { + // Order = null; + // Incremental = null; + // Pow2Mod = null; + // IncrementalOrder = null; + // Pow2ModOrder = null; + // SmallPrimeFactor = null; + // SpecialMax = null; + + // SmallCycles?.Dispose(); + // SmallPrimesLastOne?.Dispose(); + // SmallPrimesLastSeven?.Dispose(); + // SmallPrimesPow2LastOne?.Dispose(); + // SmallPrimesPow2LastSeven?.Dispose(); + // SmallPrimeFactorsPrimes?.Dispose(); + // SmallPrimeFactorsSquares?.Dispose(); + } +} diff --git a/PerfectNumbers.Core/Gpu/ResidueAutomatonArgs.cs b/PerfectNumbers.Core/Gpu/ResidueAutomatonArgs.cs new file mode 100644 index 0000000..c4feff8 --- /dev/null +++ b/PerfectNumbers.Core/Gpu/ResidueAutomatonArgs.cs @@ -0,0 +1,15 @@ +namespace PerfectNumbers.Core.Gpu; + +public readonly struct ResidueAutomatonArgs( + ulong q0m10, ulong step10, ulong q0m8, ulong step8, ulong q0m3, ulong step3, ulong q0m5, ulong step5 +) +{ + public readonly ulong Q0M10 = q0m10; + public readonly ulong Step10 = step10; + public readonly ulong Q0M8 = q0m8; + public readonly ulong Step8 = step8; + public readonly ulong Q0M3 = q0m3; + public readonly ulong Step3 = step3; + public readonly ulong Q0M5 = q0m5; + public readonly ulong Step5 = step5; +} diff --git a/PerfectNumbers.Core/Gpu/ResiduePrimeViews.cs b/PerfectNumbers.Core/Gpu/ResiduePrimeViews.cs new file mode 100644 index 0000000..3bf5bcc --- /dev/null +++ b/PerfectNumbers.Core/Gpu/ResiduePrimeViews.cs @@ -0,0 +1,16 @@ +using ILGPU; +using ILGPU.Runtime; + +namespace PerfectNumbers.Core.Gpu; + +public readonly struct ResiduePrimeViews( + ArrayView1D lastOne, + ArrayView1D lastSeven, + ArrayView1D lastOnePow2, + ArrayView1D lastSevenPow2) +{ + public readonly ArrayView1D LastOne = lastOne; + public readonly ArrayView1D LastSeven = lastSeven; + public readonly ArrayView1D LastOnePow2 = lastOnePow2; + public readonly ArrayView1D LastSevenPow2 = lastSevenPow2; +} diff --git a/PerfectNumbers.Core/Gpu/SmallPrimeFactorViews.cs b/PerfectNumbers.Core/Gpu/SmallPrimeFactorViews.cs new file mode 100644 index 0000000..0fb1765 --- /dev/null +++ b/PerfectNumbers.Core/Gpu/SmallPrimeFactorViews.cs @@ -0,0 +1,33 @@ +using System.Runtime.CompilerServices; +using ILGPU; +using ILGPU.Runtime; + +namespace PerfectNumbers.Core.Gpu; + +public readonly struct SmallPrimeFactorViews( + MemoryBuffer1D primes, + MemoryBuffer1D squares, + int count) +{ + public readonly MemoryBuffer1D Primes = primes; + public readonly MemoryBuffer1D Squares = squares; + public readonly int Count = count; + + public ArrayView1D PrimesView + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + return Primes.View; + } + } + + public ArrayView1D SquaresView + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + return Squares.View; + } + } +}