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

Rewrite the way application exits

Centralize in a single object all the global resources
management and avoid a bunch of sparse exit() calls.

This is more reliable and clean and more stick to C++ coding
practices.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
Marco Costalba 2009-05-07 12:45:46 +02:00
parent 2155fb7825
commit a88e762b4e
13 changed files with 217 additions and 123 deletions

80
src/application.cpp Normal file
View file

@ -0,0 +1,80 @@
/*
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
Copyright (C) 2008 Marco Costalba
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/>.
*/
////
//// Includes
////
#include "bitboard.h"
#include "direction.h"
#include "endgame.h"
#include "evaluate.h"
#include "material.h"
#include "mersenne.h"
#include "misc.h"
#include "movepick.h"
#include "position.h"
#include "search.h"
#include "thread.h"
#include "ucioption.h"
/// Application class is in charge of initializing global resources
/// at startup and cleanly releases them when program terminates.
Application::Application() {
init_mersenne();
init_direction_table();
init_bitboards();
init_uci_options();
Position::init_zobrist();
Position::init_piece_square_tables();
MovePicker::init_phase_table();
init_eval(1);
init_bitbases();
init_threads();
// Make random number generation less deterministic, for book moves
for (int i = abs(get_system_time() % 10000); i > 0; i--)
genrand_int32();
}
Application::~Application() {
stop_threads();
quit_eval();
}
void Application::initialize() {
instance();
}
Application& Application::instance() {
static Application singleton;
return singleton;
}
void Application::exit_with_failure() {
exit(EXIT_FAILURE); // d'tor will be called automatically
}

46
src/application.h Normal file
View file

@ -0,0 +1,46 @@
/*
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
Copyright (C) 2008 Marco Costalba
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/>.
*/
#if !defined(APPLICATION_H_INCLUDED)
#define APPLICATION_H_INCLUDED
/// Singleton class used to housekeep memory and global resources
/// so to be sure we always leave in a clean state.
class Application {
Application();
Application(const Application&);
public:
static void initialize();
static void exit_with_failure();
~Application();
private:
static Application& instance();
void init();
void deallocateAll();
};
#endif // !defined(APPLICATION_H_INCLUDED)

View file

@ -79,15 +79,14 @@ void benchmark(const std::string& commandLine) {
if (val < 4 || val > 1024) if (val < 4 || val > 1024)
{ {
std::cerr << "The hash table size must be between 4 and 1024" << std::endl; std::cerr << "The hash table size must be between 4 and 1024" << std::endl;
exit(EXIT_FAILURE); Application::exit_with_failure();
} }
csStr >> threads; csStr >> threads;
csVal >> val; csVal >> val;
if (val < 1 || val > THREAD_MAX) if (val < 1 || val > THREAD_MAX)
{ {
std::cerr << "The number of threads must be between 1 and " << THREAD_MAX std::cerr << "The number of threads must be between 1 and " << THREAD_MAX << std::endl;
<< std::endl; Application::exit_with_failure();
exit(EXIT_FAILURE);
} }
set_option_value("Hash", ttSize); set_option_value("Hash", ttSize);
set_option_value("Threads", threads); set_option_value("Threads", threads);
@ -115,9 +114,8 @@ void benchmark(const std::string& commandLine) {
std::ifstream fenFile(fileName.c_str()); std::ifstream fenFile(fileName.c_str());
if (!fenFile.is_open()) if (!fenFile.is_open())
{ {
std::cerr << "Unable to open positions file " << fileName std::cerr << "Unable to open positions file " << fileName << std::endl;
<< std::endl; Application::exit_with_failure();
exit(EXIT_FAILURE);
} }
std::string pos; std::string pos;
while (fenFile.good()) while (fenFile.good())
@ -141,7 +139,8 @@ void benchmark(const std::string& commandLine) {
int dummy[2] = {0, 0}; int dummy[2] = {0, 0};
Position pos(*it); Position pos(*it);
std::cout << "\nProcessing position " << cnt << '/' << positions.size() << std::endl << std::endl; std::cout << "\nProcessing position " << cnt << '/' << positions.size() << std::endl << std::endl;
think(pos, true, false, 0, dummy, dummy, 0, maxDepth, maxNodes, secsPerPos, moves); if (!think(pos, true, false, 0, dummy, dummy, 0, maxDepth, maxNodes, secsPerPos, moves))
break;
totalNodes += nodes_searched(); totalNodes += nodes_searched();
} }
std::cout << "\nProcessing time (ms) " << get_system_time() - startTime << std::endl std::cout << "\nProcessing time (ms) " << get_system_time() - startTime << std::endl

View file

@ -372,7 +372,7 @@ void Book::open(const string& fName) {
if (!good()) if (!good())
{ {
cerr << "Failed to open book file " << fileName << endl; cerr << "Failed to open book file " << fileName << endl;
exit(EXIT_FAILURE); Application::exit_with_failure();
} }
} }
@ -489,7 +489,7 @@ void Book::read_entry(BookEntry& entry, int idx) {
if (!good()) if (!good())
{ {
cerr << "Failed to read book entry at index " << idx << endl; cerr << "Failed to read book entry at index " << idx << endl;
exit(EXIT_FAILURE); Application::exit_with_failure();
} }
} }

View file

@ -505,6 +505,8 @@ void quit_eval() {
{ {
delete PawnTable[i]; delete PawnTable[i];
delete MaterialTable[i]; delete MaterialTable[i];
PawnTable[i] = NULL;
MaterialTable[i] = NULL;
} }
} }

View file

@ -20,32 +20,24 @@
// To profile with callgrind uncomment following line // To profile with callgrind uncomment following line
//#define USE_CALLGRIND //#define USE_CALLGRIND
//// ////
//// Includes //// Includes
//// ////
#include <iostream> #include <iostream>
#include <string>
#include "benchmark.h" #include "benchmark.h"
#include "bitboard.h"
#include "direction.h"
#include "endgame.h"
#include "evaluate.h"
#include "material.h"
#include "mersenne.h"
#include "misc.h" #include "misc.h"
#include "movepick.h"
#include "position.h"
#include "search.h"
#include "thread.h"
#include "uci.h" #include "uci.h"
#include "ucioption.h"
#ifdef USE_CALLGRIND #ifdef USE_CALLGRIND
#include <valgrind/callgrind.h> #include <valgrind/callgrind.h>
#endif #endif
using std::string; using namespace std;
//// ////
//// Functions //// Functions
@ -54,54 +46,38 @@ using std::string;
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
// Disable IO buffering // Disable IO buffering
std::cout.rdbuf()->pubsetbuf(NULL, 0); cout.rdbuf()->pubsetbuf(NULL, 0);
std::cin.rdbuf()->pubsetbuf(NULL, 0); cin.rdbuf()->pubsetbuf(NULL, 0);
// Initialization // Initialization through global resources manager
init_mersenne(); Application::initialize();
init_direction_table();
init_bitboards();
init_uci_options();
Position::init_zobrist();
Position::init_piece_square_tables();
MovePicker::init_phase_table();
init_eval(1);
init_bitbases();
init_threads();
#ifdef USE_CALLGRIND #ifdef USE_CALLGRIND
CALLGRIND_START_INSTRUMENTATION; CALLGRIND_START_INSTRUMENTATION;
#endif #endif
// Make random number generation less deterministic, for book moves // Process command line arguments if any
for (int i = abs(get_system_time() % 10000); i > 0; i--) if (argc > 1)
genrand_int32();
// Process command line arguments
if (argc >= 2 && string(argv[1]) == "bench")
{ {
if (argc < 4 || argc > 7) if (string(argv[1]) != "bench" || argc < 4 || argc > 7)
cout << "Usage: stockfish bench <hash size> <threads> "
<< "[time = 60s] [fen positions file = default] "
<< "[time, depth or node limited = time]" << endl;
else
{ {
std::cout << "Usage: stockfish bench <hash size> <threads> " string time = argc > 4 ? argv[4] : "60";
<< "[time = 60s] [fen positions file = default] " string fen = argc > 5 ? argv[5] : "default";
<< "[time, depth or node limited = time]" string lim = argc > 6 ? argv[6] : "time";
<< std::endl; benchmark(string(argv[2]) + " " + string(argv[3]) + " " + time + " " + fen + " " + lim);
exit(0);
} }
string time = argc > 4 ? argv[4] : "60";
string fen = argc > 5 ? argv[5] : "default";
string lim = argc > 6 ? argv[6] : "time";
benchmark(string(argv[2]) + " " + string(argv[3]) + " " + time + " " + fen + " " + lim);
return 0; return 0;
} }
// Print copyright notice // Print copyright notice
std::cout << engine_name() << ". Copyright (C) " cout << engine_name() << ". Copyright (C) "
<< "2004-2008 Tord Romstad, Marco Costalba. " << "2004-2008 Tord Romstad, Marco Costalba. " << endl;
<< std::endl;
// Enter UCI mode // Enter UCI mode
uci_main_loop(); uci_main_loop();
return 0; return 0;
} }

View file

@ -90,7 +90,7 @@ MaterialInfoTable::MaterialInfoTable(unsigned int numOfEntries) {
{ {
std::cerr << "Failed to allocate " << (numOfEntries * sizeof(MaterialInfo)) std::cerr << "Failed to allocate " << (numOfEntries * sizeof(MaterialInfo))
<< " bytes for material hash table." << std::endl; << " bytes for material hash table." << std::endl;
exit(EXIT_FAILURE); Application::exit_with_failure();
} }
clear(); clear();
} }

View file

@ -29,6 +29,7 @@
#include <fstream> #include <fstream>
#include <string> #include <string>
#include "application.h"
//// ////
//// Macros //// Macros
@ -47,6 +48,7 @@ extern int get_system_time();
extern int cpu_count(); extern int cpu_count();
extern int Bioskey(); extern int Bioskey();
//// ////
//// Debug //// Debug
//// ////

View file

@ -144,7 +144,7 @@ PawnInfoTable::PawnInfoTable(unsigned numOfEntries) {
{ {
std::cerr << "Failed to allocate " << (numOfEntries * sizeof(PawnInfo)) std::cerr << "Failed to allocate " << (numOfEntries * sizeof(PawnInfo))
<< " bytes for pawn hash table." << std::endl; << " bytes for pawn hash table." << std::endl;
exit(EXIT_FAILURE); Application::exit_with_failure();
} }
clear(); clear();
} }

View file

@ -359,10 +359,11 @@ void SearchStack::initKillers() {
//// ////
/// think() is the external interface to Stockfish's search, and is called when /// think() is the external interface to Stockfish's search, and is called when
/// the program receives the UCI 'go' command. It initializes various /// the program receives the UCI 'go' command. It initializes various
/// search-related global variables, and calls root_search() /// search-related global variables, and calls root_search(). It returns false
/// when a quit command is received during the search.
void think(const Position &pos, bool infinite, bool ponder, int side_to_move, bool think(const Position &pos, bool infinite, bool ponder, int side_to_move,
int time[], int increment[], int movesToGo, int maxDepth, int time[], int increment[], int movesToGo, int maxDepth,
int maxNodes, int maxTime, Move searchMoves[]) { int maxNodes, int maxTime, Move searchMoves[]) {
@ -377,7 +378,7 @@ void think(const Position &pos, bool infinite, bool ponder, int side_to_move,
if (bookMove != MOVE_NONE) if (bookMove != MOVE_NONE)
{ {
std::cout << "bestmove " << bookMove << std::endl; std::cout << "bestmove " << bookMove << std::endl;
return; return true;
} }
} }
@ -541,13 +542,8 @@ void think(const Position &pos, bool infinite, bool ponder, int side_to_move,
if (UseLogFile) if (UseLogFile)
LogFile.close(); LogFile.close();
if (Quit)
{
stop_threads();
quit_eval();
exit(0);
}
Idle = true; Idle = true;
return !Quit;
} }
@ -2454,6 +2450,7 @@ namespace {
AbortSearch = true; AbortSearch = true;
PonderSearch = false; PonderSearch = false;
Quit = true; Quit = true;
return;
} }
else if(command == "stop") else if(command == "stop")
{ {
@ -2551,19 +2548,21 @@ namespace {
// after which the bestmove and pondermove will be printed (in id_loop()). // after which the bestmove and pondermove will be printed (in id_loop()).
void wait_for_stop_or_ponderhit() { void wait_for_stop_or_ponderhit() {
std::string command; std::string command;
while(true) { while (true)
if(!std::getline(std::cin, command)) {
command = "quit"; if (!std::getline(std::cin, command))
command = "quit";
if(command == "quit") { if (command == "quit")
stop_threads(); {
quit_eval(); Quit = true;
exit(0); break;
} }
else if(command == "ponderhit" || command == "stop") else if(command == "ponderhit" || command == "stop")
break; break;
} }
} }

View file

@ -84,7 +84,7 @@ extern History H;
extern void init_threads(); extern void init_threads();
extern void stop_threads(); extern void stop_threads();
extern void think(const Position &pos, bool infinite, bool ponder, int side_to_move, extern bool think(const Position &pos, bool infinite, bool ponder, int side_to_move,
int time[], int increment[], int movesToGo, int maxDepth, int time[], int increment[], int movesToGo, int maxDepth,
int maxNodes, int maxTime, Move searchMoves[]); int maxNodes, int maxTime, Move searchMoves[]);
extern int64_t nodes_searched(); extern int64_t nodes_searched();

View file

@ -69,7 +69,7 @@ void TranspositionTable::set_size(unsigned mbSize) {
{ {
std::cerr << "Failed to allocate " << mbSize std::cerr << "Failed to allocate " << mbSize
<< " MB for transposition table." << std::endl; << " MB for transposition table." << std::endl;
exit(EXIT_FAILURE); Application::exit_with_failure();
} }
clear(); clear();
} }

View file

@ -56,11 +56,10 @@ namespace {
Position RootPosition; Position RootPosition;
// Local functions // Local functions
void get_command(); bool handle_command(const std::string& command);
void handle_command(const std::string &command); void set_option(UCIInputParser& uip);
void set_option(UCIInputParser &uip); void set_position(UCIInputParser& uip);
void set_position(UCIInputParser &uip); bool go(UCIInputParser& uip);
void go(UCIInputParser &uip);
} }
@ -68,17 +67,25 @@ namespace {
//// Functions //// Functions
//// ////
/// uci_main_loop() is the only global function in this file. It is /// uci_main_loop() is the only global function in this file. It is
/// called immediately after the program has finished initializing. /// called immediately after the program has finished initializing.
/// The program remains in this loop until it receives the "quit" UCI /// The program remains in this loop until it receives the "quit" UCI
/// command. /// command. It waits for a command from the user, and passes this
/// command to handle_command and also intercepts EOF from stdin,
/// by translating EOF to the "quit" command. This ensures that Stockfish
/// exits gracefully if the GUI dies unexpectedly.
void uci_main_loop() { void uci_main_loop() {
RootPosition.from_fen(StartPosition); RootPosition.from_fen(StartPosition);
std::string command;
while (1) do {
get_command(); // Wait for a command from stdin
if (!std::getline(std::cin, command))
command = "quit";
} while (handle_command(command));
} }
@ -88,29 +95,12 @@ void uci_main_loop() {
namespace { namespace {
// get_command() waits for a command from the user, and passes
// this command to handle_command. get_command also intercepts
// EOF from stdin, by translating EOF to the "quit" command. This
// ensures that Stockfish exits gracefully if the GUI dies
// unexpectedly.
void get_command() {
std::string command;
if (!std::getline(std::cin, command))
command = "quit";
handle_command(command);
}
// handle_command() takes a text string as input, uses a // handle_command() takes a text string as input, uses a
// UCIInputParser object to parse this text string as a UCI command, // UCIInputParser object to parse this text string as a UCI command,
// and calls the appropriate functions. In addition to the UCI // and calls the appropriate functions. In addition to the UCI
// commands, the function also supports a few debug commands. // commands, the function also supports a few debug commands.
void handle_command(const std::string &command) { bool handle_command(const std::string& command) {
UCIInputParser uip(command); UCIInputParser uip(command);
std::string token; std::string token;
@ -118,12 +108,12 @@ namespace {
uip >> token; // operator >> skips any whitespace uip >> token; // operator >> skips any whitespace
if (token == "quit") if (token == "quit")
{ return false;
stop_threads();
quit_eval(); if (token == "go")
exit(0); return go(uip);
}
else if (token == "uci") if (token == "uci")
{ {
std::cout << "id name " << engine_name() << std::endl std::cout << "id name " << engine_name() << std::endl
<< "id author Tord Romstad, Marco Costalba" << "id author Tord Romstad, Marco Costalba"
@ -143,8 +133,6 @@ namespace {
set_position(uip); set_position(uip);
else if (token == "setoption") else if (token == "setoption")
set_option(uip); set_option(uip);
else if (token == "go")
go(uip);
// The remaining commands are for debugging purposes only. // The remaining commands are for debugging purposes only.
// Perhaps they should be removed later in order to reduce the // Perhaps they should be removed later in order to reduce the
@ -183,6 +171,7 @@ namespace {
std::cout << token << std::endl; std::cout << token << std::endl;
} }
} }
return true;
} }
@ -192,7 +181,7 @@ namespace {
// ("position"), and is ready to read the second token ("startpos" // ("position"), and is ready to read the second token ("startpos"
// or "fen", if the input is well-formed). // or "fen", if the input is well-formed).
void set_position(UCIInputParser &uip) { void set_position(UCIInputParser& uip) {
std::string token; std::string token;
@ -242,7 +231,7 @@ namespace {
// ("setoption"), and is ready to read the second token ("name", if // ("setoption"), and is ready to read the second token ("name", if
// the input is well-formed). // the input is well-formed).
void set_option(UCIInputParser &uip) { void set_option(UCIInputParser& uip) {
std::string token, name; std::string token, name;
@ -269,14 +258,15 @@ namespace {
// go() is called when Stockfish receives the "go" UCI command. The // go() is called when Stockfish receives the "go" UCI command. The
// input parameter is a UCIInputParser. It is assumed that this // input parameter is a UCIInputParser. It is assumed that this
// parser has consumed the first token of the UCI command ("go"), // parser has consumed the first token of the UCI command ("go"),
// and is ready to read the second token. The function sets the // and is ready to read the second token. The function sets the
// thinking time and other parameters from the input string, and // thinking time and other parameters from the input string, and
// calls think() (defined in search.cpp) with the appropriate // calls think() (defined in search.cpp) with the appropriate
// parameters. // parameters. Returns false if a quit command is received while
// thinking, returns true otherwise.
void go(UCIInputParser &uip) { bool go(UCIInputParser& uip) {
std::string token; std::string token;
@ -328,7 +318,7 @@ namespace {
assert(RootPosition.is_ok()); assert(RootPosition.is_ok());
think(RootPosition, infinite, ponder, RootPosition.side_to_move(), time, return think(RootPosition, infinite, ponder, RootPosition.side_to_move(), time,
inc, movesToGo, depth, nodes, moveTime, searchMoves); inc, movesToGo, depth, nodes, moveTime, searchMoves);
} }
} }