Rudamentary locked multithreading
2x faster, but 8x threads.. not very good yet
This commit is contained in:
@@ -47,8 +47,8 @@ internal static class Program
|
|||||||
|
|
||||||
var config = new SolverConfig()
|
var config = new SolverConfig()
|
||||||
{
|
{
|
||||||
Iterations = 30_000,//1_000_000
|
Iterations = 30_000 / 8,//1_000_000
|
||||||
ThreadCount = 2,
|
ThreadCount = 8,
|
||||||
};
|
};
|
||||||
|
|
||||||
var s = Stopwatch.StartNew();
|
var s = Stopwatch.StartNew();
|
||||||
|
|||||||
@@ -129,6 +129,7 @@ public class Solver
|
|||||||
var s = new Vector<float>(scoreSums);
|
var s = new Vector<float>(scoreSums);
|
||||||
var m = new Vector<float>(maxScores);
|
var m = new Vector<float>(maxScores);
|
||||||
var v = new Vector<float>(visits);
|
var v = new Vector<float>(visits);
|
||||||
|
v = Vector.Max(v, Vector<float>.One);
|
||||||
var exploitation = (W * (s / v)) + (w * m);
|
var exploitation = (W * (s / v)) + (w * m);
|
||||||
var exploration = CVector * Intrinsics.ReciprocalSqrt(v);
|
var exploration = CVector * Intrinsics.ReciprocalSqrt(v);
|
||||||
var evalScores = exploitation + exploration;
|
var evalScores = exploitation + exploration;
|
||||||
@@ -151,13 +152,17 @@ public class Solver
|
|||||||
var node = RootNode;
|
var node = RootNode;
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
|
if (!Monitor.TryEnter(node, 5))
|
||||||
|
return Select();
|
||||||
var expandable = node.State.AvailableActions.Count != 0;
|
var expandable = node.State.AvailableActions.Count != 0;
|
||||||
var likelyTerminal = node.Children.Count == 0;
|
var likelyTerminal = node.Children.Count == 0;
|
||||||
if (expandable || likelyTerminal)
|
if (expandable || likelyTerminal)
|
||||||
return node;
|
return node;
|
||||||
|
|
||||||
// select the node with the highest score
|
// select the node with the highest score
|
||||||
node = EvalBestChild(node.State.Scores.Visits, CollectionsMarshal.AsSpan(node.Children));
|
var n = EvalBestChild(node.State.Scores.Visits, CollectionsMarshal.AsSpan(node.Children));
|
||||||
|
Monitor.Exit(node);
|
||||||
|
node = n;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -226,6 +231,7 @@ public class Solver
|
|||||||
|
|
||||||
var selectedNode = Select();
|
var selectedNode = Select();
|
||||||
var (endNode, _, score) = ExpandAndRollout(simulator, selectedNode);
|
var (endNode, _, score) = ExpandAndRollout(simulator, selectedNode);
|
||||||
|
Monitor.Exit(selectedNode);
|
||||||
|
|
||||||
Backpropagate(endNode, score);
|
Backpropagate(endNode, score);
|
||||||
}
|
}
|
||||||
@@ -235,9 +241,7 @@ public class Solver
|
|||||||
{
|
{
|
||||||
var tasks = new Task[Config.ThreadCount];
|
var tasks = new Task[Config.ThreadCount];
|
||||||
for (var i = 0; i < Config.ThreadCount; ++i)
|
for (var i = 0; i < Config.ThreadCount; ++i)
|
||||||
{
|
|
||||||
tasks[i] = Task.Run(() => SearchThread(token), token);
|
tasks[i] = Task.Run(() => SearchThread(token), token);
|
||||||
}
|
|
||||||
Task.WaitAll(tasks, token);
|
Task.WaitAll(tasks, token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user