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