d70f546b4db443413a35c214ea2c0d4f1fc36450
[gnushogi.git] / gnushogi / sizetest.c
1 /*
2  * FILE: sizetest.c
3  *
4  *     Display memory usage of GNU Shogi data structures.
5  *
6  * ----------------------------------------------------------------------
7  * Copyright (c) 1993, 1994, 1995 Matthias Mutz
8  * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
9  *
10  * GNU SHOGI is based on GNU CHESS
11  *
12  * Copyright (c) 1988, 1989, 1990 John Stanback
13  * Copyright (c) 1992 Free Software Foundation
14  *
15  * This file is part of GNU SHOGI.
16  *
17  * GNU Shogi is free software; you can redistribute it and/or modify it
18  * under the terms of the GNU General Public License as published by the
19  * Free Software Foundation; either version 3 of the License,
20  * or (at your option) any later version.
21  *
22  * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
23  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25  * for more details.
26  *
27  * You should have received a copy of the GNU General Public License along
28  * with GNU Shogi; see the file COPYING. If not, see
29  * <http://www.gnu.org/licenses/>.
30  * ----------------------------------------------------------------------
31  *
32  */
33
34 #include "gnushogi.h"
35
36 #include <signal.h>
37
38 struct leaf  *Tree, *root;
39
40 short FROMsquare, TOsquare;
41
42 small_short ChkFlag[MAXDEPTH], CptrFlag[MAXDEPTH], TesujiFlag[MAXDEPTH];
43 short Pscore[MAXDEPTH], Tscore[MAXDEPTH];
44 small_short Pindex[NO_SQUARES];
45
46 short mtl[2], hung[2];
47 small_short PieceCnt[2];
48
49 struct GameRec  *GameList;
50
51 char ColorStr[2][10];
52
53 long znodes;
54
55
56 /*
57  * In a networked enviroment gnushogi might be compiled on different hosts
58  * with different random number generators; that is not acceptable if they
59  * are going to share the same transposition table.
60  */
61
62 unsigned long next = 1;
63
64 unsigned int
65 urand(void)
66 {
67     next *= 1103515245;
68     next += 12345;
69     return ((unsigned int) (next >> 16) & 0xFFFF);
70 }
71
72
73 void
74 gsrand(unsigned int seed)
75 {
76     next = seed;
77 }
78
79
80 #if ttblsz
81 struct hashentry *ttable[2];
82 unsigned int ttblsize;
83 #endif
84
85 #ifdef BINBOOK
86 extern char *binbookfile;
87 #endif
88
89 extern char *bookfile;
90
91 char savefile[128] = "";
92 char listfile[128] = "";
93
94 #if defined HISTORY
95 unsigned short *history;
96 #endif
97
98 short rpthash[2][256];
99 short TrPnt[MAXDEPTH];
100 small_short PieceList[2][NO_SQUARES];
101 small_short PawnCnt[2][NO_COLS];
102 small_short Captured[2][NO_PIECES];
103 small_short Mvboard[NO_SQUARES];
104
105 #if !defined SAVE_SVALUE
106 short svalue[NO_SQUARES];
107 #endif
108
109 struct flags flag;
110
111 short opponent, computer, WAwindow, WBwindow, BAwindow, BBwindow, dither,
112     INCscore;
113 long ResponseTime, ExtraTime, MaxResponseTime, et, et0, time0, ft;
114 long GenCnt, NodeCnt, ETnodes, EvalNodes, HashCnt,
115     HashAdd, FHashCnt, FHashAdd,
116     HashCol, THashCol, filesz, hashmask, hashbase;
117 long replus, reminus;
118 short HashDepth = HASHDEPTH, HashMoveLimit = HASHMOVELIMIT;
119 short player, xwndw;
120 /*unsigned*/ short rehash; /* -1 is used as a flag --tpm */
121 short Sdepth, Game50, MaxSearchDepth;
122 short GameCnt = 0;
123 short contempt;
124 int Book;
125 struct TimeControlRec TimeControl;
126 int TCadd = 0;
127 short TCflag, TCmoves, TCminutes, TCseconds, OperatorTime;
128 short XCmoves[3], XCminutes[3], XCseconds[3], XC, XCmore;
129 const short otherside[3] = { white, black, neutral };
130 unsigned short hint;
131 short TOflag;       /* force search re-init if we backup search */
132
133 unsigned short killr0[MAXDEPTH], killr1[MAXDEPTH];
134 unsigned short killr2[MAXDEPTH], killr3[MAXDEPTH];
135 unsigned short PV, SwagHt, Swag0, Swag1, Swag2, Swag3, Swag4, sidebit;
136
137 small_short HasPiece[2][NO_PIECES];
138 const short kingP[3] = { 4, 76, 0 };
139
140 const long control[NO_PIECES] =
141 { 0, ctlP, ctlL, ctlN, ctlS, ctlG, ctlB, ctlR,
142   ctlPp, ctlLp, ctlNp, ctlSp, ctlBp, ctlRp, ctlK };
143
144 short stage, stage2;
145
146 FILE *hashfile;
147
148 unsigned int starttime;
149 short ahead = true, hash = true;
150
151
152 int timeopp[MINGAMEIN], timecomp[MINGAMEIN];
153 int compptr, oppptr;
154
155 void
156 TimeCalc()
157 {
158     /* adjust number of moves remaining in gamein games */
159     int increment = 0;
160     int topsum = 0;
161     int tcompsum = 0;
162     int me, him;
163     int i;
164
165     /* don't do anything til you have enough numbers */
166     if (GameCnt < (MINGAMEIN * 2))
167         return;
168
169     /* calculate average time in sec for last MINGAMEIN moves */
170     for (i = 0; i < MINGAMEIN; i++)
171     {
172         tcompsum += timecomp[i];
173         topsum += timeopp[i];
174     }
175
176     topsum /= (100 * MINGAMEIN);
177     tcompsum /= (100 * MINGAMEIN);
178     /* if I have less time than opponent add another move */
179     me = TimeControl.clock[computer] / 100;
180     him = TimeControl.clock[opponent] / 100;
181
182     if (me < him)
183         increment += 2;
184
185     if (((him - me) > 60) || ((me < him) && (me < 120)))
186         increment++;
187
188     /* if I am losing more time with each move add another */
189     /* if (!((me - him) > 60) && tcompsum > topsum) increment++; */
190
191     if (tcompsum > topsum)
192     {
193         increment += 2;
194     }
195     else if ((TimeControl.moves[computer] < MINMOVES) && !increment)
196     {
197         /* but don't let moves go below MINMOVES */
198         increment++;
199     }
200     else if ((me > him) && (tcompsum < topsum))
201     {
202         /* if I am doing really well use more time per move */
203         increment = -1;
204     }
205
206     TimeControl.moves[computer] += increment;
207 }
208
209
210
211 int
212 main(int argc, char **argv)
213 {
214     long l;
215     int  n;
216
217 #if ttblsz
218     l = (long)sizeof(struct hashentry);
219     n = (int)((l * (ttblsz + rehash) * 2) / 1000);
220     printf("ttable:\t\t%4d\tkByte\t[hashentry:%ld "
221            "* (ttblsz:%d + rehash:%d) * 2]\n",
222            n, l, ttblsz, rehash);
223 #endif
224
225 #if defined CACHE
226     l = (long)sizeof(struct etable);
227     n = (int)((l * (size_t)ETABLE) / 1000);
228 #else
229     l = n = 0;
230 #endif
231
232     printf("etab:\t\t%4d\tkByte\t[etable:%ld ETABLE:%d]\n",
233            n, l, ETABLE);
234
235     l = (long)sizeof(struct leaf);
236     n = (int)(l * TREE / 1000);
237     printf("Tree:\t\t%4d\tkByte\t[leaf:%ld * TREE:%d]\n",
238            n, l, TREE);
239
240 #if defined HISTORY
241     n = (int)(sizeof_history / 1000);
242 #else
243     n = 0;
244 #endif
245
246     printf("history:\t%4d\tkByte\t[unsigned short:%d "
247            "* HISTORY_SIZE:%ld]\n",
248            n, sizeof(unsigned short), (long)HISTORY_SIZE);
249
250 #ifndef SAVE_NEXTPOS
251     l = (long)sizeof(next_array);
252     n = (int)((l * NO_PTYPE_PIECES) / 1000);
253
254     printf("nextpos:\t%4d\tkByte\t[next_array:%ld "
255            "* NO_PTYPE_PIECES:%d]\n",
256            n, l, NO_PTYPE_PIECES);
257
258     l = (long)sizeof(next_array);
259     n = (int)((l * NO_PTYPE_PIECES) / 1000);
260     printf("nextdir:\t%4d\tkByte\t[next_array:%ld "
261            "* NO_PTYPE_PIECES:%d]\n",
262            n, l, NO_PTYPE_PIECES);
263 #endif
264
265 #ifndef SAVE_DISTDATA
266     n = (int)(sizeof(distdata_array) / 1000);
267     printf("distdata:\t%4d\tkByte\n", n);
268 #endif
269
270 #ifndef SAVE_PTYPE_DISTDATA
271     l = (long)sizeof(distdata_array);
272     n = (int)((l * NO_PTYPE_PIECES) / 1000);
273     printf("ptype_distdata:\t%4d\tkByte\t[distdata_array:%ld "
274            "* NO_PTYPE_PIECES:%d]\n",
275            n, l, NO_PTYPE_PIECES);
276 #endif
277
278     l = (long)sizeof(hashcode_array);
279     n = (int)(l / 1000);
280     printf("hashcode:\t%4d\tkByte\t[hashval:%ld]\n",
281            n, (long)sizeof(struct hashval));
282
283     l = (long)sizeof(drop_hashcode_array);
284     n = (int)(l / 1000);
285     printf("drop_hashcode:\t%4d\tkByte\t[hashval:%ld]\n",
286            n, (long)sizeof(struct hashval));
287
288     l = (long)sizeof(value_array);
289     n = (int)(l / 1000);
290     printf("value:\t\t%4d\tkByte\n", n);
291
292     l = (long)sizeof(fscore_array);
293     n = (int)(l / 1000);
294     printf("fscore:\t\t%4d\tkByte\n", n);
295
296     return 0;
297 }
298