1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-30 08:43:09 +00:00

Import C++11 branch

Import C++11 branch from:

https://github.com/mcostalba/Stockfish/tree/c++11

The version imported is teh last one as of today:
6670e93e50

Branch is fully equivalent with master but syzygy
tablebases that are missing (but will be added with
next commit).

bench: 8080602
This commit is contained in:
Marco Costalba 2015-01-18 08:00:50 +01:00
parent c73f33f37e
commit 3c07603dac
21 changed files with 152 additions and 435 deletions

View file

@ -36,7 +36,7 @@ PGOBENCH = ./$(EXE) bench 16 1 1000 default time
### Object files
OBJS = benchmark.o bitbase.o bitboard.o endgame.o evaluate.o main.o \
material.o misc.o movegen.o movepick.o pawns.o position.o \
search.o thread.o timeman.o tt.o uci.o ucioption.o syzygy/tbprobe.o
search.o thread.o timeman.o tt.o uci.o ucioption.o
### ==========================================================================
### Section 2. High-level Configuration
@ -140,7 +140,7 @@ endif
### 3.1 Selecting compiler (default = gcc)
CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -fno-rtti $(EXTRACXXFLAGS)
CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -fno-rtti -std=c++11 $(EXTRACXXFLAGS)
LDFLAGS += $(EXTRALDFLAGS)
ifeq ($(COMP),)
@ -150,7 +150,8 @@ endif
ifeq ($(COMP),gcc)
comp=gcc
CXX=g++
CXXFLAGS += -ansi -pedantic -Wno-long-long -Wextra -Wshadow
CXXFLAGS += -pedantic -Wno-long-long -Wextra -Wshadow
LDFLAGS += -Wl,--no-as-needed
endif
ifeq ($(COMP),mingw)
@ -374,14 +375,14 @@ profile-build:
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_prepare)
@echo ""
@echo "Step 1/4. Building executable for benchmark ..."
@touch *.cpp *.h syzygy/*.cpp syzygy/*.h
@touch *.cpp *.h
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
@echo ""
@echo "Step 2/4. Running benchmark for pgo-build ..."
@$(PGOBENCH) > /dev/null
@echo ""
@echo "Step 3/4. Building final executable ..."
@touch *.cpp *.h syzygy/*.cpp syzygy/*.h
@touch *.cpp *.h
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
@echo ""
@echo "Step 4/4. Deleting profile data ..."
@ -396,7 +397,7 @@ install:
-strip $(BINDIR)/$(EXE)
clean:
$(RM) $(EXE) $(EXE).exe *.o .depend *~ core bench.txt *.gcda ./syzygy/*.o ./syzygy/*.gcda
$(RM) $(EXE) $(EXE).exe *.o .depend *~ core bench.txt *.gcda
default:
help
@ -461,7 +462,7 @@ gcc-profile-use:
all
gcc-profile-clean:
@rm -rf *.gcda *.gcno syzygy/*.gcda syzygy/*.gcno bench.txt
@rm -rf *.gcda *.gcno bench.txt
icc-profile-prepare:
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) icc-profile-clean

View file

@ -17,7 +17,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include <fstream>
#include <iostream>
#include <istream>
@ -34,7 +33,7 @@ using namespace std;
namespace {
const char* Defaults[] = {
const vector<string> Defaults = {
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 10",
"8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 11",
@ -108,19 +107,19 @@ void benchmark(const Position& current, istream& is) {
TT.clear();
if (limitType == "time")
limits.movetime = atoi(limit.c_str()); // movetime is in ms
limits.movetime = stoi(limit); // movetime is in ms
else if (limitType == "nodes")
limits.nodes = atoi(limit.c_str());
limits.nodes = stoi(limit);
else if (limitType == "mate")
limits.mate = atoi(limit.c_str());
limits.mate = stoi(limit);
else
limits.depth = atoi(limit.c_str());
limits.depth = stoi(limit);
if (fenFile == "default")
fens.assign(Defaults, Defaults + 37);
fens = Defaults;
else if (fenFile == "current")
fens.push_back(current.fen());
@ -128,7 +127,7 @@ void benchmark(const Position& current, istream& is) {
else
{
string fen;
ifstream file(fenFile.c_str());
ifstream file(fenFile);
if (!file.is_open())
{
@ -164,7 +163,7 @@ void benchmark(const Position& current, istream& is) {
}
}
elapsed = std::max(Time::now() - elapsed, Time::point(1)); // Avoid a 'divide by zero'
elapsed = Time::now() - elapsed + 1; // Ensure positivity to avoid a 'divide by zero'
dbg_print(); // Just before to exit

View file

@ -96,12 +96,9 @@ namespace {
string fen = sides[0] + char(8 - sides[0].length() + '0') + "/8/8/8/8/8/8/"
+ sides[1] + char(8 - sides[1].length() + '0') + " w - - 0 10";
return Position(fen, false, NULL).material_key();
return Position(fen, false, nullptr).material_key();
}
template<typename M>
void delete_endgame(const typename M::value_type& p) { delete p.second; }
} // namespace
@ -128,17 +125,12 @@ Endgames::Endgames() {
add<KRPPKRP>("KRPPKRP");
}
Endgames::~Endgames() {
for_each(m1.begin(), m1.end(), delete_endgame<M1>);
for_each(m2.begin(), m2.end(), delete_endgame<M2>);
}
template<EndgameType E>
template<EndgameType E, typename T>
void Endgames::add(const string& code) {
map((Endgame<E>*)0)[key(code, WHITE)] = new Endgame<E>(WHITE);
map((Endgame<E>*)0)[key(code, BLACK)] = new Endgame<E>(BLACK);
map<T>()[key(code, WHITE)] = std::unique_ptr<T>(new Endgame<E>(WHITE));
map<T>()[key(code, BLACK)] = std::unique_ptr<T>(new Endgame<E>(BLACK));
}

View file

@ -21,7 +21,10 @@
#define ENDGAME_H_INCLUDED
#include <map>
#include <memory>
#include <string>
#include <type_traits>
#include <utility>
#include "position.h"
#include "types.h"
@ -63,11 +66,9 @@ enum EndgameType {
/// Endgame functions can be of two types depending on whether they return a
/// Value or a ScaleFactor. Type eg_fun<int>::type returns either ScaleFactor
/// or Value depending on whether the template parameter is 0 or 1.
template<int> struct eg_fun { typedef Value type; };
template<> struct eg_fun<1> { typedef ScaleFactor type; };
/// Value or a ScaleFactor.
template<EndgameType E>
using eg_fun = std::conditional<(E < SCALING_FUNCTIONS), Value, ScaleFactor>;
/// Base and derived templates for endgame evaluation and scaling functions
@ -81,7 +82,7 @@ struct EndgameBase {
};
template<EndgameType E, typename T = typename eg_fun<(E > SCALING_FUNCTIONS)>::type>
template<EndgameType E, typename T = typename eg_fun<E>::type>
struct Endgame : public EndgameBase<T> {
explicit Endgame(Color c) : strongSide(c), weakSide(~c) {}
@ -99,24 +100,21 @@ private:
class Endgames {
typedef std::map<Key, EndgameBase<eg_fun<0>::type>*> M1;
typedef std::map<Key, EndgameBase<eg_fun<1>::type>*> M2;
template<typename T> using Map = std::map<Key, std::unique_ptr<T>>;
M1 m1;
M2 m2;
template<EndgameType E, typename T = EndgameBase<typename eg_fun<E>::type>>
void add(const std::string& code);
M1& map(M1::mapped_type) { return m1; }
M2& map(M2::mapped_type) { return m2; }
template<typename T, int I = std::is_same<T, EndgameBase<ScaleFactor>>::value>
Map<T>& map() { return std::get<I>(maps); }
template<EndgameType E> void add(const std::string& code);
std::pair<Map<EndgameBase<Value>>, Map<EndgameBase<ScaleFactor>>> maps;
public:
Endgames();
~Endgames();
template<typename T> T probe(Key key, T& eg) {
return eg = map(eg).count(key) ? map(eg)[key] : NULL;
}
template<typename T> T* probe(Key key, T** eg)
{ return *eg = map<T>().count(key) ? map<T>()[key].get() : nullptr; }
};
#endif // #ifndef ENDGAME_H_INCLUDED

View file

@ -26,7 +26,6 @@
#include "thread.h"
#include "tt.h"
#include "uci.h"
#include "syzygy/tbprobe.h"
int main(int argc, char* argv[]) {
@ -40,7 +39,6 @@ int main(int argc, char* argv[]) {
Eval::init();
Pawns::init();
Threads.init();
Tablebases::init(Options["SyzygyPath"]);
TT.resize(Options["Hash"]);
UCI::loop(argc, argv);

View file

@ -139,7 +139,7 @@ Entry* probe(const Position& pos) {
// Let's look if we have a specialized evaluation function for this particular
// material configuration. Firstly we look for a fixed configuration one, then
// for a generic one if the previous search failed.
if (pos.this_thread()->endgames.probe(key, e->evaluationFunction))
if (pos.this_thread()->endgames.probe(key, &e->evaluationFunction))
return e;
if (is_KXK<WHITE>(pos))
@ -158,7 +158,7 @@ Entry* probe(const Position& pos) {
// configuration. Is there a suitable specialized scaling function?
EndgameBase<ScaleFactor>* sf;
if (pos.this_thread()->endgames.probe(key, sf))
if (pos.this_thread()->endgames.probe(key, &sf))
{
e->scalingFunction[sf->strong_side()] = sf; // Only strong color assigned
return e;

View file

@ -40,7 +40,7 @@ struct Entry {
Score imbalance() const { return make_score(value, value); }
Phase game_phase() const { return gamePhase; }
bool specialized_eval_exists() const { return evaluationFunction != NULL; }
bool specialized_eval_exists() const { return evaluationFunction != nullptr; }
Value evaluate(const Position& pos) const { return (*evaluationFunction)(pos); }
// scale_factor takes a position and a color as input and returns a scale factor

View file

@ -17,6 +17,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <chrono>
#include <fstream>
#include <iomanip>
#include <iostream>
@ -26,6 +27,7 @@
#include "thread.h"
using namespace std;
using namespace std::chrono;
namespace {
@ -123,6 +125,13 @@ const string engine_info(bool to_uci) {
}
/// Convert system time to milliseconds. That's all we need.
Time::point Time::now() {
return duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count();
}
/// Debug functions used mainly to collect run-time statistics
void dbg_hit_on(bool b) { ++hits[0]; if (b) ++hits[1]; }
@ -146,7 +155,7 @@ void dbg_print() {
std::ostream& operator<<(std::ostream& os, SyncCout sc) {
static Mutex m;
static std::mutex m;
if (sc == IO_LOCK)
m.lock();
@ -162,25 +171,6 @@ std::ostream& operator<<(std::ostream& os, SyncCout sc) {
void start_logger(bool b) { Logger::start(b); }
/// timed_wait() waits for msec milliseconds. It is mainly a helper to wrap
/// the conversion from milliseconds to struct timespec, as used by pthreads.
void timed_wait(WaitCondition& sleepCond, Lock& sleepLock, int msec) {
#ifdef _WIN32
int tm = msec;
#else
timespec ts, *tm = &ts;
uint64_t ms = Time::now() + msec;
ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * 1000000LL;
#endif
cond_timedwait(sleepCond, sleepLock, tm);
}
/// prefetch() preloads the given address in L1/L2 cache. This is a non-blocking
/// function that doesn't stall the CPU waiting for data to be loaded from memory,
/// which can be quite slow.

View file

@ -28,7 +28,6 @@
#include "types.h"
const std::string engine_info(bool to_uci = false);
void timed_wait(WaitCondition&, Lock&, int);
void prefetch(char* addr);
void start_logger(bool b);
@ -40,7 +39,7 @@ void dbg_print();
namespace Time {
typedef int64_t point;
inline point now() { return system_time_to_msec(); }
point now();
}

View file

@ -256,7 +256,7 @@ namespace {
template<Color Us, GenType Type> FORCE_INLINE
ExtMove* generate_all(const Position& pos, ExtMove* moveList, Bitboard target,
const CheckInfo* ci = NULL) {
const CheckInfo* ci = nullptr) {
const bool Checks = Type == QUIET_CHECKS;

View file

@ -50,18 +50,17 @@ ExtMove* generate(const Position& pos, ExtMove* moveList);
template<GenType T>
struct MoveList {
explicit MoveList(const Position& pos) : cur(moveList), last(generate<T>(pos, moveList)) { last->move = MOVE_NONE; }
void operator++() { ++cur; }
Move operator*() const { return cur->move; }
explicit MoveList(const Position& pos) : last(generate<T>(pos, moveList)) {}
const ExtMove* begin() const { return moveList; }
const ExtMove* end() const { return last; }
size_t size() const { return last - moveList; }
bool contains(Move m) const {
for (const ExtMove* it(moveList); it != last; ++it) if (it->move == m) return true;
for (const ExtMove& ms : *this) if (ms.move == m) return true;
return false;
}
private:
ExtMove moveList[MAX_MOVES];
ExtMove *cur, *last;
ExtMove moveList[MAX_MOVES], *last;
};
#endif // #ifndef MOVEGEN_H_INCLUDED

View file

@ -1,116 +0,0 @@
/*
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
Stockfish is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Stockfish is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PLATFORM_H_INCLUDED
#define PLATFORM_H_INCLUDED
#ifdef _MSC_VER
// Disable some silly and noisy warnings from MSVC compiler
#pragma warning(disable: 4127) // Conditional expression is constant
#pragma warning(disable: 4146) // Unary minus operator applied to unsigned type
#pragma warning(disable: 4800) // Forcing value to bool 'true' or 'false'
#pragma warning(disable: 4996) // Function _ftime() may be unsafe
// MSVC does not support <inttypes.h>
typedef signed __int8 int8_t;
typedef unsigned __int8 uint8_t;
typedef signed __int16 int16_t;
typedef unsigned __int16 uint16_t;
typedef signed __int32 int32_t;
typedef unsigned __int32 uint32_t;
typedef signed __int64 int64_t;
typedef unsigned __int64 uint64_t;
#else
# include <inttypes.h>
#endif
#ifndef _WIN32 // Linux - Unix
# include <sys/time.h>
inline int64_t system_time_to_msec() {
timeval t;
gettimeofday(&t, NULL);
return t.tv_sec * 1000LL + t.tv_usec / 1000;
}
# include <pthread.h>
typedef pthread_mutex_t Lock;
typedef pthread_cond_t WaitCondition;
typedef pthread_t NativeHandle;
typedef void*(*pt_start_fn)(void*);
# define lock_init(x) pthread_mutex_init(&(x), NULL)
# define lock_grab(x) pthread_mutex_lock(&(x))
# define lock_release(x) pthread_mutex_unlock(&(x))
# define lock_destroy(x) pthread_mutex_destroy(&(x))
# define cond_destroy(x) pthread_cond_destroy(&(x))
# define cond_init(x) pthread_cond_init(&(x), NULL)
# define cond_signal(x) pthread_cond_signal(&(x))
# define cond_wait(x,y) pthread_cond_wait(&(x),&(y))
# define cond_timedwait(x,y,z) pthread_cond_timedwait(&(x),&(y),z)
# define thread_create(x,f,t) pthread_create(&(x),NULL,(pt_start_fn)f,t)
# define thread_join(x) pthread_join(x, NULL)
#else // Windows and MinGW
# include <sys/timeb.h>
inline int64_t system_time_to_msec() {
_timeb t;
_ftime(&t);
return t.time * 1000LL + t.millitm;
}
#ifndef NOMINMAX
# define NOMINMAX // disable macros min() and max()
#endif
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN
#undef NOMINMAX
// We use critical sections on Windows to support Windows XP and older versions.
// Unfortunately, cond_wait() is racy between lock_release() and WaitForSingleObject()
// but apart from this they have the same speed performance of SRW locks.
typedef CRITICAL_SECTION Lock;
typedef HANDLE WaitCondition;
typedef HANDLE NativeHandle;
// On Windows 95 and 98 parameter lpThreadId may not be null
inline DWORD* dwWin9xKludge() { static DWORD dw; return &dw; }
# define lock_init(x) InitializeCriticalSection(&(x))
# define lock_grab(x) EnterCriticalSection(&(x))
# define lock_release(x) LeaveCriticalSection(&(x))
# define lock_destroy(x) DeleteCriticalSection(&(x))
# define cond_init(x) { x = CreateEvent(0, FALSE, FALSE, 0); }
# define cond_destroy(x) CloseHandle(x)
# define cond_signal(x) SetEvent(x)
# define cond_wait(x,y) { lock_release(y); WaitForSingleObject(x, INFINITE); lock_grab(y); }
# define cond_timedwait(x,y,z) { lock_release(y); WaitForSingleObject(x,z); lock_grab(y); }
# define thread_create(x,f,t) (x = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)f,t,0,dwWin9xKludge()))
# define thread_join(x) { WaitForSingleObject(x, INFINITE); CloseHandle(x); }
#endif
#endif // #ifndef PLATFORM_H_INCLUDED

View file

@ -170,12 +170,11 @@ public:
uint64_t nodes_searched() const;
void set_nodes_searched(uint64_t n);
bool is_draw() const;
int rule50_count() const;
Score psq_score() const;
Value non_pawn_material(Color c) const;
// Position consistency check, for debugging
bool pos_is_ok(int* step = NULL) const;
bool pos_is_ok(int* step = nullptr) const;
void flip();
private:
@ -348,10 +347,6 @@ inline int Position::game_ply() const {
return gamePly;
}
inline int Position::rule50_count() const {
return st->rule50;
}
inline uint64_t Position::nodes_searched() const {
return nodes;
}

View file

@ -33,7 +33,6 @@
#include "thread.h"
#include "tt.h"
#include "uci.h"
#include "syzygy/tbprobe.h"
namespace Search {
@ -45,18 +44,6 @@ namespace Search {
StateStackPtr SetupStates;
}
namespace Tablebases {
int Cardinality;
uint64_t Hits;
bool RootInTB;
bool UseRule50;
Depth ProbeDepth;
Value Score;
}
namespace TB = Tablebases;
using std::string;
using Eval::evaluate;
using namespace Search;
@ -167,19 +154,19 @@ uint64_t Search::perft(Position& pos, Depth depth) {
CheckInfo ci(pos);
const bool leaf = (depth == 2 * ONE_PLY);
for (MoveList<LEGAL> it(pos); *it; ++it)
for (const ExtMove& ms : MoveList<LEGAL>(pos))
{
if (Root && depth <= ONE_PLY)
cnt = 1, nodes++;
else
{
pos.do_move(*it, st, ci, pos.gives_check(*it, ci));
pos.do_move(ms.move, st, ci, pos.gives_check(ms.move, ci));
cnt = leaf ? MoveList<LEGAL>(pos).size() : perft<false>(pos, depth - ONE_PLY);
nodes += cnt;
pos.undo_move(*it);
pos.undo_move(ms.move);
}
if (Root)
sync_cout << UCI::move(*it, pos.is_chess960()) << ": " << cnt << sync_endl;
sync_cout << UCI::move(ms.move, pos.is_chess960()) << ": " << cnt << sync_endl;
}
return nodes;
}
@ -199,19 +186,6 @@ void Search::think() {
DrawValue[ RootPos.side_to_move()] = VALUE_DRAW - Value(contempt);
DrawValue[~RootPos.side_to_move()] = VALUE_DRAW + Value(contempt);
TB::Hits = 0;
TB::RootInTB = false;
TB::UseRule50 = Options["Syzygy50MoveRule"];
TB::ProbeDepth = Options["SyzygyProbeDepth"] * ONE_PLY;
TB::Cardinality = Options["SyzygyProbeLimit"];
// Skip TB probing when no TB found: !TBLargest -> !TB::Cardinality
if (TB::Cardinality > TB::MaxCardinality)
{
TB::Cardinality = TB::MaxCardinality;
TB::ProbeDepth = DEPTH_ZERO;
}
if (RootMoves.empty())
{
RootMoves.push_back(MOVE_NONE);
@ -221,39 +195,8 @@ void Search::think() {
}
else
{
if (TB::Cardinality >= RootPos.count<ALL_PIECES>(WHITE)
+ RootPos.count<ALL_PIECES>(BLACK))
{
// If the current root position is in the tablebases then RootMoves
// contains only moves that preserve the draw or win.
TB::RootInTB = Tablebases::root_probe(RootPos, RootMoves, TB::Score);
if (TB::RootInTB)
TB::Cardinality = 0; // Do not probe tablebases during the search
else // If DTZ tables are missing, use WDL tables as a fallback
{
// Filter out moves that do not preserve a draw or win
TB::RootInTB = Tablebases::root_probe_wdl(RootPos, RootMoves, TB::Score);
// Only probe during search if winning
if (TB::Score <= VALUE_DRAW)
TB::Cardinality = 0;
}
if (TB::RootInTB)
{
TB::Hits = RootMoves.size();
if (!TB::UseRule50)
TB::Score = TB::Score > VALUE_DRAW ? VALUE_MATE - MAX_PLY - 1
: TB::Score < VALUE_DRAW ? -VALUE_MATE + MAX_PLY + 1
: VALUE_DRAW;
}
}
for (size_t i = 0; i < Threads.size(); ++i)
Threads[i]->maxPly = 0;
for (Thread* th : Threads)
th->maxPly = 0;
Threads.timer->run = true;
Threads.timer->notify_one(); // Wake up the recurring timer
@ -323,8 +266,8 @@ namespace {
// Save the last iteration's scores before first PV line is searched and
// all the move scores except the (new) PV are set to -VALUE_INFINITE.
for (size_t i = 0; i < RootMoves.size(); ++i)
RootMoves[i].previousScore = RootMoves[i].score;
for (RootMove& rm : RootMoves)
rm.previousScore = rm.score;
// MultiPV loop. We perform a full root search for each PV line
for (PVIdx = 0; PVIdx < std::min(multiPV, RootMoves.size()) && !Signals.stop; ++PVIdx)
@ -476,7 +419,7 @@ namespace {
splitPoint = ss->splitPoint;
bestMove = splitPoint->bestMove;
bestValue = splitPoint->bestValue;
tte = NULL;
tte = nullptr;
ttHit = false;
ttMove = excludedMove = MOVE_NONE;
ttValue = VALUE_NONE;
@ -539,41 +482,11 @@ namespace {
// If ttMove is quiet, update killers, history, counter move and followup move on TT hit
if (ttValue >= beta && ttMove && !pos.capture_or_promotion(ttMove) && !inCheck)
update_stats(pos, ss, ttMove, depth, NULL, 0);
update_stats(pos, ss, ttMove, depth, nullptr, 0);
return ttValue;
}
// Step 4a. Tablebase probe
if (!RootNode && TB::Cardinality)
{
int piecesCnt = pos.count<ALL_PIECES>(WHITE) + pos.count<ALL_PIECES>(BLACK);
if ( piecesCnt <= TB::Cardinality
&& (piecesCnt < TB::Cardinality || depth >= TB::ProbeDepth)
&& pos.rule50_count() == 0)
{
int found, v = Tablebases::probe_wdl(pos, &found);
if (found)
{
TB::Hits++;
int drawScore = TB::UseRule50 ? 1 : 0;
value = v < -drawScore ? -VALUE_MATE + MAX_PLY + ss->ply
: v > drawScore ? VALUE_MATE - MAX_PLY - ss->ply
: VALUE_DRAW + 2 * v * drawScore;
tte->save(posKey, value_to_tt(value, ss->ply), BOUND_EXACT,
std::min(DEPTH_MAX - ONE_PLY, depth + 6 * ONE_PLY),
MOVE_NONE, VALUE_NONE, TT.generation());
return value;
}
}
}
// Step 5. Evaluate the position statically and update parent's gain statistics
if (inCheck)
{
@ -788,7 +701,7 @@ moves_loop: // When in check and at SpNode search starts from here
}
if (PvNode)
(ss+1)->pv = NULL;
(ss+1)->pv = nullptr;
extension = DEPTH_ZERO;
captureOrPromotion = pos.capture_or_promotion(move);
@ -1420,9 +1333,9 @@ moves_loop: // When in check and at SpNode search starts from here
size_t uciPVSize = std::min((size_t)Options["MultiPV"], RootMoves.size());
int selDepth = 0;
for (size_t i = 0; i < Threads.size(); ++i)
if (Threads[i]->maxPly > selDepth)
selDepth = Threads[i]->maxPly;
for (Thread* th : Threads)
if (th->maxPly > selDepth)
selDepth = th->maxPly;
for (size_t i = 0; i < uciPVSize; ++i)
{
@ -1434,9 +1347,6 @@ moves_loop: // When in check and at SpNode search starts from here
Depth d = updated ? depth : depth - ONE_PLY;
Value v = updated ? RootMoves[i].score : RootMoves[i].previousScore;
bool tb = TB::RootInTB && abs(v) < VALUE_MATE - MAX_PLY;
v = tb ? TB::Score : v;
if (ss.rdbuf()->in_avail()) // Not at first line
ss << "\n";
@ -1445,12 +1355,11 @@ moves_loop: // When in check and at SpNode search starts from here
<< " multipv " << i + 1
<< " score " << UCI::value(v);
if (!tb && i == PVIdx)
if (i == PVIdx)
ss << (v >= beta ? " lowerbound" : v <= alpha ? " upperbound" : "");
ss << " nodes " << pos.nodes_searched()
<< " nps " << pos.nodes_searched() * 1000 / elapsed
<< " tbhits " << TB::Hits
<< " time " << elapsed
<< " pv";
@ -1496,7 +1405,7 @@ void Thread::idle_loop() {
// Pointer 'this_sp' is not null only if we are called from split(), and not
// at the thread creation. This means we are the split point's master.
SplitPoint* this_sp = splitPointsSize ? activeSplitPoint : NULL;
SplitPoint* this_sp = splitPointsSize ? activeSplitPoint : nullptr;
assert(!this_sp || (this_sp->masterThread == this && searching));
@ -1520,7 +1429,7 @@ void Thread::idle_loop() {
sp->mutex.lock();
assert(activePosition == NULL);
assert(activePosition == nullptr);
activePosition = &pos;
@ -1539,7 +1448,7 @@ void Thread::idle_loop() {
assert(searching);
searching = false;
activePosition = NULL;
activePosition = nullptr;
sp->slavesMask.reset(idx);
sp->allSlavesSearching = false;
sp->nodes += pos.nodes_searched();
@ -1564,7 +1473,7 @@ void Thread::idle_loop() {
for (size_t i = 0; i < Threads.size(); ++i)
{
const int size = Threads[i]->splitPointsSize; // Local copy
sp = size ? &Threads[i]->splitPoints[size - 1] : NULL;
sp = size ? &Threads[i]->splitPoints[size - 1] : nullptr;
if ( sp
&& sp->allSlavesSearching
@ -1591,22 +1500,19 @@ void Thread::idle_loop() {
}
// Grab the lock to avoid races with Thread::notify_one()
mutex.lock();
std::unique_lock<std::mutex> lk(mutex);
// If we are master and all slaves have finished then exit idle_loop
if (this_sp && this_sp->slavesMask.none())
{
assert(!searching);
mutex.unlock();
break;
}
// If we are not searching, wait for a condition to be signaled instead of
// wasting CPU time polling for work.
if (!searching && !exit)
sleepCondition.wait(mutex);
mutex.unlock();
sleepCondition.wait(lk);
}
}
@ -1651,10 +1557,10 @@ void check_time() {
// Loop across all split points and sum accumulated SplitPoint nodes plus
// all the currently active positions nodes.
for (size_t i = 0; i < Threads.size(); ++i)
for (int j = 0; j < Threads[i]->splitPointsSize; ++j)
for (Thread* th : Threads)
for (int i = 0; i < th->splitPointsSize; ++i)
{
SplitPoint& sp = Threads[i]->splitPoints[j];
SplitPoint& sp = th->splitPoints[i];
sp.mutex.lock();

View file

@ -95,7 +95,7 @@ struct SignalsType {
bool stop, stopOnPonderhit, firstRootMove, failedLowAtRoot;
};
typedef std::auto_ptr<std::stack<StateInfo> > StateStackPtr;
typedef std::unique_ptr<std::stack<StateInfo>> StateStackPtr;
extern volatile SignalsType Signals;
extern LimitsType Limits;

View file

@ -33,19 +33,13 @@ extern void check_time();
namespace {
// start_routine() is the C function which is called when a new thread
// is launched. It is a wrapper to the virtual function idle_loop().
extern "C" { long start_routine(ThreadBase* th) { th->idle_loop(); return 0; } }
// Helpers to launch a thread after creation and joining before delete. Must be
// outside Thread c'tor and d'tor because the object must be fully initialized
// when start_routine (and hence virtual idle_loop) is called and when joining.
template<typename T> T* new_thread() {
T* th = new T();
thread_create(th->handle, start_routine, th); // Will go to sleep
th->nativeThread = std::thread(&ThreadBase::idle_loop, th); // Will go to sleep
return th;
}
@ -56,7 +50,7 @@ namespace {
th->mutex.unlock();
th->notify_one();
thread_join(th->handle); // Wait for thread termination
th->nativeThread.join(); // Wait for thread termination
delete th;
}
@ -67,9 +61,8 @@ namespace {
void ThreadBase::notify_one() {
mutex.lock();
std::unique_lock<std::mutex>(this->mutex);
sleepCondition.notify_one();
mutex.unlock();
}
@ -77,9 +70,8 @@ void ThreadBase::notify_one() {
void ThreadBase::wait_for(volatile const bool& condition) {
mutex.lock();
while (!condition) sleepCondition.wait(mutex);
mutex.unlock();
std::unique_lock<std::mutex> lk(mutex);
sleepCondition.wait(lk, [&]{ return condition; });
}
@ -90,8 +82,8 @@ Thread::Thread() /* : splitPoints() */ { // Initialization of non POD broken in
searching = false;
maxPly = splitPointsSize = 0;
activeSplitPoint = NULL;
activePosition = NULL;
activeSplitPoint = nullptr;
activePosition = nullptr;
idx = Threads.size(); // Starts from 0
}
@ -178,11 +170,11 @@ void Thread::split(Position& pos, Stack* ss, Value alpha, Value beta, Value* bes
sp.allSlavesSearching = true; // Must be set under lock protection
++splitPointsSize;
activeSplitPoint = &sp;
activePosition = NULL;
activePosition = nullptr;
Thread* slave;
while ((slave = Threads.available_slave(this)) != NULL)
while ((slave = Threads.available_slave(this)) != nullptr)
{
sp.slavesMask.set(slave->idx);
slave->activeSplitPoint = &sp;
@ -231,12 +223,12 @@ void TimerThread::idle_loop() {
while (!exit)
{
mutex.lock();
std::unique_lock<std::mutex> lk(mutex);
if (!exit)
sleepCondition.wait_for(mutex, run ? Resolution : INT_MAX);
sleepCondition.wait_for(lk, std::chrono::milliseconds(run ? Resolution : INT_MAX));
mutex.unlock();
lk.unlock();
if (run)
check_time();
@ -251,17 +243,17 @@ void MainThread::idle_loop() {
while (!exit)
{
mutex.lock();
std::unique_lock<std::mutex> lk(mutex);
thinking = false;
while (!thinking && !exit)
{
Threads.sleepCondition.notify_one(); // Wake up the UI thread if needed
sleepCondition.wait(mutex);
sleepCondition.wait(lk);
}
mutex.unlock();
lk.unlock();
if (!exit)
{
@ -297,8 +289,8 @@ void ThreadPool::exit() {
delete_thread(timer); // As first because check_time() accesses threads data
for (iterator it = begin(); it != end(); ++it)
delete_thread(*it);
for (Thread* th : *this)
delete_thread(th);
}
@ -335,11 +327,11 @@ void ThreadPool::read_uci_options() {
Thread* ThreadPool::available_slave(const Thread* master) const {
for (const_iterator it = begin(); it != end(); ++it)
if ((*it)->available_to(master))
return *it;
for (Thread* th : *this)
if (th->available_to(master))
return th;
return NULL;
return nullptr;
}
@ -347,10 +339,8 @@ Thread* ThreadPool::available_slave(const Thread* master) const {
void ThreadPool::wait_for_think_finished() {
MainThread* th = main();
th->mutex.lock();
while (th->thinking) sleepCondition.wait(th->mutex);
th->mutex.unlock();
std::unique_lock<std::mutex> lk(main()->mutex);
sleepCondition.wait(lk, [&]{ return !main()->thinking; });
}
@ -371,14 +361,14 @@ void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits,
Limits = limits;
if (states.get()) // If we don't set a new position, preserve current state
{
SetupStates = states; // Ownership transfer here
SetupStates = std::move(states); // Ownership transfer here
assert(!states.get());
}
for (MoveList<LEGAL> it(pos); *it; ++it)
for (const ExtMove& ms : MoveList<LEGAL>(pos))
if ( limits.searchmoves.empty()
|| std::count(limits.searchmoves.begin(), limits.searchmoves.end(), *it))
RootMoves.push_back(RootMove(*it));
|| std::count(limits.searchmoves.begin(), limits.searchmoves.end(), ms.move))
RootMoves.push_back(RootMove(ms.move));
main()->thinking = true;
main()->notify_one(); // Starts main thread

View file

@ -21,6 +21,9 @@
#define THREAD_H_INCLUDED
#include <bitset>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <vector>
#include "material.h"
@ -34,34 +37,7 @@ struct Thread;
const int MAX_THREADS = 128;
const int MAX_SPLITPOINTS_PER_THREAD = 8;
/// Mutex and ConditionVariable struct are wrappers of the low level locking
/// machinery and are modeled after the corresponding C++11 classes.
struct Mutex {
Mutex() { lock_init(l); }
~Mutex() { lock_destroy(l); }
void lock() { lock_grab(l); }
void unlock() { lock_release(l); }
private:
friend struct ConditionVariable;
Lock l;
};
struct ConditionVariable {
ConditionVariable() { cond_init(c); }
~ConditionVariable() { cond_destroy(c); }
void wait(Mutex& m) { cond_wait(c, m.l); }
void wait_for(Mutex& m, int ms) { timed_wait(c, m.l, ms); }
void notify_one() { cond_signal(c); }
private:
WaitCondition c;
};
struct Thread;
/// SplitPoint struct stores information shared by the threads searching in
/// parallel below the same split point. It is populated at splitting time.
@ -82,7 +58,7 @@ struct SplitPoint {
SplitPoint* parentSplitPoint;
// Shared variable data
Mutex mutex;
std::mutex mutex;
std::bitset<MAX_THREADS> slavesMask;
volatile bool allSlavesSearching;
volatile uint64_t nodes;
@ -99,15 +75,15 @@ struct SplitPoint {
struct ThreadBase {
ThreadBase() : handle(NativeHandle()), exit(false) {}
ThreadBase() : exit(false) {}
virtual ~ThreadBase() {}
virtual void idle_loop() = 0;
void notify_one();
void wait_for(volatile const bool& b);
Mutex mutex;
ConditionVariable sleepCondition;
NativeHandle handle;
std::thread nativeThread;
std::mutex mutex;
std::condition_variable sleepCondition;
volatile bool exit;
};
@ -176,8 +152,8 @@ struct ThreadPool : public std::vector<Thread*> {
void start_thinking(const Position&, const Search::LimitsType&, Search::StateStackPtr&);
Depth minimumSplitDepth;
Mutex mutex;
ConditionVariable sleepCondition;
std::mutex mutex;
std::condition_variable sleepCondition;
TimerThread* timer;
};

View file

@ -37,9 +37,15 @@
#include <cassert>
#include <cctype>
#include <climits>
#include <cstdint>
#include <cstdlib>
#include "platform.h"
#if defined(_MSC_VER)
// Disable some silly and noisy warning from MSVC compiler
#pragma warning(disable: 4127) // Conditional expression is constant
#pragma warning(disable: 4146) // Unary minus operator applied to unsigned type
#pragma warning(disable: 4800) // Forcing value to bool 'true' or 'false'
#endif
/// Predefined macros hell:
///
@ -170,7 +176,7 @@ enum Bound {
BOUND_EXACT = BOUND_UPPER | BOUND_LOWER
};
enum Value {
enum Value : int {
VALUE_ZERO = 0,
VALUE_DRAW = 0,
VALUE_KNOWN_WIN = 10000,
@ -178,11 +184,8 @@ enum Value {
VALUE_INFINITE = 32001,
VALUE_NONE = 32002,
VALUE_MATE_IN_MAX_PLY = VALUE_MATE - 2 * MAX_PLY,
VALUE_MATED_IN_MAX_PLY = -VALUE_MATE + 2 * MAX_PLY,
VALUE_ENSURE_INTEGER_SIZE_P = INT_MAX,
VALUE_ENSURE_INTEGER_SIZE_N = INT_MIN,
VALUE_MATE_IN_MAX_PLY = VALUE_MATE - MAX_PLY,
VALUE_MATED_IN_MAX_PLY = -VALUE_MATE + MAX_PLY,
PawnValueMg = 198, PawnValueEg = 258,
KnightValueMg = 817, KnightValueEg = 846,
@ -254,16 +257,10 @@ enum Rank {
};
/// Score enum stores a middlegame and an endgame value in a single integer.
/// The least significant 16 bits are used to store the endgame value and
/// the upper 16 bits are used to store the middlegame value. The compiler
/// is free to choose the enum type as long as it can store the data, so we
/// ensure that Score is an integer type by assigning some big int values.
enum Score {
SCORE_ZERO,
SCORE_ENSURE_INTEGER_SIZE_P = INT_MAX,
SCORE_ENSURE_INTEGER_SIZE_N = INT_MIN
};
/// Score enum stores a middlegame and an endgame value in a single integer
/// (enum). The least significant 16 bits are used to store the endgame value
/// and the upper 16 bits are used to store the middlegame value.
enum Score : int { SCORE_ZERO };
inline Score make_score(int mg, int eg) {
return Score((mg << 16) + eg);

View file

@ -220,7 +220,7 @@ string UCI::value(Value v) {
stringstream ss;
if (abs(v) < VALUE_MATE - MAX_PLY)
if (abs(v) < VALUE_MATE_IN_MAX_PLY)
ss << "cp " << v * 100 / PawnValueEg;
else
ss << "mate " << (v > 0 ? VALUE_MATE - v + 1 : -VALUE_MATE - v) / 2;
@ -274,9 +274,9 @@ Move UCI::to_move(const Position& pos, string& str) {
if (str.length() == 5) // Junior could send promotion piece in uppercase
str[4] = char(tolower(str[4]));
for (MoveList<LEGAL> it(pos); *it; ++it)
if (str == UCI::move(*it, pos.is_chess960()))
return *it;
for (const ExtMove& ms : MoveList<LEGAL>(pos))
if (str == UCI::move(ms.move, pos.is_chess960()))
return ms.move;
return MOVE_NONE;
}

View file

@ -45,10 +45,10 @@ class Option {
typedef void (*OnChange)(const Option&);
public:
Option(OnChange = NULL);
Option(bool v, OnChange = NULL);
Option(const char* v, OnChange = NULL);
Option(int v, int min, int max, OnChange = NULL);
Option(OnChange = nullptr);
Option(bool v, OnChange = nullptr);
Option(const char* v, OnChange = nullptr);
Option(int v, int min, int max, OnChange = nullptr);
Option& operator=(const std::string&);
void operator<<(const Option&);

View file

@ -19,14 +19,12 @@
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <sstream>
#include "misc.h"
#include "thread.h"
#include "tt.h"
#include "uci.h"
#include "syzygy/tbprobe.h"
using std::string;
@ -39,14 +37,13 @@ void on_clear_hash(const Option&) { TT.clear(); }
void on_hash_size(const Option& o) { TT.resize(o); }
void on_logger(const Option& o) { start_logger(o); }
void on_threads(const Option&) { Threads.read_uci_options(); }
void on_tb_path(const Option& o) { Tablebases::init(o); }
/// Our case insensitive less() function as required by UCI protocol
bool ci_less(char c1, char c2) { return tolower(c1) < tolower(c2); }
bool CaseInsensitiveLess::operator() (const string& s1, const string& s2) const {
return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(), ci_less);
return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(),
[](char c1, char c2) { return tolower(c1) < tolower(c2); });
}
@ -69,10 +66,6 @@ void init(OptionsMap& o) {
o["Minimum Thinking Time"] << Option(20, 0, 5000);
o["Slow Mover"] << Option(80, 10, 1000);
o["UCI_Chess960"] << Option(false);
o["SyzygyPath"] << Option("<empty>", on_tb_path);
o["SyzygyProbeDepth"] << Option(1, 1, 100);
o["Syzygy50MoveRule"] << Option(true);
o["SyzygyProbeLimit"] << Option(6, 0, 6);
}
@ -82,11 +75,11 @@ void init(OptionsMap& o) {
std::ostream& operator<<(std::ostream& os, const OptionsMap& om) {
for (size_t idx = 0; idx < om.size(); ++idx)
for (OptionsMap::const_iterator it = om.begin(); it != om.end(); ++it)
if (it->second.idx == idx)
for (auto it : om)
if (it.second.idx == idx)
{
const Option& o = it->second;
os << "\noption name " << it->first << " type " << o.type;
const Option& o = it.second;
os << "\noption name " << it.first << " type " << o.type;
if (o.type != "button")
os << " default " << o.defaultValue;
@ -96,6 +89,7 @@ std::ostream& operator<<(std::ostream& os, const OptionsMap& om) {
break;
}
return os;
}
@ -112,12 +106,11 @@ Option::Option(OnChange f) : type("button"), min(0), max(0), on_change(f)
{}
Option::Option(int v, int minv, int maxv, OnChange f) : type("spin"), min(minv), max(maxv), on_change(f)
{ std::ostringstream ss; ss << v; defaultValue = currentValue = ss.str(); }
{ defaultValue = currentValue = std::to_string(v); }
Option::operator int() const {
assert(type == "check" || type == "spin");
return (type == "spin" ? atoi(currentValue.c_str()) : currentValue == "true");
return (type == "spin" ? stoi(currentValue) : currentValue == "true");
}
Option::operator std::string() const {
@ -147,7 +140,7 @@ Option& Option::operator=(const string& v) {
if ( (type != "button" && v.empty())
|| (type == "check" && v != "true" && v != "false")
|| (type == "spin" && (atoi(v.c_str()) < min || atoi(v.c_str()) > max)))
|| (type == "spin" && (stoi(v) < min || stoi(v) > max)))
return *this;
if (type != "button")