// Step 19. Check for splitting the search
if ( !SpNode
+ && Threads.size() >= 2
&& depth >= Threads.minimumSplitDepth
- && Threads.available_slave(thisThread)
+ && ( !thisThread->activeSplitPoint
+ || !thisThread->activeSplitPoint->allowLatejoin)
&& thisThread->splitPointsSize < MAX_SPLITPOINTS_PER_THREAD)
{
assert(bestValue > -VALUE_INFINITE && bestValue < beta);
assert(searching);
- searching = false;
activePosition = NULL;
sp->slavesMask.reset(idx);
+ sp->allowLatejoin = false;
sp->nodes += pos.nodes_searched();
// Wake up the master thread so to allow it to return from the idle
// the sp master. Also accessing other Thread objects is unsafe because
// if we are exiting there is a chance that they are already freed.
sp->mutex.unlock();
+
+ // Try to late join to another splitpoint
+ if (Threads.size() <= 2 || !attempt_to_latejoin()) // FIXME: attempt_to_latejoin() is theoretically unsafe when were are exiting the program...
+ searching = false;
}
// If this thread is the master of a split point and all slaves have finished
}
}
+bool Thread::attempt_to_latejoin()
+{
+ SplitPoint *sp;
+ size_t i;
+ bool success = false;
+
+ for (i = 0; i < Threads.size(); ++i)
+ {
+ int size = Threads[i]->splitPointsSize; // Make a local copy to prevent size from changing under our feet.
+
+ sp = size ? &Threads[i]->splitPoints[size - 1] : NULL;
+
+ if ( sp
+ && sp->allowLatejoin
+ && available_to(Threads[i], true))
+ break;
+ }
+
+ if (i == Threads.size())
+ return false; // No suitable splitpoint found!
+
+ // Recheck conditions under lock protection
+ Threads.mutex.lock();
+ sp->mutex.lock();
+
+ if ( sp->allowLatejoin
+ && available_to(Threads[i], true))
+ {
+ activeSplitPoint = sp;
+ sp->slavesMask.set(this->idx);
+ success = true;
+ }
+
+ sp->mutex.unlock();
+ Threads.mutex.unlock();
+
+ return success;
+}
/// check_time() is called by the timer thread when the timer triggers. It is
/// used to print debug info and, more importantly, to detect when we are out of
// which are busy searching the split point at the top of slave's split point
// stack (the "helpful master concept" in YBWC terminology).
-bool Thread::available_to(const Thread* master) const {
+bool Thread::available_to(const Thread* master, bool latejoin) const {
- if (searching)
+ if (searching && !latejoin)
return false;
// Make a local copy to be sure it doesn't become zero under our feet while
Thread* ThreadPool::available_slave(const Thread* master) const {
for (const_iterator it = begin(); it != end(); ++it)
- if ((*it)->available_to(master))
+ if ((*it)->available_to(master, false))
return *it;
return NULL;
Threads.mutex.lock();
sp.mutex.lock();
+ sp.allowLatejoin = true; // Only set this under lock protection
++splitPointsSize;
activeSplitPoint = &sp;
activePosition = NULL;
// Shared data
Mutex mutex;
std::bitset<MAX_THREADS> slavesMask;
+ volatile bool allowLatejoin;
volatile uint64_t nodes;
volatile Value alpha;
volatile Value bestValue;
Thread();
virtual void idle_loop();
+ bool attempt_to_latejoin();
bool cutoff_occurred() const;
- bool available_to(const Thread* master) const;
+ bool available_to(const Thread* master, bool latejoin) const;
template <bool Fake>
void split(Position& pos, const Search::Stack* ss, Value alpha, Value beta, Value* bestValue, Move* bestMove,