File: speed.c

package info (click to toggle)
gnubg 0.90-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 30,800 kB
  • ctags: 8,224
  • sloc: ansic: 92,884; xml: 13,338; sh: 9,675; makefile: 425; python: 392; yacc: 295; sql: 237; lex: 220; php: 102; cs: 81; awk: 25; sed: 16
file content (184 lines) | stat: -rw-r--r-- 3,768 bytes parent folder | download
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
/*
 * speed.c
 *
 * by Gary Wong <gtw@gnu.org>, 2003
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 3 or later of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: speed.c,v 1.21 2007/12/29 14:32:31 Superfly_Jon Exp $
 */

#include "config.h"

#if USE_GTK
#include "gtkgame.h"
#else
#include "backgammon.h"
#endif
#include <glib/gi18n.h>
#if USE_MULTITHREAD
#include "multithread.h"
#endif
#ifndef WIN32
#include <stdlib.h>
#endif

#include "speed.h"

#include <isaac.h>
#include <time.h>

#define EVALS_PER_ITERATION 1024

randctx rc;
double timeTaken;

extern void RunEvals(void)
{
	int aanBoard[ EVALS_PER_ITERATION ][ 2 ][ 25 ];
    int i, j, k;
	double t;
	float ar[ NUM_OUTPUTS ];

#if USE_MULTITHREAD
	MT_Exclusive();
#endif
	for( i = 0; i < EVALS_PER_ITERATION; i++ ) {
	    /* Generate a random board.  Don't allow chequers on the bar
	       or borne off, so we can trivially guarantee the position
	       is legal. */
	    for( j = 0; j < 25; j++ )
			aanBoard[ i ][ 0 ][ j ] = aanBoard[ i ][ 1 ][ j ] = 0;

	    for( j = 0; j < 15; j++ )
		{
			do
			{
				k = irand( &rc ) % 24;
			} while( aanBoard[ i ][ 1 ][ 23 - k ] );
			aanBoard[ i ][ 0 ][ k ]++;

			do
			{
				k = irand( &rc ) % 24;
			} while( aanBoard[ i ][ 0 ][ 23 - k ] );
			aanBoard[ i ][ 1 ][ k ]++;
		}
	}

#if USE_MULTITHREAD
	MT_Release();
	MT_SyncStart();
#else
	t = get_time();
#endif

	for( i = 0; i < EVALS_PER_ITERATION; i++ )
	{
		EvaluatePosition( NULL, (ConstTanBoard)aanBoard[ i ], ar, &ciCubeless, NULL );
	}

#if USE_MULTITHREAD
	if ((t = MT_SyncEnd()) != 0)
		timeTaken += t;
#else
	timeTaken += (get_time() - t);
#endif
}

extern void CommandCalibrate( char *sz )
{
	int iIter, n = -1;
	unsigned int i;
#if USE_GTK
    void *pcc = NULL;
#endif

#if USE_MULTITHREAD
	MT_SyncInit();
#endif

    if( sz && *sz )
	{
		n = ParseNumber( &sz );

		if( n < 1 ) {
			outputl( _("If you specify a parameter to `calibrate', "
				"it must be a number of iterations to run.") );
			return;
		}
	}

	if (clock() < 0)
	{
	    outputl( _("Calibration not available.") );
		return;
	}

    rc.randrsl[ 0 ] = (ub4)time( NULL );
    for( i = 0; i < RANDSIZ; i++ )
        rc.randrsl[ i ] = rc.randrsl[ 0 ];
    irandinit( &rc, TRUE);

#if USE_GTK
	if( fX )
		pcc = GTKCalibrationStart();
#endif
    
	timeTaken = 0;
    for( iIter = 0; iIter < n || n < 0; )
	{
		double spd;
		if (fInterrupt)
			break;

#if USE_MULTITHREAD
		mt_add_tasks(MT_GetNumThreads(), TT_RUNCALIBRATIONEVALS, NULL);
		MT_WaitForTasks(NULL, 0);
		iIter += MT_GetNumThreads();
#else
		RunEvals();
		iIter++;
#endif

		if (timeTaken == 0)
			spd = 0;
		else
			spd = iIter * (EVALS_PER_ITERATION * CLOCKS_PER_SEC / timeTaken);
#if USE_GTK
		if( fX )
			GTKCalibrationUpdate(pcc, (float)spd);
		else
#endif
		if( fShowProgress ) {
			outputf( "        \rCalibrating: %.0f static evaluations/second", spd);
			fflush( stdout );
		}
    }
    
#if USE_GTK
	if( fX )
		GTKCalibrationEnd( pcc );
#endif

    if( timeTaken )
	{
		rEvalsPerSec = iIter * (float)(EVALS_PER_ITERATION *
			CLOCKS_PER_SEC / timeTaken);
		outputf( "\rCalibration result: %.0f static evaluations/second.\n",
				rEvalsPerSec );
    }
	else
		outputl( _("Calibration incomplete.") );
}