summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
0439a79)
No functional change.
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
for (size_t i = 0; i < fens.size(); i++)
{
for (size_t i = 0; i < fens.size(); i++)
{
- Position pos(fens[i], false, 0);
+ Position pos(fens[i], false, NULL);
cerr << "\nPosition: " << i + 1 << '/' << fens.size() << endl;
cerr << "\nPosition: " << i + 1 << '/' << fens.size() << endl;
string fen = sides[0] + char('0' + int(8 - code.length()))
+ sides[1] + "/8/8/8/8/8/8/8 w - - 0 10";
string fen = sides[0] + char('0' + int(8 - code.length()))
+ sides[1] + "/8/8/8/8/8/8/8 w - - 0 10";
- return Position(fen, false, 0).material_key();
+ return Position(fen, false, NULL).material_key();
margins[WHITE] = margins[BLACK] = VALUE_ZERO;
// Probe the material hash table
margins[WHITE] = margins[BLACK] = VALUE_ZERO;
// Probe the material hash table
- ei.mi = Threads[pos.this_thread()].materialTable.probe(pos);
+ ei.mi = pos.this_thread().materialTable.probe(pos);
score += ei.mi->material_value();
// If we have a specialized evaluation function for the current material
score += ei.mi->material_value();
// If we have a specialized evaluation function for the current material
}
// Probe the pawn hash table
}
// Probe the pawn hash table
- ei.pi = Threads[pos.this_thread()].pawnTable.probe(pos);
+ ei.pi = pos.this_thread().pawnTable.probe(pos);
score += ei.pi->pawns_value();
// Initialize attack and king safety bitboards
score += ei.pi->pawns_value();
// Initialize attack and king safety bitboards
-/// Position c'tors. Here we always create a copy of the original position
-/// or the FEN string, we want the new born Position object do not depend
-/// on any external data so we detach state pointer from the source one.
+/// Position::copy() creates a copy of 'pos'. We want the new born Position
+/// object do not depend on any external data so we detach state pointer from
+/// the source one.
-void Position::copy(const Position& pos, int th) {
+void Position::copy(const Position& pos, Thread* th) {
memcpy(this, &pos, sizeof(Position));
startState = *st;
st = &startState;
memcpy(this, &pos, sizeof(Position));
startState = *st;
st = &startState;
nodes = 0;
assert(pos_is_ok());
}
nodes = 0;
assert(pos_is_ok());
}
-Position::Position(const string& fen, bool isChess960, int th) {
-
- from_fen(fen, isChess960);
- threadID = th;
-}
-
/// Position::from_fen() initializes the position object with the given FEN
/// string. This function is not very robust - make sure that input FENs are
/// correct (this is assumed to be the responsibility of the GUI).
/// Position::from_fen() initializes the position object with the given FEN
/// string. This function is not very robust - make sure that input FENs are
/// correct (this is assumed to be the responsibility of the GUI).
-void Position::from_fen(const string& fenStr, bool isChess960) {
+void Position::from_fen(const string& fenStr, bool isChess960, Thread* th) {
/*
A FEN string defines a particular position using only the ASCII character set.
/*
A FEN string defines a particular position using only the ASCII character set.
st->npMaterial[BLACK] = compute_non_pawn_material(BLACK);
st->checkersBB = attackers_to(king_square(sideToMove)) & pieces(~sideToMove);
chess960 = isChess960;
st->npMaterial[BLACK] = compute_non_pawn_material(BLACK);
st->checkersBB = attackers_to(king_square(sideToMove)) & pieces(~sideToMove);
chess960 = isChess960;
- Position p(*this, this_thread());
+ Position p(*this, thisThread);
cout << "\nMove is: " << (sideToMove == BLACK ? ".." : "") << move_to_san(p, move);
}
cout << "\nMove is: " << (sideToMove == BLACK ? ".." : "") << move_to_san(p, move);
}
}
// Prefetch pawn and material hash tables
}
// Prefetch pawn and material hash tables
- prefetch((char*)Threads[threadID].pawnTable.entries[st->pawnKey]);
- prefetch((char*)Threads[threadID].materialTable.entries[st->materialKey]);
+ prefetch((char*)thisThread->pawnTable.entries[st->pawnKey]);
+ prefetch((char*)thisThread->materialTable.entries[st->materialKey]);
// Update incremental scores
st->psqScore += psq_delta(piece, from, to);
// Update incremental scores
st->psqScore += psq_delta(piece, from, to);
void Position::flip() {
// Make a copy of current position before to start changing
void Position::flip() {
// Make a copy of current position before to start changing
- const Position pos(*this, threadID);
+ const Position pos(*this, thisThread);
- threadID = pos.this_thread();
+ thisThread = &pos.this_thread();
// Board
for (Square s = SQ_A1; s <= SQ_H8; s++)
// Board
for (Square s = SQ_A1; s <= SQ_H8; s++)
/// The checkInfo struct is initialized at c'tor time and keeps info used
/// to detect if a move gives check.
class Position;
/// The checkInfo struct is initialized at c'tor time and keeps info used
/// to detect if a move gives check.
class Position;
- Position(const Position& pos, int th) { copy(pos, th); }
- Position(const std::string& fen, bool isChess960, int th);
+ Position(const Position& p, Thread* t) { copy(p, t); }
+ Position(const std::string& f, bool c960, Thread* t) { from_fen(f, c960, t); }
- void copy(const Position& pos, int th);
- void from_fen(const std::string& fen, bool isChess960);
+ void copy(const Position& pos, Thread* th);
+ void from_fen(const std::string& fen, bool isChess960, Thread* th);
const std::string to_fen() const;
void print(Move m = MOVE_NONE) const;
const std::string to_fen() const;
void print(Move m = MOVE_NONE) const;
Color side_to_move() const;
int startpos_ply_counter() const;
bool is_chess960() const;
Color side_to_move() const;
int startpos_ply_counter() const;
bool is_chess960() const;
- int this_thread() const;
+ Thread& this_thread() const;
int64_t nodes_searched() const;
void set_nodes_searched(int64_t n);
template<bool SkipRepetition> bool is_draw() const;
int64_t nodes_searched() const;
void set_nodes_searched(int64_t n);
template<bool SkipRepetition> bool is_draw() const;
int64_t nodes;
int startPosPly;
Color sideToMove;
int64_t nodes;
int startPosPly;
Color sideToMove;
StateInfo* st;
int chess960;
StateInfo* st;
int chess960;
return st->capturedType;
}
return st->capturedType;
}
-inline int Position::this_thread() const {
- return threadID;
+inline Thread& Position::this_thread() const {
+ return *thisThread;
}
#endif // !defined(POSITION_H_INCLUDED)
}
#endif // !defined(POSITION_H_INCLUDED)
// but if we are pondering or in infinite search, we shouldn't print the best
// move before we are told to do so.
if (!Signals.stop && (Limits.ponder || Limits.infinite))
// but if we are pondering or in infinite search, we shouldn't print the best
// move before we are told to do so.
if (!Signals.stop && (Limits.ponder || Limits.infinite))
- Threads[pos.this_thread()].wait_for_stop_or_ponderhit();
+ pos.this_thread().wait_for_stop_or_ponderhit();
// Best move could be MOVE_NONE when searching on a stalemate position
cout << "bestmove " << move_to_uci(RootMoves[0].pv[0], Chess960)
// Best move could be MOVE_NONE when searching on a stalemate position
cout << "bestmove " << move_to_uci(RootMoves[0].pv[0], Chess960)
assert(alpha >= -VALUE_INFINITE && alpha < beta && beta <= VALUE_INFINITE);
assert((alpha == beta - 1) || PvNode);
assert(depth > DEPTH_ZERO);
assert(alpha >= -VALUE_INFINITE && alpha < beta && beta <= VALUE_INFINITE);
assert((alpha == beta - 1) || PvNode);
assert(depth > DEPTH_ZERO);
- assert(pos.this_thread() >= 0 && pos.this_thread() < Threads.size());
Move movesSearched[MAX_MOVES];
StateInfo st;
Move movesSearched[MAX_MOVES];
StateInfo st;
bool isPvMove, inCheck, singularExtensionNode, givesCheck;
bool captureOrPromotion, dangerous, doFullDepthSearch;
int moveCount = 0, playedMoveCount = 0;
bool isPvMove, inCheck, singularExtensionNode, givesCheck;
bool captureOrPromotion, dangerous, doFullDepthSearch;
int moveCount = 0, playedMoveCount = 0;
- Thread& thread = Threads[pos.this_thread()];
+ Thread& thread = pos.this_thread();
SplitPoint* sp = NULL;
refinedValue = bestValue = value = -VALUE_INFINITE;
SplitPoint* sp = NULL;
refinedValue = bestValue = value = -VALUE_INFINITE;
{
Signals.firstRootMove = (moveCount == 1);
{
Signals.firstRootMove = (moveCount == 1);
- if (pos.this_thread() == 0 && SearchTime.elapsed() > 2000)
+ if (&thread == Threads.main_thread() && SearchTime.elapsed() > 2000)
cout << "info depth " << depth / ONE_PLY
<< " currmove " << move_to_uci(move, Chess960)
<< " currmovenumber " << moveCount + PVIdx << endl;
cout << "info depth " << depth / ONE_PLY
<< " currmove " << move_to_uci(move, Chess960)
<< " currmovenumber " << moveCount + PVIdx << endl;
if ( !SpNode
&& depth >= Threads.min_split_depth()
&& bestValue < beta
if ( !SpNode
&& depth >= Threads.min_split_depth()
&& bestValue < beta
- && Threads.available_slave_exists(pos.this_thread())
+ && Threads.available_slave_exists(thread)
&& !Signals.stop
&& !thread.cutoff_occurred())
bestValue = Threads.split<FakeSplit>(pos, ss, alpha, beta, bestValue, &bestMove,
&& !Signals.stop
&& !thread.cutoff_occurred())
bestValue = Threads.split<FakeSplit>(pos, ss, alpha, beta, bestValue, &bestMove,
assert(alpha >= -VALUE_INFINITE && alpha < beta && beta <= VALUE_INFINITE);
assert((alpha == beta - 1) || PvNode);
assert(depth <= DEPTH_ZERO);
assert(alpha >= -VALUE_INFINITE && alpha < beta && beta <= VALUE_INFINITE);
assert((alpha == beta - 1) || PvNode);
assert(depth <= DEPTH_ZERO);
- assert(pos.this_thread() >= 0 && pos.this_thread() < Threads.size());
StateInfo st;
Move ttMove, move, bestMove;
StateInfo st;
Move ttMove, move, bestMove;
lock_release(Threads.splitLock);
Stack ss[MAX_PLY_PLUS_2];
lock_release(Threads.splitLock);
Stack ss[MAX_PLY_PLUS_2];
- Position pos(*sp->pos, threadID);
- int master = sp->master;
+ Position pos(*sp->pos, this);
+ Thread* master = sp->master;
memcpy(ss, sp->ss - 1, 4 * sizeof(Stack));
(ss+1)->sp = sp;
memcpy(ss, sp->ss - 1, 4 * sizeof(Stack));
(ss+1)->sp = sp;
assert(is_searching);
is_searching = false;
assert(is_searching);
is_searching = false;
- sp->slavesMask &= ~(1ULL << threadID);
+ sp->slavesMask &= ~(1ULL << idx);
sp->nodes += pos.nodes_searched();
// After releasing the lock we cannot access anymore any SplitPoint
sp->nodes += pos.nodes_searched();
// After releasing the lock we cannot access anymore any SplitPoint
// Wake up master thread so to allow it to return from the idle loop in
// case we are the last slave of the split point.
if ( Threads.use_sleeping_threads()
// Wake up master thread so to allow it to return from the idle loop in
// case we are the last slave of the split point.
if ( Threads.use_sleeping_threads()
- && threadID != master
- && !Threads[master].is_searching)
- Threads[master].wake_up();
+ && this != master
+ && !master->is_searching)
+ master->wake_up();
maxPly = splitPointsCnt = 0;
curSplitPoint = NULL;
start_fn = fn;
maxPly = splitPointsCnt = 0;
curSplitPoint = NULL;
start_fn = fn;
- threadID = Threads.size();
do_sleep = (fn != &Thread::main_loop); // Avoid a race with start_searching()
do_sleep = (fn != &Thread::main_loop); // Avoid a race with start_searching()
if (!thread_create(handle, start_routine, this))
{
if (!thread_create(handle, start_routine, this))
{
- std::cerr << "Failed to create thread number " << threadID << std::endl;
+ std::cerr << "Failed to create thread number " << idx << std::endl;
::exit(EXIT_FAILURE);
}
}
::exit(EXIT_FAILURE);
}
}
// Thread::is_available_to() checks whether the thread is available to help the
// Thread::is_available_to() checks whether the thread is available to help the
-// thread with threadID "master" at a split point. An obvious requirement is that
-// thread must be idle. With more than two threads, this is not sufficient: If
-// the thread is the master of some active split point, it is only available as a
-// slave to the threads which are busy searching the split point at the top of
-// "slave"'s split point stack (the "helpful master concept" in YBWC terminology).
+// thread 'master' at a split point. An obvious requirement is that thread must
+// be idle. With more than two threads, this is not sufficient: If the thread is
+// the master of some active split point, it is only available as a slave to the
+// slaves which are busy searching the split point at the top of slaves split
+// point stack (the "helpful master concept" in YBWC terminology).
-bool Thread::is_available_to(int master) const {
+bool Thread::is_available_to(const Thread& master) const {
if (is_searching)
return false;
if (is_searching)
return false;
// No active split points means that the thread is available as a slave for any
// other thread otherwise apply the "helpful master" concept if possible.
// No active split points means that the thread is available as a slave for any
// other thread otherwise apply the "helpful master" concept if possible.
- return !spCnt || (splitPoints[spCnt - 1].slavesMask & (1ULL << master));
+ return !spCnt || (splitPoints[spCnt - 1].slavesMask & (1ULL << master.idx));
// available_slave_exists() tries to find an idle thread which is available as
// available_slave_exists() tries to find an idle thread which is available as
-// a slave for the thread with threadID 'master'.
+// a slave for the thread 'master'.
-bool ThreadsManager::available_slave_exists(int master) const {
-
- assert(master >= 0 && master < size());
+bool ThreadsManager::available_slave_exists(const Thread& master) const {
for (int i = 0; i < size(); i++)
if (threads[i]->is_available_to(master))
for (int i = 0; i < size(); i++)
if (threads[i]->is_available_to(master))
assert(beta <= VALUE_INFINITE);
assert(depth > DEPTH_ZERO);
assert(beta <= VALUE_INFINITE);
assert(depth > DEPTH_ZERO);
- int master = pos.this_thread();
- Thread& masterThread = *threads[master];
+ Thread& master = pos.this_thread();
- if (masterThread.splitPointsCnt >= MAX_SPLITPOINTS_PER_THREAD)
+ if (master.splitPointsCnt >= MAX_SPLITPOINTS_PER_THREAD)
return bestValue;
// Pick the next available split point from the split point stack
return bestValue;
// Pick the next available split point from the split point stack
- SplitPoint* sp = &masterThread.splitPoints[masterThread.splitPointsCnt++];
+ SplitPoint* sp = &master.splitPoints[master.splitPointsCnt++];
- sp->parent = masterThread.curSplitPoint;
- sp->master = master;
+ sp->parent = master.curSplitPoint;
+ sp->master = &master;
- sp->slavesMask = 1ULL << master;
+ sp->slavesMask = 1ULL << master.idx;
sp->depth = depth;
sp->bestMove = *bestMove;
sp->threatMove = threatMove;
sp->depth = depth;
sp->bestMove = *bestMove;
sp->threatMove = threatMove;
sp->nodes = 0;
sp->ss = ss;
sp->nodes = 0;
sp->ss = ss;
- assert(masterThread.is_searching);
+ assert(master.is_searching);
- masterThread.curSplitPoint = sp;
+ master.curSplitPoint = sp;
int slavesCnt = 0;
// Try to allocate available threads and ask them to start searching setting
int slavesCnt = 0;
// Try to allocate available threads and ask them to start searching setting
// their work at this split point.
if (slavesCnt || Fake)
{
// their work at this split point.
if (slavesCnt || Fake)
{
- 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.
// 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);
+ assert(!master.is_searching);
}
// We have returned from the idle loop, which means that all threads are
}
// We have returned from the idle loop, which means that all threads are
lock_grab(sp->lock); // To protect sp->nodes
lock_grab(splitLock);
lock_grab(sp->lock); // To protect sp->nodes
lock_grab(splitLock);
- masterThread.is_searching = true;
- masterThread.splitPointsCnt--;
- masterThread.curSplitPoint = sp->parent;
+ master.is_searching = true;
+ master.splitPointsCnt--;
+ master.curSplitPoint = sp->parent;
pos.set_nodes_searched(pos.nodes_searched() + sp->nodes);
*bestMove = sp->bestMove;
pos.set_nodes_searched(pos.nodes_searched() + sp->nodes);
*bestMove = sp->bestMove;
void ThreadsManager::wait_for_search_finished() {
void ThreadsManager::wait_for_search_finished() {
- Thread* main = threads[0];
- lock_grab(main->sleepLock);
- cond_signal(main->sleepCond); // In case is waiting for stop or ponderhit
- while (!main->do_sleep) cond_wait(sleepCond, main->sleepLock);
- lock_release(main->sleepLock);
+ Thread* t = main_thread();
+ lock_grab(t->sleepLock);
+ cond_signal(t->sleepCond); // In case is waiting for stop or ponderhit
+ while (!t->do_sleep) cond_wait(sleepCond, t->sleepLock);
+ lock_release(t->sleepLock);
Signals.stopOnPonderhit = Signals.firstRootMove = false;
Signals.stop = Signals.failedLowAtRoot = false;
Signals.stopOnPonderhit = Signals.firstRootMove = false;
Signals.stop = Signals.failedLowAtRoot = false;
- RootPosition.copy(pos, 0);
+ RootPosition.copy(pos, main_thread());
Limits = limits;
RootMoves.clear();
Limits = limits;
RootMoves.clear();
if (searchMoves.empty() || count(searchMoves.begin(), searchMoves.end(), ml.move()))
RootMoves.push_back(RootMove(ml.move()));
if (searchMoves.empty() || count(searchMoves.begin(), searchMoves.end(), ml.move()))
RootMoves.push_back(RootMove(ml.move()));
- threads[0]->do_sleep = false;
- threads[0]->wake_up();
+ main_thread()->do_sleep = false;
+ main_thread()->wake_up();
const int MAX_THREADS = 32;
const int MAX_SPLITPOINTS_PER_THREAD = 8;
const int MAX_THREADS = 32;
const int MAX_SPLITPOINTS_PER_THREAD = 8;
struct SplitPoint {
// Const data after split point has been setup
struct SplitPoint {
// Const data after split point has been setup
Depth depth;
Value beta;
int nodeType;
Depth depth;
Value beta;
int nodeType;
Move threatMove;
// Const pointers to shared data
Move threatMove;
// Const pointers to shared data
void wake_up();
bool cutoff_occurred() const;
void wake_up();
bool cutoff_occurred() const;
- bool is_available_to(int master) const;
+ bool is_available_to(const Thread& master) const;
void idle_loop(SplitPoint* sp_master);
void idle_loop() { idle_loop(NULL); } // Hack to allow storing in start_fn
void main_loop();
void idle_loop(SplitPoint* sp_master);
void idle_loop() { idle_loop(NULL); } // Hack to allow storing in start_fn
void main_loop();
SplitPoint splitPoints[MAX_SPLITPOINTS_PER_THREAD];
MaterialTable materialTable;
PawnTable pawnTable;
SplitPoint splitPoints[MAX_SPLITPOINTS_PER_THREAD];
MaterialTable materialTable;
PawnTable pawnTable;
int maxPly;
Lock sleepLock;
WaitCondition sleepCond;
int maxPly;
Lock sleepLock;
WaitCondition sleepCond;
bool use_sleeping_threads() const { return useSleepingThreads; }
int min_split_depth() const { return minimumSplitDepth; }
int size() const { return (int)threads.size(); }
bool use_sleeping_threads() const { return useSleepingThreads; }
int min_split_depth() const { return minimumSplitDepth; }
int size() const { return (int)threads.size(); }
+ Thread* main_thread() { return threads[0]; }
void wake_up() const;
void sleep() const;
void read_uci_options();
void wake_up() const;
void sleep() const;
void read_uci_options();
- bool available_slave_exists(int master) const;
+ bool available_slave_exists(const Thread& master) const;
void set_timer(int msec);
void wait_for_search_finished();
void start_searching(const Position& pos, const Search::LimitsType& limits,
void set_timer(int msec);
void wait_for_search_finished();
void start_searching(const Position& pos, const Search::LimitsType& limits,
void uci_loop(const string& args) {
void uci_loop(const string& args) {
- Position pos(StartFEN, false, 0); // The root position
+ Position pos(StartFEN, false, Threads.main_thread()); // The root position
string cmd, token;
while (token != "quit")
string cmd, token;
while (token != "quit")
- pos.from_fen(fen, Options["UCI_Chess960"]);
+ pos.from_fen(fen, Options["UCI_Chess960"], Threads.main_thread());
// Parse move list (if any)
while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
// Parse move list (if any)
while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)