1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
/**
Copyright (c) Scott Gasch
Module Name:
dumptree.c
Abstract:
Support routines for dumping a search tree into an XML file for
offline analysis by typhoonui.exe.
Author:
Scott Gasch ([email protected]) 17 Jul 2004
Revision History:
**/
#ifdef DUMP_TREE
#include "chess.h"
#define NHUMAN_READABLE
#ifdef HUMAN_READABLE
#define SPACES_TO_INDENT_PER_PLY (2)
#else
#define SPACES_TO_INDENT_PER_PLY (0)
#endif
#define DISK_SECTOR_SIZE_BYTES (512)
#define NUM_SECTORS_TO_BUFFER (2)
FILE *pfTreeDump = NULL;
ULONG g_uBufferSize = 0;
ULONG g_uBufferUsed = 0;
CHAR *g_Buffer = NULL;
#ifdef MP
volatile static ULONG g_uBufferLock = 0;
#define BUF_IS_LOCKED (g_uBufferLock != 0)
#define LOCK_BUF \
AcquireSpinLock(&g_uBufferLock); \
ASSERT(BUF_IS_LOCKED);
#define UNLOCK_BUF \
ASSERT(BUF_IS_LOCKED); \
ReleaseSpinLock(&g_uBufferLock);
#else // no MP
#define BUF_IS_LOCKED
#define LOCK_BUF
#define UNLOCK_BUF
#endif
void
InitializeTreeDump(void)
/**
Routine description:
Initialize tree dumping system.
Parameters:
void
Return value:
void
**/
{
//
// Allocate an internal buffer so that our writes to disk
// approximate a sector size.
//
g_uBufferSize = DISK_SECTOR_SIZE_BYTES * NUM_SECTORS_TO_BUFFER;
g_Buffer = SystemAllocateMemory(g_uBufferSize);
//
// Here's the file we'll want to write to...
//
pfTreeDump = fopen("typhoon.xml", "wb+");
if (NULL == pfTreeDump)
{
UtilPanic(FATAL_ACCESS_DENIED,
NULL, "typhoon.xml", "write", "NULL", __FILE__, __LINE__);
}
fprintf(pfTreeDump, "<?xml version=\"1.0\" encoding=\"utf-8\""
" standalone=\"no\"?>\n");
}
static
void _FlushTheBuffer(void)
/**
Routine description:
Flush the buffer to the disk file.
Parameters:
void
Return value:
void
**/
{
fprintf(pfTreeDump, g_Buffer);
g_Buffer[0] = '\0';
g_uBufferUsed = 0;
}
static void CDECL
_AppendToBuffer(CHAR *buf)
/**
Routine description:
Append a string to the buffer; possibly flush the buffer to disk
first to make enough room for the new string.
Parameters:
CHAR *buf : the string to append
Return value:
void
**/
{
ULONG uSpaceNeeded;
ASSERT(BUF_IS_LOCKED);
//
// How much space do we need?
//
uSpaceNeeded = strlen(buf);
ASSERT(uSpaceNeeded < MEDIUM_STRING_LEN_CHAR);
ASSERT(uSpaceNeeded < g_uBufferSize);
//
// See if we need to flush the real buffer to the disk before we can
// append this new message.
//
LOCK_BUF;
if (g_uBufferUsed + uSpaceNeeded >= g_uBufferSize)
{
_FlushTheBuffer();
ASSERT(g_uBufferUsed + uSpaceNeeded < g_uBufferSize);
}
//
// Append the new data to the big buffer
//
strcat(g_Buffer, buf);
g_uBufferUsed += uSpaceNeeded;
ASSERT(g_uBufferUsed < g_uBufferSize);
UNLOCK_BUF;
}
static void _IndentTreeDumpMessage(ULONG uPly)
/**
Routine description:
Append a number of spaces to the buffer to indent the next output
appropriately based upon the current search ply.
Parameters:
ULONG uPly : the current search ply
Return value:
void
**/
{
ULONG uSpaces = uPly * SPACES_TO_INDENT_PER_PLY;
ASSERT(uSpaces < g_uBufferSizeBytes);
ASSERT(HUMAN_READABLE);
while(uSpaces)
{
_AppendToBuffer(" ");
uSpaces--;
}
}
void CDECL
DTTrace(ULONG uPly, CHAR *szMessage, ...)
/**
Routine description:
A printf-like routine for adding a message to the buffer / dumpfile.
Note: messages that exceed MEDIUM_STRING_LEN_CHAR are truncated.
Parameters:
ULONG uPly : the current search ply
CHAR *szMessage : the message format string
... : variable number of interpolated arguments
Return value:
void
**/
{
va_list ap;
CHAR buf[MEDIUM_STRING_LEN_CHAR];
buf[0] = '\0';
//
// Populate the local buffer
//
va_start(ap, szMessage);
COMPILER_VSNPRINTF(buf, MEDIUM_STRING_LEN_CHAR - 2, szMessage, ap);
va_end(ap);
#ifdef HUMAN_READABLE
strcat(buf, "\n");
_IndentTreeDumpMessage(uPly);
#endif
_AppendToBuffer(buf);
}
void
CleanupTreeDump(void)
/**
Routine description:
Tear down the tree dumping system
Parameters:
void
Return value:
void
**/
{
//
// Cleanup file and buffer
//
if (NULL != pfTreeDump)
{
_FlushTheBuffer();
fflush(pfTreeDump);
fclose(pfTreeDump);
}
if (NULL != g_Buffer)
{
SystemFreeMemory(g_Buffer);
g_Buffer = NULL;
g_uBufferSize = 0;
}
}
void
DTEnterNode(SEARCHER_THREAD_CONTEXT *ctx,
ULONG uDepth,
FLAG fIsQNode,
SCORE iAlpha,
SCORE iBeta)
/**
Routine description:
Called by search or qsearch upon entering a new node.
Parameters:
SEARCHER_THREAD_CONTEXT *ctx : searcher context
ULONG uDepth : depth
FLAG fIsQNode : is it a q node?
SCORE iAlpha : current alpha
SCORE iBeta : current beta
Return value:
void
**/
{
ULONG uPly = ctx->uPly;
UINT64 u64NodeNum = ctx->sCounters.tree.u64TotalNodeCount;
CHAR *p;
if (FALSE == fIsQNode)
{
DTTrace(uPly, "<n n=\"%"
COMPILER_LONGLONG_UNSIGNED_FORMAT
"\">", u64NodeNum);
}
else
{
DTTrace(uPly, "<qn n=\"%"
COMPILER_LONGLONG_UNSIGNED_FORMAT
"\">", u64NodeNum);
p = PositionToFen(&ctx->sPosition);
ASSERT(p);
DTTrace(uPly + 1, "<fen p=\"%s\"/>", p);
SystemFreeMemory(p);
DTTrace(uPly + 1, "<i ab=\"%d, %d\" ply=\"%u\" depth=\"%u\"/>",
iAlpha, iBeta, uPly, uDepth);
}
}
void
DTLeaveNode(SEARCHER_THREAD_CONTEXT *ctx,
FLAG fQNode,
SCORE iBestScore,
MOVE mvBestMove)
/**
Routine description:
Called by the search upon leaving a normal (non q) node.
Parameters:
SEARCHER_THREAD_CONTEXT *ctx : searcher context
SCORE iBestScore : best score found
MOVE mvBestMove : best move found
Return value:
void
**/
{
ULONG uPly = ctx->uPly;
DTTrace(uPly + 1, "<d score=\"%d\" mv=\"%s\"/>",
iBestScore, MoveToSan(mvBestMove, &ctx->sPosition));
if (!fQNode)
{
DTTrace(uPly, "</n>");
}
else
{
DTTrace(uPly, "</qn>");
}
}
#endif
|