summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
dafd5b5)
This greatly removes clutter from the difficult idle_loop() function
No functional change.
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
-// ThreadsManager::idle_loop() is where the threads are parked when they have no work
-// to do. The parameter 'sp', if non-NULL, is a pointer to an active SplitPoint
-// object for which the current thread is the master.
+// Little helper used by idle_loop() to check that all the slaves of a
+// master thread have finished searching.
-void ThreadsManager::idle_loop(int threadID, SplitPoint* sp) {
+static bool all_slaves_finished(SplitPoint* sp) {
- assert(threadID >= 0 && threadID < MAX_THREADS);
+ assert(sp);
+
+ for (int i = 0; i < Threads.size(); i++)
+ if (sp->is_slave[i])
+ return false;
+
+ return true;
+}
- int i;
- bool allFinished;
+
+// Thread::idle_loop() is where the thread is parked when it has no work to do.
+// The parameter 'sp', if non-NULL, is a pointer to an active SplitPoint object
+// for which the thread is the master.
+
+void Thread::idle_loop(SplitPoint* sp) {
- // Slave threads can exit as soon as allThreadsShouldExit flag raises,
- // master should exit as last one.
- if (allThreadsShouldExit)
- {
- assert(!sp);
- threads[threadID].state = Thread::TERMINATED;
- return;
- }
-
// If we are not searching, wait for a condition to be signaled
// instead of wasting CPU time polling for work.
// If we are not searching, wait for a condition to be signaled
// instead of wasting CPU time polling for work.
- while ( threadID >= activeThreads
- || threads[threadID].state == Thread::INITIALIZING
- || (useSleepingThreads && threads[threadID].state == Thread::AVAILABLE))
+ while ( do_sleep
+ || do_terminate
+ || (Threads.use_sleeping_threads() && state == Thread::AVAILABLE))
- assert(!sp || useSleepingThreads);
- assert(threadID != 0 || useSleepingThreads);
+ assert(!sp || Threads.use_sleeping_threads());
+ assert(threadID != 0 || Threads.use_sleeping_threads());
- if (threads[threadID].state == Thread::INITIALIZING)
- threads[threadID].state = Thread::AVAILABLE;
+ // Slave thread should exit as soon as do_terminate flag raises
+ if (do_terminate)
+ {
+ assert(!sp);
+ state = Thread::TERMINATED;
+ return;
+ }
+
+ if (state == Thread::INITIALIZING)
+ state = Thread::AVAILABLE;
// Grab the lock to avoid races with Thread::wake_up()
// Grab the lock to avoid races with Thread::wake_up()
- lock_grab(&threads[threadID].sleepLock);
// If we are master and all slaves have finished don't go to sleep
// If we are master and all slaves have finished don't go to sleep
- for (i = 0; sp && i < activeThreads && !sp->is_slave[i]; i++) {}
- allFinished = (i == activeThreads);
-
- if (allFinished || allThreadsShouldExit)
+ if (sp && all_slaves_finished(sp))
- lock_release(&threads[threadID].sleepLock);
+ lock_release(&sleepLock);
// particular we need to avoid a deadlock in case a master thread has,
// in the meanwhile, allocated us and sent the wake_up() call before we
// had the chance to grab the lock.
// particular we need to avoid a deadlock in case a master thread has,
// in the meanwhile, allocated us and sent the wake_up() call before we
// had the chance to grab the lock.
- if (threadID >= activeThreads || threads[threadID].state == Thread::AVAILABLE)
- cond_wait(&threads[threadID].sleepCond, &threads[threadID].sleepLock);
+ if (do_sleep || state == Thread::AVAILABLE)
+ cond_wait(&sleepCond, &sleepLock);
- lock_release(&threads[threadID].sleepLock);
+ lock_release(&sleepLock);
}
// If this thread has been assigned work, launch a search
}
// If this thread has been assigned work, launch a search
- if (threads[threadID].state == Thread::WORKISWAITING)
+ if (state == Thread::WORKISWAITING)
- assert(!allThreadsShouldExit);
- threads[threadID].state = Thread::SEARCHING;
+ state = Thread::SEARCHING;
// Copy split point position and search stack and call search()
SearchStack ss[PLY_MAX_PLUS_2];
// Copy split point position and search stack and call search()
SearchStack ss[PLY_MAX_PLUS_2];
- SplitPoint* tsp = threads[threadID].splitPoint;
+ SplitPoint* tsp = splitPoint;
Position pos(*tsp->pos, threadID);
memcpy(ss, tsp->ss - 1, 4 * sizeof(SearchStack));
Position pos(*tsp->pos, threadID);
memcpy(ss, tsp->ss - 1, 4 * sizeof(SearchStack));
- assert(threads[threadID].state == Thread::SEARCHING);
+ assert(state == Thread::SEARCHING);
- threads[threadID].state = Thread::AVAILABLE;
+ state = Thread::AVAILABLE;
// Wake up master thread so to allow it to return from the idle loop in
// case we are the last slave of the split point.
// Wake up master thread so to allow it to return from the idle loop in
// case we are the last slave of the split point.
- if ( useSleepingThreads
+ if ( Threads.use_sleeping_threads()
&& threadID != tsp->master
&& threadID != tsp->master
- && threads[tsp->master].state == Thread::AVAILABLE)
- threads[tsp->master].wake_up();
+ && Threads[tsp->master].state == Thread::AVAILABLE)
+ Threads[tsp->master].wake_up();
}
// If this thread is the master of a split point and all slaves have
// finished their work at this split point, return from the idle loop.
}
// If this thread is the master of a split point and all slaves have
// finished their work at this split point, return from the idle loop.
- for (i = 0; sp && i < activeThreads && !sp->is_slave[i]; i++) {}
- allFinished = (i == activeThreads);
-
- if (allFinished)
+ if (sp && all_slaves_finished(sp))
{
// Because sp->is_slave[] is reset under lock protection,
// be sure sp->lock has been released before to return.
{
// Because sp->is_slave[] is reset under lock protection,
// be sure sp->lock has been released before to return.
DWORD WINAPI start_routine(LPVOID threadID) {
DWORD WINAPI start_routine(LPVOID threadID) {
- Threads.idle_loop(*(int*)threadID, NULL);
+ Threads[*(int*)threadID].idle_loop(NULL);
void* start_routine(void* threadID) {
void* start_routine(void* threadID) {
- Threads.idle_loop(*(int*)threadID, NULL);
+ Threads[*(int*)threadID].idle_loop(NULL);
maxThreadsPerSplitPoint = Options["Maximum Number of Threads per Split Point"].value<int>();
minimumSplitDepth = Options["Minimum Split Depth"].value<int>() * ONE_PLY;
useSleepingThreads = Options["Use Sleeping Threads"].value<bool>();
maxThreadsPerSplitPoint = Options["Maximum Number of Threads per Split Point"].value<int>();
minimumSplitDepth = Options["Minimum Split Depth"].value<int>() * ONE_PLY;
useSleepingThreads = Options["Use Sleeping Threads"].value<bool>();
- activeThreads = Options["Threads"].value<int>();
+
+ set_size(Options["Threads"].value<int>());
+}
+
+
+// set_size() changes the number of active threads and raises do_sleep flag for
+// all the unused threads that will go immediately to sleep.
+
+void ThreadsManager::set_size(int cnt) {
+
+ assert(cnt > 0 && cnt <= MAX_THREADS);
+
+ activeThreads = cnt;
+
+ for (int i = 0; i < MAX_THREADS; i++)
+ threads[i].do_sleep = !(i < activeThreads);
void ThreadsManager::init() {
void ThreadsManager::init() {
- int threadID[MAX_THREADS];
-
- // This flag is needed to properly end the threads when program exits
- allThreadsShouldExit = false;
-
// Threads will sent to sleep as soon as created, only main thread is kept alive
// Threads will sent to sleep as soon as created, only main thread is kept alive
threads[0].state = Thread::SEARCHING;
threads[0].state = Thread::SEARCHING;
+ threads[0].threadID = 0;
// Allocate pawn and material hash tables for main thread
init_hash_tables();
// Allocate pawn and material hash tables for main thread
init_hash_tables();
for (int i = 1; i < MAX_THREADS; i++)
{
threads[i].state = Thread::INITIALIZING;
for (int i = 1; i < MAX_THREADS; i++)
{
threads[i].state = Thread::INITIALIZING;
+ threads[i].threadID = i;
- bool ok = (CreateThread(NULL, 0, start_routine, (LPVOID)&threadID[i], 0, NULL) != NULL);
+ bool ok = (CreateThread(NULL, 0, start_routine, (LPVOID)&threads[i].threadID , 0, NULL) != NULL);
#else
pthread_t pthreadID;
#else
pthread_t pthreadID;
- bool ok = (pthread_create(&pthreadID, NULL, start_routine, (void*)&threadID[i]) == 0);
+ bool ok = (pthread_create(&pthreadID, NULL, start_routine, (void*)&threads[i].threadID) == 0);
pthread_detach(pthreadID);
#endif
if (!ok)
pthread_detach(pthreadID);
#endif
if (!ok)
void ThreadsManager::exit() {
void ThreadsManager::exit() {
- // Force the woken up threads to exit idle_loop() and hence terminate
- allThreadsShouldExit = true;
-
for (int i = 0; i < MAX_THREADS; i++)
{
for (int i = 0; i < MAX_THREADS; i++)
{
- // Wake up all the threads and wait for termination
+ // Wake up all the slave threads and wait for termination
+ threads[i].do_terminate = true;
threads[i].wake_up();
while (threads[i].state != Thread::TERMINATED) {}
}
threads[i].wake_up();
while (threads[i].state != Thread::TERMINATED) {}
}
// Thread::WORKISWAITING. We send the split point as a second parameter to
// the 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.
// Thread::WORKISWAITING. We send the split point as a second parameter to
// the 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.
+ masterThread.idle_loop(sp);
// In helpful master concept a master can help only a sub-tree, and
// because here is all finished is not possible master is booked.
// In helpful master concept a master can help only a sub-tree, and
// because here is all finished is not possible master is booked.
void wake_up();
bool cutoff_occurred() const;
bool is_available_to(int master) const;
void wake_up();
bool cutoff_occurred() const;
bool is_available_to(int master) const;
+ void idle_loop(SplitPoint* sp);
SplitPoint splitPoints[MAX_ACTIVE_SPLIT_POINTS];
MaterialInfoTable materialTable;
PawnInfoTable pawnTable;
SplitPoint splitPoints[MAX_ACTIVE_SPLIT_POINTS];
MaterialInfoTable materialTable;
PawnInfoTable pawnTable;
int maxPly;
Lock sleepLock;
WaitCondition sleepCond;
volatile ThreadState state;
SplitPoint* volatile splitPoint;
volatile int activeSplitPoints;
int maxPly;
Lock sleepLock;
WaitCondition sleepCond;
volatile ThreadState state;
SplitPoint* volatile splitPoint;
volatile int activeSplitPoints;
+ volatile bool do_sleep;
+ volatile bool do_terminate;
void exit();
void init_hash_tables();
void exit();
void init_hash_tables();
+ bool use_sleeping_threads() const { return useSleepingThreads; }
int min_split_depth() const { return minimumSplitDepth; }
int size() const { return activeThreads; }
int min_split_depth() const { return minimumSplitDepth; }
int size() const { return activeThreads; }
- void set_size(int cnt) { activeThreads = cnt; }
+ void set_size(int cnt);
void read_uci_options();
bool available_slave_exists(int master) const;
void read_uci_options();
bool available_slave_exists(int master) const;
- void idle_loop(int threadID, SplitPoint* sp);
template <bool Fake>
Value split(Position& pos, SearchStack* ss, Value alpha, Value beta, Value bestValue,
template <bool Fake>
Value split(Position& pos, SearchStack* ss, Value alpha, Value beta, Value bestValue,
int maxThreadsPerSplitPoint;
int activeThreads;
bool useSleepingThreads;
int maxThreadsPerSplitPoint;
int activeThreads;
bool useSleepingThreads;
- volatile bool allThreadsShouldExit;
};
extern ThreadsManager Threads;
};
extern ThreadsManager Threads;