2008-08-31 23:59:13 -06:00
|
|
|
/*
|
2008-10-19 10:56:28 -06:00
|
|
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
|
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
2011-12-30 05:24:41 -07:00
|
|
|
Copyright (C) 2008-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2008-10-19 10:56:28 -06:00
|
|
|
Stockfish is free software: you can redistribute it and/or modify
|
2008-08-31 23:59:13 -06:00
|
|
|
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.
|
2008-09-06 04:30:07 -06:00
|
|
|
|
2008-10-19 10:56:28 -06:00
|
|
|
Stockfish is distributed in the hope that it will be useful,
|
2008-08-31 23:59:13 -06:00
|
|
|
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.
|
2008-09-06 04:30:07 -06:00
|
|
|
|
2008-08-31 23:59:13 -06:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2012-01-24 22:29:30 -07:00
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
2011-12-04 02:53:40 -07:00
|
|
|
|
|
|
|
#define NOMINMAX // disable macros min() and max()
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#else
|
2008-08-31 23:59:13 -06:00
|
|
|
|
|
|
|
# include <unistd.h>
|
2010-02-11 22:49:16 -07:00
|
|
|
# if defined(__hpux)
|
|
|
|
# include <sys/pstat.h>
|
|
|
|
# endif
|
2008-08-31 23:59:13 -06:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2010-06-19 04:10:54 -06:00
|
|
|
#if !defined(NO_PREFETCH)
|
|
|
|
# include <xmmintrin.h>
|
|
|
|
#endif
|
|
|
|
|
2011-12-30 08:14:24 -07:00
|
|
|
#include <algorithm>
|
2008-08-31 23:59:13 -06:00
|
|
|
#include <iomanip>
|
2008-09-06 04:22:10 -06:00
|
|
|
#include <iostream>
|
2008-08-31 23:59:13 -06:00
|
|
|
#include <sstream>
|
2012-03-17 20:20:43 -06:00
|
|
|
#include <streambuf>
|
2008-08-31 23:59:13 -06:00
|
|
|
|
|
|
|
#include "misc.h"
|
2010-04-10 04:35:30 -06:00
|
|
|
#include "thread.h"
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2009-04-27 06:38:47 -06:00
|
|
|
using namespace std;
|
|
|
|
|
2011-12-30 04:53:51 -07:00
|
|
|
/// Version number. If Version is left empty, then Tag plus current
|
|
|
|
/// date (in the format YYMMDD) is used as a version number.
|
2009-04-27 06:38:47 -06:00
|
|
|
|
2012-01-02 10:23:05 -07:00
|
|
|
static const string Version = "";
|
2011-12-31 09:52:14 -07:00
|
|
|
static const string Tag = "";
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2011-12-30 04:53:51 -07:00
|
|
|
|
|
|
|
/// engine_info() returns the full name of the current Stockfish version.
|
2011-04-13 07:12:33 -06:00
|
|
|
/// This will be either "Stockfish YYMMDD" (where YYMMDD is the date when
|
|
|
|
/// the program was compiled) or "Stockfish <version number>", depending
|
2011-12-30 04:53:51 -07:00
|
|
|
/// on whether Version is empty.
|
2008-09-06 04:22:10 -06:00
|
|
|
|
2011-12-30 04:53:51 -07:00
|
|
|
const string engine_info(bool to_uci) {
|
2008-10-11 11:25:16 -06:00
|
|
|
|
2011-04-13 07:12:33 -06:00
|
|
|
const string months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
|
2011-12-30 03:30:26 -07:00
|
|
|
const string cpu64(Is64Bit ? " 64bit" : "");
|
|
|
|
const string popcnt(HasPopCnt ? " SSE4.2" : "");
|
2008-09-06 04:22:10 -06:00
|
|
|
|
2011-04-13 07:12:33 -06:00
|
|
|
string month, day, year;
|
2011-12-30 04:53:51 -07:00
|
|
|
stringstream s, date(__DATE__); // From compiler, format is "Sep 21 2008"
|
2008-12-14 09:26:05 -07:00
|
|
|
|
2011-12-30 04:53:51 -07:00
|
|
|
if (Version.empty())
|
|
|
|
{
|
|
|
|
date >> month >> day >> year;
|
2008-12-14 09:26:05 -07:00
|
|
|
|
2011-12-30 04:53:51 -07:00
|
|
|
s << "Stockfish " << Tag
|
|
|
|
<< setfill('0') << " " << year.substr(2)
|
|
|
|
<< setw(2) << (1 + months.find(month) / 4)
|
2012-03-03 01:35:56 -07:00
|
|
|
<< setw(2) << day;
|
2011-12-30 04:53:51 -07:00
|
|
|
}
|
|
|
|
else
|
2012-03-03 01:35:56 -07:00
|
|
|
s << "Stockfish " << Version;
|
2008-12-14 09:26:05 -07:00
|
|
|
|
2012-03-03 01:35:56 -07:00
|
|
|
s << cpu64 << popcnt << (to_uci ? "\nid author ": " by ")
|
2011-12-30 04:53:51 -07:00
|
|
|
<< "Tord Romstad, Marco Costalba and Joona Kiiski";
|
2008-12-14 09:26:05 -07:00
|
|
|
|
2011-12-30 04:53:51 -07:00
|
|
|
return s.str();
|
2008-12-14 09:26:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-30 08:14:24 -07:00
|
|
|
/// Debug functions used mainly to collect run-time statistics
|
2008-09-06 04:22:10 -06:00
|
|
|
|
2011-12-30 08:14:24 -07:00
|
|
|
static uint64_t hits[2], means[2];
|
2008-10-11 11:25:16 -06:00
|
|
|
|
2011-12-30 08:14:24 -07:00
|
|
|
void dbg_hit_on(bool b) { hits[0]++; if (b) hits[1]++; }
|
|
|
|
void dbg_hit_on_c(bool c, bool b) { if (c) dbg_hit_on(b); }
|
|
|
|
void dbg_mean_of(int v) { means[0]++; means[1] += v; }
|
2008-12-14 06:57:17 -07:00
|
|
|
|
2011-12-30 08:14:24 -07:00
|
|
|
void dbg_print() {
|
2008-10-04 05:21:28 -06:00
|
|
|
|
2011-12-30 08:14:24 -07:00
|
|
|
if (hits[0])
|
|
|
|
cerr << "Total " << hits[0] << " Hits " << hits[1]
|
|
|
|
<< " hit rate (%) " << 100 * hits[1] / hits[0] << endl;
|
2009-04-27 06:38:47 -06:00
|
|
|
|
2011-12-30 08:14:24 -07:00
|
|
|
if (means[0])
|
|
|
|
cerr << "Total " << means[0] << " Mean "
|
|
|
|
<< (float)means[1] / means[0] << endl;
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
2011-01-09 07:57:18 -07:00
|
|
|
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2012-03-17 20:20:43 -06:00
|
|
|
/// Our fancy logging facility. The trick here is to replace cout.rdbuf() with
|
|
|
|
/// this one that sends the output both to console and to a file, this allow us
|
|
|
|
/// to toggle the logging of std::cout to a file while preserving output to
|
|
|
|
/// stdout and without changing a single line of code! Idea and code from:
|
|
|
|
/// http://groups.google.com/group/comp.lang.c++/msg/1d941c0f26ea0d81
|
|
|
|
|
2012-03-19 14:36:25 -06:00
|
|
|
class Tee: public streambuf {
|
2012-03-17 20:20:43 -06:00
|
|
|
public:
|
|
|
|
typedef char_traits<char> traits_type;
|
|
|
|
typedef traits_type::int_type int_type;
|
|
|
|
|
2012-03-19 14:36:25 -06:00
|
|
|
Tee(ios& s, ofstream& f) : stream(s), file(f), stream_buf(s.rdbuf()) {}
|
|
|
|
~Tee() { set(false); }
|
2012-03-17 20:20:43 -06:00
|
|
|
|
2012-03-19 14:36:25 -06:00
|
|
|
void set(bool b) { stream.rdbuf(b ? this : stream_buf); }
|
2012-03-17 20:20:43 -06:00
|
|
|
|
|
|
|
private:
|
|
|
|
int_type overflow(int_type c) {
|
|
|
|
|
|
|
|
if (traits_type::eq_int_type(c, traits_type::eof()))
|
|
|
|
return traits_type::not_eof(c);
|
|
|
|
|
2012-03-19 14:36:25 -06:00
|
|
|
c = stream_buf->sputc(traits_type::to_char_type(c));
|
2012-03-17 20:20:43 -06:00
|
|
|
|
|
|
|
if (!traits_type::eq_int_type(c, traits_type::eof()))
|
|
|
|
c = file.rdbuf()->sputc(traits_type::to_char_type(c));
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sync() {
|
|
|
|
|
2012-03-19 14:36:25 -06:00
|
|
|
int c = stream_buf->pubsync();
|
2012-03-17 20:20:43 -06:00
|
|
|
|
|
|
|
if (c != -1)
|
|
|
|
c = file.rdbuf()->pubsync();
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2012-03-19 14:36:25 -06:00
|
|
|
int underflow() { return traits_type::not_eof(stream_buf->sgetc()); }
|
|
|
|
|
|
|
|
int uflow() {
|
|
|
|
|
|
|
|
int c = stream_buf->sbumpc();
|
|
|
|
|
|
|
|
if (!traits_type::eq_int_type(c, traits_type::eof()))
|
|
|
|
file.rdbuf()->sputc(traits_type::to_char_type(c));
|
|
|
|
|
|
|
|
return traits_type::not_eof(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
ios& stream;
|
|
|
|
ofstream& file;
|
|
|
|
streambuf* stream_buf;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Logger {
|
|
|
|
public:
|
|
|
|
Logger() : in(cin, file), out(cout, file) {}
|
|
|
|
~Logger() { set(false); }
|
|
|
|
|
|
|
|
void set(bool b) {
|
|
|
|
|
|
|
|
if (b && !file.is_open())
|
|
|
|
{
|
|
|
|
file.open("io_log.txt", ifstream::out | ifstream::app);
|
|
|
|
in.set(true);
|
|
|
|
out.set(true);
|
|
|
|
}
|
|
|
|
else if (!b && file.is_open())
|
|
|
|
{
|
|
|
|
out.set(false);
|
|
|
|
in.set(false);
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Tee in, out;
|
2012-03-17 20:20:43 -06:00
|
|
|
ofstream file;
|
|
|
|
};
|
|
|
|
|
|
|
|
void logger_set(bool b) {
|
|
|
|
|
|
|
|
static Logger l;
|
|
|
|
l.set(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-13 07:12:33 -06:00
|
|
|
/// cpu_count() tries to detect the number of CPU cores
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2011-04-13 07:12:33 -06:00
|
|
|
int cpu_count() {
|
|
|
|
|
2012-01-24 22:29:30 -07:00
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
2011-04-13 07:12:33 -06:00
|
|
|
SYSTEM_INFO s;
|
|
|
|
GetSystemInfo(&s);
|
2011-10-30 22:38:44 -06:00
|
|
|
return std::min(int(s.dwNumberOfProcessors), MAX_THREADS);
|
2011-04-13 07:12:33 -06:00
|
|
|
#else
|
2008-08-31 23:59:13 -06:00
|
|
|
|
|
|
|
# if defined(_SC_NPROCESSORS_ONLN)
|
2011-10-31 13:03:30 -06:00
|
|
|
return std::min((int)sysconf(_SC_NPROCESSORS_ONLN), MAX_THREADS);
|
2010-02-11 22:49:16 -07:00
|
|
|
# elif defined(__hpux)
|
|
|
|
struct pst_dynamic psd;
|
|
|
|
if (pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0) == -1)
|
|
|
|
return 1;
|
2011-10-31 13:03:30 -06:00
|
|
|
return std::min((int)psd.psd_proc_cnt, MAX_THREADS);
|
2008-08-31 23:59:13 -06:00
|
|
|
# else
|
|
|
|
return 1;
|
|
|
|
# endif
|
|
|
|
|
|
|
|
#endif
|
2011-04-13 07:12:33 -06:00
|
|
|
}
|
2008-08-31 23:59:13 -06:00
|
|
|
|
|
|
|
|
2011-11-05 04:19:21 -06:00
|
|
|
/// timed_wait() waits for msec milliseconds. It is mainly an helper to wrap
|
|
|
|
/// conversion from milliseconds to struct timespec, as used by pthreads.
|
|
|
|
|
2012-01-23 07:20:53 -07:00
|
|
|
void timed_wait(WaitCondition& sleepCond, Lock& sleepLock, int msec) {
|
2011-11-05 04:19:21 -06:00
|
|
|
|
2012-01-24 22:29:30 -07:00
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
2011-12-04 02:53:40 -07:00
|
|
|
int tm = msec;
|
2011-11-05 04:19:21 -06:00
|
|
|
#else
|
2012-03-03 10:53:37 -07:00
|
|
|
timespec ts, *tm = &ts;
|
|
|
|
uint64_t ms = Time::current_time().msec() + msec;
|
2011-11-05 04:19:21 -06:00
|
|
|
|
2012-03-03 10:53:37 -07:00
|
|
|
ts.tv_sec = ms / 1000;
|
|
|
|
ts.tv_nsec = (ms % 1000) * 1000000LL;
|
2011-12-04 02:53:40 -07:00
|
|
|
#endif
|
2011-11-05 04:19:21 -06:00
|
|
|
|
2011-12-04 02:53:40 -07:00
|
|
|
cond_timedwait(sleepCond, sleepLock, tm);
|
2011-11-05 04:19:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-19 04:10:54 -06:00
|
|
|
/// prefetch() preloads the given address in L1/L2 cache. This is a non
|
|
|
|
/// blocking function and do not stalls the CPU waiting for data to be
|
2011-04-13 07:12:33 -06:00
|
|
|
/// loaded from memory, that can be quite slow.
|
2010-06-19 04:10:54 -06:00
|
|
|
#if defined(NO_PREFETCH)
|
2011-04-13 07:12:33 -06:00
|
|
|
|
2010-06-19 04:10:54 -06:00
|
|
|
void prefetch(char*) {}
|
2011-04-13 07:12:33 -06:00
|
|
|
|
2010-06-19 04:10:54 -06:00
|
|
|
#else
|
|
|
|
|
|
|
|
void prefetch(char* addr) {
|
|
|
|
|
2011-12-30 09:08:07 -07:00
|
|
|
# if defined(__INTEL_COMPILER) || defined(__ICL)
|
2010-06-19 04:10:54 -06:00
|
|
|
// This hack prevents prefetches to be optimized away by
|
|
|
|
// Intel compiler. Both MSVC and gcc seems not affected.
|
|
|
|
__asm__ ("");
|
2011-12-30 09:08:07 -07:00
|
|
|
# endif
|
2010-06-19 04:10:54 -06:00
|
|
|
|
|
|
|
_mm_prefetch(addr, _MM_HINT_T2);
|
|
|
|
_mm_prefetch(addr+64, _MM_HINT_T2); // 64 bytes ahead
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|