1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-05-06 03:19:36 +00:00

[cluster] Some formatting cleanup

standarize whitespace a bit.
Also adds two TODOs for follow up work.

No functional change.
This commit is contained in:
Joost VandeVondele 2018-12-13 23:02:59 +01:00 committed by Stéphane Nicolet
parent 1cd2c7861a
commit 54a0a228f6
5 changed files with 49 additions and 24 deletions

View file

@ -357,15 +357,11 @@ ifeq ($(OS), Android)
endif endif
### 3.10 MPI ### 3.10 MPI
ifeq ($(CXX),$(filter $(CXX),mpicxx mpic++ mpiCC mpicxx.mpich)) ifneq (,$(findstring mpi, $(CXX)))
mpi = yes mpi = yes
endif
ifeq ($(mpi),yes)
CXXFLAGS += -DUSE_MPI -Wno-cast-qual CXXFLAGS += -DUSE_MPI -Wno-cast-qual
endif endif
### ========================================================================== ### ==========================================================================
### Section 4. Public targets ### Section 4. Public targets
### ========================================================================== ### ==========================================================================

View file

@ -50,6 +50,7 @@ static std::vector<KeyedTTEntry> TTBuff;
static MPI_Datatype MIDatatype = MPI_DATATYPE_NULL; static MPI_Datatype MIDatatype = MPI_DATATYPE_NULL;
void init() { void init() {
int thread_support; int thread_support;
MPI_Init_thread(nullptr, nullptr, MPI_THREAD_MULTIPLE, &thread_support); MPI_Init_thread(nullptr, nullptr, MPI_THREAD_MULTIPLE, &thread_support);
if (thread_support < MPI_THREAD_MULTIPLE) if (thread_support < MPI_THREAD_MULTIPLE)
@ -78,10 +79,12 @@ void init() {
} }
void finalize() { void finalize() {
MPI_Finalize(); MPI_Finalize();
} }
bool getline(std::istream& input, std::string& str) { bool getline(std::istream& input, std::string& str) {
int size; int size;
std::vector<char> vec; std::vector<char> vec;
bool state; bool state;
@ -98,27 +101,31 @@ bool getline(std::istream& input, std::string& str) {
MPI_Ibcast(&size, 1, MPI_INT, 0, InputComm, &reqInput); MPI_Ibcast(&size, 1, MPI_INT, 0, InputComm, &reqInput);
if (is_root()) if (is_root())
MPI_Wait(&reqInput, MPI_STATUS_IGNORE); MPI_Wait(&reqInput, MPI_STATUS_IGNORE);
else { else
while (true) { {
while (true)
{
int flag; int flag;
MPI_Test(&reqInput, &flag, MPI_STATUS_IGNORE); MPI_Test(&reqInput, &flag, MPI_STATUS_IGNORE);
if (flag) if (flag)
break; break;
else { else
std::this_thread::sleep_for(std::chrono::milliseconds(10)); std::this_thread::sleep_for(std::chrono::milliseconds(10));
} }
} }
}
if (!is_root()) if (!is_root())
vec.resize(size); vec.resize(size);
MPI_Bcast(vec.data(), size, MPI_CHAR, 0, InputComm); MPI_Bcast(vec.data(), size, MPI_CHAR, 0, InputComm);
if (!is_root()) if (!is_root())
str.assign(vec.begin(), vec.end()); str.assign(vec.begin(), vec.end());
MPI_Bcast(&state, 1, MPI_CXX_BOOL, 0, InputComm); MPI_Bcast(&state, 1, MPI_CXX_BOOL, 0, InputComm);
return state; return state;
} }
void sync_start() { void sync_start() {
stop_signal = false; stop_signal = false;
// Start listening to stop signal // Start listening to stop signal
@ -127,15 +134,19 @@ void sync_start() {
} }
void sync_stop() { void sync_stop() {
if (is_root()) {
if (!stop_signal && Threads.stop) { if (is_root())
{
if (!stop_signal && Threads.stop)
{
// Signal the cluster about stopping // Signal the cluster about stopping
stop_signal = true; stop_signal = true;
MPI_Ibarrier(StopComm, &reqStop); MPI_Ibarrier(StopComm, &reqStop);
MPI_Wait(&reqStop, MPI_STATUS_IGNORE); MPI_Wait(&reqStop, MPI_STATUS_IGNORE);
} }
} }
else { else
{
int flagStop; int flagStop;
// Check if we've received any stop signal // Check if we've received any stop signal
MPI_Test(&reqStop, &flagStop, MPI_STATUS_IGNORE); MPI_Test(&reqStop, &flagStop, MPI_STATUS_IGNORE);
@ -145,10 +156,12 @@ void sync_stop() {
} }
int size() { int size() {
return world_size; return world_size;
} }
int rank() { int rank() {
return world_rank; return world_rank;
} }
@ -185,6 +198,7 @@ void save(Thread* thread, TTEntry* tte,
{ {
if (irank == size_t(rank())) if (irank == size_t(rank()))
continue; continue;
for (size_t i = irank * TTSendBufferSize ; i < (irank + 1) * TTSendBufferSize; ++i) for (size_t i = irank * TTSendBufferSize ; i < (irank + 1) * TTSendBufferSize; ++i)
{ {
auto&& e = TTBuff[i]; auto&& e = TTBuff[i];
@ -198,7 +212,8 @@ void save(Thread* thread, TTEntry* tte,
send_buff = {}; send_buff = {};
// Build up new send buffer: best 16 found across all threads // Build up new send buffer: best 16 found across all threads
for (auto&& th : Threads) { for (auto&& th : Threads)
{
std::lock_guard<Mutex> lk(th->ttBuffer.mutex); std::lock_guard<Mutex> lk(th->ttBuffer.mutex);
for (auto&& e : th->ttBuffer.buffer) for (auto&& e : th->ttBuffer.buffer)
send_buff.replace(e); send_buff.replace(e);
@ -216,24 +231,32 @@ void save(Thread* thread, TTEntry* tte,
} }
void pick_moves(MoveInfo& mi) { void pick_moves(MoveInfo& mi) {
MoveInfo* pMoveInfo = NULL; MoveInfo* pMoveInfo = NULL;
if (is_root()) { if (is_root())
{
pMoveInfo = (MoveInfo*)malloc(sizeof(MoveInfo) * size()); pMoveInfo = (MoveInfo*)malloc(sizeof(MoveInfo) * size());
} }
MPI_Gather(&mi, 1, MIDatatype, pMoveInfo, 1, MIDatatype, 0, MoveComm); MPI_Gather(&mi, 1, MIDatatype, pMoveInfo, 1, MIDatatype, 0, MoveComm);
if (is_root()) {
if (is_root())
{
std::map<int, int> votes; std::map<int, int> votes;
int minScore = pMoveInfo[0].score; int minScore = pMoveInfo[0].score;
for (int i = 0; i < size(); i++) { for (int i = 0; i < size(); ++i)
{
minScore = std::min(minScore, pMoveInfo[i].score); minScore = std::min(minScore, pMoveInfo[i].score);
votes[pMoveInfo[i].move] = 0; votes[pMoveInfo[i].move] = 0;
} }
for (int i = 0; i < size(); i++) { for (int i = 0; i < size(); ++i)
{
votes[pMoveInfo[i].move] += pMoveInfo[i].score - minScore + pMoveInfo[i].depth; votes[pMoveInfo[i].move] += pMoveInfo[i].score - minScore + pMoveInfo[i].depth;
} }
int bestVote = votes[pMoveInfo[0].move]; int bestVote = votes[pMoveInfo[0].move];
for (int i = 0; i < size(); i++) { for (int i = 0; i < size(); ++i)
if (votes[pMoveInfo[i].move] > bestVote) { {
if (votes[pMoveInfo[i].move] > bestVote)
{
bestVote = votes[pMoveInfo[i].move]; bestVote = votes[pMoveInfo[i].move];
mi = pMoveInfo[i]; mi = pMoveInfo[i];
} }

View file

@ -44,6 +44,7 @@ using KeyedTTEntry = std::pair<Key, TTEntry>;
constexpr std::size_t TTSendBufferSize = 16; constexpr std::size_t TTSendBufferSize = 16;
template <std::size_t N> class TTSendBuffer : public std::array<KeyedTTEntry, N> { template <std::size_t N> class TTSendBuffer : public std::array<KeyedTTEntry, N> {
struct Compare { struct Compare {
inline bool operator()(const KeyedTTEntry& lhs, const KeyedTTEntry& rhs) { inline bool operator()(const KeyedTTEntry& lhs, const KeyedTTEntry& rhs) {
return lhs.second.depth() > rhs.second.depth(); return lhs.second.depth() > rhs.second.depth();
@ -53,7 +54,9 @@ template <std::size_t N> class TTSendBuffer : public std::array<KeyedTTEntry, N>
public: public:
bool replace(const KeyedTTEntry& value) { bool replace(const KeyedTTEntry& value) {
if (compare(value, this->front())) {
if (compare(value, this->front()))
{
std::pop_heap(this->begin(), this->end(), compare); std::pop_heap(this->begin(), this->end(), compare);
this->back() = value; this->back() = value;
std::push_heap(this->begin(), this->end(), compare); std::push_heap(this->begin(), this->end(), compare);
@ -69,8 +72,7 @@ bool getline(std::istream& input, std::string& str);
int size(); int size();
int rank(); int rank();
inline bool is_root() { return rank() == 0; } inline bool is_root() { return rank() == 0; }
void save(Thread* thread, TTEntry* tte, void save(Thread* thread, TTEntry* tte, Key k, Value v, Bound b, Depth d, Move m, Value ev);
Key k, Value v, Bound b, Depth d, Move m, Value ev);
void pick_moves(MoveInfo& mi); void pick_moves(MoveInfo& mi);
void sync_start(); void sync_start();
void sync_stop(); void sync_stop();
@ -80,13 +82,14 @@ void sync_stop();
inline void init() { } inline void init() { }
inline void finalize() { } inline void finalize() { }
inline bool getline(std::istream& input, std::string& str) { inline bool getline(std::istream& input, std::string& str) {
return static_cast<bool>(std::getline(input, str)); return static_cast<bool>(std::getline(input, str));
} }
constexpr int size() { return 1; } constexpr int size() { return 1; }
constexpr int rank() { return 0; } constexpr int rank() { return 0; }
constexpr bool is_root() { return true; } constexpr bool is_root() { return true; }
inline void save(Thread* thread, TTEntry* tte, inline void save(Thread* thread, TTEntry* tte, Key k, Value v, Bound b, Depth d, Move m, Value ev) {
Key k, Value v, Bound b, Depth d, Move m, Value ev) {
(void)thread; (void)thread;
tte->save(k, v, b, d, m, ev); tte->save(k, v, b, d, m, ev);
} }

View file

@ -296,6 +296,7 @@ void MainThread::search() {
previousScore = static_cast<Value>(mi.score); previousScore = static_cast<Value>(mi.score);
// TODO output should be done on the cluster_root
if (mi.rank == Cluster::rank()) { if (mi.rank == Cluster::rank()) {
// Send again PV info if we have a new best thread // Send again PV info if we have a new best thread
if (!Cluster::is_root() || bestThread != this) if (!Cluster::is_root() || bestThread != this)

View file

@ -141,6 +141,8 @@ namespace {
// a list of UCI commands is setup according to bench parameters, then // a list of UCI commands is setup according to bench parameters, then
// it is run one by one printing a summary at the end. // it is run one by one printing a summary at the end.
// TODO make (output?) cluster compatible
void bench(Position& pos, istream& args, StateListPtr& states) { void bench(Position& pos, istream& args, StateListPtr& states) {
string token; string token;