Clear heuristic candidate list without try-finally
This commit is contained in:
1 parent
51730bcba3
commit
0a23502441
1 file changed
+99
-103
@@ -407,131 +407,127 @@ internal static partial class PrimeOrderCalculator
|
|||||||
|
|
||||||
int capacity = config.MaxPowChecks <= 0 ? 64 : config.MaxPowChecks * 4;
|
int capacity = config.MaxPowChecks <= 0 ? 64 : config.MaxPowChecks * 4;
|
||||||
List<ulong> candidates = AcquireHeuristicCandidateList(capacity);
|
List<ulong> candidates = AcquireHeuristicCandidateList(capacity);
|
||||||
try
|
FactorEntry[] factorArray = orderFactors.Factors!;
|
||||||
|
// DebugLog("Building candidates list");
|
||||||
|
BuildCandidates(order, factorArray, orderFactors.Count, candidates, capacity);
|
||||||
|
if (candidates.Count == 0)
|
||||||
{
|
{
|
||||||
FactorEntry[] factorArray = orderFactors.Factors!;
|
candidates.Clear();
|
||||||
// DebugLog("Building candidates list");
|
return false;
|
||||||
BuildCandidates(order, factorArray, orderFactors.Count, candidates, capacity);
|
}
|
||||||
if (candidates.Count == 0)
|
|
||||||
|
// DebugLog("Sorting candidates");
|
||||||
|
SortCandidates(prime, previousOrder, candidates);
|
||||||
|
|
||||||
|
int powBudget = config.MaxPowChecks <= 0 ? candidates.Count : config.MaxPowChecks;
|
||||||
|
int powUsed = 0;
|
||||||
|
int candidateCount = candidates.Count;
|
||||||
|
bool allowGpuBatch = true;
|
||||||
|
Span<ulong> candidateSpan = CollectionsMarshal.AsSpan(candidates);
|
||||||
|
|
||||||
|
// DebugLog(() => $"Checking candidates ({candidateCount} candidates, {powBudget} pow budget)");
|
||||||
|
int index = 0;
|
||||||
|
const int MaxGpuBatchSize = 256;
|
||||||
|
const int StackGpuBatchSize = 64;
|
||||||
|
Span<ulong> stackGpuRemainders = stackalloc ulong[StackGpuBatchSize];
|
||||||
|
ArrayPool<ulong> pool = ThreadStaticPools.UlongPool;
|
||||||
|
while (index < candidateCount && powUsed < powBudget)
|
||||||
|
{
|
||||||
|
int remaining = candidateCount - index;
|
||||||
|
int budgetRemaining = powBudget - powUsed;
|
||||||
|
int batchSize = Math.Min(remaining, Math.Min(budgetRemaining, MaxGpuBatchSize));
|
||||||
|
if (batchSize <= 0)
|
||||||
{
|
{
|
||||||
return false;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// DebugLog("Sorting candidates");
|
ReadOnlySpan<ulong> batch = candidateSpan.Slice(index, batchSize);
|
||||||
SortCandidates(prime, previousOrder, candidates);
|
ulong[]? gpuPool = null;
|
||||||
|
Span<ulong> pooledGpuRemainders = default;
|
||||||
int powBudget = config.MaxPowChecks <= 0 ? candidates.Count : config.MaxPowChecks;
|
bool gpuSuccess = false;
|
||||||
int powUsed = 0;
|
bool gpuStackRemainders = false;
|
||||||
int candidateCount = candidates.Count;
|
GpuPow2ModStatus status = GpuPow2ModStatus.Unavailable;
|
||||||
bool allowGpuBatch = true;
|
|
||||||
Span<ulong> candidateSpan = CollectionsMarshal.AsSpan(candidates);
|
if (allowGpuBatch && IsGpuPow2Allowed)
|
||||||
|
|
||||||
// DebugLog(() => $"Checking candidates ({candidateCount} candidates, {powBudget} pow budget)");
|
|
||||||
int index = 0;
|
|
||||||
const int MaxGpuBatchSize = 256;
|
|
||||||
const int StackGpuBatchSize = 64;
|
|
||||||
Span<ulong> stackGpuRemainders = stackalloc ulong[StackGpuBatchSize];
|
|
||||||
ArrayPool<ulong> pool = ThreadStaticPools.UlongPool;
|
|
||||||
while (index < candidateCount && powUsed < powBudget)
|
|
||||||
{
|
{
|
||||||
int remaining = candidateCount - index;
|
if (batchSize <= StackGpuBatchSize)
|
||||||
int budgetRemaining = powBudget - powUsed;
|
|
||||||
int batchSize = Math.Min(remaining, Math.Min(budgetRemaining, MaxGpuBatchSize));
|
|
||||||
if (batchSize <= 0)
|
|
||||||
{
|
{
|
||||||
break;
|
Span<ulong> localRemainders = stackGpuRemainders.Slice(0, batchSize);
|
||||||
}
|
status = PrimeOrderGpuHeuristics.TryPow2ModBatch(batch, prime, localRemainders, divisorData);
|
||||||
|
if (status == GpuPow2ModStatus.Success)
|
||||||
ReadOnlySpan<ulong> batch = candidateSpan.Slice(index, batchSize);
|
|
||||||
ulong[]? gpuPool = null;
|
|
||||||
Span<ulong> pooledGpuRemainders = default;
|
|
||||||
bool gpuSuccess = false;
|
|
||||||
bool gpuStackRemainders = false;
|
|
||||||
GpuPow2ModStatus status = GpuPow2ModStatus.Unavailable;
|
|
||||||
|
|
||||||
if (allowGpuBatch && IsGpuPow2Allowed)
|
|
||||||
{
|
|
||||||
if (batchSize <= StackGpuBatchSize)
|
|
||||||
{
|
{
|
||||||
Span<ulong> localRemainders = stackGpuRemainders.Slice(0, batchSize);
|
gpuSuccess = true;
|
||||||
status = PrimeOrderGpuHeuristics.TryPow2ModBatch(batch, prime, localRemainders, divisorData);
|
gpuStackRemainders = true;
|
||||||
if (status == GpuPow2ModStatus.Success)
|
|
||||||
{
|
|
||||||
gpuSuccess = true;
|
|
||||||
gpuStackRemainders = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
gpuPool = pool.Rent(batchSize);
|
|
||||||
Span<ulong> pooledRemainders = gpuPool.AsSpan(0, batchSize);
|
|
||||||
status = PrimeOrderGpuHeuristics.TryPow2ModBatch(batch, prime, pooledRemainders, divisorData);
|
|
||||||
if (status == GpuPow2ModStatus.Success)
|
|
||||||
{
|
|
||||||
pooledGpuRemainders = pooledRemainders;
|
|
||||||
gpuSuccess = true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
pool.Return(gpuPool, clearArray: false);
|
|
||||||
gpuPool = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!gpuSuccess && (status == GpuPow2ModStatus.Overflow || status == GpuPow2ModStatus.Unavailable))
|
|
||||||
{
|
|
||||||
allowGpuBatch = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
for (int i = 0; i < batchSize && powUsed < powBudget; i++)
|
|
||||||
{
|
{
|
||||||
ulong candidate = batch[i];
|
gpuPool = pool.Rent(batchSize);
|
||||||
powUsed++;
|
Span<ulong> pooledRemainders = gpuPool.AsSpan(0, batchSize);
|
||||||
|
status = PrimeOrderGpuHeuristics.TryPow2ModBatch(batch, prime, pooledRemainders, divisorData);
|
||||||
bool equalsOne;
|
if (status == GpuPow2ModStatus.Success)
|
||||||
if (gpuSuccess)
|
|
||||||
{
|
{
|
||||||
ulong remainderValue = gpuStackRemainders ? stackGpuRemainders[i] : pooledGpuRemainders[i];
|
pooledGpuRemainders = pooledRemainders;
|
||||||
equalsOne = remainderValue == 1UL;
|
gpuSuccess = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
|
||||||
equalsOne = Pow2EqualsOneCpu(candidate, prime, divisorData);
|
|
||||||
}
|
|
||||||
if (!equalsOne)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!TryConfirmCandidateCpu(prime, candidate, divisorData, config, ref powUsed, powBudget))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (gpuPool is not null)
|
|
||||||
{
|
{
|
||||||
pool.Return(gpuPool, clearArray: false);
|
pool.Return(gpuPool, clearArray: false);
|
||||||
|
gpuPool = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = candidate;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!gpuSuccess && (status == GpuPow2ModStatus.Overflow || status == GpuPow2ModStatus.Unavailable))
|
||||||
|
{
|
||||||
|
allowGpuBatch = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < batchSize && powUsed < powBudget; i++)
|
||||||
|
{
|
||||||
|
ulong candidate = batch[i];
|
||||||
|
powUsed++;
|
||||||
|
|
||||||
|
bool equalsOne;
|
||||||
|
if (gpuSuccess)
|
||||||
|
{
|
||||||
|
ulong remainderValue = gpuStackRemainders ? stackGpuRemainders[i] : pooledGpuRemainders[i];
|
||||||
|
equalsOne = remainderValue == 1UL;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
equalsOne = Pow2EqualsOneCpu(candidate, prime, divisorData);
|
||||||
|
}
|
||||||
|
if (!equalsOne)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!TryConfirmCandidateCpu(prime, candidate, divisorData, config, ref powUsed, powBudget))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (gpuPool is not null)
|
if (gpuPool is not null)
|
||||||
{
|
{
|
||||||
pool.Return(gpuPool, clearArray: false);
|
pool.Return(gpuPool, clearArray: false);
|
||||||
}
|
}
|
||||||
|
|
||||||
index += batchSize;
|
candidates.Clear();
|
||||||
|
result = candidate;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (gpuPool is not null)
|
||||||
|
{
|
||||||
|
pool.Return(gpuPool, clearArray: false);
|
||||||
|
}
|
||||||
|
|
||||||
|
index += batchSize;
|
||||||
}
|
}
|
||||||
finally
|
|
||||||
{
|
candidates.Clear();
|
||||||
candidates.Clear();
|
// DebugLog("No candidate confirmed");
|
||||||
}
|
|
||||||
// DebugLog("No candidate confirmed");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
|
|||||||
Reference in new issue
Block a user