1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-05-02 09:39:36 +00:00

Space inflate bottom part of search.cpp

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
Marco Costalba 2010-01-03 21:30:46 +01:00
parent 9e6d38d224
commit 0e15b0f1d3

View file

@ -592,7 +592,7 @@ void init_threads() {
} }
// Launch the helper threads // Launch the helper threads
for(i = 1; i < THREAD_MAX; i++) for (i = 1; i < THREAD_MAX; i++)
{ {
#if !defined(_MSC_VER) #if !defined(_MSC_VER)
pthread_create(pthread, NULL, init_thread, (void*)(&i)); pthread_create(pthread, NULL, init_thread, (void*)(&i));
@ -619,7 +619,7 @@ void stop_threads() {
for (int i = 1; i < THREAD_MAX; i++) for (int i = 1; i < THREAD_MAX; i++)
{ {
Threads[i].stop = true; Threads[i].stop = true;
while(Threads[i].running); while (Threads[i].running);
} }
destroy_split_point_stack(); destroy_split_point_stack();
} }
@ -2167,7 +2167,7 @@ namespace {
void RootMoveList::set_move_pv(int moveNum, const Move pv[]) { void RootMoveList::set_move_pv(int moveNum, const Move pv[]) {
int j; int j;
for(j = 0; pv[j] != MOVE_NONE; j++) for (j = 0; pv[j] != MOVE_NONE; j++)
moves[moveNum].pv[j] = pv[j]; moves[moveNum].pv[j] = pv[j];
moves[moveNum].pv[j] = MOVE_NONE; moves[moveNum].pv[j] = MOVE_NONE;
} }
@ -2250,7 +2250,7 @@ namespace {
ss[ply].pv[ply] = ss[ply].currentMove; ss[ply].pv[ply] = ss[ply].currentMove;
int p; int p;
for(p = ply + 1; ss[ply+1].pv[p] != MOVE_NONE; p++) for (p = ply + 1; ss[ply+1].pv[p] != MOVE_NONE; p++)
ss[ply].pv[p] = ss[ply+1].pv[p]; ss[ply].pv[p] = ss[ply+1].pv[p];
ss[ply].pv[p] = MOVE_NONE; ss[ply].pv[p] = MOVE_NONE;
} }
@ -2265,7 +2265,7 @@ namespace {
ss[ply].pv[ply] = pss[ply].pv[ply] = ss[ply].currentMove; ss[ply].pv[ply] = pss[ply].pv[ply] = ss[ply].currentMove;
int p; int p;
for(p = ply + 1; ss[ply+1].pv[p] != MOVE_NONE; p++) for (p = ply + 1; ss[ply+1].pv[p] != MOVE_NONE; p++)
ss[ply].pv[p] = pss[ply].pv[p] = ss[ply+1].pv[p]; ss[ply].pv[p] = pss[ply].pv[p] = ss[ply+1].pv[p];
ss[ply].pv[p] = pss[ply].pv[p] = MOVE_NONE; ss[ply].pv[p] = pss[ply].pv[p] = MOVE_NONE;
} }
@ -2550,7 +2550,7 @@ namespace {
bool fail_high_ply_1() { bool fail_high_ply_1() {
for(int i = 0; i < ActiveThreads; i++) for (int i = 0; i < ActiveThreads; i++)
if (Threads[i].failHighPly1) if (Threads[i].failHighPly1)
return true; return true;
@ -2562,6 +2562,7 @@ namespace {
// since the beginning of the current search. // since the beginning of the current search.
int current_search_time() { int current_search_time() {
return get_system_time() - SearchStartTime; return get_system_time() - SearchStartTime;
} }
@ -2569,8 +2570,9 @@ namespace {
// nps() computes the current nodes/second count. // nps() computes the current nodes/second count.
int nps() { int nps() {
int t = current_search_time(); int t = current_search_time();
return (t > 0)? int((nodes_searched() * 1000) / t) : 0; return (t > 0 ? int((nodes_searched() * 1000) / t) : 0);
} }
@ -2588,6 +2590,7 @@ namespace {
{ {
// We are line oriented, don't read single chars // We are line oriented, don't read single chars
std::string command; std::string command;
if (!std::getline(std::cin, command)) if (!std::getline(std::cin, command))
command = "quit"; command = "quit";
@ -2606,6 +2609,7 @@ namespace {
else if (command == "ponderhit") else if (command == "ponderhit")
ponderhit(); ponderhit();
} }
// Print search information // Print search information
if (t < 1000) if (t < 1000)
lastInfoTime = 0; lastInfoTime = 0;
@ -2619,6 +2623,7 @@ namespace {
{ {
lastInfoTime = t; lastInfoTime = t;
lock_grab(&IOLock); lock_grab(&IOLock);
if (dbg_show_mean) if (dbg_show_mean)
dbg_print_mean(); dbg_print_mean();
@ -2627,20 +2632,32 @@ namespace {
cout << "info nodes " << nodes_searched() << " nps " << nps() cout << "info nodes " << nodes_searched() << " nps " << nps()
<< " time " << t << " hashfull " << TT.full() << endl; << " time " << t << " hashfull " << TT.full() << endl;
lock_release(&IOLock); lock_release(&IOLock);
if (ShowCurrentLine) if (ShowCurrentLine)
Threads[0].printCurrentLine = true; Threads[0].printCurrentLine = true;
} }
// Should we stop the search? // Should we stop the search?
if (PonderSearch) if (PonderSearch)
return; return;
bool overTime = t > AbsoluteMaxSearchTime bool stillAtFirstMove = RootMoveNumber == 1
|| (RootMoveNumber == 1 && t > MaxSearchTime + ExtraSearchTime && !FailLow) //FIXME: We are not checking any problem flags, BUG? && !FailLow
|| ( !FailHigh && !FailLow && !fail_high_ply_1() && !Problem && t > MaxSearchTime + ExtraSearchTime;
&& t > 6*(MaxSearchTime + ExtraSearchTime));
if ( (Iteration >= 3 && (!InfiniteSearch && overTime)) bool noProblemFound = !FailHigh
&& !FailLow
&& !fail_high_ply_1()
&& !Problem
&& t > 6 * (MaxSearchTime + ExtraSearchTime);
bool noMoreTime = t > AbsoluteMaxSearchTime
|| stillAtFirstMove //FIXME: We are not checking any problem flags, BUG?
|| noProblemFound;
if ( (Iteration >= 3 && !InfiniteSearch && noMoreTime)
|| (ExactMaxTime && t >= ExactMaxTime) || (ExactMaxTime && t >= ExactMaxTime)
|| (Iteration >= 3 && MaxNodes && nodes_searched() >= MaxNodes)) || (Iteration >= 3 && MaxNodes && nodes_searched() >= MaxNodes))
AbortSearch = true; AbortSearch = true;
@ -2655,13 +2672,22 @@ namespace {
int t = current_search_time(); int t = current_search_time();
PonderSearch = false; PonderSearch = false;
if (Iteration >= 3 &&
(!InfiniteSearch && (StopOnPonderhit || bool stillAtFirstMove = RootMoveNumber == 1
t > AbsoluteMaxSearchTime || && !FailLow
(RootMoveNumber == 1 && && t > MaxSearchTime + ExtraSearchTime;
t > MaxSearchTime + ExtraSearchTime && !FailLow) ||
(!FailHigh && !FailLow && !fail_high_ply_1() && !Problem && bool noProblemFound = !FailHigh
t > 6*(MaxSearchTime + ExtraSearchTime))))) && !FailLow
&& !fail_high_ply_1()
&& !Problem
&& t > 6 * (MaxSearchTime + ExtraSearchTime);
bool noMoreTime = t > AbsoluteMaxSearchTime
|| stillAtFirstMove
|| noProblemFound;
if (Iteration >= 3 && !InfiniteSearch && (noMoreTime || StopOnPonderhit))
AbortSearch = true; AbortSearch = true;
} }
@ -2734,21 +2760,26 @@ namespace {
// object for which the current thread is the master. // object for which the current thread is the master.
void idle_loop(int threadID, SplitPoint* waitSp) { void idle_loop(int threadID, SplitPoint* waitSp) {
assert(threadID >= 0 && threadID < THREAD_MAX); assert(threadID >= 0 && threadID < THREAD_MAX);
Threads[threadID].running = true; Threads[threadID].running = true;
while(true) { while (true)
if(AllThreadsShouldExit && threadID != 0) {
if (AllThreadsShouldExit && threadID != 0)
break; break;
// If we are not thinking, wait for a condition to be signaled instead // If we are not thinking, wait for a condition to be signaled instead
// of wasting CPU time polling for work: // of wasting CPU time polling for work.
while(threadID != 0 && (Idle || threadID >= ActiveThreads)) { while (threadID != 0 && (Idle || threadID >= ActiveThreads))
{
#if !defined(_MSC_VER) #if !defined(_MSC_VER)
pthread_mutex_lock(&WaitLock); pthread_mutex_lock(&WaitLock);
if(Idle || threadID >= ActiveThreads) if (Idle || threadID >= ActiveThreads)
pthread_cond_wait(&WaitCond, &WaitLock); pthread_cond_wait(&WaitCond, &WaitLock);
pthread_mutex_unlock(&WaitLock); pthread_mutex_unlock(&WaitLock);
#else #else
WaitForSingleObject(SitIdleEvent[threadID], INFINITE); WaitForSingleObject(SitIdleEvent[threadID], INFINITE);
@ -2756,18 +2787,20 @@ namespace {
} }
// If this thread has been assigned work, launch a search // If this thread has been assigned work, launch a search
if(Threads[threadID].workIsWaiting) { if (Threads[threadID].workIsWaiting)
{
Threads[threadID].workIsWaiting = false; Threads[threadID].workIsWaiting = false;
if(Threads[threadID].splitPoint->pvNode) if (Threads[threadID].splitPoint->pvNode)
sp_search_pv(Threads[threadID].splitPoint, threadID); sp_search_pv(Threads[threadID].splitPoint, threadID);
else else
sp_search(Threads[threadID].splitPoint, threadID); sp_search(Threads[threadID].splitPoint, threadID);
Threads[threadID].idle = true; Threads[threadID].idle = true;
} }
// If this thread is the master of a split point and all threads have // If this thread is the master of a split point and all threads have
// finished their work at this split point, return from the idle loop. // finished their work at this split point, return from the idle loop.
if(waitSp != NULL && waitSp->cpus == 0) if (waitSp != NULL && waitSp->cpus == 0)
return; return;
} }
@ -2779,8 +2812,10 @@ namespace {
// initializes all split point objects. // initializes all split point objects.
void init_split_point_stack() { void init_split_point_stack() {
for(int i = 0; i < THREAD_MAX; i++)
for(int j = 0; j < ACTIVE_SPLIT_POINTS_MAX; j++) { for (int i = 0; i < THREAD_MAX; i++)
for (int j = 0; j < ACTIVE_SPLIT_POINTS_MAX; j++)
{
SplitPointStack[i][j].parent = NULL; SplitPointStack[i][j].parent = NULL;
lock_init(&(SplitPointStack[i][j].lock), NULL); lock_init(&(SplitPointStack[i][j].lock), NULL);
} }
@ -2791,28 +2826,31 @@ namespace {
// destroys all locks in the precomputed split point objects. // destroys all locks in the precomputed split point objects.
void destroy_split_point_stack() { void destroy_split_point_stack() {
for(int i = 0; i < THREAD_MAX; i++)
for(int j = 0; j < ACTIVE_SPLIT_POINTS_MAX; j++) for (int i = 0; i < THREAD_MAX; i++)
for (int j = 0; j < ACTIVE_SPLIT_POINTS_MAX; j++)
lock_destroy(&(SplitPointStack[i][j].lock)); lock_destroy(&(SplitPointStack[i][j].lock));
} }
// thread_should_stop() checks whether the thread with a given threadID has // thread_should_stop() checks whether the thread with a given threadID has
// been asked to stop, directly or indirectly. This can happen if a beta // been asked to stop, directly or indirectly. This can happen if a beta
// cutoff has occured in thre thread's currently active split point, or in // cutoff has occured in the thread's currently active split point, or in
// some ancestor of the current split point. // some ancestor of the current split point.
bool thread_should_stop(int threadID) { bool thread_should_stop(int threadID) {
assert(threadID >= 0 && threadID < ActiveThreads); assert(threadID >= 0 && threadID < ActiveThreads);
SplitPoint* sp; SplitPoint* sp;
if(Threads[threadID].stop) if (Threads[threadID].stop)
return true; return true;
if(ActiveThreads <= 2) if (ActiveThreads <= 2)
return false; return false;
for(sp = Threads[threadID].splitPoint; sp != NULL; sp = sp->parent) for (sp = Threads[threadID].splitPoint; sp != NULL; sp = sp->parent)
if(sp->finished) { if (sp->finished)
{
Threads[threadID].stop = true; Threads[threadID].stop = true;
return true; return true;
} }
@ -2829,23 +2867,24 @@ namespace {
// split point stack (the "helpful master concept" in YBWC terminology). // split point stack (the "helpful master concept" in YBWC terminology).
bool thread_is_available(int slave, int master) { bool thread_is_available(int slave, int master) {
assert(slave >= 0 && slave < ActiveThreads); assert(slave >= 0 && slave < ActiveThreads);
assert(master >= 0 && master < ActiveThreads); assert(master >= 0 && master < ActiveThreads);
assert(ActiveThreads > 1); assert(ActiveThreads > 1);
if(!Threads[slave].idle || slave == master) if (!Threads[slave].idle || slave == master)
return false; return false;
if(Threads[slave].activeSplitPoints == 0) if (Threads[slave].activeSplitPoints == 0)
// No active split points means that the thread is available as a slave // No active split points means that the thread is available as
// for any other thread. // a slave for any other thread.
return true; return true;
if(ActiveThreads == 2) if (ActiveThreads == 2)
return true; return true;
// Apply the "helpful master" concept if possible. // Apply the "helpful master" concept if possible.
if(SplitPointStack[slave][Threads[slave].activeSplitPoints-1].slaves[master]) if (SplitPointStack[slave][Threads[slave].activeSplitPoints - 1].slaves[master])
return true; return true;
return false; return false;
@ -2856,12 +2895,14 @@ namespace {
// a slave for the thread with threadID "master". // a slave for the thread with threadID "master".
bool idle_thread_exists(int master) { bool idle_thread_exists(int master) {
assert(master >= 0 && master < ActiveThreads); assert(master >= 0 && master < ActiveThreads);
assert(ActiveThreads > 1); assert(ActiveThreads > 1);
for(int i = 0; i < ActiveThreads; i++) for (int i = 0; i < ActiveThreads; i++)
if(thread_is_available(i, master)) if (thread_is_available(i, master))
return true; return true;
return false; return false;
} }
@ -2899,8 +2940,9 @@ namespace {
// If no other thread is available to help us, or if we have too many // If no other thread is available to help us, or if we have too many
// active split points, don't split. // active split points, don't split.
if(!idle_thread_exists(master) || if ( !idle_thread_exists(master)
Threads[master].activeSplitPoints >= ACTIVE_SPLIT_POINTS_MAX) { || Threads[master].activeSplitPoints >= ACTIVE_SPLIT_POINTS_MAX)
{
lock_release(&MPLock); lock_release(&MPLock);
return false; return false;
} }
@ -2909,12 +2951,12 @@ namespace {
splitPoint = SplitPointStack[master] + Threads[master].activeSplitPoints; splitPoint = SplitPointStack[master] + Threads[master].activeSplitPoints;
Threads[master].activeSplitPoints++; Threads[master].activeSplitPoints++;
// Initialize the split point object // Initialize the split point object and copy current position
splitPoint->parent = Threads[master].splitPoint; splitPoint->parent = Threads[master].splitPoint;
splitPoint->finished = false; splitPoint->finished = false;
splitPoint->ply = ply; splitPoint->ply = ply;
splitPoint->depth = depth; splitPoint->depth = depth;
splitPoint->alpha = pvNode? *alpha : (*beta - 1); splitPoint->alpha = pvNode ? *alpha : (*beta - 1);
splitPoint->beta = *beta; splitPoint->beta = *beta;
splitPoint->pvNode = pvNode; splitPoint->pvNode = pvNode;
splitPoint->bestValue = *bestValue; splitPoint->bestValue = *bestValue;
@ -2925,18 +2967,18 @@ namespace {
splitPoint->cpus = 1; splitPoint->cpus = 1;
splitPoint->pos.copy(p); splitPoint->pos.copy(p);
splitPoint->parentSstack = sstck; splitPoint->parentSstack = sstck;
for(i = 0; i < ActiveThreads; i++) for (i = 0; i < ActiveThreads; i++)
splitPoint->slaves[i] = 0; splitPoint->slaves[i] = 0;
// Copy the current position and the search stack to the master thread // Copy the current search stack to the master thread
memcpy(splitPoint->sstack[master], sstck, (ply+1)*sizeof(SearchStack)); memcpy(splitPoint->sstack[master], sstck, (ply+1) * sizeof(SearchStack));
Threads[master].splitPoint = splitPoint; Threads[master].splitPoint = splitPoint;
// Make copies of the current position and search stack for each thread // Make copies of the current position and search stack for each thread
for(i = 0; i < ActiveThreads && splitPoint->cpus < MaxThreadsPerSplitPoint; for (i = 0; i < ActiveThreads && splitPoint->cpus < MaxThreadsPerSplitPoint; i++)
i++) if (thread_is_available(i, master))
if(thread_is_available(i, master)) { {
memcpy(splitPoint->sstack[i], sstck, (ply+1)*sizeof(SearchStack)); memcpy(splitPoint->sstack[i], sstck, (ply+1) * sizeof(SearchStack));
Threads[i].splitPoint = splitPoint; Threads[i].splitPoint = splitPoint;
splitPoint->slaves[i] = 1; splitPoint->slaves[i] = 1;
splitPoint->cpus++; splitPoint->cpus++;
@ -2944,8 +2986,9 @@ namespace {
// Tell the threads that they have work to do. This will make them leave // Tell the threads that they have work to do. This will make them leave
// their idle loop. // their idle loop.
for(i = 0; i < ActiveThreads; i++) for (i = 0; i < ActiveThreads; i++)
if(i == master || splitPoint->slaves[i]) { if (i == master || splitPoint->slaves[i])
{
Threads[i].workIsWaiting = true; Threads[i].workIsWaiting = true;
Threads[i].idle = false; Threads[i].idle = false;
Threads[i].stop = false; Threads[i].stop = false;
@ -2958,21 +3001,24 @@ namespace {
// slot is 'true'. We send the split point as a second parameter to the // slot is 'true'. We send the split point as a second parameter to the
// idle loop, which means that the main thread will return from the idle // idle loop, which means that the main thread will return from the idle
// loop when all threads have finished their work at this split point // loop when all threads have finished their work at this split point
// (i.e. when // splitPoint->cpus == 0). // (i.e. when splitPoint->cpus == 0).
idle_loop(master, splitPoint); idle_loop(master, splitPoint);
// We have returned from the idle loop, which means that all threads are // We have returned from the idle loop, which means that all threads are
// finished. Update alpha, beta and bestvalue, and return. // finished. Update alpha, beta and bestValue, and return.
lock_grab(&MPLock); lock_grab(&MPLock);
if(pvNode) *alpha = splitPoint->alpha;
if (pvNode)
*alpha = splitPoint->alpha;
*beta = splitPoint->beta; *beta = splitPoint->beta;
*bestValue = splitPoint->bestValue; *bestValue = splitPoint->bestValue;
Threads[master].stop = false; Threads[master].stop = false;
Threads[master].idle = false; Threads[master].idle = false;
Threads[master].activeSplitPoints--; Threads[master].activeSplitPoints--;
Threads[master].splitPoint = splitPoint->parent; Threads[master].splitPoint = splitPoint->parent;
lock_release(&MPLock);
lock_release(&MPLock);
return true; return true;
} }
@ -2981,17 +3027,21 @@ namespace {
// to start a new search from the root. // to start a new search from the root.
void wake_sleeping_threads() { void wake_sleeping_threads() {
if(ActiveThreads > 1) {
for(int i = 1; i < ActiveThreads; i++) { if (ActiveThreads > 1)
{
for (int i = 1; i < ActiveThreads; i++)
{
Threads[i].idle = true; Threads[i].idle = true;
Threads[i].workIsWaiting = false; Threads[i].workIsWaiting = false;
} }
#if !defined(_MSC_VER) #if !defined(_MSC_VER)
pthread_mutex_lock(&WaitLock); pthread_mutex_lock(&WaitLock);
pthread_cond_broadcast(&WaitCond); pthread_cond_broadcast(&WaitCond);
pthread_mutex_unlock(&WaitLock); pthread_mutex_unlock(&WaitLock);
#else #else
for(int i = 1; i < THREAD_MAX; i++) for (int i = 1; i < THREAD_MAX; i++)
SetEvent(SitIdleEvent[i]); SetEvent(SitIdleEvent[i]);
#endif #endif
} }
@ -3000,20 +3050,22 @@ namespace {
// init_thread() is the function which is called when a new thread is // init_thread() is the function which is called when a new thread is
// launched. It simply calls the idle_loop() function with the supplied // launched. It simply calls the idle_loop() function with the supplied
// threadID. There are two versions of this function; one for POSIX threads // threadID. There are two versions of this function; one for POSIX
// and one for Windows threads. // threads and one for Windows threads.
#if !defined(_MSC_VER) #if !defined(_MSC_VER)
void *init_thread(void *threadID) { void* init_thread(void *threadID) {
idle_loop(*(int *)threadID, NULL);
idle_loop(*(int*)threadID, NULL);
return NULL; return NULL;
} }
#else #else
DWORD WINAPI init_thread(LPVOID threadID) { DWORD WINAPI init_thread(LPVOID threadID) {
idle_loop(*(int *)threadID, NULL);
idle_loop(*(int*)threadID, NULL);
return NULL; return NULL;
} }