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:
@@ -1,9 +1,6 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Craftimizer.Solver.Crafty;
|
||||
|
||||
@@ -28,51 +25,26 @@ public struct NodeScoresBuffer
|
||||
// The benchmark reaches 20 at most, but here we have a little leeway just in case.
|
||||
private const int MaxSize = 24;
|
||||
|
||||
private static int BatchSize = Vector<float>.Count;
|
||||
private static int BatchSizeBits = int.Log2(BatchSize);
|
||||
private static int BatchSizeMask = BatchSize - 1;
|
||||
private static readonly int BatchSize = Vector<float>.Count;
|
||||
private static readonly int BatchSizeBits = int.Log2(BatchSize);
|
||||
private static readonly int BatchSizeMask = BatchSize - 1;
|
||||
|
||||
private static int BatchCount = MaxSize / BatchSize;
|
||||
private static readonly int BatchCount = MaxSize / BatchSize;
|
||||
|
||||
public ScoresBatch[] Data;
|
||||
private int index;
|
||||
private int count;
|
||||
|
||||
public readonly int Count => count;
|
||||
|
||||
public void AddConcurrent()
|
||||
{
|
||||
if (Data == null)
|
||||
Interlocked.CompareExchange(ref Data, new ScoresBatch[BatchCount], null);
|
||||
|
||||
var idx = Interlocked.Increment(ref index) - 1;
|
||||
|
||||
var (arrayIdx, _) = GetArrayIndex(idx);
|
||||
|
||||
if (Data[arrayIdx] == null)
|
||||
Interlocked.CompareExchange(ref Data[arrayIdx], new ScoresBatch(), null);
|
||||
|
||||
Interlocked.Increment(ref count);
|
||||
}
|
||||
public int Count { get; private set; }
|
||||
|
||||
public void Add()
|
||||
{
|
||||
Data ??= new ScoresBatch[BatchCount];
|
||||
|
||||
var idx = count++;
|
||||
var idx = Count++;
|
||||
|
||||
var (arrayIdx, _) = GetArrayIndex(idx);
|
||||
|
||||
Data[arrayIdx] ??= new();
|
||||
}
|
||||
|
||||
public readonly void VisitConcurrent((int arrayIdx, int subIdx) at, float score)
|
||||
{
|
||||
Intrinsics.CASAdd(ref Data[at.arrayIdx].ScoreSum.Span[at.subIdx], score);
|
||||
Intrinsics.CASMax(ref Data[at.arrayIdx].MaxScore.Span[at.subIdx], score);
|
||||
Interlocked.Increment(ref Data[at.arrayIdx].Visits.Span[at.subIdx]);
|
||||
}
|
||||
|
||||
public readonly void Visit((int arrayIdx, int subIdx) at, float score)
|
||||
{
|
||||
Data[at.arrayIdx].ScoreSum.Span[at.subIdx] += score;
|
||||
|
||||
Reference in New Issue
Block a user