1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-05-01 09:13:08 +00:00

Increase max threads to 128

Thanks to std::bitset we can easily increase
the limit of active threads above 64.

Thanks to Lucas Braesch for pointing at the
correct solution of using std::bitset.

No functional change.
This commit is contained in:
Marco Costalba 2014-03-18 12:07:26 +01:00
parent fa3f6dcbea
commit aab5863dd4
3 changed files with 15 additions and 17 deletions

View file

@ -1470,7 +1470,7 @@ void Thread::idle_loop() {
mutex.lock(); mutex.lock();
// If we are master and all slaves have finished then exit idle_loop // If we are master and all slaves have finished then exit idle_loop
if (this_sp && !this_sp->slavesMask) if (this_sp && this_sp->slavesMask.none())
{ {
mutex.unlock(); mutex.unlock();
break; break;
@ -1529,14 +1529,14 @@ void Thread::idle_loop() {
searching = false; searching = false;
activePosition = NULL; activePosition = NULL;
sp->slavesMask &= ~(1ULL << idx); sp->slavesMask.reset(idx);
sp->nodes += pos.nodes_searched(); sp->nodes += pos.nodes_searched();
// Wake up the master thread so to allow it to return from the idle // Wake up the master thread so to allow it to return from the idle
// loop in case we are the last slave of the split point. // loop in case we are the last slave of the split point.
if ( Threads.sleepWhileIdle if ( Threads.sleepWhileIdle
&& this != sp->masterThread && this != sp->masterThread
&& !sp->slavesMask) && sp->slavesMask.none())
{ {
assert(!sp->masterThread->searching); assert(!sp->masterThread->searching);
sp->masterThread->notify_one(); sp->masterThread->notify_one();
@ -1551,10 +1551,10 @@ void Thread::idle_loop() {
// If this thread is the master of a split point and all slaves have finished // 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. // their work at this split point, return from the idle loop.
if (this_sp && !this_sp->slavesMask) if (this_sp && this_sp->slavesMask.none())
{ {
this_sp->mutex.lock(); this_sp->mutex.lock();
bool finished = !this_sp->slavesMask; // Retest under lock protection bool finished = this_sp->slavesMask.none(); // Retest under lock protection
this_sp->mutex.unlock(); this_sp->mutex.unlock();
if (finished) if (finished)
return; return;
@ -1597,13 +1597,10 @@ void check_time() {
sp.mutex.lock(); sp.mutex.lock();
nodes += sp.nodes; nodes += sp.nodes;
Bitboard sm = sp.slavesMask;
while (sm) for (size_t idx = 0; idx < Threads.size(); ++idx)
{ if (sp.slavesMask.test(idx) && Threads[idx]->activePosition)
Position* pos = Threads[pop_lsb(&sm)]->activePosition; nodes += Threads[idx]->activePosition->nodes_searched();
if (pos)
nodes += pos->nodes_searched();
}
sp.mutex.unlock(); sp.mutex.unlock();
} }

View file

@ -123,7 +123,7 @@ bool Thread::available_to(const Thread* master) const {
// No split points means that the thread is available as a slave for any // No split points means that the thread is available as a slave for any
// other thread otherwise apply the "helpful master" concept if possible. // other thread otherwise apply the "helpful master" concept if possible.
return !size || (splitPoints[size - 1].slavesMask & (1ULL << master->idx)); return !size || splitPoints[size - 1].slavesMask.test(master->idx);
} }
@ -271,7 +271,7 @@ void Thread::split(Position& pos, const Stack* ss, Value alpha, Value beta, Valu
sp.masterThread = this; sp.masterThread = this;
sp.parentSplitPoint = activeSplitPoint; sp.parentSplitPoint = activeSplitPoint;
sp.slavesMask = 1ULL << idx; sp.slavesMask = 0, sp.slavesMask.set(idx);
sp.depth = depth; sp.depth = depth;
sp.bestValue = *bestValue; sp.bestValue = *bestValue;
sp.bestMove = *bestMove; sp.bestMove = *bestMove;
@ -299,7 +299,7 @@ void Thread::split(Position& pos, const Stack* ss, Value alpha, Value beta, Valu
if (!Fake) if (!Fake)
for (Thread* slave; (slave = Threads.available_slave(this)) != NULL; ) for (Thread* slave; (slave = Threads.available_slave(this)) != NULL; )
{ {
sp.slavesMask |= 1ULL << slave->idx; sp.slavesMask.set(slave->idx);
slave->activeSplitPoint = &sp; slave->activeSplitPoint = &sp;
slave->searching = true; // Slave leaves idle_loop() slave->searching = true; // Slave leaves idle_loop()
slave->notify_one(); // Could be sleeping slave->notify_one(); // Could be sleeping

View file

@ -20,6 +20,7 @@
#ifndef THREAD_H_INCLUDED #ifndef THREAD_H_INCLUDED
#define THREAD_H_INCLUDED #define THREAD_H_INCLUDED
#include <bitset>
#include <vector> #include <vector>
#include "material.h" #include "material.h"
@ -28,7 +29,7 @@
#include "position.h" #include "position.h"
#include "search.h" #include "search.h"
const int MAX_THREADS = 64; // Because SplitPoint::slavesMask is a uint64_t const int MAX_THREADS = 128;
const int MAX_SPLITPOINTS_PER_THREAD = 8; const int MAX_SPLITPOINTS_PER_THREAD = 8;
struct Mutex { struct Mutex {
@ -75,7 +76,7 @@ struct SplitPoint {
// Shared data // Shared data
Mutex mutex; Mutex mutex;
volatile uint64_t slavesMask; std::bitset<MAX_THREADS> slavesMask;
volatile uint64_t nodes; volatile uint64_t nodes;
volatile Value alpha; volatile Value alpha;
volatile Value bestValue; volatile Value bestValue;