&& Threads.size() >= 2
&& depth >= Threads.minimumSplitDepth
&& ( !thisThread->activeSplitPoint
- || !thisThread->activeSplitPoint->allSlavesSearching)
+ || !thisThread->activeSplitPoint->allSlavesSearching
+ || ( int(Threads.size()) > MAX_SLAVES_PER_SPLITPOINT
+ && thisThread->activeSplitPoint->slavesCount == MAX_SLAVES_PER_SPLITPOINT))
&& thisThread->splitPointsSize < MAX_SPLITPOINTS_PER_THREAD)
{
assert(bestValue > -VALUE_INFINITE && bestValue < beta);
// Try to late join to another split point if none of its slaves has
// already finished.
if (Threads.size() > 2)
+ {
+ SplitPoint *bestSp = NULL;
+ int bestThread = 0;
+ int bestScore = INT_MAX;
+
for (size_t i = 0; i < Threads.size(); ++i)
{
const int size = Threads[i]->splitPointsSize; // Local copy
if ( sp
&& sp->allSlavesSearching
+ && sp->slavesCount < MAX_SLAVES_PER_SPLITPOINT
&& available_to(Threads[i]))
{
- // Recheck the conditions under lock protection
- Threads.mutex.lock();
- sp->mutex.lock();
+ int score = sp->spLevel * 256 * 256 + sp->slavesCount * 256 - sp->depth * 1;
- if ( sp->allSlavesSearching
- && available_to(Threads[i]))
+ if (score < bestScore)
{
- sp->slavesMask.set(idx);
- activeSplitPoint = sp;
- searching = true;
+ bestSp = sp;
+ bestThread = i;
+ bestScore = score;
}
+ }
+ }
- sp->mutex.unlock();
- Threads.mutex.unlock();
-
- break; // Just a single attempt
+ if (bestSp)
+ {
+ sp = bestSp;
+
+ // Recheck the conditions under lock protection
+ Threads.mutex.lock();
+ sp->mutex.lock();
+
+ if ( sp->allSlavesSearching
+ && sp->slavesCount < MAX_SLAVES_PER_SPLITPOINT
+ && available_to(Threads[bestThread]))
+ {
+ sp->slavesMask.set(idx);
+ sp->slavesCount++;
+ activeSplitPoint = sp;
+ searching = true;
}
+
+ sp->mutex.unlock();
+ Threads.mutex.unlock();
}
+ }
}
// Grab the lock to avoid races with Thread::notify_one()
sp.masterThread = this;
sp.parentSplitPoint = activeSplitPoint;
+ sp.spLevel = activeSplitPoint ? activeSplitPoint->spLevel + 1 : 0;
sp.slavesMask = 0, sp.slavesMask.set(idx);
+ sp.slavesCount = 1;
sp.depth = depth;
sp.bestValue = *bestValue;
sp.bestMove = *bestMove;
Thread* slave;
- while ((slave = Threads.available_slave(this)) != NULL)
+ while ( sp.slavesCount < MAX_SLAVES_PER_SPLITPOINT
+ && (slave = Threads.available_slave(this)) != NULL)
{
sp.slavesMask.set(slave->idx);
+ sp.slavesCount++;
slave->activeSplitPoint = &sp;
slave->searching = true; // Slave leaves idle_loop()
slave->notify_one(); // Could be sleeping
const int MAX_THREADS = 128;
const int MAX_SPLITPOINTS_PER_THREAD = 8;
+const int MAX_SLAVES_PER_SPLITPOINT = 4;
/// Mutex and ConditionVariable struct are wrappers of the low level locking
/// machinery and are modeled after the corresponding C++11 classes.
const Position* pos;
Search::Stack* ss;
Thread* masterThread;
+ int spLevel;
Depth depth;
Value beta;
int nodeType;
// Shared variable data
Mutex mutex;
std::bitset<MAX_THREADS> slavesMask;
+ int slavesCount;
volatile bool allSlavesSearching;
volatile uint64_t nodes;
volatile Value alpha;