[go: up one dir, main page]

Menu

[r1070]: / src / coordination.c  Maximize  Restore  History

Download this file

396 lines (310 with data), 9.4 kB

  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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
Protector -- a UCI chess engine
Copyright (C) 2009-2010 Raimund Heid (Raimund_Heid@yahoo.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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, see <http://www.gnu.org/licenses/>.
*/
#include "coordination.h"
#include "protector.h"
#include "search.h"
#include "matesearch.h"
#include "io.h"
#include "hash.h"
#include <stdio.h>
#include <assert.h>
#include <pthread.h>
#include <time.h>
/* #define DEBUG_COORDINATION */
pthread_t searchThread[MAX_THREADS];
pthread_t timer;
static pthread_mutex_t guiSearchMutex = PTHREAD_MUTEX_INITIALIZER;
long searchThreadId[MAX_THREADS];
static int numThreads = 1;
static SearchTask dummyTask;
static SearchTask *currentTask = &dummyTask;
static Variation variations[MAX_THREADS];
static Hashtable sharedHashtable;
static PawnHashInfo pawnHashtable[MAX_THREADS][PAWN_HASHTABLE_SIZE];
Hashtable *getSharedHashtable()
{
return &sharedHashtable;
}
int setNumberOfThreads(int _numThreads)
{
numThreads = max(1, min(MAX_THREADS, _numThreads));
return numThreads;
}
int getNumberOfThreads()
{
return numThreads;
}
UINT64 getNodeCount(void)
{
int threadCount;
UINT64 sum = 0;
for (threadCount = 0; threadCount < numThreads; threadCount++)
{
sum += variations[threadCount].nodes;
}
return sum;
}
Variation *getCurrentVariation()
{
return &variations[0];
}
void getGuiSearchMutex(void)
{
#ifdef DEBUG_COORDINATION
logDebug("aquiring search lock...\n");
#endif
pthread_mutex_lock(&guiSearchMutex);
#ifdef DEBUG_COORDINATION
logDebug("search lock aquired...\n");
#endif
}
void releaseGuiSearchMutex(void)
{
pthread_mutex_unlock(&guiSearchMutex);
#ifdef DEBUG_COORDINATION
logDebug("search lock released...\n");
#endif
}
static int startSearch(Variation * currentVariation)
{
currentVariation->searchStatus = SEARCH_STATUS_RUNNING;
switch (currentTask->type)
{
case TASKTYPE_BEST_MOVE:
currentTask->bestMove = search(currentVariation, NULL);
break;
case TASKTYPE_TEST_BEST_MOVE:
currentTask->bestMove =
search(currentVariation, &currentTask->solutions);
break;
case TASKTYPE_MATE_IN_N:
searchForMate(currentVariation,
&currentTask->calculatedSolutions,
currentTask->numberOfMoves);
break;
case TASKTYPE_TEST_MATE_IN_N:
searchForMate(currentVariation,
&currentTask->calculatedSolutions,
currentTask->numberOfMoves);
break;
default:
break;
}
currentTask->nodes = getNodeCount();
if (currentVariation->threadNumber == 0)
{
int threadCount;
for (threadCount = 1; threadCount < numThreads; threadCount++)
{
variations[threadCount].terminate = TRUE;
}
pthread_cancel(timer);
}
#ifdef DEBUG_COORDINATION
logDebug("Search thread terminated.\n");
#endif
getGuiSearchMutex();
currentVariation->searchStatus = SEARCH_STATUS_FINISHED;
releaseGuiSearchMutex();
return 0;
}
long getElapsedTime()
{
return getTimestamp() - variations[0].startTime;
}
static void *executeSearch(void *arg)
{
Variation *currentVariation = arg;
startSearch(currentVariation);
return 0;
}
static void *watchTime(void *arg)
{
Variation *currentVariation = arg;
long timeLimit = currentVariation->timeLimit;
struct timespec requested, remaining;
int result;
requested.tv_sec = timeLimit / 1000;
requested.tv_nsec = 1000000 * (timeLimit - 1000 * requested.tv_sec);
/* logReport("### Timer thread working sec=%ld nsec=%ld ###\n",
requested.tv_sec, requested.tv_nsec); */
result = nanosleep(&requested, &remaining);
if (result != -1)
{
getGuiSearchMutex();
prepareSearchAbort();
releaseGuiSearchMutex();
}
return 0;
}
void startTimerThread(SearchTask * task)
{
if (task->variation->timeLimit > 0 && task->variation->ponderMode == FALSE)
{
if (pthread_create(&timer, NULL, &watchTime, task->variation) == 0)
{
#ifdef DEBUG_COORDINATION
logDebug("Timer thread started.\n");
#endif
}
else
{
logDebug("### Timer thread could not be started. ###\n");
exit(EXIT_FAILURE);
}
}
}
void scheduleTask(SearchTask * task)
{
const unsigned long startTime = getTimestamp();
int threadCount;
sharedHashtable.entriesUsed = 0;
startTimerThread(task);
for (threadCount = 0; threadCount < numThreads; threadCount++)
{
Variation *currentVariation = &variations[threadCount];
currentVariation->terminate = TRUE;
#ifdef DEBUG_COORDINATION
logDebug("Schedule task: Search abort signaled.\n");
#endif
currentTask = task;
*currentVariation = *(currentTask->variation);
currentVariation->searchStatus = SEARCH_STATUS_TERMINATE;
currentVariation->bestBaseMove = NO_MOVE;
currentVariation->terminate = FALSE;
currentVariation->pawnHashtable = &(pawnHashtable[threadCount][0]);
currentVariation->kingsafetyHashtable =
&(kingSafetyHashtable[threadCount][0]);
currentVariation->threadNumber = threadCount;
currentVariation->startTime = startTime;
if (pthread_create(&searchThread[threadCount], NULL,
&executeSearch, currentVariation) == 0)
{
#ifdef DEBUG_COORDINATION
logDebug("Search thread %d started.\n", threadCount);
#endif
}
else
{
logDebug("### Search thread could not be started. ###\n");
exit(EXIT_FAILURE);
}
}
}
void waitForSearchTermination(void)
{
int threadCount;
bool finished;
int count = 0;
do
{
finished = TRUE;
if (count > 1000)
{
logDebug("waiting for search termination.\n");
count = 0;
}
for (threadCount = 0; threadCount < numThreads; threadCount++)
{
Variation *currentVariation = &variations[threadCount];
if (currentVariation->searchStatus != SEARCH_STATUS_FINISHED)
{
finished = FALSE;
if (searchThread[threadCount] != 0)
{
pthread_join(searchThread[threadCount], 0);
}
break;
}
else
{
searchThread[threadCount] = 0;
}
#ifdef DEBUG_COORDINATION
logDebug("Task %d finished.\n", threadCount);
#endif
}
count++;
}
while (finished == FALSE);
}
void completeTask(SearchTask * task)
{
scheduleTask(task);
#ifdef DEBUG_COORDINATION
logDebug("Task scheduled. Waiting for completion.\n");
#endif
waitForSearchTermination();
}
void prepareSearchAbort(void)
{
int threadCount;
const bool terminateImmediately =
FALSE == movesAreEqual(variations[0].bestBaseMove, NO_MOVE);
for (threadCount = 0; threadCount < numThreads; threadCount++)
{
variations[threadCount].terminate = TRUE;
if (terminateImmediately &&
variations[threadCount].searchStatus != SEARCH_STATUS_FINISHED)
{
variations[threadCount].searchStatus = SEARCH_STATUS_TERMINATE;
#ifdef DEBUG_COORDINATION
logDebug("terminating immediately\n");
#endif
}
}
}
void unsetPonderMode(void)
{
int threadCount;
for (threadCount = 0; threadCount < numThreads; threadCount++)
{
Variation *currentVariation = &variations[threadCount];
currentVariation->ponderMode = FALSE;
}
}
void setTimeLimit(unsigned long timeTarget, unsigned long timeLimit)
{
int threadCount;
for (threadCount = 0; threadCount < numThreads; threadCount++)
{
Variation *currentVariation = &variations[threadCount];
currentVariation->timeTarget = timeTarget;
currentVariation->timeLimit = timeLimit;
}
}
void setHashtableSizeInMb(unsigned int size)
{
UINT64 tablesize = 1024 * 1024 * (UINT64) size;
setHashtableSize(&sharedHashtable, tablesize);
resetHashtable(&sharedHashtable);
}
int initializeModuleCoordination()
{
int threadCount;
initializeHashtable(&sharedHashtable);
setHashtableSize(&sharedHashtable, 16 * 1024 * 1024);
resetHashtable(&sharedHashtable);
for (threadCount = 0; threadCount < MAX_THREADS; threadCount++)
{
Variation *currentVariation = &variations[threadCount];
currentVariation->searchStatus = SEARCH_STATUS_FINISHED;
}
return 0;
}
int testModuleCoordination()
{
return 0;
}