Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1st step to build SF as shared library #4626

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion src/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ vector<string> setup_bench(const Position& current, istream& is) {

if (!file.is_open())
{
cerr << "Unable to open file " << fenFile << endl;
std::string msg = "Unable to open file " + fenFile;
sync_cerr << msg << sync_endl;
exit(EXIT_FAILURE);
}

Expand Down
73 changes: 65 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
*/

#include <iostream>
#if defined(_WIN32) || defined(_WIN64)
#include <Windows.h>
#else
#include <unistd.h>
#endif

#include "bitboard.h"
#include "endgame.h"
Expand All @@ -28,13 +33,15 @@
#include "tt.h"
#include "uci.h"

using namespace Stockfish;

int main(int argc, char* argv[]) {
int sf_init();
#ifndef shared_lib
void input_reader();
#endif

std::cout << engine_info() << std::endl;
using namespace Stockfish;

CommandLine::init(argc, argv);
int sf_init() {
sync_cout << engine_info() << sync_endl;
UCI::init(Options);
Tune::init();
PSQT::init();
Expand All @@ -45,9 +52,59 @@ int main(int argc, char* argv[]) {
Threads.set(size_t(Options["Threads"]));
Search::clear(); // After threads are up
Eval::NNUE::init();
UCI::init_pos();
return 0;
}

UCI::loop(argc, argv);
#ifndef shared_lib
/// input_reader() waits for a command from stdin and invokes UCI::execute()
/// Also intercepts EOF from stdin to ensure gracefully exiting if the
/// GUI dies unexpectedly.
void input_reader() {
std::string cmd;
while (getline(std::cin, cmd)) {
#ifndef NDEBUG
if (cmd.substr(0, 1) == "#") {
continue;
}
if (cmd.substr(0, 5) == "wait ") {
int time = std::stoi(cmd.substr(5));
#if defined(__MINGW32__) || defined(__MINGW64__)
Sleep(time);
#else
sleep(time);
#endif
continue;
}
#endif
UCI::execute(cmd);
if (cmd == "quit")
break;
}
}

Threads.set(0);
return 0;
/// When SF is called with some command line arguments, e.g. to
/// run 'bench', once the command is executed the program stops.
int main(int argc, char* argv[]) {

CommandLine::init(argc, argv);
int res = sf_init();

if (argc > 1) {
std::string cmd;
for (int i = 1; i < argc; ++i)
cmd += std::string(argv[i]) + " ";
UCI::execute(cmd);
#if defined(__MINGW32__) || defined(__MINGW64__)
Sleep(1);
#else
sleep(1);
#endif
UCI::execute("quit");
} else {
std::thread input_reader_thread(input_reader);
input_reader_thread.join();
}
return res;
}
#endif
26 changes: 14 additions & 12 deletions src/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class Logger {

if (!l.file.is_open())
{
cerr << "Unable to open debug log file " << fname << endl;
sync_cerr << "Unable to open debug log file " << fname << sync_endl;
exit(EXIT_FAILURE);
}

Expand Down Expand Up @@ -357,26 +357,26 @@ void dbg_print() {

for (int i = 0; i < MaxDebugSlots; ++i)
if ((n = hit[i][0]))
std::cerr << "Hit #" << i
sync_cerr << "Hit #" << i
<< ": Total " << n << " Hits " << hit[i][1]
<< " Hit Rate (%) " << 100.0 * E(hit[i][1])
<< std::endl;
<< sync_endl;

for (int i = 0; i < MaxDebugSlots; ++i)
if ((n = mean[i][0]))
{
std::cerr << "Mean #" << i
sync_cerr << "Mean #" << i
<< ": Total " << n << " Mean " << E(mean[i][1])
<< std::endl;
<< sync_endl;
}

for (int i = 0; i < MaxDebugSlots; ++i)
if ((n = stdev[i][0]))
{
double r = sqrtl(E(stdev[i][2]) - sqr(E(stdev[i][1])));
std::cerr << "Stdev #" << i
sync_cerr << "Stdev #" << i
<< ": Total " << n << " Stdev " << r
<< std::endl;
<< sync_endl;
}

for (int i = 0; i < MaxDebugSlots; ++i)
Expand All @@ -385,9 +385,9 @@ void dbg_print() {
double r = (E(correl[i][5]) - E(correl[i][1]) * E(correl[i][3]))
/ ( sqrtl(E(correl[i][2]) - sqr(E(correl[i][1])))
* sqrtl(E(correl[i][4]) - sqr(E(correl[i][3]))));
std::cerr << "Correl. #" << i
sync_cerr << "Correl. #" << i
<< ": Total " << n << " Coefficient " << r
<< std::endl;
<< sync_endl;
}
}

Expand Down Expand Up @@ -589,9 +589,11 @@ void aligned_large_pages_free(void* mem) {
if (mem && !VirtualFree(mem, 0, MEM_RELEASE))
{
DWORD err = GetLastError();
std::cerr << "Failed to free large page memory. Error code: 0x"
<< std::hex << err
<< std::dec << std::endl;
std::stringstream stream;
stream << "Failed to free large page memory. Error code: 0x"
<< std::hex << err
<< std::dec;
sync_cerr << stream.str() << sync_endl;
exit(EXIT_FAILURE);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ enum SyncCout { IO_LOCK, IO_UNLOCK };
std::ostream& operator<<(std::ostream&, SyncCout);

#define sync_cout std::cout << IO_LOCK
#define sync_cerr std::cerr << IO_LOCK
#define sync_endl std::endl << IO_UNLOCK


Expand Down
7 changes: 4 additions & 3 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,13 @@ void MainThread::search() {
if (bestThread != this)
sync_cout << UCI::pv(bestThread->rootPos, bestThread->completedDepth) << sync_endl;

sync_cout << "bestmove " << UCI::move(bestThread->rootMoves[0].pv[0], rootPos.is_chess960());
std::stringstream stream;
stream << "bestmove " << UCI::move(bestThread->rootMoves[0].pv[0], rootPos.is_chess960());

if (bestThread->rootMoves[0].pv.size() > 1 || bestThread->rootMoves[0].extract_ponder_from_tt(rootPos))
std::cout << " ponder " << UCI::move(bestThread->rootMoves[0].pv[1], rootPos.is_chess960());
stream << " ponder " << UCI::move(bestThread->rootMoves[0].pv[1], rootPos.is_chess960());

std::cout << sync_endl;
sync_cout << stream.str() << sync_endl;
}


Expand Down
23 changes: 15 additions & 8 deletions src/syzygy/tbprobe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ class TBFile : public std::ifstream {

if (statbuf.st_size % 64 != 16)
{
std::cerr << "Corrupt tablebase file " << fname << std::endl;
std::string msg = "Corrupt tablebase file " + fname;
sync_cerr << msg << sync_endl;
exit(EXIT_FAILURE);
}

Expand All @@ -229,7 +230,8 @@ class TBFile : public std::ifstream {

if (*baseAddress == MAP_FAILED)
{
std::cerr << "Could not mmap() " << fname << std::endl;
std::string msg = "Could not mmap() " + fname;
sync_cerr << msg << sync_endl;
exit(EXIT_FAILURE);
}
#else
Expand All @@ -245,7 +247,8 @@ class TBFile : public std::ifstream {

if (size_low % 64 != 16)
{
std::cerr << "Corrupt tablebase file " << fname << std::endl;
std::string msg = "Corrupt tablebase file " + fname;
sync_cerr << msg << sync_endl;
exit(EXIT_FAILURE);
}

Expand All @@ -254,7 +257,8 @@ class TBFile : public std::ifstream {

if (!mmap)
{
std::cerr << "CreateFileMapping() failed" << std::endl;
std::string msg = "CreateFileMapping() failed";
sync_cerr << msg << sync_endl;
exit(EXIT_FAILURE);
}

Expand All @@ -263,8 +267,10 @@ class TBFile : public std::ifstream {

if (!*baseAddress)
{
std::cerr << "MapViewOfFile() failed, name = " << fname
<< ", error = " << GetLastError() << std::endl;
std::stringstream stream;
stream << "MapViewOfFile() failed, name = " << fname
<< ", error = " << GetLastError();
sync_cerr << stream.str() << sync_endl;
exit(EXIT_FAILURE);
}
#endif
Expand All @@ -275,7 +281,7 @@ class TBFile : public std::ifstream {

if (memcmp(data, Magics[type == WDL], 4))
{
std::cerr << "Corrupted table in file " << fname << std::endl;
sync_cerr << "Corrupted table in file " << fname << sync_endl;
unmap(*baseAddress, *mapping);
return *baseAddress = nullptr, nullptr;
}
Expand Down Expand Up @@ -444,7 +450,8 @@ class TBTables {
homeBucket = otherHomeBucket;
}
}
std::cerr << "TB hash table size too low!" << std::endl;
std::string msg = "TB hash table size too low!";
sync_cerr << msg << sync_endl;
exit(EXIT_FAILURE);
}

Expand Down
7 changes: 5 additions & 2 deletions src/tt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <cstring> // For std::memset
#include <iostream>
#include <sstream>
#include <thread>

#include "bitboard.h"
Expand Down Expand Up @@ -71,8 +72,10 @@ void TranspositionTable::resize(size_t mbSize) {
table = static_cast<Cluster*>(aligned_large_pages_alloc(clusterCount * sizeof(Cluster)));
if (!table)
{
std::cerr << "Failed to allocate " << mbSize
<< "MB for transposition table." << std::endl;
std::stringstream stream;
stream << "Failed to allocate " << mbSize
<< "MB for transposition table.";
sync_cerr << stream.str() << sync_endl;
exit(EXIT_FAILURE);
}

Expand Down
4 changes: 2 additions & 2 deletions src/tune.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ static void make_option(const string& n, int v, const SetRange& r) {
LastOption = &Options[n];

// Print formatted parameters, ready to be copy-pasted in Fishtest
std::cout << n << ","
sync_cout << n << ","
<< v << ","
<< r(v).first << "," << r(v).second << ","
<< (r(v).second - r(v).first) / 20.0 << ","
<< "0.0020"
<< std::endl;
<< sync_endl;
}

template<> void Tune::Entry<int>::init_option() { make_option(name, value, range); }
Expand Down
Loading