-// split() does the actual work of distributing the work at a node between
-// several available threads. If it does not succeed in splitting the node
-// (because no idle threads are available), the function immediately returns.
-// If splitting is possible, a SplitPoint object is initialized with all the
-// data that must be copied to the helper threads and then helper threads are
-// told that they have been assigned work. This will cause them to instantly
-// leave their idle loops and call search(). When all threads have returned from
-// search() then split() returns.
-
-template <bool Fake>
-Value ThreadPool::split(Position& pos, Stack* ss, Value alpha, Value beta,
- Value bestValue, Move* bestMove, Depth depth, Move threatMove,
- int moveCount, MovePicker& mp, int nodeType) {
-
- assert(pos.pos_is_ok());
- assert(bestValue <= alpha && alpha < beta && beta <= VALUE_INFINITE);
- assert(bestValue > -VALUE_INFINITE);
- assert(depth >= Threads.minimumSplitDepth);
-
- Thread* master = pos.this_thread();
-
- assert(master->searching);
- assert(master->splitPointsSize < MAX_SPLITPOINTS_PER_THREAD);
-
- // Pick the next available split point from the split point stack
- SplitPoint& sp = master->splitPoints[master->splitPointsSize];
-
- sp.master = master;
- sp.parent = master->activeSplitPoint;
- sp.slavesMask = 1ULL << master->idx;
- sp.depth = depth;
- sp.bestMove = *bestMove;
- sp.threatMove = threatMove;
- sp.alpha = alpha;
- sp.beta = beta;
- sp.nodeType = nodeType;
- sp.bestValue = bestValue;
- sp.mp = ∓
- sp.moveCount = moveCount;
- sp.pos = &pos;
- sp.nodes = 0;
- sp.cutoff = false;
- sp.ss = ss;
-
- // Try to allocate available threads and ask them to start searching setting
- // 'searching' flag. This must be done under lock protection to avoid concurrent
- // allocation of the same slave by another master.
- mutex.lock();
- sp.mutex.lock();
-
- master->splitPointsSize++;
- master->activeSplitPoint = &sp;
-
- size_t slavesCnt = 1; // Master is always included
-
- for (size_t i = 0; i < threads.size() && !Fake; ++i)
- if (threads[i]->is_available_to(master) && ++slavesCnt <= maxThreadsPerSplitPoint)
- {
- sp.slavesMask |= 1ULL << threads[i]->idx;
- threads[i]->activeSplitPoint = &sp;
- threads[i]->searching = true; // Slave leaves idle_loop()
- threads[i]->notify_one(); // Could be sleeping
- }
-
- sp.mutex.unlock();
- mutex.unlock();
-
- // Everything is set up. The master thread enters the idle loop, from which
- // it will instantly launch a search, because its 'searching' flag is set.
- // The thread will return from the idle loop when all slaves have finished
- // their work at this split point.
- if (slavesCnt > 1 || Fake)
- {
- master->Thread::idle_loop(); // Force a call to base class idle_loop()
-
- // In helpful master concept a master can help only a sub-tree of its split
- // point, and because here is all finished is not possible master is booked.
- assert(!master->searching);
- }