Kernels made part of the accelerators for better performance.

This commit is contained in:
mlemanczyk committed 2025-11-13 18:52:46 +01:00
1 parent 0d2a88c45f
commit be435b7760
5 files changed
+29 -53

No files matched your search

@@ -104,22 +104,7 @@ internal sealed class HeuristicPrimeTesterAccelerator
return cached;
}
[ThreadStatic]
public static Dictionary<Accelerator, Action<AcceleratorStream, Index1D, ArrayView<int>, ulong, ulong, HeuristicGpuDivisorTableKind, HeuristicGpuDivisorTables>>? HeuristicTrialDivisionKernel;
internal static Action<AcceleratorStream, Index1D, ArrayView<int>, ulong, ulong, HeuristicGpuDivisorTableKind, HeuristicGpuDivisorTables> GetHeuristicTrialDivisionKernel(Accelerator accelerator)
{
var pool = HeuristicTrialDivisionKernel ??= [];
if (pool.TryGetValue(accelerator, out var cached))
{
return cached;
}
cached = KernelUtil.GetKernel(accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<int>, ulong, ulong, HeuristicGpuDivisorTableKind, HeuristicGpuDivisorTables>(PrimeTesterKernels.HeuristicTrialDivisionKernel)).CreateLauncherDelegate<Action<AcceleratorStream, Index1D, ArrayView<int>, ulong, ulong, HeuristicGpuDivisorTableKind, HeuristicGpuDivisorTables>>();
pool[accelerator] = cached;
return cached;
}
public readonly Kernel HeuristicTrialDivisionKernel;
public MemoryBuffer1D<ulong, Stride1D.Dense> Input = null!;
public MemoryBuffer1D<byte, Stride1D.Dense> Output = null!;
@@ -152,17 +137,19 @@ internal sealed class HeuristicPrimeTesterAccelerator
Context = accelerator.Context;
Accelerator = accelerator;
AcceleratorStream stream = accelerator.CreateStream();
// var kernels = _kernels[index]; //GpuKernelPool.Kernels;
EnsureCapacity(minBufferCapacity);
HeuristicTrialDivisionKernel = KernelUtil.GetKernel(accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<int>, ulong, ulong, HeuristicGpuDivisorTableKind, HeuristicGpuDivisorTables>(PrimeTesterKernels.HeuristicTrialDivisionKernel));
AcceleratorStream stream = AcceleratorStreamPool.Rent(accelerator);
// GpuStaticTableInitializer.EnsureStaticTables(accelerator, kernels, stream);
_sharedTables = Accelerators.HeuristicGpuTables.EnsureStaticTables(accelerator, stream);
var heuristicDivisorTables = _sharedTables.CreateHeuristicDivisorTables();
_heuristicDivisorTables = heuristicDivisorTables;
_heuristicDivisorTables = _sharedTables.CreateHeuristicDivisorTables();
EnsureCapacity(minBufferCapacity);
stream.Synchronize();
stream.Dispose();
AcceleratorStreamPool.Return(stream);
}
public void EnsureCapacity(int minCapacity)
@@ -172,16 +159,13 @@ internal sealed class HeuristicPrimeTesterAccelerator
return;
}
Input?.Dispose();
Output?.Dispose();
Input.Dispose();
Output.Dispose();
HeuristicFlag?.Dispose();
// lock (Accelerator)
{
Input = Accelerator.Allocate1D<ulong>(minCapacity);
Output = Accelerator.Allocate1D<byte>(minCapacity);
HeuristicFlag = Accelerator.Allocate1D<int>(minCapacity);
}
Input = Accelerator.Allocate1D<ulong>(minCapacity);
Output = Accelerator.Allocate1D<byte>(minCapacity);
HeuristicFlag = Accelerator.Allocate1D<int>(minCapacity);
BufferCapacity = minCapacity;
}
@@ -110,6 +110,8 @@ public sealed class PrimeOrderCalculatorAccelerator
public readonly Kernel ConvertToStandardKernel;
public readonly Kernel KeepMontgomeryKernel;
public readonly Kernel PollardRhoKernel;
public Kernel SharesFactorKernel;
public void EnsureCapacity(int factorsCount, int primeTesterCapacity)
{
@@ -199,6 +201,8 @@ public sealed class PrimeOrderCalculatorAccelerator
SmallPrimeSieveKernel = KernelUtil.GetKernel(accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<ulong>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<byte>>(PrimeTesterKernels.SmallPrimeSieveKernel));
PollardRhoKernel = KernelUtil.GetKernel(accelerator.LoadStreamKernel<ulong, int, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<byte, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>>(Pow2MontgomeryKernels.TryPollardRhoKernel));
SharesFactorKernel = KernelUtil.GetKernel(accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<ulong>, ArrayView<byte>>(PrimeTesterKernels.SharesFactorKernel)); ;
}
public void Dispose()