Stream handling simplified to avoid dictionary lookups in favor of array lookups with known indexes.

This commit is contained in:
mlemanczyk committed 2025-11-16 15:01:51 +01:00
1 parent cd7636ddbe
commit a84445ef2a
15 files changed
+67 -73

No files matched your search

@@ -7,8 +7,7 @@ public static class AcceleratorStreamPool
{
private static readonly Accelerator[] _accelerators = AcceleratorPool.Shared.Accelerators;
private static readonly SemaphoreSlim[] _locks = new SemaphoreSlim[PerfectNumberConstants.RollingAccelerators];
private static readonly ConcurrentQueue<AcceleratorStream>[] _streams = new ConcurrentQueue<AcceleratorStream> [PerfectNumberConstants.RollingAccelerators];
private static readonly AcceleratorStream[] _streams = new AcceleratorStream[PerfectNumberConstants.RollingAccelerators];
private static SemaphoreSlim CreateLock() => new(PerfectNumberConstants.ThreadsByAccelerator);
@@ -17,26 +16,27 @@ public static class AcceleratorStreamPool
public static AcceleratorStream Rent(int acceleratorIndex)
{
var streamLock = _locks[acceleratorIndex];
var queue = _streams[acceleratorIndex];
streamLock.Wait();
var queue = _streams[acceleratorIndex];
if (queue != null)
{
return queue;
}
return queue.TryDequeue(out var stream)
? stream
: _accelerators[acceleratorIndex].CreateStream();
queue = _accelerators[acceleratorIndex].CreateStream();
_streams[acceleratorIndex] = queue;
return queue;
}
public static void Return(int acceleratorIndex, AcceleratorStream stream)
public static void Return(int acceleratorIndex)
{
var streamLock = _locks[acceleratorIndex];
var queue = _streams[acceleratorIndex];
queue.Enqueue(stream);
streamLock.Release();
}
public static void WarmUp(int acceleratorIndex)
{
_streams[acceleratorIndex] = new();
_locks[acceleratorIndex] = CreateLock();
}
}
@@ -45,25 +45,25 @@ internal sealed class HeuristicGpuTables
HeuristicGroupBDivisorsEnding9 = CopyUintSpanToDevice(accelerator, stream, DivisorGenerator.SmallPrimesLastNineWithoutLastThree);
HeuristicGroupBDivisorSquaresEnding9 = CopySpanToDevice(accelerator, stream, DivisorGenerator.SmallPrimesPow2LastNineWithoutLastThree);
var combinedEnding1 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding1;
HeuristicCombinedDivisorsEnding1 = CopySpanToDevice(accelerator, stream, combinedEnding1);
var combinedSquaresEnding1 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding1Squares;
HeuristicCombinedDivisorSquaresEnding1 = CopySpanToDevice(accelerator, stream, combinedSquaresEnding1);
// var combinedEnding1 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding1;
// HeuristicCombinedDivisorsEnding1 = CopySpanToDevice(accelerator, stream, combinedEnding1);
// var combinedSquaresEnding1 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding1Squares;
// HeuristicCombinedDivisorSquaresEnding1 = CopySpanToDevice(accelerator, stream, combinedSquaresEnding1);
var combinedEnding3 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding3;
HeuristicCombinedDivisorsEnding3 = CopySpanToDevice(accelerator, stream, combinedEnding3);
var combinedSquaresEnding3 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding3Squares;
HeuristicCombinedDivisorSquaresEnding3 = CopySpanToDevice(accelerator, stream, combinedSquaresEnding3);
// var combinedEnding3 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding3;
// HeuristicCombinedDivisorsEnding3 = CopySpanToDevice(accelerator, stream, combinedEnding3);
// var combinedSquaresEnding3 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding3Squares;
// HeuristicCombinedDivisorSquaresEnding3 = CopySpanToDevice(accelerator, stream, combinedSquaresEnding3);
var combinedEnding7 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding7;
HeuristicCombinedDivisorsEnding7 = CopySpanToDevice(accelerator, stream, combinedEnding7);
var combinedSquaresEnding7 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding7Squares;
HeuristicCombinedDivisorSquaresEnding7 = CopySpanToDevice(accelerator, stream, combinedSquaresEnding7);
// var combinedEnding7 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding7;
// HeuristicCombinedDivisorsEnding7 = CopySpanToDevice(accelerator, stream, combinedEnding7);
// var combinedSquaresEnding7 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding7Squares;
// HeuristicCombinedDivisorSquaresEnding7 = CopySpanToDevice(accelerator, stream, combinedSquaresEnding7);
var combinedEnding9 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding9;
HeuristicCombinedDivisorsEnding9 = CopySpanToDevice(accelerator, stream, combinedEnding9);
var combinedSquaresEnding9 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding9Squares;
HeuristicCombinedDivisorSquaresEnding9 = CopySpanToDevice(accelerator, stream, combinedSquaresEnding9);
// var combinedEnding9 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding9;
// HeuristicCombinedDivisorsEnding9 = CopySpanToDevice(accelerator, stream, combinedEnding9);
// var combinedSquaresEnding9 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding9Squares;
// HeuristicCombinedDivisorSquaresEnding9 = CopySpanToDevice(accelerator, stream, combinedSquaresEnding9);
}
internal readonly MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicGroupADivisors;
@@ -136,7 +136,7 @@ internal sealed class HeuristicPrimeTesterAccelerator
Accelerator = accelerator;
EnsureCapacity(minBufferCapacity);
HeuristicTrialDivisionKernel = KernelUtil.GetKernel(accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<int>, ulong, ulong, HeuristicGpuDivisorTableKind, HeuristicGpuDivisorTables>(PrimeTesterKernels.HeuristicTrialDivisionKernel));
HeuristicTrialDivisionKernel = KernelUtil.GetKernel(accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<int>, ulong, ulong, ArrayView1D<ulong, Stride1D.Dense>, ArrayView1D<ulong, Stride1D.Dense>>(PrimeTesterKernels.HeuristicTrialDivisionKernel));
AcceleratorStream stream = AcceleratorStreamPool.Rent(acceleratorIndex);
@@ -146,7 +146,7 @@ internal sealed class HeuristicPrimeTesterAccelerator
_heuristicDivisorTables = _sharedTables.CreateHeuristicDivisorTables();
stream.Synchronize();
AcceleratorStreamPool.Return(acceleratorIndex, stream);
AcceleratorStreamPool.Return(acceleratorIndex);
}
public void EnsureCapacity(int minCapacity)
+1 -1
View File
@@ -25,7 +25,7 @@ public class GpuKernelPool
internal static KernelContainer GetOrAddKernels(int acceleratorIndex, AcceleratorStream stream, KernelType kernelType)
{
var pool = _pool ??= [];
var pool = _pool ??= new KernelContainer[PerfectNumberConstants.RollingAccelerators];
if (pool[acceleratorIndex] is not {} kernels)
{
kernels = new();
@@ -83,20 +83,10 @@ internal static class PrimeTesterKernels
ArrayView<int> resultFlag,
ulong n,
ulong maxDivisorSquare,
HeuristicGpuDivisorTableKind tableKind,
HeuristicGpuDivisorTables tables)
ArrayView1D<ulong, Stride1D.Dense> divisors,
ArrayView1D<ulong, Stride1D.Dense> divisorSquares)
{
byte nMod10 = (byte)(n % 10UL);
ArrayView1D<ulong, Stride1D.Dense> divisors = tables.SelectDivisors(tableKind, nMod10);
ArrayView1D<ulong, Stride1D.Dense> divisorSquares = tables.SelectDivisorSquares(tableKind, nMod10);
int divisorLength = (int)divisorSquares.Length;
int threadIndex = index;
if (threadIndex >= divisorLength)
{
return;
}
ulong divisorSquare = divisorSquares[threadIndex];
if (divisorSquare > maxDivisorSquare)
{
@@ -32,7 +32,7 @@ public static class Pow2MontgomeryGpuCalculator
resultBuffer.View.CopyToCPU(stream, ref result, 1);
stream.Synchronize();
AcceleratorStreamPool.Return(acceleratorIndex, stream);
AcceleratorStreamPool.Return(acceleratorIndex);
// GpuPrimeWorkLimiter.Release();
PrimeOrderCalculatorAccelerator.Return(gpu);
@@ -60,7 +60,7 @@ public static class Pow2MontgomeryGpuCalculator
resultBuffer.View.CopyToCPU(stream, ref result, 1);
stream.Synchronize();
AcceleratorStreamPool.Return(acceleratorIndex, stream);
AcceleratorStreamPool.Return(acceleratorIndex);
// GpuPrimeWorkLimiter.Release();
PrimeOrderCalculatorAccelerator.Return(gpu);
@@ -86,7 +86,7 @@ public static class Pow2MontgomeryGpuCalculator
resultBuffer.View.CopyToCPU(stream, ref result, 1);
stream.Synchronize();
AcceleratorStreamPool.Return(acceleratorIndex, stream);
AcceleratorStreamPool.Return(acceleratorIndex);
return result;
}
@@ -102,7 +102,7 @@ internal static partial class PrimeOrderGpuHeuristics
private static SmallPrimeDeviceCache GetSmallPrimeDeviceCache(int acceleratorIndex, AcceleratorStream stream)
{
var pool = _smallPrimeDeviceCache ??= [];
var pool = _smallPrimeDeviceCache ??= new SmallPrimeDeviceCache[PerfectNumberConstants.RollingAccelerators];
if (pool[acceleratorIndex] is {} cached)
{
return cached;
@@ -132,7 +132,7 @@ internal static partial class PrimeOrderGpuHeuristics
private static Kernel GetPartialFactorKernel(int acceleratorIndex)
{
var pool = _partialFactorKernel ??= [];
var pool = _partialFactorKernel ??= new Kernel[PerfectNumberConstants.RollingAccelerators];
if (pool[acceleratorIndex] is {} cached)
{
return cached;
@@ -487,7 +487,7 @@ internal static partial class PrimeOrderGpuHeuristics
private static Kernel GetOrderKernel(int acceleratorIndex)
{
var pool = _orderKernel ??= [];
var pool = _orderKernel ??= new Kernel[PerfectNumberConstants.RollingAccelerators];
if (pool[acceleratorIndex] is {} cached)
{
return cached;
@@ -637,7 +637,7 @@ internal static partial class PrimeOrderGpuHeuristics
remainderBuffer.View.CopyToCPU(stream, ref MemoryMarshal.GetReference(resultSpan), length);
stream.Synchronize();
AcceleratorStreamPool.Return(acceleratorIndex, stream);
AcceleratorStreamPool.Return(acceleratorIndex);
for (int i = 0; i < length; i++)
{
results[i] = (UInt128)resultSpan[i];
@@ -675,7 +675,7 @@ internal static partial class PrimeOrderGpuHeuristics
private static Kernel GetPow2ModKernel(int acceleratorIndex)
{
var pool = _pow2ModKernel ??= [];
var pool = _pow2ModKernel ??= new Kernel[PerfectNumberConstants.RollingAccelerators];
if (pool[acceleratorIndex] is {} cached)
{
return cached;