Various improvements to make sure scratch buffers don't override each others memory
This commit is contained in:
1 parent
dcda4b5cba
commit
fc22c01ec8
16 files changed
+489
-565
No files matched your search
@@ -213,7 +213,7 @@ public sealed class HeuristicPrimeTester
|
||||
int processed = 0;
|
||||
while (processed < interleaveBatchSize)
|
||||
{
|
||||
if (!TrySelectNextGroupBDivisor(endingOrder, indices, maxDivisorSquare, out ulong divisor))
|
||||
if (!TrySelectNextGroupBDivisor(endingOrder, indices, maxDivisorSquare, out ulong divisor))
|
||||
{
|
||||
groupBHasMore = false;
|
||||
break;
|
||||
@@ -238,77 +238,77 @@ public sealed class HeuristicPrimeTester
|
||||
}
|
||||
|
||||
|
||||
private bool HeuristicTrialDivisionGpuDetectsDivisor(ulong n, ulong maxDivisorSquare, byte nMod10)
|
||||
{
|
||||
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();
|
||||
private bool HeuristicTrialDivisionGpuDetectsDivisor(ulong n, ulong maxDivisorSquare, byte nMod10)
|
||||
{
|
||||
GpuPrimeWorkLimiter.Acquire();
|
||||
var gpu = PrimeTester.PrimeTesterGpuContextPool.Rent(1);
|
||||
var accelerator = gpu.Accelerator;
|
||||
var kernel = PrimeTester.PrimeTesterGpuContextPool.PrimeTesterGpuContextLease.HeuristicTrialDivisionKernel;
|
||||
var flagView1D = gpu.HeuristicFlag.View;
|
||||
var flagView = flagView1D.AsContiguous();
|
||||
|
||||
bool compositeDetected = false;
|
||||
int compositeFlag = 0;
|
||||
bool compositeDetected = false;
|
||||
int compositeFlag = 0;
|
||||
|
||||
int groupALength = HeuristicPrimeSieves.GroupADivisors.Length;
|
||||
int groupALength = HeuristicPrimeSieves.GroupADivisors.Length;
|
||||
|
||||
flagView1D.CopyFromCPU(ref compositeFlag, 1);
|
||||
kernel(
|
||||
groupALength,
|
||||
flagView,
|
||||
n,
|
||||
maxDivisorSquare,
|
||||
HeuristicGpuDivisorTableKind.GroupA,
|
||||
gpu.HeuristicGpuTables);
|
||||
accelerator.Synchronize();
|
||||
flagView1D.CopyToCPU(ref compositeFlag, 1);
|
||||
compositeDetected = compositeFlag != 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 (!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;
|
||||
}
|
||||
if (tableKind == HeuristicGpuDivisorTableKind.GroupA)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
int divisorLength = GetGroupBDivisors(ending).Length;
|
||||
int divisorLength = GetGroupBDivisors(ending).Length;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
gpu.Dispose();
|
||||
GpuPrimeWorkLimiter.Release();
|
||||
return compositeDetected;
|
||||
}
|
||||
|
||||
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static bool EvaluateWithOpenNumericFallback(ulong n)
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static bool EvaluateWithOpenNumericFallback(ulong n)
|
||||
{
|
||||
return Prime.Numbers.IsPrime(n);
|
||||
}
|
||||
@@ -327,7 +327,7 @@ public sealed class HeuristicPrimeTester
|
||||
};
|
||||
|
||||
[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;
|
||||
int bestEndingIndex = -1;
|
||||
|
||||
Reference in new issue
Block a user