All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
usiReporter.cc
Go to the documentation of this file.
1 /* usiReporter.cc
2  */
4 #include "osl/record/usi.h"
5 #include "osl/misc/lightMutex.h"
7 #include "osl/oslConfig.h"
8 #include <iostream>
9 
10 void osl::search::
11 UsiReporter::newDepth(std::ostream& os, int depth)
12 {
13  if (OslConfig::usiModeInSilent())
14  return;
15  os << "info depth " << depth << "\n";
16 }
17 void osl::search::
18 UsiReporter::showPV(std::ostream& os, int depth, size_t node_count, double elapsed, int value, Move cur, const Move *first, const Move *last, bool ignore_silent)
19 {
20  if (OslConfig::usiModeInSilent() && ! ignore_silent)
21  return;
22  int seldepth = last-first;
23  if (last == first || *first != cur)
24  ++seldepth;
25  if (ignore_silent)
26  std::cerr << "info depth " << depth << " seldepth " << seldepth
27  << " time " << static_cast<int>(elapsed*1000) << " score cp " << value
28  << " nodes " << node_count
29  << " nps " << static_cast<int>(node_count/elapsed)
30  << " pv " << record::usi::show(cur) << "\n";
31  os << "info depth " << depth << " seldepth " << seldepth
32  << " time " << static_cast<int>(elapsed*1000) << " score cp " << value
33  << " nodes " << node_count << " nps " << static_cast<int>(node_count/elapsed);
34  os << " pv " << record::usi::show(cur);
35  if (first != last) {
36  if (cur == *first)
37  ++first;
38  while (first != last) {
39  os << ' ' << record::usi::show(*first++);
40  }
41  }
42  os << "\n";
43 }
44 
45 void osl::search::
46 UsiReporter::showPVExtended(std::ostream& os, int depth, size_t node_count, double elapsed, int value, Move cur, const Move *first, const Move *last, const bool *tfirst, const bool *tlast)
47 {
48  if (OslConfig::usiModeInSilent())
49  return;
50  int seldepth = last-first;
51  assert(seldepth == tlast-tfirst);
52  if (last - first && *first != cur)
53  ++seldepth;
54  os << "info depth " << depth << " seldepth " << seldepth
55  << " time " << static_cast<int>(elapsed*1000) << " score cp " << value
56  << " nodes " << node_count << " nps " << static_cast<int>(node_count/elapsed);
57  os << " pv " << record::usi::show(cur);
58  if (first != last) {
59  if (cur == *first)
60  ++first, ++tfirst;
61  while (first != last) {
62  os << ' ' << record::usi::show(*first++);
63  if (tfirst != tlast && *tfirst++)
64  os << "(^)";
65  }
66  }
67  os << "\n";
68 }
69 
71 void osl::search::
72 UsiReporter::rootMove(std::ostream& os, Move cur, bool allow_frequent_display)
73 {
74  if (OslConfig::usiModeInSilent())
75  return;
76  static MilliSeconds prev;
77  if (! allow_frequent_display)
78  {
79  MilliSeconds now = MilliSeconds::now();
80  {
81  SCOPED_LOCK(lk, usi_root_move_mutex);
82  if ((now - prev).toSeconds() < 0.5)
83  return;
84  prev = now;
85  }
86  }
87  os << "info currmove " << record::usi::show(cur) << "\n";
88 }
89 
90 void osl::search::
91 UsiReporter::timeInfo(std::ostream& os, size_t node_count, double elapsed)
92 {
93  if (OslConfig::usiModeInSilent())
94  return;
95  os << "info time " << static_cast<int>(elapsed*1000)
96  << " nodes " << node_count << " nps " << static_cast<int>(node_count/elapsed) << "\n";
97  os << std::flush;
98 }
99 
100 void osl::search::
101 UsiReporter::hashInfo(std::ostream& os, double ratio)
102 {
103  if (OslConfig::usiModeInSilent())
104  return;
105  os << "info hashfull " << static_cast<int>(ratio*1000) << "\n";
106  os << std::flush;
107 }
108 
109 
110 
112 UsiMonitor::UsiMonitor(bool ex, std::ostream& o, double s)
113  : silent_period(s), extended(ex), os(o), udp_socket(0), udp_endpoint(0),
114  client_id("")
115 {
116 }
117 
120 {
121 }
122 
123 void osl::search::
125 {
126  if ((!forced && depth0.elapsedSeconds() < silent_period)
127  || deferred.empty())
128  return;
129  os << deferred << std::flush;
130  if (udp_socket) {
131  if (client_id != "")
132  deferred = client_id + " " + deferred;
133  boost::system::error_code ignored_error;
134  udp_socket->send_to(boost::asio::buffer(deferred.c_str(), deferred.length()),
135  *udp_endpoint,
136  0, ignored_error);
137  }
138  deferred.clear();
139 }
140 
141 void osl::search::
143 {
144  last_root_move = Move();
145  if (depth == 0) {
146  depth0 = MilliSeconds::now();
147  return;
148  }
149  if (depth0.elapsedSeconds() >= silent_period) {
150  showDeferred();
151  UsiReporter::newDepth(os, depth);
152  }
153 }
154 
155 void osl::search::
156 UsiMonitor::showPV(int depth, size_t node_count, double elapsed, int value, Move cur, const Move *first, const Move *last,
157  const bool *threatmate_first, const bool *threatmate_last)
158 {
159  const bool defer = depth0.elapsedSeconds() < silent_period;
160  std::ostringstream ss;
161  if (extended)
162  UsiReporter::showPVExtended(ss, depth, node_count, elapsed, value, cur, first, last,
163  threatmate_first, threatmate_last);
164  else
165  UsiReporter::showPV(ss, depth, node_count, elapsed, value, cur, first, last);
166  if (defer)
167  deferred = ss.str();
168  else {
169  std::string msg = ss.str();
170  os << msg << std::flush;
171  if (udp_socket) {
172  if (client_id != "")
173  msg = client_id + " " + msg;
174  boost::system::error_code ignored_error;
175  udp_socket->send_to(boost::asio::buffer(msg.c_str(), msg.length()),
176  *udp_endpoint, 0,
177  ignored_error);
178  }
179  deferred.clear(); // msg sent was newer than deferred
180  }
181 }
182 
184 showFailLow(int depth, size_t node_count, double elapsed, int value, Move cur)
185 {
186  showPV(depth, node_count, elapsed, value, cur, 0, 0, 0, 0);
187 }
188 
189 void osl::search::
191 {
192  showDeferred();
193  last_root_move = cur;
194 }
195 
196 void osl::search::
198 {
199  showDeferred();
200  last_root_move = cur;
201  UsiReporter::rootMove(os, cur, true);
202 }
203 
204 void osl::search::
205 UsiMonitor::timeInfo(size_t node_count, double elapsed)
206 {
207  showDeferred();
208  {
209  std::ostringstream ss;
210  UsiReporter::timeInfo(ss, node_count, elapsed);
211  std::string msg = ss.str();
212  os << msg << std::flush;
213  if (udp_socket) {
214  if (client_id != "")
215  msg = client_id + " " + msg;
216  boost::system::error_code ignored_error;
217  udp_socket->send_to(boost::asio::buffer(msg.c_str(), msg.length()),
218  *udp_endpoint, 0,
219  ignored_error);
220  }
221  }
222  UsiReporter::rootMove(os, last_root_move);
223 }
224 
225 void osl::search::
227 {
228  showDeferred();
229  UsiReporter::hashInfo(os, ratio);
230 }
231 
232 void osl::search::
234 {
236  return;
237  showDeferred();
238  os << "info string forced move at the root: "
239  << record::usi::show(the_move) << "\n";
240  os << std::flush;
241 }
242 
243 void osl::search::
245 {
247  return;
248  deferred.clear();
249  os << "info string loss by checkmate\n";
250  os << std::flush;
251 }
252 
253 void osl::search::
254 UsiMonitor::setUdpLogging(std::string& udp_client_id,
255  boost::asio::ip::udp::socket *s,
256  boost::asio::ip::udp::endpoint *e)
257 {
258  client_id = udp_client_id;
259  udp_socket = s;
260  udp_endpoint = e;
261 }
262 
263 void osl::search::
265 {
266  showDeferred(true);
267 }
268 
269 // ;;; Local Variables:
270 // ;;; mode:c++
271 // ;;; c-basic-offset:2
272 // ;;; End: