Remove all concurrency code

Muddled the code too much, and only gave a marginal performance improvement in the grand scheme of things. Other ways to parallelize MCTS will be nicer to implement and could yield better results.
This commit is contained in:
Asriel Camora
2023-07-07 20:17:35 +02:00
parent 3ab50d389e
commit 636501ab86
11 changed files with 153 additions and 431 deletions
+9 -60
View File
@@ -7,6 +7,8 @@ namespace Craftimizer.Solver.Crafty;
public struct ActionSet
{
private const bool IsDeterministic = true;
private uint bits;
[Pure]
@@ -19,24 +21,6 @@ public struct ActionSet
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint ToMask(ActionType action) => 1u << FromAction(action) + 1;
// Return true if action was newly added and not there before.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool AddActionConcurrent(ActionType action)
{
var mask = ToMask(action);
var old = Interlocked.Or(ref bits, mask);
return (old & mask) == 0;
}
// Return true if action was newly removed and not already gone.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool RemoveActionConcurrent(ActionType action)
{
var mask = ToMask(action);
var old = Interlocked.And(ref bits, ~mask);
return (old & mask) != 0;
}
// Return true if action was newly added and not there before.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool AddAction(ActionType action)
@@ -71,52 +55,17 @@ public struct ActionSet
public readonly bool IsEmpty => bits == 0;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ActionType SelectRandom(Random random) => ElementAt(random.Next(Count));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ActionType? PopRandomConcurrent(Random random)
{
uint snapshot;
uint newValue;
ActionType action;
do
{
snapshot = bits;
if (snapshot == 0)
return null;
var count = BitOperations.PopCount(snapshot);
var index = random.Next(count);
action = ToAction(Intrinsics.NthBitSet(snapshot, index) - 1);
newValue = snapshot & ~ToMask(action);
}
while (Interlocked.CompareExchange(ref bits, newValue, snapshot) != snapshot);
return action;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ActionType? PopFirstConcurrent()
{
uint snapshot;
uint newValue;
ActionType action;
do
{
snapshot = bits;
if (snapshot == 0)
return null;
action = ToAction(Intrinsics.NthBitSet(snapshot, 0) - 1);
newValue = snapshot & ~ToMask(action);
}
while (Interlocked.CompareExchange(ref bits, newValue, snapshot) != snapshot);
return action;
}
public readonly ActionType SelectRandom(Random random) =>
IsDeterministic ?
First() :
ElementAt(random.Next(Count));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ActionType PopRandom(Random random)
{
if (IsDeterministic)
return PopFirst();
var action = ElementAt(random.Next(Count));
RemoveAction(action);
return action;