Skip to content

Commit

Permalink
Merge pull request #46 from kobanium/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
KunihitoHoki authored Nov 23, 2020
2 parents a4d1ef3 + 502c461 commit d577ed5
Show file tree
Hide file tree
Showing 14 changed files with 367 additions and 86 deletions.
7 changes: 6 additions & 1 deletion server.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ MaxConnPerAddrMin 128
CutConnPerAddrMin 1024
MaxComPerAddr 5242880 # 5MiB in byte

# resign threshold
ResignEMADeno 5000
ResignEMAInit 0.10
ResignMRate 0.05

# weight keeping
DirWeight ./weight
WeightPolling 60 # in second
Expand All @@ -22,7 +27,7 @@ DirPool ./pool
SizeQueue 256
MaxSizeCSA 2097152
Log2LenRedundant 18 # 2^18 entry
MinLenPlay 16
MinLenPlay 2
MinAveChildren 3

# log keeping
Expand Down
12 changes: 9 additions & 3 deletions src/autousi/autousi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ static void output() noexcept {
steady_clock::time_point time_now = steady_clock::now();
if (time_now < time_last + seconds(print_status)) return;

static uint prev_nsend = 0;
static uint prev_nsend = 0;
double time_ave_tot = 0.0;
uint ntot = PlayManager::get().get_ngen_records();
uint nsend = Client::get().get_nsend();
Expand All @@ -190,9 +190,12 @@ static void output() noexcept {
char spid[16], sdev[16], buf[46];
int eid = PlayManager::get().get_eid(u);
int did = PlayManager::get().get_did(u);
bool do_resign = PlayManager::get().get_do_resign(u);
sprintf(spid, "%3u", eid);
if (did == -2) sprintf(sdev, "CPU");
else sprintf(sdev, "%3d", did);
if (!do_resign) sdev[0] = '*';

fill_n(buf, sizeof(buf), '#');
uint len = std::min(PlayManager::get().get_nmove(u) / 5,
static_cast<uint>(sizeof(buf)) - 1U);
Expand All @@ -208,7 +211,8 @@ static void output() noexcept {
int64_t wght_id = Client::get().get_wght()->get_id();
bool is_downloading = Client::get().is_downloading();
const char *buf_time = Client::get().get_buf_wght_time();
printf("- Recv: Weights' ID %" PRIi64 ", ", wght_id);
printf("- Recv: Weight's ID %" PRIi64 ", resign-th %.3f, ",
wght_id, Client::get().get_th_resign());

if (is_downloading) puts("NOW DOWNLOADING NEW WEIGHTS");
else printf("Last Check %s\n", buf_time);
Expand Down Expand Up @@ -250,9 +254,11 @@ int main() {
while (! flag_signal) {
output();
wght = Client::get().get_wght();
float th_resign = Client::get().get_th_resign();
deque<string> recs
= PlayManager::get().manage_play(Client::get().has_conn(),
wght->get_fname(), wght->get_crc64());
wght->get_fname(), wght->get_crc64(),
th_resign);

for (const string &rec : recs) {
Client::get().add_rec(rec.c_str(), rec.size());
Expand Down
16 changes: 8 additions & 8 deletions src/autousi/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ using uint = unsigned int;
constexpr uint snd_retry_interval = 5U; // in sec
constexpr uint snd_max_retry = 3U; // in sec
constexpr uint snd_sleep = 200U; // in msec
constexpr uint wght_polling = 30U; // in sec
constexpr uint wght_polling = 30U; // in sec
constexpr uint wght_retry_interval = 7U; // in sec

constexpr uint maxlen_rec_xz = 1024U * 1024U;
Expand Down Expand Up @@ -105,28 +105,28 @@ WghtFile::~WghtFile() noexcept {
remove(_fname.get_fname());
_all.erase(_fname); }

static uint16_t receive_header(const OSI::Conn &conn, uint TO, uint bufsiz) {
static float receive_header(const OSI::Conn &conn, uint TO, uint bufsiz) {
char buf[8];
conn.recv(buf, 8, TO, bufsiz);

uint Major = static_cast<uchar>(buf[0]);
uint Minor = static_cast<uchar>(buf[1]);
if (Major != Ver::major || Ver::minor < Minor)
die(ERR_INT("Please update autousi!"));

return bytes_to_int<uint16_t>(buf + 2); }

return (static_cast<float>(bytes_to_int<uint16_t>(buf + 2))
* (1.0f / 65536.0f)); }

Client::Client() noexcept
: _quit(false), _has_conn(false), _downloading(false), _nsend(0), _ndiscard(0),
_wght_id(-1), _ver_engine(-1) {
_buf_wght_time[0] = '\0'; }
_wght_id(-1) { _buf_wght_time[0] = '\0'; }

Client::~Client() noexcept {}

void Client::get_new_wght() {
// get new weight information
OSI::Conn conn(_saddr.get(), _port);
_ver_engine = receive_header(conn, _recvTO, _recv_bufsiz);
_th_resign = receive_header(conn, _recvTO, _recv_bufsiz);

char buf[BUFSIZ];
static_assert(12 <= BUFSIZ, "BUSIZ too small");
Expand Down Expand Up @@ -286,7 +286,7 @@ void Client::sender() noexcept {

try {
OSI::Conn conn(_saddr.get(), _port);
_ver_engine = receive_header(conn, _recvTO, _recv_bufsiz);
_th_resign = receive_header(conn, _recvTO, _recv_bufsiz);
conn.send(buf, len_head, _sendTO, _send_bufsiz);
conn.send(pJob->get_p(), pJob->get_len(), _sendTO, _send_bufsiz);
_nsend += 1U;
Expand Down
4 changes: 2 additions & 2 deletions src/autousi/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Client {
std::shared_ptr<const WghtFile> _wght;
char _buf_wght_time[256];
FName _dwght;
int _ver_engine;
float _th_resign;
uint _max_retry, _retry_count, _recvTO, _recv_bufsiz, _sendTO, _send_bufsiz;
uint _read_bufsiz, _port, _keep_wght;

Expand All @@ -68,6 +68,6 @@ class Client {
bool is_downloading() { return _downloading; }
uint get_nsend() const noexcept { return _nsend; };
uint get_ndiscard() const noexcept { return _ndiscard; }
int get_ver_engine() const noexcept { return _ver_engine; }
float get_th_resign() const noexcept { return _th_resign; }
bool has_conn() const noexcept { return _has_conn; }
};
97 changes: 72 additions & 25 deletions src/autousi/play.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <cctype>
#include <cinttypes>
#include <climits>
#include <cmath>
#include <csignal>
#include <cstdarg>
#include <cstdio>
Expand Down Expand Up @@ -50,6 +51,7 @@ using uint = unsigned int;

constexpr double time_average_rate = 0.999;
constexpr char fmt_log[] = "engine%03u-%03u.log";
static uint resign_count = 0;
static mutex m_seq;
static deque<SeqPRNService> seq_s;

Expand Down Expand Up @@ -177,7 +179,7 @@ class USIEngine : public Child {
uint64_t _crc64_wght;
int _device_id, _nnet_id, _version;
uint _eid, _nmove;
bool _flag_playing, _flag_ready, _flag_thinking;
bool _flag_playing, _flag_ready, _flag_thinking, _flag_do_resign;

void out_log(const char *p) noexcept {
assert(_ofs && p);
Expand Down Expand Up @@ -212,7 +214,8 @@ class USIEngine : public Child {
_id_wght(wfname.get_id()), _crc64_wght(crc64),
_device_id(device_id), _nnet_id(nnet_id), _version(-1), _eid(eid),
_nmove(0),
_flag_playing(false), _flag_ready(false), _flag_thinking(false) {
_flag_playing(false), _flag_ready(false), _flag_thinking(false),
_flag_do_resign(false) {
assert(cname.ok() && 0 < cname.get_len_fname());
assert(wfname.ok() && 0 < wfname.get_len_fname());
assert(isalnum(ch) && -2 < nnet_id && nnet_id < 65536);
Expand All @@ -226,6 +229,11 @@ class USIEngine : public Child {
char opt_q[] = "-q";
if (!verbose_eng) argv[argc++] = opt_q;

char opt_r[] = "-r";
char opt_r_value[] = "0.0";
argv[argc++] = opt_r;
argv[argc++] = opt_r_value;

char opt_p[] = "-p";
char opt_p_value[] = "800";
argv[argc++] = opt_p;
Expand Down Expand Up @@ -266,11 +274,13 @@ class USIEngine : public Child {

void start_newgame() noexcept {
_node.clear();
_flag_playing = true;
_flag_thinking = false;
_record_main = string("PI\n+\n");
_nmove = 0;
_startpos = string("position startpos moves");
_flag_playing = true;
_flag_thinking = false;
if ( (resign_count++ % 10) == 0 ) _flag_do_resign = false;
else _flag_do_resign = true;
_record_main = string("PI\n+\n");
_nmove = 0;
_startpos = string("position startpos moves");

char buf[256];
sprintf(buf, "%16" PRIx64, _crc64_wght);
Expand All @@ -290,7 +300,8 @@ class USIEngine : public Child {
_crc64_wght = crc64;
engine_out("setoption name USI_WeightFile value %s", wfname.get_fname()); }

string update(char *line, queue<string> &moves_eid0) noexcept {
string update(char *line, queue<string> &moves_eid0, float th_resign)
noexcept {
assert(line);
char *token, *saveptr;
token = OSI::strtok(line, " ,", &saveptr);
Expand Down Expand Up @@ -325,7 +336,8 @@ class USIEngine : public Child {
long int ver = strtol(token, &endptr, 10);
if (endptr == token || *endptr != '\0' || ver < 0 || 65535 < ver)
die(ERR_INT("Bad message from engine (%s).", get_fp()));
_version = ver; }
_version = ver;
if (ver < Ver::usi_engine) die(ERR_INT("Please update usi engine!")); }

return string(""); }

Expand All @@ -349,6 +361,8 @@ class USIEngine : public Child {
str_move_usi, get_fp(),
static_cast<const char *>(_node.to_str())));

bool flag_resign = false;
string new_record;
if (actionPlay.is_move()) {
steady_clock::time_point time_now = steady_clock::now();
auto rep = duration_cast<milliseconds>(time_now - _time_last).count();
Expand All @@ -358,9 +372,9 @@ class USIEngine : public Child {
_time_average = _time_average_nume / _time_average_deno;
_startpos += " ";
_startpos += str_move_usi;
_record_main += _node.get_turn().to_str();
_nmove += 1U;
_record_main += actionPlay.to_str(SAux::csa);
new_record += _node.get_turn().to_str();
new_record += actionPlay.to_str(SAux::csa);
if (_eid == 0) {
char buf[256];
sprintf(buf, " (%5.0fms)", _time_average);
Expand All @@ -369,30 +383,43 @@ class USIEngine : public Child {
smove += buf;
moves_eid0.push(std::move(smove)); }

const char *str_value = OSI::strtok(nullptr, " ,", &saveptr);
if (!str_value || str_value[0] != 'v' || str_value[1] != '=')
die(ERR_INT("cannot read value (engine %s)", get_fp()));

char *endptr;
float value = strtof(str_value+2, &endptr);
if (endptr == str_value+2 || *endptr != '\0' || value < 0.0f
|| value == HUGE_VALF)
die(ERR_INT("cannot interpret value %s (engine %s)",
str_value+2, get_fp()));
if (value < th_resign) flag_resign = true;

const char *str_count = OSI::strtok(nullptr, " ,", &saveptr);
if (!str_count) die(ERR_INT("cannot read count (engine %s)", get_fp()));

char *endptr;
long int num = strtol(str_count, &endptr, 10);
if (endptr == str_count || *endptr != '\0' || num < 1
|| num == LONG_MAX)
die(ERR_INT("cannot interpret a visit count %s (engine %s)",
if (endptr == str_count || *endptr != '\0' || num < 1 || num == LONG_MAX)
die(ERR_INT("cannot interpret visit count %s (engine %s)",
str_count, get_fp()));

num_best = num;
_record_main += ",'";
_record_main += to_string(num);
{
char buf[256];
sprintf(buf, ",'v=%.3f,%d", value, num);
new_record += buf;
}

// read candidate moves
while (true) {
str_move_usi = OSI::strtok(nullptr, " ,", &saveptr);
if (!str_move_usi) { _record_main += "\n"; break; }
if (!str_move_usi) { new_record += "\n"; break; }

Action action = _node.action_interpret(str_move_usi, SAux::usi);
if (!action.is_move())
die(ERR_INT("bad candidate %s (engine %s)", str_move_usi, get_fp()));
_record_main += ",";
_record_main += action.to_str(SAux::csa);
new_record += ",";
new_record += action.to_str(SAux::csa);

str_count = OSI::strtok(nullptr, " ,", &saveptr);
if (!str_count)
Expand All @@ -405,8 +432,8 @@ class USIEngine : public Child {
str_count, get_fp()));

num_tot += num;
_record_main += ",";
_record_main += to_string(num); } }
new_record += ",";
new_record += to_string(num); } }

if (num_best < num_tot) die(ERR_INT("bad counts (engine %s)", get_fp()));
_node.take_action(actionPlay);
Expand All @@ -417,11 +444,25 @@ class USIEngine : public Child {
assert(_node.ok());

// terminal test
if (_node.get_type().is_term()) {
if (flag_resign && _flag_do_resign) {
string rec;
rec += _record_wght + string(", ") + _record_version + string("\n");
rec += string("'") + _record_settings + string("\n");
rec += _record_main;
rec += "%TORYO" + string(",'autousi\n");
_flag_playing = false;
return move(rec); }

_record_main += new_record;
if (_node.get_type().is_term()) {
string rec;
if (_flag_do_resign) {
rec += _record_wght + string(", ") + _record_version + string("\n"); }
else {
rec += _record_wght + string(", ");
rec += _record_version + string(", no-resign\n"); }
rec += string("'") + _record_settings + string("\n");
rec += _record_main;
rec += "%" + string(_node.get_type().to_str()) + string("\n");
_flag_playing = false;
return move(rec); }
Expand All @@ -442,6 +483,7 @@ class USIEngine : public Child {
bool is_playing() const noexcept { return _flag_playing; }
bool is_ready() const noexcept { return _flag_ready; }
bool is_thinking() const noexcept { return _flag_thinking; }
bool get_do_resign() const noexcept { return _flag_do_resign; }
uint get_eid() const noexcept { return _eid; }
uint get_nmove() const noexcept { return _nmove; }
int get_did() const noexcept { return _device_id; }
Expand Down Expand Up @@ -509,7 +551,8 @@ void PlayManager::end() noexcept {
seq_s.clear(); }

deque<string> PlayManager::manage_play(bool has_conn, const FNameID &wfname,
uint64_t crc64) noexcept {
uint64_t crc64, float th_resign)
noexcept {
deque<string> recs;

Child::wait(1000U);
Expand All @@ -524,7 +567,7 @@ deque<string> PlayManager::manage_play(bool has_conn, const FNameID &wfname,
if (e->getline_in(line, sizeof(line)) == 0) flag_eof = true;
else {
bool flag_thinking = e->is_thinking();
string s = e->update(line, _moves_eid0);
string s = e->update(line, _moves_eid0, th_resign);
if (!s.empty()) {
_ngen_records += 1U;
recs.push_back(move(s)); }
Expand Down Expand Up @@ -567,6 +610,10 @@ bool PlayManager::get_moves_eid0(string &move) noexcept {
_moves_eid0.pop();
return true; }

bool PlayManager::get_do_resign(uint u) const noexcept {
assert(u < _engines.size());
return _engines[u]->get_do_resign(); }

uint PlayManager::get_eid(uint u) const noexcept {
assert(u < _engines.size());
return _engines[u]->get_eid(); }
Expand Down
4 changes: 3 additions & 1 deletion src/autousi/play.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ class PlayManager {
noexcept;
void end() noexcept;
std::deque<std::string> manage_play(bool has_conn, const FNameID &wfname,
uint64_t crc64) noexcept;
uint64_t crc64, float th_resign)
noexcept;
bool get_moves_eid0(std::string &move) noexcept;
uint get_nengine() const noexcept {
return static_cast<uint>(_engines.size()); }
uint get_ngen_records() const noexcept { return _ngen_records; };
uint get_nthinking() const noexcept { return _num_thinking; }
bool get_do_resign(uint u) const noexcept;
uint get_eid(uint u) const noexcept;
uint get_nmove(uint u) const noexcept;
int get_did(uint u) const noexcept;
Expand Down
Loading

0 comments on commit d577ed5

Please sign in to comment.