-// 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>
-void Thread::split(Position& pos, const Stack* ss, Value alpha, Value beta, Value* bestValue,
- Move* bestMove, Depth depth, int moveCount,
- MovePicker* movePicker, int nodeType, bool cutNode) {
-
- assert(pos.pos_is_ok());
- assert(-VALUE_INFINITE < *bestValue && *bestValue <= alpha && alpha < beta && beta <= VALUE_INFINITE);
- assert(depth >= Threads.minimumSplitDepth);
- assert(searching);
- assert(splitPointsSize < MAX_SPLITPOINTS_PER_THREAD);
-
- // Pick the next available split point from the split point stack
- SplitPoint& sp = splitPoints[splitPointsSize];
-
- sp.masterThread = this;
- sp.parentSplitPoint = activeSplitPoint;
- sp.slavesMask = 1ULL << idx;
- sp.depth = depth;
- sp.bestValue = *bestValue;
- sp.bestMove = *bestMove;
- sp.alpha = alpha;
- sp.beta = beta;
- sp.nodeType = nodeType;
- sp.cutNode = cutNode;
- sp.movePicker = movePicker;
- 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.
- Threads.mutex.lock();
- sp.mutex.lock();
-
- ++splitPointsSize;
- activeSplitPoint = &sp;
- activePosition = NULL;
-
- size_t slavesCnt = 1; // This thread is always included
- Thread* slave;
-
- while ( (slave = Threads.available_slave(this)) != NULL
- && ++slavesCnt <= Threads.maxThreadsPerSplitPoint && !Fake)
- {
- sp.slavesMask |= 1ULL << slave->idx;
- slave->activeSplitPoint = &sp;
- slave->searching = true; // Slave leaves idle_loop()
- slave->notify_one(); // Could be sleeping
- }