Simplify scheduler configuration and pending composite caching
This commit is contained in:
1 parent
e9cff7bfcd
commit
a7ed762428
3 files changed
+16
-41
No files matched your search
@@ -858,13 +858,13 @@ internal static partial class PrimeOrderCalculator
|
|||||||
if (currentTimestamp > deadlineTimestamp)
|
if (currentTimestamp > deadlineTimestamp)
|
||||||
{
|
{
|
||||||
pollardRhoDeadlineReached = true;
|
pollardRhoDeadlineReached = true;
|
||||||
pending.Add(new PendingEntry(composite, knownComposite: false).WithPrimality(false));
|
pending.Add(new PendingEntry(composite, knownComposite: true));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!TryPollardRho(composite, deadlineTimestamp, out ulong factor))
|
if (!TryPollardRho(composite, deadlineTimestamp, out ulong factor))
|
||||||
{
|
{
|
||||||
pending.Add(new PendingEntry(composite, knownComposite: false).WithPrimality(false));
|
pending.Add(new PendingEntry(composite, knownComposite: true));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
namespace PerfectNumbers.Core;
|
namespace PerfectNumbers.Core;
|
||||||
|
|
||||||
internal static partial class PrimeOrderCalculator
|
internal static partial class PrimeOrderCalculator
|
||||||
@@ -14,10 +16,8 @@ internal static partial class PrimeOrderCalculator
|
|||||||
|
|
||||||
public static PrimeOrderSearchConfig HeuristicDefault
|
public static PrimeOrderSearchConfig HeuristicDefault
|
||||||
{
|
{
|
||||||
get
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
{
|
get => _heuristicDefault;
|
||||||
return _heuristicDefault;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static readonly PrimeOrderSearchConfig StrictDefault = new(smallFactorLimit: 1_000_000, pollardRhoMilliseconds: 0, maxPowChecks: 0, PrimeOrderMode.Strict);
|
public static readonly PrimeOrderSearchConfig StrictDefault = new(smallFactorLimit: 1_000_000, pollardRhoMilliseconds: 0, maxPowChecks: 0, PrimeOrderMode.Strict);
|
||||||
|
|||||||
@@ -1,31 +1,18 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace PerfectNumbers.Core;
|
namespace PerfectNumbers.Core;
|
||||||
|
|
||||||
public sealed class UnboundedTaskScheduler : TaskScheduler
|
public sealed class UnboundedTaskScheduler : TaskScheduler
|
||||||
{
|
{
|
||||||
private static readonly object InstanceLock = new();
|
|
||||||
private static UnboundedTaskScheduler? _instance;
|
private static UnboundedTaskScheduler? _instance;
|
||||||
private static int _configuredThreadCount = Environment.ProcessorCount;
|
private static int _configuredThreadCount = Environment.ProcessorCount;
|
||||||
|
|
||||||
public static UnboundedTaskScheduler Instance
|
public static UnboundedTaskScheduler Instance => _instance ??= new UnboundedTaskScheduler(_configuredThreadCount);
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
lock (InstanceLock)
|
|
||||||
{
|
|
||||||
_instance ??= new UnboundedTaskScheduler(GetNormalizedThreadCount(_configuredThreadCount));
|
|
||||||
return _instance;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int ConfiguredThreadCount => GetNormalizedThreadCount(Volatile.Read(ref _configuredThreadCount));
|
public static int ConfiguredThreadCount => _configuredThreadCount;
|
||||||
|
|
||||||
private readonly object _poolLock = new();
|
|
||||||
private TaskThreadPool _threadPool;
|
private TaskThreadPool _threadPool;
|
||||||
|
|
||||||
private UnboundedTaskScheduler(int threadCount)
|
private UnboundedTaskScheduler(int threadCount)
|
||||||
@@ -35,20 +22,15 @@ public sealed class UnboundedTaskScheduler : TaskScheduler
|
|||||||
|
|
||||||
public static void ConfigureThreadCount(int threadCount)
|
public static void ConfigureThreadCount(int threadCount)
|
||||||
{
|
{
|
||||||
int normalizedThreadCount = GetNormalizedThreadCount(threadCount);
|
_configuredThreadCount = threadCount;
|
||||||
|
PrimeOrderCalculator.PrimeOrderSearchConfig.ConfigureHeuristicDefault(threadCount);
|
||||||
|
|
||||||
lock (InstanceLock)
|
// On EvenPerfectBitScanner's execution path the scheduler instance is created after configuration,
|
||||||
{
|
// so updating an existing pool would never run. Leave the update path disabled for now.
|
||||||
Volatile.Write(ref _configuredThreadCount, normalizedThreadCount);
|
// if (_instance is not null)
|
||||||
PrimeOrderCalculator.PrimeOrderSearchConfig.ConfigureHeuristicDefault(normalizedThreadCount);
|
// {
|
||||||
|
// _instance.UpdateThreadPool(threadCount);
|
||||||
if (_instance is null)
|
// }
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_instance.UpdateThreadPool(normalizedThreadCount);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override int MaximumConcurrencyLevel => int.MaxValue;
|
public override int MaximumConcurrencyLevel => int.MaxValue;
|
||||||
@@ -59,8 +41,6 @@ public sealed class UnboundedTaskScheduler : TaskScheduler
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateThreadPool(int threadCount)
|
private void UpdateThreadPool(int threadCount)
|
||||||
{
|
|
||||||
lock (_poolLock)
|
|
||||||
{
|
{
|
||||||
if (_threadPool.ThreadCount == threadCount)
|
if (_threadPool.ThreadCount == threadCount)
|
||||||
{
|
{
|
||||||
@@ -72,7 +52,6 @@ public sealed class UnboundedTaskScheduler : TaskScheduler
|
|||||||
_threadPool = newPool;
|
_threadPool = newPool;
|
||||||
oldPool.Dispose();
|
oldPool.Dispose();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void ExecuteTask(Task task)
|
private void ExecuteTask(Task task)
|
||||||
{
|
{
|
||||||
@@ -89,8 +68,4 @@ public sealed class UnboundedTaskScheduler : TaskScheduler
|
|||||||
return _threadPool.GetScheduledTasks();
|
return _threadPool.GetScheduledTasks();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int GetNormalizedThreadCount(int threadCount)
|
|
||||||
{
|
|
||||||
return threadCount < 1 ? 1 : threadCount;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user