-// 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, or because we have no unused split
-// point objects), 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 ThreadsManager::split(Position& pos, Stack* ss, Value alpha, Value beta,
- Value bestValue, Depth depth, Move threatMove,
- int moveCount, MovePicker* mp, int nodeType) {
- assert(pos.pos_is_ok());
- assert(bestValue > -VALUE_INFINITE);
- assert(bestValue <= alpha);
- assert(alpha < beta);
- assert(beta <= VALUE_INFINITE);
- assert(depth > DEPTH_ZERO);
- assert(pos.thread() >= 0 && pos.thread() < activeThreads);
- assert(activeThreads > 1);
-
- int i, master = pos.thread();
- Thread& masterThread = threads[master];
-
- // If we already have too many active split points, don't split
- if (masterThread.activeSplitPoints >= MAX_ACTIVE_SPLIT_POINTS)
- return bestValue;
-
- // Pick the next available split point from the split point stack
- SplitPoint* sp = &masterThread.splitPoints[masterThread.activeSplitPoints];
-
- // Initialize the split point
- sp->parent = masterThread.splitPoint;
- sp->master = master;
- sp->is_betaCutoff = false;
- sp->depth = depth;
- sp->threatMove = threatMove;
- sp->alpha = alpha;
- sp->beta = beta;
- sp->nodeType = nodeType;
- sp->bestValue = bestValue;
- sp->mp = mp;
- sp->moveCount = moveCount;
- sp->pos = &pos;
- sp->nodes = 0;
- sp->ss = ss;
-
- for (i = 0; i < activeThreads; i++)
- sp->is_slave[i] = false;
-
- // If we are here it means we are not available
- assert(masterThread.is_searching);
-
- int workersCnt = 1; // At least the master is included
-
- // Try to allocate available threads and ask them to start searching setting
- // is_searching flag. This must be done under lock protection to avoid concurrent
- // allocation of the same slave by another master.
- lock_grab(threadsLock);
-
- for (i = 0; !Fake && i < activeThreads && workersCnt < maxThreadsPerSplitPoint; i++)
- if (threads[i].is_available_to(master))
- {
- workersCnt++;
- sp->is_slave[i] = true;
- threads[i].splitPoint = sp;
-
- // This makes the slave to exit from idle_loop()
- threads[i].is_searching = true;
-
- if (useSleepingThreads)
- threads[i].wake_up();
- }
-
- lock_release(threadsLock);
-
- // We failed to allocate even one slave, return
- if (!Fake && workersCnt == 1)
- return bestValue;
-
- masterThread.splitPoint = sp;
- masterThread.activeSplitPoints++;
-
- // Everything is set up. The master thread enters the idle loop, from which
- // it will instantly launch a search, because its is_searching flag is set.
- // We pass the split point as a parameter to the idle loop, which means that
- // the thread will return from the idle loop when all slaves have finished
- // their work at this split point.
- masterThread.idle_loop(sp);
-
- // 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(!masterThread.is_searching);