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
@@ -55,12 +55,15 @@ internal static class Program
|
|||||||
ulong remainder = currentP % 6UL;
|
ulong remainder = currentP % 6UL;
|
||||||
bool startPrimeProvided = _cliArguments.StartPrimeProvided;
|
bool startPrimeProvided = _cliArguments.StartPrimeProvided;
|
||||||
int threadCount = Math.Max(1, _cliArguments.ThreadCount);
|
int threadCount = Math.Max(1, _cliArguments.ThreadCount);
|
||||||
|
int gpuPrimeBatch = Math.Max(1, _cliArguments.GpuPrimeBatch);
|
||||||
UnboundedTaskScheduler.ConfigureThreadCount(threadCount);
|
UnboundedTaskScheduler.ConfigureThreadCount(threadCount);
|
||||||
|
PrimeTester.GpuBatchSize = gpuPrimeBatch;
|
||||||
|
GpuContextPool.WarmUpPool(threadCount);
|
||||||
|
PrimeTester.WarmUpGpuKernels(threadCount);
|
||||||
Console.WriteLine("Starting up threads...");
|
Console.WriteLine("Starting up threads...");
|
||||||
_ = UnboundedTaskScheduler.Instance;
|
_ = UnboundedTaskScheduler.Instance;
|
||||||
int blockSize = Math.Max(1, _cliArguments.BlockSize);
|
int blockSize = Math.Max(1, _cliArguments.BlockSize);
|
||||||
int gpuPrimeThreads = Math.Max(1, _cliArguments.GpuPrimeThreads);
|
int gpuPrimeThreads = Math.Max(1, _cliArguments.GpuPrimeThreads);
|
||||||
int gpuPrimeBatch = Math.Max(1, _cliArguments.GpuPrimeBatch);
|
|
||||||
GpuKernelType kernelType = _cliArguments.KernelType;
|
GpuKernelType kernelType = _cliArguments.KernelType;
|
||||||
bool useBitTransform = _cliArguments.UseBitTransform;
|
bool useBitTransform = _cliArguments.UseBitTransform;
|
||||||
bool useOrder = _cliArguments.UseOrder;
|
bool useOrder = _cliArguments.UseOrder;
|
||||||
@@ -114,7 +117,7 @@ internal static class Program
|
|||||||
|
|
||||||
// Apply GPU prime sieve runtime configuration
|
// Apply GPU prime sieve runtime configuration
|
||||||
GpuPrimeWorkLimiter.SetLimit(gpuPrimeThreads);
|
GpuPrimeWorkLimiter.SetLimit(gpuPrimeThreads);
|
||||||
PrimeTester.GpuBatchSize = Math.Max(1, gpuPrimeBatch);
|
PrimeTester.GpuBatchSize = gpuPrimeBatch;
|
||||||
|
|
||||||
MersenneDivisorCycles mersenneDivisorCycles = new();
|
MersenneDivisorCycles mersenneDivisorCycles = new();
|
||||||
if (!File.Exists(cyclesPath) || continueCyclesGeneration)
|
if (!File.Exists(cyclesPath) || continueCyclesGeneration)
|
||||||
|
|||||||
@@ -62,28 +62,28 @@ public class HeuristicCombinedPrimeTesterTests
|
|||||||
[InlineData((byte)9)]
|
[InlineData((byte)9)]
|
||||||
public void One_a_one_b_pattern_interleaves_group_a_and_group_b_values(byte lastDigit)
|
public void One_a_one_b_pattern_interleaves_group_a_and_group_b_values(byte lastDigit)
|
||||||
{
|
{
|
||||||
ReadOnlySpan<uint> combined = HeuristicCombinedPrimeTester.GetCombinedDivisors(
|
ReadOnlySpan<ulong> combined = HeuristicCombinedPrimeTester.GetCombinedDivisors(
|
||||||
lastDigit,
|
lastDigit,
|
||||||
HeuristicCombinedPrimeTester.CombinedDivisorPattern.OneAOneB);
|
HeuristicCombinedPrimeTester.CombinedDivisorPattern.OneAOneB);
|
||||||
|
|
||||||
combined.Length.Should().BeGreaterThan(18);
|
combined.Length.Should().BeGreaterThan(18);
|
||||||
combined[0].Should().Be(3U);
|
combined[0].Should().Be(3UL);
|
||||||
combined[1].Should().Be(7U);
|
combined[1].Should().Be(7UL);
|
||||||
combined[2].Should().Be(11U);
|
combined[2].Should().Be(11UL);
|
||||||
combined[3].Should().Be(13U);
|
combined[3].Should().Be(13UL);
|
||||||
|
|
||||||
int index = 4;
|
int index = 4;
|
||||||
const int segmentsToCheck = 6;
|
const int segmentsToCheck = 6;
|
||||||
|
|
||||||
for (int segment = 0; segment < segmentsToCheck; segment++)
|
for (int segment = 0; segment < segmentsToCheck; segment++)
|
||||||
{
|
{
|
||||||
uint groupAValue = combined[index++];
|
ulong groupAValue = combined[index++];
|
||||||
groupAValue.Should().BeGreaterThan(13U);
|
groupAValue.Should().BeGreaterThan(13UL);
|
||||||
(groupAValue % 10U).Should().Be(3U);
|
(groupAValue % 10UL).Should().Be(3UL);
|
||||||
|
|
||||||
uint groupBValue = combined[index++];
|
ulong groupBValue = combined[index++];
|
||||||
groupBValue.Should().BeGreaterThan(13U);
|
groupBValue.Should().BeGreaterThan(13UL);
|
||||||
(groupBValue % 10U).Should().NotBe(3U);
|
(groupBValue % 10UL).Should().NotBe(3UL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,15 +94,15 @@ public class HeuristicCombinedPrimeTesterTests
|
|||||||
[InlineData((byte)9)]
|
[InlineData((byte)9)]
|
||||||
public void Two_a_one_b_pattern_interleaves_two_group_a_values_before_group_b(byte lastDigit)
|
public void Two_a_one_b_pattern_interleaves_two_group_a_values_before_group_b(byte lastDigit)
|
||||||
{
|
{
|
||||||
ReadOnlySpan<uint> combined = HeuristicCombinedPrimeTester.GetCombinedDivisors(
|
ReadOnlySpan<ulong> combined = HeuristicCombinedPrimeTester.GetCombinedDivisors(
|
||||||
lastDigit,
|
lastDigit,
|
||||||
HeuristicCombinedPrimeTester.CombinedDivisorPattern.TwoAOneB);
|
HeuristicCombinedPrimeTester.CombinedDivisorPattern.TwoAOneB);
|
||||||
|
|
||||||
combined.Length.Should().BeGreaterThan(20);
|
combined.Length.Should().BeGreaterThan(20);
|
||||||
combined[0].Should().Be(3U);
|
combined[0].Should().Be(3UL);
|
||||||
combined[1].Should().Be(7U);
|
combined[1].Should().Be(7UL);
|
||||||
combined[2].Should().Be(11U);
|
combined[2].Should().Be(11UL);
|
||||||
combined[3].Should().Be(13U);
|
combined[3].Should().Be(13UL);
|
||||||
|
|
||||||
int index = 4;
|
int index = 4;
|
||||||
const int segmentsToCheck = 5;
|
const int segmentsToCheck = 5;
|
||||||
@@ -111,14 +111,14 @@ public class HeuristicCombinedPrimeTesterTests
|
|||||||
{
|
{
|
||||||
for (int i = 0; i < 2; i++)
|
for (int i = 0; i < 2; i++)
|
||||||
{
|
{
|
||||||
uint groupAValue = combined[index++];
|
ulong groupAValue = combined[index++];
|
||||||
groupAValue.Should().BeGreaterThan(13U);
|
groupAValue.Should().BeGreaterThan(13UL);
|
||||||
(groupAValue % 10U).Should().Be(3U);
|
(groupAValue % 10UL).Should().Be(3UL);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint groupBValue = combined[index++];
|
ulong groupBValue = combined[index++];
|
||||||
groupBValue.Should().BeGreaterThan(13U);
|
groupBValue.Should().BeGreaterThan(13UL);
|
||||||
(groupBValue % 10U).Should().NotBe(3U);
|
(groupBValue % 10UL).Should().NotBe(3UL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,15 +129,15 @@ public class HeuristicCombinedPrimeTesterTests
|
|||||||
[InlineData((byte)9)]
|
[InlineData((byte)9)]
|
||||||
public void Three_a_two_b_pattern_interleaves_three_group_a_values_before_two_group_b_values(byte lastDigit)
|
public void Three_a_two_b_pattern_interleaves_three_group_a_values_before_two_group_b_values(byte lastDigit)
|
||||||
{
|
{
|
||||||
ReadOnlySpan<uint> combined = HeuristicCombinedPrimeTester.GetCombinedDivisors(
|
ReadOnlySpan<ulong> combined = HeuristicCombinedPrimeTester.GetCombinedDivisors(
|
||||||
lastDigit,
|
lastDigit,
|
||||||
HeuristicCombinedPrimeTester.CombinedDivisorPattern.ThreeATwoB);
|
HeuristicCombinedPrimeTester.CombinedDivisorPattern.ThreeATwoB);
|
||||||
|
|
||||||
combined.Length.Should().BeGreaterThan(28);
|
combined.Length.Should().BeGreaterThan(28);
|
||||||
combined[0].Should().Be(3U);
|
combined[0].Should().Be(3UL);
|
||||||
combined[1].Should().Be(7U);
|
combined[1].Should().Be(7UL);
|
||||||
combined[2].Should().Be(11U);
|
combined[2].Should().Be(11UL);
|
||||||
combined[3].Should().Be(13U);
|
combined[3].Should().Be(13UL);
|
||||||
|
|
||||||
int index = 4;
|
int index = 4;
|
||||||
const int segmentsToCheck = 4;
|
const int segmentsToCheck = 4;
|
||||||
@@ -146,16 +146,16 @@ public class HeuristicCombinedPrimeTesterTests
|
|||||||
{
|
{
|
||||||
for (int i = 0; i < 3; i++)
|
for (int i = 0; i < 3; i++)
|
||||||
{
|
{
|
||||||
uint groupAValue = combined[index++];
|
ulong groupAValue = combined[index++];
|
||||||
groupAValue.Should().BeGreaterThan(13U);
|
groupAValue.Should().BeGreaterThan(13UL);
|
||||||
(groupAValue % 10U).Should().Be(3U);
|
(groupAValue % 10UL).Should().Be(3UL);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < 2; i++)
|
for (int i = 0; i < 2; i++)
|
||||||
{
|
{
|
||||||
uint groupBValue = combined[index++];
|
ulong groupBValue = combined[index++];
|
||||||
groupBValue.Should().BeGreaterThan(13U);
|
groupBValue.Should().BeGreaterThan(13UL);
|
||||||
(groupBValue % 10U).Should().NotBe(3U);
|
(groupBValue % 10UL).Should().NotBe(3UL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -167,15 +167,15 @@ public class HeuristicCombinedPrimeTesterTests
|
|||||||
[InlineData((byte)9)]
|
[InlineData((byte)9)]
|
||||||
public void Three_a_one_b_pattern_interleaves_three_group_a_values_before_group_b(byte lastDigit)
|
public void Three_a_one_b_pattern_interleaves_three_group_a_values_before_group_b(byte lastDigit)
|
||||||
{
|
{
|
||||||
ReadOnlySpan<uint> combined = HeuristicCombinedPrimeTester.GetCombinedDivisors(
|
ReadOnlySpan<ulong> combined = HeuristicCombinedPrimeTester.GetCombinedDivisors(
|
||||||
lastDigit,
|
lastDigit,
|
||||||
HeuristicCombinedPrimeTester.CombinedDivisorPattern.ThreeAOneB);
|
HeuristicCombinedPrimeTester.CombinedDivisorPattern.ThreeAOneB);
|
||||||
|
|
||||||
combined.Length.Should().BeGreaterThan(24);
|
combined.Length.Should().BeGreaterThan(24);
|
||||||
combined[0].Should().Be(3U);
|
combined[0].Should().Be(3UL);
|
||||||
combined[1].Should().Be(7U);
|
combined[1].Should().Be(7UL);
|
||||||
combined[2].Should().Be(11U);
|
combined[2].Should().Be(11UL);
|
||||||
combined[3].Should().Be(13U);
|
combined[3].Should().Be(13UL);
|
||||||
|
|
||||||
int index = 4;
|
int index = 4;
|
||||||
const int segmentsToCheck = 4;
|
const int segmentsToCheck = 4;
|
||||||
@@ -184,14 +184,14 @@ public class HeuristicCombinedPrimeTesterTests
|
|||||||
{
|
{
|
||||||
for (int i = 0; i < 3; i++)
|
for (int i = 0; i < 3; i++)
|
||||||
{
|
{
|
||||||
uint groupAValue = combined[index++];
|
ulong groupAValue = combined[index++];
|
||||||
groupAValue.Should().BeGreaterThan(13U);
|
groupAValue.Should().BeGreaterThan(13UL);
|
||||||
(groupAValue % 10U).Should().Be(3U);
|
(groupAValue % 10UL).Should().Be(3UL);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint groupBValue = combined[index++];
|
ulong groupBValue = combined[index++];
|
||||||
groupBValue.Should().BeGreaterThan(13U);
|
groupBValue.Should().BeGreaterThan(13UL);
|
||||||
(groupBValue % 10U).Should().NotBe(3U);
|
(groupBValue % 10UL).Should().NotBe(3UL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using System;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using ILGPU;
|
using ILGPU;
|
||||||
using ILGPU.Runtime;
|
using ILGPU.Runtime;
|
||||||
@@ -47,6 +48,8 @@ public static class GpuContextPool
|
|||||||
|
|
||||||
private static readonly ConcurrentQueue<PooledContext> CpuPool = new();
|
private static readonly ConcurrentQueue<PooledContext> CpuPool = new();
|
||||||
private static readonly ConcurrentQueue<PooledContext> GpuPool = new();
|
private static readonly ConcurrentQueue<PooledContext> GpuPool = new();
|
||||||
|
private static readonly object WarmUpLock = new();
|
||||||
|
private static int WarmedGpuContextCount;
|
||||||
|
|
||||||
public static GpuContextLease Rent()
|
public static GpuContextLease Rent()
|
||||||
{
|
{
|
||||||
@@ -73,6 +76,48 @@ public static class GpuContextPool
|
|||||||
return new GpuContextLease(new PooledContext(preferCpu));
|
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()
|
public static void DisposeAll()
|
||||||
{
|
{
|
||||||
if (!PoolingEnabled)
|
if (!PoolingEnabled)
|
||||||
@@ -91,6 +136,7 @@ public static class GpuContextPool
|
|||||||
}
|
}
|
||||||
|
|
||||||
PrimeTester.DisposeGpuContexts();
|
PrimeTester.DisposeGpuContexts();
|
||||||
|
WarmedGpuContextCount = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void Return(PooledContext ctx)
|
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 System.Runtime.CompilerServices;
|
||||||
using ILGPU;
|
using ILGPU;
|
||||||
using ILGPU.Algorithms;
|
using ILGPU.Algorithms;
|
||||||
|
using ILGPU.Runtime;
|
||||||
|
|
||||||
namespace PerfectNumbers.Core.Gpu;
|
namespace PerfectNumbers.Core.Gpu;
|
||||||
|
|
||||||
|
internal enum HeuristicGpuDivisorTableKind : byte
|
||||||
|
{
|
||||||
|
GroupA = 0,
|
||||||
|
GroupBEnding1 = 1,
|
||||||
|
GroupBEnding7 = 7,
|
||||||
|
GroupBEnding9 = 9,
|
||||||
|
Combined = 255,
|
||||||
|
}
|
||||||
|
|
||||||
internal static class PrimeTesterKernels
|
internal static class PrimeTesterKernels
|
||||||
{
|
{
|
||||||
// GPU kernel: small-prime sieve only. Returns 1 if passes sieve (probable prime), 0 otherwise.
|
// 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;
|
int length = (int)primes.Length;
|
||||||
for (int i = 0; i < length; i++)
|
for (int i = 0; i < length; i++)
|
||||||
{
|
{
|
||||||
ulong prime = primes[i];
|
|
||||||
ulong primeSquare = primeSquares[i];
|
ulong primeSquare = primeSquares[i];
|
||||||
if (primeSquare > n)
|
if (primeSquare > n)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ulong prime = primes[i];
|
||||||
|
if (prime == 0)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (n % prime == 0UL)
|
if (n % prime == 0UL)
|
||||||
{
|
{
|
||||||
result = 0;
|
result = 0;
|
||||||
@@ -68,19 +83,46 @@ internal static class PrimeTesterKernels
|
|||||||
results[index] = result;
|
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];
|
byte nMod10 = (byte)(n % 10UL);
|
||||||
if (divisor <= 1UL)
|
ArrayView1D<ulong, Stride1D.Dense> divisors = tables.SelectDivisors(tableKind, nMod10);
|
||||||
{
|
ArrayView1D<ulong, Stride1D.Dense> divisorSquares = tables.SelectDivisorSquares(tableKind, nMod10);
|
||||||
return;
|
if (divisors.Length == 0 || divisorSquares.Length == 0)
|
||||||
}
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
ulong residue = n % divisor;
|
int divisorLength = (int)divisors.Length;
|
||||||
if (residue == 0UL)
|
int squareLength = (int)divisorSquares.Length;
|
||||||
{
|
int threadIndex = index;
|
||||||
resultFlag[0] = 1;
|
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)
|
public static void SharesFactorKernel(Index1D index, ArrayView<ulong> numbers, ArrayView<byte> results)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using Open.Numeric.Primes;
|
using Open.Numeric.Primes;
|
||||||
using System.Runtime.CompilerServices;
|
using System;
|
||||||
using ILGPU.Runtime;
|
using ILGPU.Runtime;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
using PerfectNumbers.Core.Gpu;
|
using PerfectNumbers.Core.Gpu;
|
||||||
|
|
||||||
namespace PerfectNumbers.Core;
|
namespace PerfectNumbers.Core;
|
||||||
@@ -40,32 +41,48 @@ public sealed class HeuristicCombinedPrimeTester
|
|||||||
CombinedDivisorsEnding9Squares = CombinedDivisorsEnding9OneAOneBSquares;
|
CombinedDivisorsEnding9Squares = CombinedDivisorsEnding9OneAOneBSquares;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void EnsureInitialized()
|
||||||
|
{
|
||||||
|
// Intentionally left blank. Accessing this method forces the static constructor to run.
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static ReadOnlySpan<ulong> CombinedDivisorsEnding1Span => CombinedDivisorsEnding1;
|
||||||
|
internal static ReadOnlySpan<ulong> CombinedDivisorsEnding3Span => CombinedDivisorsEnding3;
|
||||||
|
internal static ReadOnlySpan<ulong> CombinedDivisorsEnding7Span => CombinedDivisorsEnding7;
|
||||||
|
internal static ReadOnlySpan<ulong> CombinedDivisorsEnding9Span => CombinedDivisorsEnding9;
|
||||||
|
|
||||||
|
internal static ReadOnlySpan<ulong> CombinedDivisorsEnding1SquaresSpan => CombinedDivisorsEnding1Squares;
|
||||||
|
internal static ReadOnlySpan<ulong> CombinedDivisorsEnding3SquaresSpan => CombinedDivisorsEnding3Squares;
|
||||||
|
internal static ReadOnlySpan<ulong> CombinedDivisorsEnding7SquaresSpan => CombinedDivisorsEnding7Squares;
|
||||||
|
internal static ReadOnlySpan<ulong> CombinedDivisorsEnding9SquaresSpan => CombinedDivisorsEnding9Squares;
|
||||||
|
|
||||||
private const ulong Wheel210 = 210UL;
|
private const ulong Wheel210 = 210UL;
|
||||||
|
|
||||||
private static readonly uint[] CombinedDivisorsEnding1TwoAOneB;
|
private static readonly ulong[] CombinedDivisorsEnding1TwoAOneB;
|
||||||
private static readonly uint[] CombinedDivisorsEnding3TwoAOneB;
|
private static readonly ulong[] CombinedDivisorsEnding3TwoAOneB;
|
||||||
private static readonly uint[] CombinedDivisorsEnding7TwoAOneB;
|
private static readonly ulong[] CombinedDivisorsEnding7TwoAOneB;
|
||||||
private static readonly uint[] CombinedDivisorsEnding9TwoAOneB;
|
private static readonly ulong[] CombinedDivisorsEnding9TwoAOneB;
|
||||||
|
|
||||||
private static readonly uint[] CombinedDivisorsEnding1OneAOneB;
|
private static readonly ulong[] CombinedDivisorsEnding1OneAOneB;
|
||||||
private static readonly uint[] CombinedDivisorsEnding3OneAOneB;
|
private static readonly ulong[] CombinedDivisorsEnding3OneAOneB;
|
||||||
private static readonly uint[] CombinedDivisorsEnding7OneAOneB;
|
private static readonly ulong[] CombinedDivisorsEnding7OneAOneB;
|
||||||
private static readonly uint[] CombinedDivisorsEnding9OneAOneB;
|
private static readonly ulong[] CombinedDivisorsEnding9OneAOneB;
|
||||||
|
|
||||||
private static readonly uint[] CombinedDivisorsEnding1ThreeAOneB;
|
private static readonly ulong[] CombinedDivisorsEnding1ThreeAOneB;
|
||||||
private static readonly uint[] CombinedDivisorsEnding3ThreeAOneB;
|
private static readonly ulong[] CombinedDivisorsEnding3ThreeAOneB;
|
||||||
private static readonly uint[] CombinedDivisorsEnding7ThreeAOneB;
|
private static readonly ulong[] CombinedDivisorsEnding7ThreeAOneB;
|
||||||
private static readonly uint[] CombinedDivisorsEnding9ThreeAOneB;
|
private static readonly ulong[] CombinedDivisorsEnding9ThreeAOneB;
|
||||||
|
|
||||||
private static readonly uint[] CombinedDivisorsEnding1ThreeATwoB;
|
private static readonly ulong[] CombinedDivisorsEnding1ThreeATwoB;
|
||||||
private static readonly uint[] CombinedDivisorsEnding3ThreeATwoB;
|
private static readonly ulong[] CombinedDivisorsEnding3ThreeATwoB;
|
||||||
private static readonly uint[] CombinedDivisorsEnding7ThreeATwoB;
|
private static readonly ulong[] CombinedDivisorsEnding7ThreeATwoB;
|
||||||
private static readonly uint[] CombinedDivisorsEnding9ThreeATwoB;
|
private static readonly ulong[] CombinedDivisorsEnding9ThreeATwoB;
|
||||||
|
|
||||||
private static readonly uint[] CombinedDivisorsEnding1;
|
private static readonly ulong[] CombinedDivisorsEnding1;
|
||||||
private static readonly uint[] CombinedDivisorsEnding3;
|
private static readonly ulong[] CombinedDivisorsEnding3;
|
||||||
private static readonly uint[] CombinedDivisorsEnding7;
|
private static readonly ulong[] CombinedDivisorsEnding7;
|
||||||
private static readonly uint[] CombinedDivisorsEnding9;
|
private static readonly ulong[] CombinedDivisorsEnding9;
|
||||||
|
|
||||||
private static readonly ulong[] CombinedDivisorsEnding1TwoAOneBSquares;
|
private static readonly ulong[] CombinedDivisorsEnding1TwoAOneBSquares;
|
||||||
private static readonly ulong[] CombinedDivisorsEnding3TwoAOneBSquares;
|
private static readonly ulong[] CombinedDivisorsEnding3TwoAOneBSquares;
|
||||||
@@ -173,7 +190,7 @@ public sealed class HeuristicCombinedPrimeTester
|
|||||||
|
|
||||||
private static bool HeuristicTrialDivisionCpu(ulong n, ulong maxDivisorSquare, byte nMod10)
|
private static bool HeuristicTrialDivisionCpu(ulong n, ulong maxDivisorSquare, byte nMod10)
|
||||||
{
|
{
|
||||||
ReadOnlySpan<uint> combinedDivisors = GetCombinedDivisors(nMod10);
|
ReadOnlySpan<ulong> combinedDivisors = GetCombinedDivisors(nMod10);
|
||||||
ReadOnlySpan<ulong> combinedDivisorSquares = GetCombinedDivisorSquares(nMod10);
|
ReadOnlySpan<ulong> combinedDivisorSquares = GetCombinedDivisorSquares(nMod10);
|
||||||
|
|
||||||
int length = combinedDivisors.Length;
|
int length = combinedDivisors.Length;
|
||||||
@@ -205,78 +222,43 @@ public sealed class HeuristicCombinedPrimeTester
|
|||||||
_ => 256,
|
_ => 256,
|
||||||
};
|
};
|
||||||
|
|
||||||
private static bool HeuristicTrialDivisionGpuDetectsDivisor(ulong n, ulong maxDivisorSquare, byte nMod10)
|
|
||||||
{
|
private static bool HeuristicTrialDivisionGpuDetectsDivisor(ulong n, ulong maxDivisorSquare, byte nMod10)
|
||||||
int batchCapacity = GetBatchSize(n);
|
{
|
||||||
|
var limiter = GpuPrimeWorkLimiter.Acquire();
|
||||||
|
var gpu = PrimeTester.PrimeTesterGpuContextPool.Rent(1);
|
||||||
|
var accelerator = gpu.Accelerator;
|
||||||
|
var kernel = gpu.HeuristicTrialDivisionKernel;
|
||||||
|
var flagView1D = gpu.HeuristicFlag.View;
|
||||||
|
var flagView = flagView1D.AsContiguous();
|
||||||
|
|
||||||
|
bool compositeDetected = false;
|
||||||
|
int divisorLength = GetCombinedDivisors(nMod10).Length;
|
||||||
|
|
||||||
|
if (divisorLength > 0)
|
||||||
|
{
|
||||||
|
int compositeFlag = 0;
|
||||||
|
flagView1D.CopyFromCPU(ref compositeFlag, 1);
|
||||||
|
kernel(
|
||||||
|
divisorLength,
|
||||||
|
flagView,
|
||||||
|
n,
|
||||||
|
maxDivisorSquare,
|
||||||
|
HeuristicGpuDivisorTableKind.Combined,
|
||||||
|
gpu.HeuristicGpuTables);
|
||||||
|
accelerator.Synchronize();
|
||||||
|
flagView1D.CopyToCPU(ref compositeFlag, 1);
|
||||||
|
compositeDetected = compositeFlag != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
gpu.Dispose();
|
||||||
|
limiter.Dispose();
|
||||||
|
return compositeDetected;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
var limiter = GpuPrimeWorkLimiter.Acquire();
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
var gpu = PrimeTester.PrimeTesterGpuContextPool.Rent(batchCapacity);
|
private static bool EvaluateWithOpenNumericFallback(ulong n)
|
||||||
var accelerator = gpu.Accelerator;
|
|
||||||
var kernel = gpu.HeuristicTrialDivisionKernel;
|
|
||||||
|
|
||||||
bool compositeDetected = false;
|
|
||||||
int count = 0;
|
|
||||||
|
|
||||||
var inputView = gpu.Input.View;
|
|
||||||
var flagView = gpu.HeuristicFlag.View;
|
|
||||||
ulong[] divisorArray = gpu.DivisorArray;
|
|
||||||
|
|
||||||
bool ProcessBatch(int length)
|
|
||||||
{
|
|
||||||
inputView.CopyFromCPU(ref divisorArray[0], length);
|
|
||||||
int compositeFlag = 0;
|
|
||||||
flagView.CopyFromCPU(ref compositeFlag, 1);
|
|
||||||
kernel(length, inputView, n, flagView);
|
|
||||||
accelerator.Synchronize();
|
|
||||||
flagView.CopyToCPU(ref compositeFlag, 1);
|
|
||||||
|
|
||||||
return compositeFlag != 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
ReadOnlySpan<uint> combinedDivisors = GetCombinedDivisors(nMod10);
|
|
||||||
ReadOnlySpan<ulong> combinedDivisorSquares = GetCombinedDivisorSquares(nMod10);
|
|
||||||
|
|
||||||
int length = combinedDivisors.Length;
|
|
||||||
|
|
||||||
for (int i = 0; i < length; i++)
|
|
||||||
{
|
|
||||||
ulong divisorSquare = combinedDivisorSquares[i];
|
|
||||||
if (divisorSquare > maxDivisorSquare)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
divisorArray[count] = combinedDivisors[i];
|
|
||||||
count++;
|
|
||||||
|
|
||||||
if (count == batchCapacity)
|
|
||||||
{
|
|
||||||
if (ProcessBatch(count))
|
|
||||||
{
|
|
||||||
compositeDetected = true;
|
|
||||||
goto Cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
count = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (count > 0 && ProcessBatch(count))
|
|
||||||
{
|
|
||||||
compositeDetected = true;
|
|
||||||
goto Cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Cleanup:
|
|
||||||
gpu.Dispose();
|
|
||||||
limiter.Dispose();
|
|
||||||
return compositeDetected;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private static bool EvaluateWithOpenNumericFallback(ulong n)
|
|
||||||
{
|
{
|
||||||
return Prime.Numbers.IsPrime(n);
|
return Prime.Numbers.IsPrime(n);
|
||||||
}
|
}
|
||||||
@@ -285,7 +267,7 @@ public sealed class HeuristicCombinedPrimeTester
|
|||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ReadOnlySpan<uint> GetCombinedDivisors(byte nMod10)
|
private static ReadOnlySpan<ulong> GetCombinedDivisors(byte nMod10)
|
||||||
{
|
{
|
||||||
return nMod10 switch
|
return nMod10 switch
|
||||||
{
|
{
|
||||||
@@ -309,7 +291,7 @@ public sealed class HeuristicCombinedPrimeTester
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static ReadOnlySpan<uint> GetCombinedDivisors(byte nMod10, CombinedDivisorPattern pattern)
|
internal static ReadOnlySpan<ulong> GetCombinedDivisors(byte nMod10, CombinedDivisorPattern pattern)
|
||||||
{
|
{
|
||||||
return pattern switch
|
return pattern switch
|
||||||
{
|
{
|
||||||
@@ -389,17 +371,17 @@ public sealed class HeuristicCombinedPrimeTester
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static uint[] BuildCombinedDivisors(byte nMod10, CombinedDivisorPattern pattern, out ulong[] squares)
|
private static ulong[] BuildCombinedDivisors(byte nMod10, CombinedDivisorPattern pattern, out ulong[] squares)
|
||||||
{
|
{
|
||||||
ReadOnlySpan<int> groupA = HeuristicPrimeSieves.GroupADivisors;
|
ReadOnlySpan<ulong> groupA = HeuristicPrimeSieves.GroupADivisors;
|
||||||
ReadOnlySpan<uint> groupB = GetGroupBDivisors(nMod10);
|
ReadOnlySpan<uint> groupB = GetGroupBDivisors(nMod10);
|
||||||
|
|
||||||
var combined = new List<uint>(groupA.Length + groupB.Length);
|
var combined = new List<ulong>(groupA.Length + groupB.Length);
|
||||||
|
|
||||||
int groupAIndex = 0;
|
int groupAIndex = 0;
|
||||||
while (groupAIndex < groupA.Length && groupA[groupAIndex] <= 19)
|
while (groupAIndex < groupA.Length && groupA[groupAIndex] <= 19)
|
||||||
{
|
{
|
||||||
combined.Add((uint)groupA[groupAIndex]);
|
combined.Add(groupA[groupAIndex]);
|
||||||
groupAIndex++;
|
groupAIndex++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -435,7 +417,7 @@ public sealed class HeuristicCombinedPrimeTester
|
|||||||
int addedA = 0;
|
int addedA = 0;
|
||||||
while (addedA < groupAInterleaveCount && groupAIndex < groupA.Length)
|
while (addedA < groupAInterleaveCount && groupAIndex < groupA.Length)
|
||||||
{
|
{
|
||||||
combined.Add((uint)groupA[groupAIndex]);
|
combined.Add(groupA[groupAIndex]);
|
||||||
groupAIndex++;
|
groupAIndex++;
|
||||||
addedA++;
|
addedA++;
|
||||||
}
|
}
|
||||||
@@ -449,7 +431,7 @@ public sealed class HeuristicCombinedPrimeTester
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint[] result = [.. combined];
|
ulong[] result = [.. combined];
|
||||||
ulong[] resultSquares = new ulong[result.Length];
|
ulong[] resultSquares = new ulong[result.Length];
|
||||||
for (int i = 0; i < result.Length; i++)
|
for (int i = 0; i < result.Length; i++)
|
||||||
{
|
{
|
||||||
@@ -474,12 +456,12 @@ public sealed class HeuristicCombinedPrimeTester
|
|||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool IsGroupAPrefixValue(uint value) => value is 3U or 7U or 11U or 13U;
|
private static bool IsGroupAPrefixValue(ulong value) => value is 3UL or 7UL or 11UL or 13UL;
|
||||||
|
|
||||||
private static HeuristicDivisorGroup ResolveGroup(uint value) => value switch
|
private static HeuristicDivisorGroup ResolveGroup(ulong value) => value switch
|
||||||
{
|
{
|
||||||
3U or 7U or 11U or 13U => HeuristicDivisorGroup.GroupAConstant,
|
3UL or 7UL or 11UL or 13UL => HeuristicDivisorGroup.GroupAConstant,
|
||||||
_ when value % 10U == 3U => HeuristicDivisorGroup.GroupAWheel,
|
_ when value % 10UL == 3UL => HeuristicDivisorGroup.GroupAWheel,
|
||||||
_ => HeuristicDivisorGroup.GroupB,
|
_ => HeuristicDivisorGroup.GroupB,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -578,7 +560,7 @@ public sealed class HeuristicCombinedPrimeTester
|
|||||||
internal ref struct HeuristicDivisorEnumerator
|
internal ref struct HeuristicDivisorEnumerator
|
||||||
{
|
{
|
||||||
private readonly ulong maxDivisorSquare;
|
private readonly ulong maxDivisorSquare;
|
||||||
private readonly ReadOnlySpan<uint> combinedDivisors;
|
private readonly ReadOnlySpan<ulong> combinedDivisors;
|
||||||
private readonly ReadOnlySpan<ulong> combinedDivisorSquares;
|
private readonly ReadOnlySpan<ulong> combinedDivisorSquares;
|
||||||
private int index;
|
private int index;
|
||||||
|
|
||||||
@@ -603,7 +585,7 @@ public sealed class HeuristicCombinedPrimeTester
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint entry = combinedDivisors[currentIndex];
|
ulong entry = combinedDivisors[currentIndex];
|
||||||
index = currentIndex + 1;
|
index = currentIndex + 1;
|
||||||
|
|
||||||
HeuristicDivisorGroup group = ResolveGroup(entry);
|
HeuristicDivisorGroup group = ResolveGroup(entry);
|
||||||
|
|||||||
@@ -15,19 +15,19 @@ public static class HeuristicPrimeSieves
|
|||||||
private static readonly ushort[] GroupBResiduesEnding7 = [17, 37, 47, 67, 97, 107, 127, 137, 157, 167, 187, 197];
|
private static readonly ushort[] GroupBResiduesEnding7 = [17, 37, 47, 67, 97, 107, 127, 137, 157, 167, 187, 197];
|
||||||
private static readonly ushort[] GroupBResiduesEnding9 = [19, 29, 59, 79, 89, 109, 139, 149, 169, 179, 199, 209];
|
private static readonly ushort[] GroupBResiduesEnding9 = [19, 29, 59, 79, 89, 109, 139, 149, 169, 179, 199, 209];
|
||||||
|
|
||||||
private static readonly int[] GroupADivisorsStorage;
|
private static readonly ulong[] GroupADivisorsStorage;
|
||||||
private static readonly ulong[] GroupADivisorSquaresStorage;
|
private static readonly ulong[] GroupADivisorSquaresStorage;
|
||||||
|
|
||||||
private static readonly int[] GroupBDivisorsEnding1Storage;
|
private static readonly ulong[] GroupBDivisorsEnding1Storage;
|
||||||
private static readonly ulong[] GroupBDivisorSquaresEnding1Storage;
|
private static readonly ulong[] GroupBDivisorSquaresEnding1Storage;
|
||||||
|
|
||||||
private static readonly int[] GroupBDivisorsEnding3Storage;
|
private static readonly ulong[] GroupBDivisorsEnding3Storage;
|
||||||
private static readonly ulong[] GroupBDivisorSquaresEnding3Storage;
|
private static readonly ulong[] GroupBDivisorSquaresEnding3Storage;
|
||||||
|
|
||||||
private static readonly int[] GroupBDivisorsEnding7Storage;
|
private static readonly ulong[] GroupBDivisorsEnding7Storage;
|
||||||
private static readonly ulong[] GroupBDivisorSquaresEnding7Storage;
|
private static readonly ulong[] GroupBDivisorSquaresEnding7Storage;
|
||||||
|
|
||||||
private static readonly int[] GroupBDivisorsEnding9Storage;
|
private static readonly ulong[] GroupBDivisorsEnding9Storage;
|
||||||
private static readonly ulong[] GroupBDivisorSquaresEnding9Storage;
|
private static readonly ulong[] GroupBDivisorSquaresEnding9Storage;
|
||||||
|
|
||||||
static HeuristicPrimeSieves()
|
static HeuristicPrimeSieves()
|
||||||
@@ -46,45 +46,46 @@ public static class HeuristicPrimeSieves
|
|||||||
// Intentionally left blank. Accessing this method forces the static constructor to run.
|
// Intentionally left blank. Accessing this method forces the static constructor to run.
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ReadOnlySpan<int> GroupADivisors => GroupADivisorsStorage;
|
public static ReadOnlySpan<ulong> GroupADivisors => GroupADivisorsStorage;
|
||||||
|
|
||||||
public static ReadOnlySpan<ulong> GroupADivisorSquares => GroupADivisorSquaresStorage;
|
public static ReadOnlySpan<ulong> GroupADivisorSquares => GroupADivisorSquaresStorage;
|
||||||
|
|
||||||
public static ReadOnlySpan<int> GroupBDivisorsEnding1 => GroupBDivisorsEnding1Storage;
|
public static ReadOnlySpan<ulong> GroupBDivisorsEnding1 => GroupBDivisorsEnding1Storage;
|
||||||
|
|
||||||
public static ReadOnlySpan<ulong> GroupBDivisorSquaresEnding1 => GroupBDivisorSquaresEnding1Storage;
|
public static ReadOnlySpan<ulong> GroupBDivisorSquaresEnding1 => GroupBDivisorSquaresEnding1Storage;
|
||||||
|
|
||||||
public static ReadOnlySpan<int> GroupBDivisorsEnding3 => GroupBDivisorsEnding3Storage;
|
public static ReadOnlySpan<ulong> GroupBDivisorsEnding3 => GroupBDivisorsEnding3Storage;
|
||||||
|
|
||||||
public static ReadOnlySpan<ulong> GroupBDivisorSquaresEnding3 => GroupBDivisorSquaresEnding3Storage;
|
public static ReadOnlySpan<ulong> GroupBDivisorSquaresEnding3 => GroupBDivisorSquaresEnding3Storage;
|
||||||
|
|
||||||
public static ReadOnlySpan<int> GroupBDivisorsEnding7 => GroupBDivisorsEnding7Storage;
|
public static ReadOnlySpan<ulong> GroupBDivisorsEnding7 => GroupBDivisorsEnding7Storage;
|
||||||
|
|
||||||
public static ReadOnlySpan<ulong> GroupBDivisorSquaresEnding7 => GroupBDivisorSquaresEnding7Storage;
|
public static ReadOnlySpan<ulong> GroupBDivisorSquaresEnding7 => GroupBDivisorSquaresEnding7Storage;
|
||||||
|
|
||||||
public static ReadOnlySpan<int> GroupBDivisorsEnding9 => GroupBDivisorsEnding9Storage;
|
public static ReadOnlySpan<ulong> GroupBDivisorsEnding9 => GroupBDivisorsEnding9Storage;
|
||||||
|
|
||||||
public static ReadOnlySpan<ulong> GroupBDivisorSquaresEnding9 => GroupBDivisorSquaresEnding9Storage;
|
public static ReadOnlySpan<ulong> GroupBDivisorSquaresEnding9 => GroupBDivisorSquaresEnding9Storage;
|
||||||
|
|
||||||
private static (int[] divisors, ulong[] squares) BuildGroupADivisors()
|
private static (ulong[] divisors, ulong[] squares) BuildGroupADivisors()
|
||||||
{
|
{
|
||||||
var divisors = new int[SieveLength];
|
var divisors = new ulong[SieveLength];
|
||||||
var squares = new ulong[SieveLength];
|
var squares = new ulong[SieveLength];
|
||||||
|
|
||||||
int index = 0;
|
int index = 0;
|
||||||
for (; index < GroupAConstants.Length && index < divisors.Length; index++)
|
for (; index < GroupAConstants.Length && index < divisors.Length; index++)
|
||||||
{
|
{
|
||||||
int value = GroupAConstants[index];
|
ulong value = (ulong)GroupAConstants[index];
|
||||||
divisors[index] = value;
|
divisors[index] = value;
|
||||||
squares[index] = (ulong)value * (ulong)value;
|
squares[index] = value * value;
|
||||||
}
|
}
|
||||||
|
|
||||||
int candidate = 23;
|
int candidate = 23;
|
||||||
int incrementIndex = 0;
|
int incrementIndex = 0;
|
||||||
while (index < divisors.Length)
|
while (index < divisors.Length)
|
||||||
{
|
{
|
||||||
divisors[index] = candidate;
|
ulong value = (ulong)candidate;
|
||||||
squares[index] = (ulong)candidate * (ulong)candidate;
|
divisors[index] = value;
|
||||||
|
squares[index] = value * value;
|
||||||
|
|
||||||
candidate += GroupAIncrementPattern[incrementIndex];
|
candidate += GroupAIncrementPattern[incrementIndex];
|
||||||
incrementIndex ^= 1;
|
incrementIndex ^= 1;
|
||||||
@@ -94,9 +95,9 @@ public static class HeuristicPrimeSieves
|
|||||||
return (divisors, squares);
|
return (divisors, squares);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static (int[] divisors, ulong[] squares) BuildGroupBResidueSieve(ReadOnlySpan<ushort> residues)
|
private static (ulong[] divisors, ulong[] squares) BuildGroupBResidueSieve(ReadOnlySpan<ushort> residues)
|
||||||
{
|
{
|
||||||
var divisors = new int[SieveLength];
|
var divisors = new ulong[SieveLength];
|
||||||
var squares = new ulong[SieveLength];
|
var squares = new ulong[SieveLength];
|
||||||
|
|
||||||
int index = 0;
|
int index = 0;
|
||||||
@@ -112,7 +113,7 @@ public static class HeuristicPrimeSieves
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
divisors[index] = (int)candidate;
|
divisors[index] = candidate;
|
||||||
squares[index] = candidate * candidate;
|
squares[index] = candidate * candidate;
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -111,7 +111,7 @@ public sealed class HeuristicPrimeTester
|
|||||||
|
|
||||||
private static bool HeuristicTrialDivisionCpu(ulong n, ulong maxDivisorSquare, byte nMod10, bool includeGroupB)
|
private static bool HeuristicTrialDivisionCpu(ulong n, ulong maxDivisorSquare, byte nMod10, bool includeGroupB)
|
||||||
{
|
{
|
||||||
ReadOnlySpan<int> groupADivisors = HeuristicPrimeSieves.GroupADivisors;
|
ReadOnlySpan<ulong> groupADivisors = HeuristicPrimeSieves.GroupADivisors;
|
||||||
ReadOnlySpan<ulong> groupADivisorSquares = HeuristicPrimeSieves.GroupADivisorSquares;
|
ReadOnlySpan<ulong> groupADivisorSquares = HeuristicPrimeSieves.GroupADivisorSquares;
|
||||||
int interleaveBatchSize = HeuristicCombinedPrimeTester.GetBatchSize(n);
|
int interleaveBatchSize = HeuristicCombinedPrimeTester.GetBatchSize(n);
|
||||||
int groupAIndex = 0;
|
int groupAIndex = 0;
|
||||||
@@ -237,149 +237,85 @@ public sealed class HeuristicPrimeTester
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool HeuristicTrialDivisionGpuDetectsDivisor(ulong n, ulong maxDivisorSquare, byte nMod10)
|
|
||||||
{
|
private bool HeuristicTrialDivisionGpuDetectsDivisor(ulong n, ulong maxDivisorSquare, byte nMod10)
|
||||||
int batchCapacity = HeuristicCombinedPrimeTester.GetBatchSize(n);
|
{
|
||||||
int interleaveBatchSize = batchCapacity;
|
var limiter = GpuPrimeWorkLimiter.Acquire();
|
||||||
|
var gpu = PrimeTester.PrimeTesterGpuContextPool.Rent(1);
|
||||||
|
var accelerator = gpu.Accelerator;
|
||||||
|
var kernel = gpu.HeuristicTrialDivisionKernel;
|
||||||
|
var flagView1D = gpu.HeuristicFlag.View;
|
||||||
|
var flagView = flagView1D.AsContiguous();
|
||||||
|
|
||||||
|
bool compositeDetected = false;
|
||||||
|
int compositeFlag = 0;
|
||||||
|
|
||||||
|
int groupALength = HeuristicPrimeSieves.GroupADivisors.Length;
|
||||||
|
|
||||||
|
if (groupALength > 0)
|
||||||
|
{
|
||||||
|
flagView1D.CopyFromCPU(ref compositeFlag, 1);
|
||||||
|
kernel(
|
||||||
|
groupALength,
|
||||||
|
flagView,
|
||||||
|
n,
|
||||||
|
maxDivisorSquare,
|
||||||
|
HeuristicGpuDivisorTableKind.GroupA,
|
||||||
|
gpu.HeuristicGpuTables);
|
||||||
|
accelerator.Synchronize();
|
||||||
|
flagView1D.CopyToCPU(ref compositeFlag, 1);
|
||||||
|
compositeDetected = compositeFlag != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!compositeDetected)
|
||||||
|
{
|
||||||
|
ReadOnlySpan<byte> endingOrder = GetGroupBEndingOrder(nMod10);
|
||||||
|
for (int i = 0; i < endingOrder.Length && !compositeDetected; i++)
|
||||||
|
{
|
||||||
|
byte ending = endingOrder[i];
|
||||||
|
HeuristicGpuDivisorTableKind tableKind = ending switch
|
||||||
|
{
|
||||||
|
1 => HeuristicGpuDivisorTableKind.GroupBEnding1,
|
||||||
|
7 => HeuristicGpuDivisorTableKind.GroupBEnding7,
|
||||||
|
9 => HeuristicGpuDivisorTableKind.GroupBEnding9,
|
||||||
|
_ => HeuristicGpuDivisorTableKind.GroupA,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (tableKind == HeuristicGpuDivisorTableKind.GroupA)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int divisorLength = GetGroupBDivisors(ending).Length;
|
||||||
|
if (divisorLength == 0)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
compositeFlag = 0;
|
||||||
|
flagView1D.CopyFromCPU(ref compositeFlag, 1);
|
||||||
|
kernel(
|
||||||
|
divisorLength,
|
||||||
|
flagView,
|
||||||
|
n,
|
||||||
|
maxDivisorSquare,
|
||||||
|
tableKind,
|
||||||
|
gpu.HeuristicGpuTables);
|
||||||
|
accelerator.Synchronize();
|
||||||
|
flagView1D.CopyToCPU(ref compositeFlag, 1);
|
||||||
|
compositeDetected = compositeFlag != 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
gpu.Dispose();
|
||||||
|
limiter.Dispose();
|
||||||
|
return compositeDetected;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
var limiter = GpuPrimeWorkLimiter.Acquire();
|
|
||||||
var gpu = PrimeTester.PrimeTesterGpuContextPool.Rent(batchCapacity);
|
|
||||||
var accelerator = gpu.Accelerator;
|
|
||||||
var kernel = gpu.HeuristicTrialDivisionKernel;
|
|
||||||
|
|
||||||
bool compositeDetected = false;
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
int count = 0;
|
private static bool EvaluateWithOpenNumericFallback(ulong n)
|
||||||
|
|
||||||
var inputView = gpu.Input.View;
|
|
||||||
var flagView = gpu.HeuristicFlag.View;
|
|
||||||
ulong[] divisorArray = gpu.DivisorArray;
|
|
||||||
|
|
||||||
bool ProcessBatch(int length)
|
|
||||||
{
|
|
||||||
if (length <= 0)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
inputView.CopyFromCPU(ref divisorArray[0], length);
|
|
||||||
int compositeFlag = 0;
|
|
||||||
flagView.CopyFromCPU(ref compositeFlag, 1);
|
|
||||||
kernel(length, inputView, n, flagView);
|
|
||||||
accelerator.Synchronize();
|
|
||||||
flagView.CopyToCPU(ref compositeFlag, 1);
|
|
||||||
return compositeFlag != 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
ReadOnlySpan<int> groupADivisors = HeuristicPrimeSieves.GroupADivisors;
|
|
||||||
ReadOnlySpan<ulong> groupADivisorSquares = HeuristicPrimeSieves.GroupADivisorSquares;
|
|
||||||
int groupAIndex = 0;
|
|
||||||
ReadOnlySpan<byte> endingOrder = GetGroupBEndingOrder(nMod10);
|
|
||||||
Span<int> indices = endingOrder.Length <= 8
|
|
||||||
? stackalloc int[endingOrder.Length]
|
|
||||||
: new int[endingOrder.Length];
|
|
||||||
|
|
||||||
bool groupAHasMore = groupAIndex < groupADivisors.Length && groupADivisorSquares[groupAIndex] <= maxDivisorSquare;
|
|
||||||
bool groupBHasMore = !endingOrder.IsEmpty && HasGroupBCandidates(endingOrder, indices, maxDivisorSquare);
|
|
||||||
|
|
||||||
while (!compositeDetected && (groupAHasMore || groupBHasMore))
|
|
||||||
{
|
|
||||||
if (groupAHasMore)
|
|
||||||
{
|
|
||||||
int processed = 0;
|
|
||||||
while (processed < interleaveBatchSize && groupAIndex < groupADivisors.Length)
|
|
||||||
{
|
|
||||||
ulong divisorSquare = groupADivisorSquares[groupAIndex];
|
|
||||||
if (divisorSquare > maxDivisorSquare)
|
|
||||||
{
|
|
||||||
groupAIndex = groupADivisors.Length;
|
|
||||||
groupAHasMore = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
divisorArray[count] = (ulong)groupADivisors[groupAIndex];
|
|
||||||
count++;
|
|
||||||
|
|
||||||
if (count == batchCapacity)
|
|
||||||
{
|
|
||||||
if (ProcessBatch(count))
|
|
||||||
{
|
|
||||||
compositeDetected = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
count = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
groupAIndex++;
|
|
||||||
processed++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (compositeDetected)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (groupAIndex >= groupADivisors.Length)
|
|
||||||
{
|
|
||||||
groupAHasMore = false;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
groupAHasMore = groupADivisorSquares[groupAIndex] <= maxDivisorSquare;
|
|
||||||
if (!groupAHasMore)
|
|
||||||
{
|
|
||||||
groupAIndex = groupADivisors.Length;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!compositeDetected && groupBHasMore)
|
|
||||||
{
|
|
||||||
int processed = 0;
|
|
||||||
while (processed < interleaveBatchSize)
|
|
||||||
{
|
|
||||||
if (!TrySelectNextGroupBDivisor(endingOrder, indices, maxDivisorSquare, out ulong divisor))
|
|
||||||
{
|
|
||||||
groupBHasMore = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
divisorArray[count] = divisor;
|
|
||||||
count++;
|
|
||||||
|
|
||||||
if (count == batchCapacity)
|
|
||||||
{
|
|
||||||
if (ProcessBatch(count))
|
|
||||||
{
|
|
||||||
compositeDetected = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
count = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
processed++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!compositeDetected && groupBHasMore)
|
|
||||||
{
|
|
||||||
groupBHasMore = HasGroupBCandidates(endingOrder, indices, maxDivisorSquare);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!compositeDetected && count > 0 && ProcessBatch(count))
|
|
||||||
{
|
|
||||||
compositeDetected = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
gpu.Dispose();
|
|
||||||
limiter.Dispose();
|
|
||||||
return compositeDetected;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool EvaluateWithOpenNumericFallback(ulong n)
|
|
||||||
{
|
{
|
||||||
return Prime.Numbers.IsPrime(n);
|
return Prime.Numbers.IsPrime(n);
|
||||||
}
|
}
|
||||||
@@ -398,7 +334,7 @@ public sealed class HeuristicPrimeTester
|
|||||||
};
|
};
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
private static bool TrySelectNextGroupBDivisor(ReadOnlySpan<byte> endingOrder, Span<int> indices, ulong maxDivisorSquare, out ulong divisor)
|
private static bool TrySelectNextGroupBDivisor(ReadOnlySpan<byte> endingOrder, Span<int> indices, ulong maxDivisorSquare, out ulong divisor)
|
||||||
{
|
{
|
||||||
ulong bestCandidate = ulong.MaxValue;
|
ulong bestCandidate = ulong.MaxValue;
|
||||||
int bestEndingIndex = -1;
|
int bestEndingIndex = -1;
|
||||||
@@ -581,7 +517,7 @@ public sealed class HeuristicPrimeTester
|
|||||||
internal ref struct HeuristicDivisorEnumerator
|
internal ref struct HeuristicDivisorEnumerator
|
||||||
{
|
{
|
||||||
private readonly ulong maxDivisorSquare;
|
private readonly ulong maxDivisorSquare;
|
||||||
private readonly ReadOnlySpan<int> groupADivisors;
|
private readonly ReadOnlySpan<ulong> groupADivisors;
|
||||||
private readonly ReadOnlySpan<ulong> groupADivisorSquares;
|
private readonly ReadOnlySpan<ulong> groupADivisorSquares;
|
||||||
private int groupAIndex;
|
private int groupAIndex;
|
||||||
private Span<HeuristicGroupBSequenceState> groupBStates;
|
private Span<HeuristicGroupBSequenceState> groupBStates;
|
||||||
@@ -609,7 +545,7 @@ public sealed class HeuristicPrimeTester
|
|||||||
}
|
}
|
||||||
|
|
||||||
int currentIndex = groupAIndex;
|
int currentIndex = groupAIndex;
|
||||||
ulong divisor = (ulong)groupADivisors[currentIndex];
|
ulong divisor = groupADivisors[currentIndex];
|
||||||
groupAIndex++;
|
groupAIndex++;
|
||||||
|
|
||||||
HeuristicDivisorGroup group = currentIndex < GroupAConstantCount
|
HeuristicDivisorGroup group = currentIndex < GroupAConstantCount
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
|
using System;
|
||||||
using System.Buffers;
|
using System.Buffers;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
@@ -140,6 +142,50 @@ public sealed class PrimeTester
|
|||||||
|
|
||||||
public static int GpuBatchSize { get; set; } = 262_144;
|
public static int GpuBatchSize { get; set; } = 262_144;
|
||||||
|
|
||||||
|
private static readonly object GpuWarmUpLock = new();
|
||||||
|
private static int WarmedGpuLeaseCount;
|
||||||
|
|
||||||
|
public static void WarmUpGpuKernels(int threadCount)
|
||||||
|
{
|
||||||
|
if (GpuContextPool.ForceCpu)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (threadCount <= 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int target = threadCount / 4;
|
||||||
|
if (target == 0)
|
||||||
|
{
|
||||||
|
target = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
lock (GpuWarmUpLock)
|
||||||
|
{
|
||||||
|
if (target <= WarmedGpuLeaseCount)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int toWarm = target - WarmedGpuLeaseCount;
|
||||||
|
var leases = new PrimeTesterGpuContextPool.PrimeTesterGpuContextLease[toWarm];
|
||||||
|
for (int i = 0; i < toWarm; i++)
|
||||||
|
{
|
||||||
|
leases[i] = PrimeTesterGpuContextPool.Rent(GpuBatchSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < toWarm; i++)
|
||||||
|
{
|
||||||
|
leases[i].Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
WarmedGpuLeaseCount = target;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static void IsPrimeBatchGpu(ReadOnlySpan<ulong> values, Span<byte> results)
|
public static void IsPrimeBatchGpu(ReadOnlySpan<ulong> values, Span<byte> results)
|
||||||
{
|
{
|
||||||
@@ -194,6 +240,41 @@ public sealed class PrimeTester
|
|||||||
internal static class PrimeTesterGpuContextPool
|
internal static class PrimeTesterGpuContextPool
|
||||||
{
|
{
|
||||||
private static readonly ConcurrentQueue<PrimeTesterGpuContextLease> Pool = new();
|
private static readonly ConcurrentQueue<PrimeTesterGpuContextLease> Pool = new();
|
||||||
|
private static readonly object SharedTablesLock = new();
|
||||||
|
private static readonly Dictionary<string, SharedHeuristicGpuTables> SharedTables = new();
|
||||||
|
|
||||||
|
private static string CreateAcceleratorKey(Accelerator accelerator)
|
||||||
|
{
|
||||||
|
return accelerator.AcceleratorType + ":" + accelerator.Name;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static SharedHeuristicGpuTables RentSharedTables(Accelerator accelerator)
|
||||||
|
{
|
||||||
|
string key = CreateAcceleratorKey(accelerator);
|
||||||
|
|
||||||
|
lock (SharedTablesLock)
|
||||||
|
{
|
||||||
|
if (!SharedTables.TryGetValue(key, out var tables))
|
||||||
|
{
|
||||||
|
tables = new SharedHeuristicGpuTables(accelerator, key);
|
||||||
|
SharedTables.Add(key, tables);
|
||||||
|
}
|
||||||
|
|
||||||
|
tables.AddReference();
|
||||||
|
return tables;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ReleaseSharedTables(SharedHeuristicGpuTables tables)
|
||||||
|
{
|
||||||
|
lock (SharedTablesLock)
|
||||||
|
{
|
||||||
|
if (tables.Release())
|
||||||
|
{
|
||||||
|
SharedTables.Remove(tables.Key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
internal static PrimeTesterGpuContextLease Rent(int minBufferCapacity = 1)
|
internal static PrimeTesterGpuContextLease Rent(int minBufferCapacity = 1)
|
||||||
{
|
{
|
||||||
@@ -253,101 +334,95 @@ public sealed class PrimeTester
|
|||||||
|
|
||||||
internal sealed class PrimeTesterGpuContextLease : IDisposable
|
internal sealed class PrimeTesterGpuContextLease : IDisposable
|
||||||
{
|
{
|
||||||
|
|
||||||
private bool _returned;
|
private bool _returned;
|
||||||
|
private readonly SharedHeuristicGpuTables _sharedTables;
|
||||||
|
private readonly HeuristicGpuDivisorTables _heuristicDivisorTables;
|
||||||
|
|
||||||
internal readonly Context AcceleratorContext;
|
internal readonly Context AcceleratorContext;
|
||||||
public readonly Accelerator Accelerator;
|
public readonly Accelerator Accelerator;
|
||||||
public readonly Action<Index1D, ArrayView<ulong>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<byte>> Kernel;
|
public readonly Action<Index1D, ArrayView<ulong>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<byte>> Kernel;
|
||||||
public readonly Action<Index1D, ArrayView<ulong>, ulong, ArrayView<int>> HeuristicTrialDivisionKernel;
|
public readonly Action<Index1D, ArrayView<int>, ulong, ulong, HeuristicGpuDivisorTableKind, HeuristicGpuDivisorTables> HeuristicTrialDivisionKernel;
|
||||||
public readonly MemoryBuffer1D<uint, Stride1D.Dense> DevicePrimesDefault;
|
public MemoryBuffer1D<ulong, Stride1D.Dense> Input = null!;
|
||||||
public readonly MemoryBuffer1D<uint, Stride1D.Dense> DevicePrimesLastOne;
|
public MemoryBuffer1D<byte, Stride1D.Dense> Output = null!;
|
||||||
public readonly MemoryBuffer1D<uint, Stride1D.Dense> DevicePrimesLastSeven;
|
public MemoryBuffer1D<int, Stride1D.Dense> HeuristicFlag = null!;
|
||||||
public readonly MemoryBuffer1D<uint, Stride1D.Dense> DevicePrimesLastThree;
|
|
||||||
public readonly MemoryBuffer1D<uint, Stride1D.Dense> DevicePrimesLastNine;
|
|
||||||
public readonly MemoryBuffer1D<ulong, Stride1D.Dense> DevicePrimesPow2Default;
|
|
||||||
public readonly MemoryBuffer1D<ulong, Stride1D.Dense> DevicePrimesPow2LastOne;
|
|
||||||
public readonly MemoryBuffer1D<ulong, Stride1D.Dense> DevicePrimesPow2LastSeven;
|
|
||||||
public readonly MemoryBuffer1D<ulong, Stride1D.Dense> DevicePrimesPow2LastThree;
|
|
||||||
public readonly MemoryBuffer1D<ulong, Stride1D.Dense> DevicePrimesPow2LastNine;
|
|
||||||
public MemoryBuffer1D<ulong, Stride1D.Dense> Input;
|
|
||||||
public MemoryBuffer1D<byte, Stride1D.Dense> Output;
|
|
||||||
public MemoryBuffer1D<int, Stride1D.Dense> HeuristicFlag;
|
|
||||||
public int BufferCapacity;
|
public int BufferCapacity;
|
||||||
public ulong[] DivisorArray;
|
|
||||||
|
public MemoryBuffer1D<uint, Stride1D.Dense> DevicePrimesDefault => _sharedTables.DevicePrimesDefault;
|
||||||
|
public MemoryBuffer1D<uint, Stride1D.Dense> DevicePrimesLastOne => _sharedTables.DevicePrimesLastOne;
|
||||||
|
public MemoryBuffer1D<uint, Stride1D.Dense> DevicePrimesLastSeven => _sharedTables.DevicePrimesLastSeven;
|
||||||
|
public MemoryBuffer1D<uint, Stride1D.Dense> DevicePrimesLastThree => _sharedTables.DevicePrimesLastThree;
|
||||||
|
public MemoryBuffer1D<uint, Stride1D.Dense> DevicePrimesLastNine => _sharedTables.DevicePrimesLastNine;
|
||||||
|
public MemoryBuffer1D<ulong, Stride1D.Dense> DevicePrimesPow2Default => _sharedTables.DevicePrimesPow2Default;
|
||||||
|
public MemoryBuffer1D<ulong, Stride1D.Dense> DevicePrimesPow2LastOne => _sharedTables.DevicePrimesPow2LastOne;
|
||||||
|
public MemoryBuffer1D<ulong, Stride1D.Dense> DevicePrimesPow2LastSeven => _sharedTables.DevicePrimesPow2LastSeven;
|
||||||
|
public MemoryBuffer1D<ulong, Stride1D.Dense> DevicePrimesPow2LastThree => _sharedTables.DevicePrimesPow2LastThree;
|
||||||
|
public MemoryBuffer1D<ulong, Stride1D.Dense> DevicePrimesPow2LastNine => _sharedTables.DevicePrimesPow2LastNine;
|
||||||
|
public MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicGroupADivisors => _sharedTables.HeuristicGroupADivisors;
|
||||||
|
public MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicGroupADivisorSquares => _sharedTables.HeuristicGroupADivisorSquares;
|
||||||
|
public MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicGroupBDivisorsEnding1 => _sharedTables.HeuristicGroupBDivisorsEnding1;
|
||||||
|
public MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicGroupBDivisorSquaresEnding1 => _sharedTables.HeuristicGroupBDivisorSquaresEnding1;
|
||||||
|
public MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicGroupBDivisorsEnding7 => _sharedTables.HeuristicGroupBDivisorsEnding7;
|
||||||
|
public MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicGroupBDivisorSquaresEnding7 => _sharedTables.HeuristicGroupBDivisorSquaresEnding7;
|
||||||
|
public MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicGroupBDivisorsEnding9 => _sharedTables.HeuristicGroupBDivisorsEnding9;
|
||||||
|
public MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicGroupBDivisorSquaresEnding9 => _sharedTables.HeuristicGroupBDivisorSquaresEnding9;
|
||||||
|
public MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicCombinedDivisorsEnding1 => _sharedTables.HeuristicCombinedDivisorsEnding1;
|
||||||
|
public MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicCombinedDivisorSquaresEnding1 => _sharedTables.HeuristicCombinedDivisorSquaresEnding1;
|
||||||
|
public MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicCombinedDivisorsEnding3 => _sharedTables.HeuristicCombinedDivisorsEnding3;
|
||||||
|
public MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicCombinedDivisorSquaresEnding3 => _sharedTables.HeuristicCombinedDivisorSquaresEnding3;
|
||||||
|
public MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicCombinedDivisorsEnding7 => _sharedTables.HeuristicCombinedDivisorsEnding7;
|
||||||
|
public MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicCombinedDivisorSquaresEnding7 => _sharedTables.HeuristicCombinedDivisorSquaresEnding7;
|
||||||
|
public MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicCombinedDivisorsEnding9 => _sharedTables.HeuristicCombinedDivisorsEnding9;
|
||||||
|
public MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicCombinedDivisorSquaresEnding9 => _sharedTables.HeuristicCombinedDivisorSquaresEnding9;
|
||||||
|
|
||||||
|
public HeuristicGpuDivisorTables HeuristicGpuTables => _heuristicDivisorTables;
|
||||||
|
|
||||||
internal PrimeTesterGpuContextLease(int minBufferCapacity)
|
internal PrimeTesterGpuContextLease(int minBufferCapacity)
|
||||||
{
|
{
|
||||||
AcceleratorContext = Context.CreateDefault();
|
AcceleratorContext = Context.CreateDefault();
|
||||||
Accelerator = AcceleratorContext.GetPreferredDevice(false).CreateAccelerator(AcceleratorContext);
|
Accelerator = AcceleratorContext.GetPreferredDevice(false).CreateAccelerator(AcceleratorContext);
|
||||||
Kernel = Accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<ulong>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<byte>>(PrimeTesterKernels.SmallPrimeSieveKernel);
|
Kernel = Accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<ulong>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<uint>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<ulong>, ArrayView<byte>>(PrimeTesterKernels.SmallPrimeSieveKernel);
|
||||||
HeuristicTrialDivisionKernel = Accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<ulong>, ulong, ArrayView<int>>(PrimeTesterKernels.HeuristicTrialDivisionKernel);
|
HeuristicTrialDivisionKernel = Accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<int>, ulong, ulong, HeuristicGpuDivisorTableKind, HeuristicGpuDivisorTables>(PrimeTesterKernels.HeuristicTrialDivisionKernel);
|
||||||
|
|
||||||
var primesDefault = DivisorGenerator.SmallPrimes;
|
_sharedTables = RentSharedTables(Accelerator);
|
||||||
DevicePrimesDefault = Accelerator.Allocate1D<uint>(primesDefault.Length);
|
|
||||||
DevicePrimesDefault.View.CopyFromCPU(primesDefault);
|
|
||||||
|
|
||||||
var primesLastOne = DivisorGenerator.SmallPrimesLastOne;
|
_heuristicDivisorTables = new HeuristicGpuDivisorTables(
|
||||||
DevicePrimesLastOne = Accelerator.Allocate1D<uint>(primesLastOne.Length);
|
_sharedTables.HeuristicCombinedDivisorsEnding1.View,
|
||||||
DevicePrimesLastOne.View.CopyFromCPU(primesLastOne);
|
_sharedTables.HeuristicCombinedDivisorsEnding3.View,
|
||||||
|
_sharedTables.HeuristicCombinedDivisorsEnding7.View,
|
||||||
var primesLastSeven = DivisorGenerator.SmallPrimesLastSeven;
|
_sharedTables.HeuristicCombinedDivisorsEnding9.View,
|
||||||
DevicePrimesLastSeven = Accelerator.Allocate1D<uint>(primesLastSeven.Length);
|
_sharedTables.HeuristicCombinedDivisorSquaresEnding1.View,
|
||||||
DevicePrimesLastSeven.View.CopyFromCPU(primesLastSeven);
|
_sharedTables.HeuristicCombinedDivisorSquaresEnding3.View,
|
||||||
|
_sharedTables.HeuristicCombinedDivisorSquaresEnding7.View,
|
||||||
var primesLastThree = DivisorGenerator.SmallPrimesLastThree;
|
_sharedTables.HeuristicCombinedDivisorSquaresEnding9.View,
|
||||||
DevicePrimesLastThree = Accelerator.Allocate1D<uint>(primesLastThree.Length);
|
_sharedTables.HeuristicGroupADivisors.View,
|
||||||
DevicePrimesLastThree.View.CopyFromCPU(primesLastThree);
|
_sharedTables.HeuristicGroupADivisorSquares.View,
|
||||||
|
_sharedTables.HeuristicGroupBDivisorsEnding1.View,
|
||||||
var primesLastNine = DivisorGenerator.SmallPrimesLastNine;
|
_sharedTables.HeuristicGroupBDivisorSquaresEnding1.View,
|
||||||
DevicePrimesLastNine = Accelerator.Allocate1D<uint>(primesLastNine.Length);
|
_sharedTables.HeuristicGroupBDivisorsEnding7.View,
|
||||||
DevicePrimesLastNine.View.CopyFromCPU(primesLastNine);
|
_sharedTables.HeuristicGroupBDivisorSquaresEnding7.View,
|
||||||
|
_sharedTables.HeuristicGroupBDivisorsEnding9.View,
|
||||||
var primesPow2Default = DivisorGenerator.SmallPrimesPow2;
|
_sharedTables.HeuristicGroupBDivisorSquaresEnding9.View);
|
||||||
DevicePrimesPow2Default = Accelerator.Allocate1D<ulong>(primesPow2Default.Length);
|
HeuristicGpuDivisorTables.InitializeShared(in _heuristicDivisorTables);
|
||||||
DevicePrimesPow2Default.View.CopyFromCPU(primesPow2Default);
|
|
||||||
|
|
||||||
var primesPow2LastOne = DivisorGenerator.SmallPrimesPow2LastOne;
|
|
||||||
DevicePrimesPow2LastOne = Accelerator.Allocate1D<ulong>(primesPow2LastOne.Length);
|
|
||||||
DevicePrimesPow2LastOne.View.CopyFromCPU(primesPow2LastOne);
|
|
||||||
|
|
||||||
var primesPow2LastSeven = DivisorGenerator.SmallPrimesPow2LastSeven;
|
|
||||||
DevicePrimesPow2LastSeven = Accelerator.Allocate1D<ulong>(primesPow2LastSeven.Length);
|
|
||||||
DevicePrimesPow2LastSeven.View.CopyFromCPU(primesPow2LastSeven);
|
|
||||||
|
|
||||||
var primesPow2LastThree = DivisorGenerator.SmallPrimesPow2LastThree;
|
|
||||||
DevicePrimesPow2LastThree = Accelerator.Allocate1D<ulong>(primesPow2LastThree.Length);
|
|
||||||
DevicePrimesPow2LastThree.View.CopyFromCPU(primesPow2LastThree);
|
|
||||||
|
|
||||||
var primesPow2LastNine = DivisorGenerator.SmallPrimesPow2LastNine;
|
|
||||||
DevicePrimesPow2LastNine = Accelerator.Allocate1D<ulong>(primesPow2LastNine.Length);
|
|
||||||
DevicePrimesPow2LastNine.View.CopyFromCPU(primesPow2LastNine);
|
|
||||||
|
|
||||||
BufferCapacity = minBufferCapacity;
|
|
||||||
Input = Accelerator.Allocate1D<ulong>(minBufferCapacity);
|
|
||||||
Output = Accelerator.Allocate1D<byte>(minBufferCapacity);
|
|
||||||
HeuristicFlag = Accelerator.Allocate1D<int>(minBufferCapacity);
|
|
||||||
DivisorArray = ThreadStaticPools.UlongPool.Rent(minBufferCapacity);
|
|
||||||
|
|
||||||
|
EnsureCapacity(minBufferCapacity);
|
||||||
_returned = false;
|
_returned = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void EnsureCapacity(int minCapacity)
|
public void EnsureCapacity(int minCapacity)
|
||||||
{
|
{
|
||||||
int required = minCapacity;
|
if (minCapacity <= BufferCapacity)
|
||||||
if (required <= BufferCapacity)
|
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Input.Dispose();
|
Input?.Dispose();
|
||||||
Output.Dispose();
|
Output?.Dispose();
|
||||||
HeuristicFlag.Dispose();
|
HeuristicFlag?.Dispose();
|
||||||
ThreadStaticPools.UlongPool.Return(DivisorArray);
|
Input = Accelerator.Allocate1D<ulong>(minCapacity);
|
||||||
Input = Accelerator.Allocate1D<ulong>(required);
|
Output = Accelerator.Allocate1D<byte>(minCapacity);
|
||||||
Output = Accelerator.Allocate1D<byte>(required);
|
HeuristicFlag = Accelerator.Allocate1D<int>(minCapacity);
|
||||||
HeuristicFlag = Accelerator.Allocate1D<int>(required);
|
BufferCapacity = minCapacity;
|
||||||
DivisorArray = ThreadStaticPools.UlongPool.Rent(required);
|
|
||||||
BufferCapacity = required;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void ResetReturnFlag()
|
internal void ResetReturnFlag()
|
||||||
@@ -368,9 +443,178 @@ public sealed class PrimeTester
|
|||||||
|
|
||||||
internal void DisposeResources()
|
internal void DisposeResources()
|
||||||
{
|
{
|
||||||
Input.Dispose();
|
Input?.Dispose();
|
||||||
Output.Dispose();
|
Output?.Dispose();
|
||||||
HeuristicFlag.Dispose();
|
HeuristicFlag?.Dispose();
|
||||||
|
ReleaseSharedTables(_sharedTables);
|
||||||
|
Accelerator.Dispose();
|
||||||
|
AcceleratorContext.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private sealed class SharedHeuristicGpuTables
|
||||||
|
{
|
||||||
|
private int _referenceCount;
|
||||||
|
|
||||||
|
internal SharedHeuristicGpuTables(Accelerator accelerator, string key)
|
||||||
|
{
|
||||||
|
Key = key;
|
||||||
|
|
||||||
|
var primesDefault = DivisorGenerator.SmallPrimes;
|
||||||
|
DevicePrimesDefault = accelerator.Allocate1D<uint>(primesDefault.Length);
|
||||||
|
DevicePrimesDefault.View.CopyFromCPU(primesDefault);
|
||||||
|
|
||||||
|
var primesLastOne = DivisorGenerator.SmallPrimesLastOne;
|
||||||
|
DevicePrimesLastOne = accelerator.Allocate1D<uint>(primesLastOne.Length);
|
||||||
|
DevicePrimesLastOne.View.CopyFromCPU(primesLastOne);
|
||||||
|
|
||||||
|
var primesLastSeven = DivisorGenerator.SmallPrimesLastSeven;
|
||||||
|
DevicePrimesLastSeven = accelerator.Allocate1D<uint>(primesLastSeven.Length);
|
||||||
|
DevicePrimesLastSeven.View.CopyFromCPU(primesLastSeven);
|
||||||
|
|
||||||
|
var primesLastThree = DivisorGenerator.SmallPrimesLastThree;
|
||||||
|
DevicePrimesLastThree = accelerator.Allocate1D<uint>(primesLastThree.Length);
|
||||||
|
DevicePrimesLastThree.View.CopyFromCPU(primesLastThree);
|
||||||
|
|
||||||
|
var primesLastNine = DivisorGenerator.SmallPrimesLastNine;
|
||||||
|
DevicePrimesLastNine = accelerator.Allocate1D<uint>(primesLastNine.Length);
|
||||||
|
DevicePrimesLastNine.View.CopyFromCPU(primesLastNine);
|
||||||
|
|
||||||
|
var primesPow2Default = DivisorGenerator.SmallPrimesPow2;
|
||||||
|
DevicePrimesPow2Default = accelerator.Allocate1D<ulong>(primesPow2Default.Length);
|
||||||
|
DevicePrimesPow2Default.View.CopyFromCPU(primesPow2Default);
|
||||||
|
|
||||||
|
var primesPow2LastOne = DivisorGenerator.SmallPrimesPow2LastOne;
|
||||||
|
DevicePrimesPow2LastOne = accelerator.Allocate1D<ulong>(primesPow2LastOne.Length);
|
||||||
|
DevicePrimesPow2LastOne.View.CopyFromCPU(primesPow2LastOne);
|
||||||
|
|
||||||
|
var primesPow2LastSeven = DivisorGenerator.SmallPrimesPow2LastSeven;
|
||||||
|
DevicePrimesPow2LastSeven = accelerator.Allocate1D<ulong>(primesPow2LastSeven.Length);
|
||||||
|
DevicePrimesPow2LastSeven.View.CopyFromCPU(primesPow2LastSeven);
|
||||||
|
|
||||||
|
var primesPow2LastThree = DivisorGenerator.SmallPrimesPow2LastThree;
|
||||||
|
DevicePrimesPow2LastThree = accelerator.Allocate1D<ulong>(primesPow2LastThree.Length);
|
||||||
|
DevicePrimesPow2LastThree.View.CopyFromCPU(primesPow2LastThree);
|
||||||
|
|
||||||
|
var primesPow2LastNine = DivisorGenerator.SmallPrimesPow2LastNine;
|
||||||
|
DevicePrimesPow2LastNine = accelerator.Allocate1D<ulong>(primesPow2LastNine.Length);
|
||||||
|
DevicePrimesPow2LastNine.View.CopyFromCPU(primesPow2LastNine);
|
||||||
|
|
||||||
|
HeuristicPrimeSieves.EnsureInitialized();
|
||||||
|
HeuristicCombinedPrimeTester.EnsureInitialized();
|
||||||
|
|
||||||
|
var heuristicGroupA = HeuristicPrimeSieves.GroupADivisors;
|
||||||
|
HeuristicGroupADivisors = CopySpanToDevice(accelerator, heuristicGroupA);
|
||||||
|
|
||||||
|
var heuristicGroupASquares = HeuristicPrimeSieves.GroupADivisorSquares;
|
||||||
|
HeuristicGroupADivisorSquares = CopySpanToDevice(accelerator, heuristicGroupASquares);
|
||||||
|
|
||||||
|
HeuristicGroupBDivisorsEnding1 = CopyUintSpanToDevice(accelerator, DivisorGenerator.SmallPrimesLastOneWithoutLastThree);
|
||||||
|
HeuristicGroupBDivisorSquaresEnding1 = CopySpanToDevice(accelerator, DivisorGenerator.SmallPrimesPow2LastOneWithoutLastThree);
|
||||||
|
|
||||||
|
HeuristicGroupBDivisorsEnding7 = CopyUintSpanToDevice(accelerator, DivisorGenerator.SmallPrimesLastSevenWithoutLastThree);
|
||||||
|
HeuristicGroupBDivisorSquaresEnding7 = CopySpanToDevice(accelerator, DivisorGenerator.SmallPrimesPow2LastSevenWithoutLastThree);
|
||||||
|
|
||||||
|
HeuristicGroupBDivisorsEnding9 = CopyUintSpanToDevice(accelerator, DivisorGenerator.SmallPrimesLastNineWithoutLastThree);
|
||||||
|
HeuristicGroupBDivisorSquaresEnding9 = CopySpanToDevice(accelerator, DivisorGenerator.SmallPrimesPow2LastNineWithoutLastThree);
|
||||||
|
|
||||||
|
var combinedEnding1 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding1Span;
|
||||||
|
HeuristicCombinedDivisorsEnding1 = CopySpanToDevice(accelerator, combinedEnding1);
|
||||||
|
var combinedSquaresEnding1 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding1SquaresSpan;
|
||||||
|
HeuristicCombinedDivisorSquaresEnding1 = CopySpanToDevice(accelerator, combinedSquaresEnding1);
|
||||||
|
|
||||||
|
var combinedEnding3 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding3Span;
|
||||||
|
HeuristicCombinedDivisorsEnding3 = CopySpanToDevice(accelerator, combinedEnding3);
|
||||||
|
var combinedSquaresEnding3 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding3SquaresSpan;
|
||||||
|
HeuristicCombinedDivisorSquaresEnding3 = CopySpanToDevice(accelerator, combinedSquaresEnding3);
|
||||||
|
|
||||||
|
var combinedEnding7 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding7Span;
|
||||||
|
HeuristicCombinedDivisorsEnding7 = CopySpanToDevice(accelerator, combinedEnding7);
|
||||||
|
var combinedSquaresEnding7 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding7SquaresSpan;
|
||||||
|
HeuristicCombinedDivisorSquaresEnding7 = CopySpanToDevice(accelerator, combinedSquaresEnding7);
|
||||||
|
|
||||||
|
var combinedEnding9 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding9Span;
|
||||||
|
HeuristicCombinedDivisorsEnding9 = CopySpanToDevice(accelerator, combinedEnding9);
|
||||||
|
var combinedSquaresEnding9 = HeuristicCombinedPrimeTester.CombinedDivisorsEnding9SquaresSpan;
|
||||||
|
HeuristicCombinedDivisorSquaresEnding9 = CopySpanToDevice(accelerator, combinedSquaresEnding9);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal string Key { get; }
|
||||||
|
internal MemoryBuffer1D<uint, Stride1D.Dense> DevicePrimesDefault { get; }
|
||||||
|
internal MemoryBuffer1D<uint, Stride1D.Dense> DevicePrimesLastOne { get; }
|
||||||
|
internal MemoryBuffer1D<uint, Stride1D.Dense> DevicePrimesLastSeven { get; }
|
||||||
|
internal MemoryBuffer1D<uint, Stride1D.Dense> DevicePrimesLastThree { get; }
|
||||||
|
internal MemoryBuffer1D<uint, Stride1D.Dense> DevicePrimesLastNine { get; }
|
||||||
|
internal MemoryBuffer1D<ulong, Stride1D.Dense> DevicePrimesPow2Default { get; }
|
||||||
|
internal MemoryBuffer1D<ulong, Stride1D.Dense> DevicePrimesPow2LastOne { get; }
|
||||||
|
internal MemoryBuffer1D<ulong, Stride1D.Dense> DevicePrimesPow2LastSeven { get; }
|
||||||
|
internal MemoryBuffer1D<ulong, Stride1D.Dense> DevicePrimesPow2LastThree { get; }
|
||||||
|
internal MemoryBuffer1D<ulong, Stride1D.Dense> DevicePrimesPow2LastNine { get; }
|
||||||
|
internal MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicGroupADivisors { get; }
|
||||||
|
internal MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicGroupADivisorSquares { get; }
|
||||||
|
internal MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicGroupBDivisorsEnding1 { get; }
|
||||||
|
internal MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicGroupBDivisorSquaresEnding1 { get; }
|
||||||
|
internal MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicGroupBDivisorsEnding7 { get; }
|
||||||
|
internal MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicGroupBDivisorSquaresEnding7 { get; }
|
||||||
|
internal MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicGroupBDivisorsEnding9 { get; }
|
||||||
|
internal MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicGroupBDivisorSquaresEnding9 { get; }
|
||||||
|
internal MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicCombinedDivisorsEnding1 { get; }
|
||||||
|
internal MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicCombinedDivisorSquaresEnding1 { get; }
|
||||||
|
internal MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicCombinedDivisorsEnding3 { get; }
|
||||||
|
internal MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicCombinedDivisorSquaresEnding3 { get; }
|
||||||
|
internal MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicCombinedDivisorsEnding7 { get; }
|
||||||
|
internal MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicCombinedDivisorSquaresEnding7 { get; }
|
||||||
|
internal MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicCombinedDivisorsEnding9 { get; }
|
||||||
|
internal MemoryBuffer1D<ulong, Stride1D.Dense> HeuristicCombinedDivisorSquaresEnding9 { get; }
|
||||||
|
|
||||||
|
internal void AddReference()
|
||||||
|
{
|
||||||
|
_referenceCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal bool Release()
|
||||||
|
{
|
||||||
|
_referenceCount--;
|
||||||
|
if (_referenceCount == 0)
|
||||||
|
{
|
||||||
|
Dispose();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static MemoryBuffer1D<ulong, Stride1D.Dense> CopySpanToDevice(Accelerator accelerator, ReadOnlySpan<ulong> span)
|
||||||
|
{
|
||||||
|
var buffer = accelerator.Allocate1D<ulong>(span.Length);
|
||||||
|
if (!span.IsEmpty)
|
||||||
|
{
|
||||||
|
ref ulong sourceRef = ref MemoryMarshal.GetReference(span);
|
||||||
|
buffer.View.CopyFromCPU(ref sourceRef, span.Length);
|
||||||
|
}
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static MemoryBuffer1D<ulong, Stride1D.Dense> CopyUintSpanToDevice(Accelerator accelerator, ReadOnlySpan<uint> span)
|
||||||
|
{
|
||||||
|
var buffer = accelerator.Allocate1D<ulong>(span.Length);
|
||||||
|
if (!span.IsEmpty)
|
||||||
|
{
|
||||||
|
var converted = new ulong[span.Length];
|
||||||
|
for (int i = 0; i < span.Length; i++)
|
||||||
|
{
|
||||||
|
converted[i] = span[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer.View.CopyFromCPU(converted);
|
||||||
|
}
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Dispose()
|
||||||
|
{
|
||||||
DevicePrimesDefault.Dispose();
|
DevicePrimesDefault.Dispose();
|
||||||
DevicePrimesLastOne.Dispose();
|
DevicePrimesLastOne.Dispose();
|
||||||
DevicePrimesLastSeven.Dispose();
|
DevicePrimesLastSeven.Dispose();
|
||||||
@@ -381,11 +625,26 @@ public sealed class PrimeTester
|
|||||||
DevicePrimesPow2LastSeven.Dispose();
|
DevicePrimesPow2LastSeven.Dispose();
|
||||||
DevicePrimesPow2LastThree.Dispose();
|
DevicePrimesPow2LastThree.Dispose();
|
||||||
DevicePrimesPow2LastNine.Dispose();
|
DevicePrimesPow2LastNine.Dispose();
|
||||||
Accelerator.Dispose();
|
HeuristicGroupADivisors.Dispose();
|
||||||
AcceleratorContext.Dispose();
|
HeuristicGroupADivisorSquares.Dispose();
|
||||||
|
HeuristicGroupBDivisorsEnding1.Dispose();
|
||||||
|
HeuristicGroupBDivisorSquaresEnding1.Dispose();
|
||||||
|
HeuristicGroupBDivisorsEnding7.Dispose();
|
||||||
|
HeuristicGroupBDivisorSquaresEnding7.Dispose();
|
||||||
|
HeuristicGroupBDivisorsEnding9.Dispose();
|
||||||
|
HeuristicGroupBDivisorSquaresEnding9.Dispose();
|
||||||
|
HeuristicCombinedDivisorsEnding1.Dispose();
|
||||||
|
HeuristicCombinedDivisorSquaresEnding1.Dispose();
|
||||||
|
HeuristicCombinedDivisorsEnding3.Dispose();
|
||||||
|
HeuristicCombinedDivisorSquaresEnding3.Dispose();
|
||||||
|
HeuristicCombinedDivisorsEnding7.Dispose();
|
||||||
|
HeuristicCombinedDivisorSquaresEnding7.Dispose();
|
||||||
|
HeuristicCombinedDivisorsEnding9.Dispose();
|
||||||
|
HeuristicCombinedDivisorSquaresEnding9.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Expose cache clearing for accelerator disposal coordination
|
// Expose cache clearing for accelerator disposal coordination
|
||||||
public static void ClearGpuCaches(Accelerator accelerator)
|
public static void ClearGpuCaches(Accelerator accelerator)
|
||||||
{
|
{
|
||||||
@@ -395,6 +654,10 @@ public sealed class PrimeTester
|
|||||||
internal static void DisposeGpuContexts()
|
internal static void DisposeGpuContexts()
|
||||||
{
|
{
|
||||||
PrimeTesterGpuContextPool.DisposeAll();
|
PrimeTesterGpuContextPool.DisposeAll();
|
||||||
|
lock (GpuWarmUpLock)
|
||||||
|
{
|
||||||
|
WarmedGpuLeaseCount = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
|||||||
Reference in new issue
Block a user