Warm GPU resources before thread startup
This commit is contained in:
1 parent
e14b77e504
commit
6864b316d5
9 files changed
+822
-403
No files matched your search
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using ILGPU;
|
||||
using ILGPU.Runtime;
|
||||
@@ -47,6 +48,8 @@ public static class GpuContextPool
|
||||
|
||||
private static readonly ConcurrentQueue<PooledContext> CpuPool = new();
|
||||
private static readonly ConcurrentQueue<PooledContext> GpuPool = new();
|
||||
private static readonly object WarmUpLock = new();
|
||||
private static int WarmedGpuContextCount;
|
||||
|
||||
public static GpuContextLease Rent()
|
||||
{
|
||||
@@ -73,6 +76,48 @@ public static class GpuContextPool
|
||||
return new GpuContextLease(new PooledContext(preferCpu));
|
||||
}
|
||||
|
||||
public static void WarmUpPool(int threadCount)
|
||||
{
|
||||
if (!PoolingEnabled || ForceCpu)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (threadCount <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int target = threadCount / 4;
|
||||
if (target == 0)
|
||||
{
|
||||
target = 1;
|
||||
}
|
||||
|
||||
lock (WarmUpLock)
|
||||
{
|
||||
if (target <= WarmedGpuContextCount)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int toCreate = target - WarmedGpuContextCount;
|
||||
var contexts = new PooledContext[toCreate];
|
||||
for (int i = 0; i < toCreate; i++)
|
||||
{
|
||||
contexts[i] = new PooledContext(preferCpu: false);
|
||||
}
|
||||
|
||||
for (int i = 0; i < toCreate; i++)
|
||||
{
|
||||
GpuPool.Enqueue(contexts[i]);
|
||||
}
|
||||
|
||||
WarmedGpuContextCount = target;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void DisposeAll()
|
||||
{
|
||||
if (!PoolingEnabled)
|
||||
@@ -91,6 +136,7 @@ public static class GpuContextPool
|
||||
}
|
||||
|
||||
PrimeTester.DisposeGpuContexts();
|
||||
WarmedGpuContextCount = 0;
|
||||
}
|
||||
|
||||
private static void Return(PooledContext ctx)
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using ILGPU;
|
||||
using ILGPU.Runtime;
|
||||
|
||||
namespace PerfectNumbers.Core.Gpu;
|
||||
|
||||
internal readonly struct HeuristicGpuDivisorTables
|
||||
{
|
||||
public static readonly HeuristicGpuDivisorTables Shared = default;
|
||||
|
||||
public readonly ArrayView1D<ulong, Stride1D.Dense> CombinedDivisorsEnding1;
|
||||
public readonly ArrayView1D<ulong, Stride1D.Dense> CombinedDivisorsEnding3;
|
||||
public readonly ArrayView1D<ulong, Stride1D.Dense> CombinedDivisorsEnding7;
|
||||
public readonly ArrayView1D<ulong, Stride1D.Dense> CombinedDivisorsEnding9;
|
||||
public readonly ArrayView1D<ulong, Stride1D.Dense> CombinedDivisorSquaresEnding1;
|
||||
public readonly ArrayView1D<ulong, Stride1D.Dense> CombinedDivisorSquaresEnding3;
|
||||
public readonly ArrayView1D<ulong, Stride1D.Dense> CombinedDivisorSquaresEnding7;
|
||||
public readonly ArrayView1D<ulong, Stride1D.Dense> CombinedDivisorSquaresEnding9;
|
||||
public readonly ArrayView1D<ulong, Stride1D.Dense> GroupADivisors;
|
||||
public readonly ArrayView1D<ulong, Stride1D.Dense> GroupADivisorSquares;
|
||||
public readonly ArrayView1D<ulong, Stride1D.Dense> GroupBDivisorsEnding1;
|
||||
public readonly ArrayView1D<ulong, Stride1D.Dense> GroupBDivisorSquaresEnding1;
|
||||
public readonly ArrayView1D<ulong, Stride1D.Dense> GroupBDivisorsEnding7;
|
||||
public readonly ArrayView1D<ulong, Stride1D.Dense> GroupBDivisorSquaresEnding7;
|
||||
public readonly ArrayView1D<ulong, Stride1D.Dense> GroupBDivisorsEnding9;
|
||||
public readonly ArrayView1D<ulong, Stride1D.Dense> GroupBDivisorSquaresEnding9;
|
||||
|
||||
public HeuristicGpuDivisorTables(
|
||||
ArrayView1D<ulong, Stride1D.Dense> combinedDivisorsEnding1,
|
||||
ArrayView1D<ulong, Stride1D.Dense> combinedDivisorsEnding3,
|
||||
ArrayView1D<ulong, Stride1D.Dense> combinedDivisorsEnding7,
|
||||
ArrayView1D<ulong, Stride1D.Dense> combinedDivisorsEnding9,
|
||||
ArrayView1D<ulong, Stride1D.Dense> combinedDivisorSquaresEnding1,
|
||||
ArrayView1D<ulong, Stride1D.Dense> combinedDivisorSquaresEnding3,
|
||||
ArrayView1D<ulong, Stride1D.Dense> combinedDivisorSquaresEnding7,
|
||||
ArrayView1D<ulong, Stride1D.Dense> combinedDivisorSquaresEnding9,
|
||||
ArrayView1D<ulong, Stride1D.Dense> groupADivisors,
|
||||
ArrayView1D<ulong, Stride1D.Dense> groupADivisorSquares,
|
||||
ArrayView1D<ulong, Stride1D.Dense> groupBDivisorsEnding1,
|
||||
ArrayView1D<ulong, Stride1D.Dense> groupBDivisorSquaresEnding1,
|
||||
ArrayView1D<ulong, Stride1D.Dense> groupBDivisorsEnding7,
|
||||
ArrayView1D<ulong, Stride1D.Dense> groupBDivisorSquaresEnding7,
|
||||
ArrayView1D<ulong, Stride1D.Dense> groupBDivisorsEnding9,
|
||||
ArrayView1D<ulong, Stride1D.Dense> groupBDivisorSquaresEnding9)
|
||||
{
|
||||
CombinedDivisorsEnding1 = combinedDivisorsEnding1;
|
||||
CombinedDivisorsEnding3 = combinedDivisorsEnding3;
|
||||
CombinedDivisorsEnding7 = combinedDivisorsEnding7;
|
||||
CombinedDivisorsEnding9 = combinedDivisorsEnding9;
|
||||
CombinedDivisorSquaresEnding1 = combinedDivisorSquaresEnding1;
|
||||
CombinedDivisorSquaresEnding3 = combinedDivisorSquaresEnding3;
|
||||
CombinedDivisorSquaresEnding7 = combinedDivisorSquaresEnding7;
|
||||
CombinedDivisorSquaresEnding9 = combinedDivisorSquaresEnding9;
|
||||
GroupADivisors = groupADivisors;
|
||||
GroupADivisorSquares = groupADivisorSquares;
|
||||
GroupBDivisorsEnding1 = groupBDivisorsEnding1;
|
||||
GroupBDivisorSquaresEnding1 = groupBDivisorSquaresEnding1;
|
||||
GroupBDivisorsEnding7 = groupBDivisorsEnding7;
|
||||
GroupBDivisorSquaresEnding7 = groupBDivisorSquaresEnding7;
|
||||
GroupBDivisorsEnding9 = groupBDivisorsEnding9;
|
||||
GroupBDivisorSquaresEnding9 = groupBDivisorSquaresEnding9;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ArrayView1D<ulong, Stride1D.Dense> SelectCombined(byte ending)
|
||||
{
|
||||
return ending switch
|
||||
{
|
||||
1 => CombinedDivisorsEnding1,
|
||||
3 => CombinedDivisorsEnding3,
|
||||
7 => CombinedDivisorsEnding7,
|
||||
9 => CombinedDivisorsEnding9,
|
||||
_ => ArrayView1D<ulong, Stride1D.Dense>.Empty,
|
||||
};
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ArrayView1D<ulong, Stride1D.Dense> SelectCombinedSquares(byte ending)
|
||||
{
|
||||
return ending switch
|
||||
{
|
||||
1 => CombinedDivisorSquaresEnding1,
|
||||
3 => CombinedDivisorSquaresEnding3,
|
||||
7 => CombinedDivisorSquaresEnding7,
|
||||
9 => CombinedDivisorSquaresEnding9,
|
||||
_ => ArrayView1D<ulong, Stride1D.Dense>.Empty,
|
||||
};
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ArrayView1D<ulong, Stride1D.Dense> SelectGroupB(byte ending)
|
||||
{
|
||||
return ending switch
|
||||
{
|
||||
1 => GroupBDivisorsEnding1,
|
||||
7 => GroupBDivisorsEnding7,
|
||||
9 => GroupBDivisorsEnding9,
|
||||
_ => ArrayView1D<ulong, Stride1D.Dense>.Empty,
|
||||
};
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ArrayView1D<ulong, Stride1D.Dense> SelectGroupBSquares(byte ending)
|
||||
{
|
||||
return ending switch
|
||||
{
|
||||
1 => GroupBDivisorSquaresEnding1,
|
||||
7 => GroupBDivisorSquaresEnding7,
|
||||
9 => GroupBDivisorSquaresEnding9,
|
||||
_ => ArrayView1D<ulong, Stride1D.Dense>.Empty,
|
||||
};
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ArrayView1D<ulong, Stride1D.Dense> SelectDivisors(HeuristicGpuDivisorTableKind kind, byte ending)
|
||||
{
|
||||
return kind switch
|
||||
{
|
||||
HeuristicGpuDivisorTableKind.GroupA => GroupADivisors,
|
||||
HeuristicGpuDivisorTableKind.GroupBEnding1 => GroupBDivisorsEnding1,
|
||||
HeuristicGpuDivisorTableKind.GroupBEnding7 => GroupBDivisorsEnding7,
|
||||
HeuristicGpuDivisorTableKind.GroupBEnding9 => GroupBDivisorsEnding9,
|
||||
HeuristicGpuDivisorTableKind.Combined => SelectCombined(ending),
|
||||
_ => ArrayView1D<ulong, Stride1D.Dense>.Empty,
|
||||
};
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ArrayView1D<ulong, Stride1D.Dense> SelectDivisorSquares(HeuristicGpuDivisorTableKind kind, byte ending)
|
||||
{
|
||||
return kind switch
|
||||
{
|
||||
HeuristicGpuDivisorTableKind.GroupA => GroupADivisorSquares,
|
||||
HeuristicGpuDivisorTableKind.GroupBEnding1 => GroupBDivisorSquaresEnding1,
|
||||
HeuristicGpuDivisorTableKind.GroupBEnding7 => GroupBDivisorSquaresEnding7,
|
||||
HeuristicGpuDivisorTableKind.GroupBEnding9 => GroupBDivisorSquaresEnding9,
|
||||
HeuristicGpuDivisorTableKind.Combined => SelectCombinedSquares(ending),
|
||||
_ => ArrayView1D<ulong, Stride1D.Dense>.Empty,
|
||||
};
|
||||
}
|
||||
|
||||
internal static void InitializeShared(in HeuristicGpuDivisorTables tables)
|
||||
{
|
||||
Unsafe.AsRef(in Shared) = tables;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,19 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using ILGPU;
|
||||
using ILGPU.Algorithms;
|
||||
using ILGPU.Runtime;
|
||||
|
||||
namespace PerfectNumbers.Core.Gpu;
|
||||
|
||||
internal enum HeuristicGpuDivisorTableKind : byte
|
||||
{
|
||||
GroupA = 0,
|
||||
GroupBEnding1 = 1,
|
||||
GroupBEnding7 = 7,
|
||||
GroupBEnding9 = 9,
|
||||
Combined = 255,
|
||||
}
|
||||
|
||||
internal static class PrimeTesterKernels
|
||||
{
|
||||
// GPU kernel: small-prime sieve only. Returns 1 if passes sieve (probable prime), 0 otherwise.
|
||||
@@ -51,13 +61,18 @@ internal static class PrimeTesterKernels
|
||||
int length = (int)primes.Length;
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
ulong prime = primes[i];
|
||||
ulong primeSquare = primeSquares[i];
|
||||
if (primeSquare > n)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
ulong prime = primes[i];
|
||||
if (prime == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (n % prime == 0UL)
|
||||
{
|
||||
result = 0;
|
||||
@@ -68,19 +83,46 @@ internal static class PrimeTesterKernels
|
||||
results[index] = result;
|
||||
}
|
||||
|
||||
public static void HeuristicTrialDivisionKernel(Index1D index, ArrayView<ulong> divisors, ulong n, ArrayView<int> resultFlag)
|
||||
public static void HeuristicTrialDivisionKernel(
|
||||
Index1D index,
|
||||
ArrayView<int> resultFlag,
|
||||
ulong n,
|
||||
ulong maxDivisorSquare,
|
||||
HeuristicGpuDivisorTableKind tableKind,
|
||||
HeuristicGpuDivisorTables tables)
|
||||
{
|
||||
ulong divisor = divisors[index];
|
||||
if (divisor <= 1UL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
byte nMod10 = (byte)(n % 10UL);
|
||||
ArrayView1D<ulong, Stride1D.Dense> divisors = tables.SelectDivisors(tableKind, nMod10);
|
||||
ArrayView1D<ulong, Stride1D.Dense> divisorSquares = tables.SelectDivisorSquares(tableKind, nMod10);
|
||||
if (divisors.Length == 0 || divisorSquares.Length == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ulong residue = n % divisor;
|
||||
if (residue == 0UL)
|
||||
{
|
||||
resultFlag[0] = 1;
|
||||
}
|
||||
int divisorLength = (int)divisors.Length;
|
||||
int squareLength = (int)divisorSquares.Length;
|
||||
int threadIndex = index;
|
||||
if (threadIndex >= divisorLength || threadIndex >= squareLength)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ulong divisorSquare = divisorSquares[threadIndex];
|
||||
if (divisorSquare == 0UL || divisorSquare > maxDivisorSquare)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ulong divisor = divisors[threadIndex];
|
||||
if (divisor == 0UL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (n % divisor == 0UL)
|
||||
{
|
||||
resultFlag[0] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
public static void SharesFactorKernel(Index1D index, ArrayView<ulong> numbers, ArrayView<byte> results)
|
||||
|
||||
Reference in new issue
Block a user