}
for (Thread* th : Threads)
+ {
th->maxPly = 0;
+ th->notify_one(); // Wake up all the threads
+ }
Threads.timer->run = true;
- Threads.timer->notify_one(); // Wake up the recurring timer
+ Threads.timer->notify_one(); // Start the recurring timer
id_loop(RootPos); // Let's start searching !
*pv = MOVE_NONE;
}
- // update_stats() updates killers, history, countermoves and followupmoves stats after a fail-high
- // of a quiet move.
+ // update_stats() updates killers, history, countermoves and followupmoves
+ // stats after a fail-high of a quiet move.
void update_stats(const Position& pos, Stack* ss, Move move, Depth depth, Move* quiets, int quietsCnt) {
ss->killers[0] = move;
}
- // Increase history value of the cut-off move and decrease all the other
- // played quiet moves.
Value bonus = Value((depth / ONE_PLY) * (depth / ONE_PLY));
+
+ Square prevSq = to_sq((ss-1)->currentMove);
+ HistoryStats& cmh = CounterMovesHistory[pos.piece_on(prevSq)][prevSq];
+
History.update(pos.moved_piece(move), to_sq(move), bonus);
- for (int i = 0; i < quietsCnt; ++i)
+ if (is_ok((ss-1)->currentMove))
{
- Move m = quiets[i];
- History.update(pos.moved_piece(m), to_sq(m), -bonus);
+ Countermoves.update(pos.piece_on(prevSq), prevSq, move);
+ cmh.update(pos.moved_piece(move), to_sq(move), bonus);
}
- if (is_ok((ss-1)->currentMove))
+ // Decrease all the other played quiet moves
+ for (int i = 0; i < quietsCnt; ++i)
{
- Square prevMoveSq = to_sq((ss-1)->currentMove);
- Piece prevMovePiece = pos.piece_on(prevMoveSq);
- Countermoves.update(prevMovePiece, prevMoveSq, move);
+ History.update(pos.moved_piece(quiets[i]), to_sq(quiets[i]), -bonus);
- HistoryStats& cmh = CounterMovesHistory[prevMovePiece][prevMoveSq];
- cmh.update(pos.moved_piece(move), to_sq(move), bonus);
- for (int i = 0; i < quietsCnt; ++i)
- {
- Move m = quiets[i];
- cmh.update(pos.moved_piece(m), to_sq(m), -bonus);
- }
+ if (is_ok((ss-1)->currentMove))
+ cmh.update(pos.moved_piece(quiets[i]), to_sq(quiets[i]), -bonus);
}
if (is_ok((ss-2)->currentMove) && (ss-1)->currentMove == (ss-1)->ttMove)
{
- Square prevOwnMoveSq = to_sq((ss-2)->currentMove);
- Followupmoves.update(pos.piece_on(prevOwnMoveSq), prevOwnMoveSq, move);
+ Square prevPrevSq = to_sq((ss-2)->currentMove);
+ Followupmoves.update(pos.piece_on(prevPrevSq), prevPrevSq, move);
}
}
assert(!this_sp || (this_sp->master == this && searching));
- while ( !exit
- && !(this_sp && this_sp->slavesMask.none()))
+ while (!exit && !(this_sp && this_sp->slavesMask.none()))
{
- // If there is nothing to do, sleep.
- while( !exit
- && !(this_sp && this_sp->slavesMask.none())
- && !searching)
- {
- if ( !this_sp
- && !Threads.main()->thinking)
- {
- std::unique_lock<Mutex> lk(mutex);
- while (!exit && !Threads.main()->thinking)
- sleepCondition.wait(lk);
- }
- else
- std::this_thread::yield();
- }
-
// If this thread has been assigned work, launch a search
while (searching)
{
- mutex.lock();
+ spinlock.acquire();
assert(activeSplitPoint);
SplitPoint* sp = activeSplitPoint;
- mutex.unlock();
+ spinlock.release();
Stack stack[MAX_PLY+4], *ss = stack+2; // To allow referencing (ss-2) and (ss+2)
Position pos(*sp->pos, this);
sp->spinlock.release();
}
}
+
+ // If search is finished then sleep, otherwise just yield
+ if (!Threads.main()->thinking)
+ {
+ assert(!this_sp);
+
+ std::unique_lock<Mutex> lk(mutex);
+ while (!exit && !Threads.main()->thinking)
+ sleepCondition.wait(lk);
+ }
+ else
+ std::this_thread::yield(); // Wait for a new job or for our slaves to finish
}
}