diff --git a/Benchmark/Program.cs b/Benchmark/Program.cs index 61e8cd3..6719530 100644 --- a/Benchmark/Program.cs +++ b/Benchmark/Program.cs @@ -48,6 +48,7 @@ internal static class Program var config = new SolverConfig() { Iterations = 30_000,//1_000_000 + ThreadCount = 2, }; var s = Stopwatch.StartNew(); diff --git a/Solver/Craftimizer.Solver.csproj b/Solver/Craftimizer.Solver.csproj index 985ce10..f50b072 100644 --- a/Solver/Craftimizer.Solver.csproj +++ b/Solver/Craftimizer.Solver.csproj @@ -4,6 +4,7 @@ net7.0 enable enable + True diff --git a/Solver/Crafty/Solver.cs b/Solver/Crafty/Solver.cs index 8b7796b..df2864c 100644 --- a/Solver/Crafty/Solver.cs +++ b/Solver/Crafty/Solver.cs @@ -213,7 +213,7 @@ public class Solver } } - public void Search(CancellationToken token) + public void SearchThread(CancellationToken token) { Simulator simulator = new(RootNode.State.State, Config.MaxStepCount); for (var i = 0; i < Config.Iterations; i++) @@ -228,6 +228,16 @@ public class Solver } } + public void Search(CancellationToken token) + { + var tasks = new Task[Config.ThreadCount]; + for (var i = 0; i < Config.ThreadCount; ++i) + { + tasks[i] = Task.Run(() => SearchThread(token), token); + } + Task.WaitAll(tasks, token); + } + [Pure] public (List Actions, SimulationNode Node) Solution() { diff --git a/Solver/Crafty/SolverConfig.cs b/Solver/Crafty/SolverConfig.cs index 5fe72c4..46c7515 100644 --- a/Solver/Crafty/SolverConfig.cs +++ b/Solver/Crafty/SolverConfig.cs @@ -10,6 +10,7 @@ public readonly record struct SolverConfig public float MaxScoreWeightingConstant { get; init; } public float ExplorationConstant { get; init; } public int MaxStepCount { get; init; } + public int ThreadCount { get; init; } public SolverConfig() { @@ -18,5 +19,6 @@ public readonly record struct SolverConfig MaxScoreWeightingConstant = 0.1f; ExplorationConstant = 4f; MaxStepCount = 25; + ThreadCount = Environment.ProcessorCount; } }