Skip to content

Commit

Permalink
Use a Thread instead of an array index
Browse files Browse the repository at this point in the history
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
  • Loading branch information
mcostalba committed Apr 4, 2012
1 parent 0439a79 commit 673bc55
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 82 deletions.
2 changes: 1 addition & 1 deletion src/benchmark.cpp
Expand Up @@ -107,7 +107,7 @@ void benchmark(istringstream& is) {

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;

Expand Down
2 changes: 1 addition & 1 deletion src/endgame.cpp
Expand Up @@ -77,7 +77,7 @@ namespace {
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();
}

template<typename M>
Expand Down
4 changes: 2 additions & 2 deletions src/evaluate.cpp
Expand Up @@ -371,7 +371,7 @@ Value do_evaluate(const Position& pos, Value& margin) {
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
Expand All @@ -383,7 +383,7 @@ Value do_evaluate(const Position& pos, Value& margin) {
}

// 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
Expand Down
29 changes: 12 additions & 17 deletions src/position.cpp
Expand Up @@ -92,33 +92,27 @@ CheckInfo::CheckInfo(const Position& pos) {
}


/// 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;
threadID = th;
thisThread = th;
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).

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.
Expand Down Expand Up @@ -234,6 +228,7 @@ void Position::from_fen(const string& fenStr, bool isChess960) {
st->npMaterial[BLACK] = compute_non_pawn_material(BLACK);
st->checkersBB = attackers_to(king_square(sideToMove)) & pieces(~sideToMove);
chess960 = isChess960;
thisThread = th;

assert(pos_is_ok());
}
Expand Down Expand Up @@ -336,7 +331,7 @@ void Position::print(Move move) const {

if (move)
{
Position p(*this, this_thread());
Position p(*this, thisThread);
cout << "\nMove is: " << (sideToMove == BLACK ? ".." : "") << move_to_san(p, move);
}

Expand Down Expand Up @@ -903,8 +898,8 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
}

// 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);
Expand Down Expand Up @@ -1546,10 +1541,10 @@ void Position::init() {
void Position::flip() {

// Make a copy of current position before to start changing
const Position pos(*this, threadID);
const Position pos(*this, thisThread);

clear();
threadID = pos.this_thread();
thisThread = &pos.this_thread();

// Board
for (Square s = SQ_A1; s <= SQ_H8; s++)
Expand Down
17 changes: 9 additions & 8 deletions src/position.h
Expand Up @@ -29,6 +29,7 @@
/// The checkInfo struct is initialized at c'tor time and keeps info used
/// to detect if a move gives check.
class Position;
class Thread;

struct CheckInfo {

Expand Down Expand Up @@ -90,12 +91,12 @@ class Position {

public:
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); }

// Text input/output
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;

Expand Down Expand Up @@ -175,7 +176,7 @@ class Position {
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;
Expand Down Expand Up @@ -223,7 +224,7 @@ class Position {
int64_t nodes;
int startPosPly;
Color sideToMove;
int threadID;
Thread* thisThread;
StateInfo* st;
int chess960;

Expand Down Expand Up @@ -433,8 +434,8 @@ inline PieceType Position::captured_piece_type() const {
return st->capturedType;
}

inline int Position::this_thread() const {
return threadID;
inline Thread& Position::this_thread() const {
return *thisThread;
}

#endif // !defined(POSITION_H_INCLUDED)
22 changes: 10 additions & 12 deletions src/search.cpp
Expand Up @@ -332,7 +332,7 @@ void Search::think() {
// 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)
Expand Down Expand Up @@ -530,7 +530,6 @@ namespace {
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;
Expand All @@ -544,7 +543,7 @@ namespace {
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;
Expand Down Expand Up @@ -847,7 +846,7 @@ namespace {
{
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;
Expand Down Expand Up @@ -1054,7 +1053,7 @@ namespace {
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,
Expand Down Expand Up @@ -1131,7 +1130,6 @@ namespace {
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;
Expand Down Expand Up @@ -1827,8 +1825,8 @@ void Thread::idle_loop(SplitPoint* sp_master) {
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;
Expand All @@ -1847,7 +1845,7 @@ void Thread::idle_loop(SplitPoint* sp_master) {
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
Expand All @@ -1858,9 +1856,9 @@ void Thread::idle_loop(SplitPoint* sp_master) {
// 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();
}
}
}
Expand Down

0 comments on commit 673bc55

Please sign in to comment.