]> git.sesse.net Git - stockfish/blob - src/cluster.cpp
Merge branch 'master' into clusterMergeMaster13
[stockfish] / src / cluster.cpp
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2020 The Stockfish developers (see AUTHORS file)
4
5   Stockfish is free software: you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation, either version 3 of the License, or
8   (at your option) any later version.
9
10   Stockfish is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifdef USE_MPI
20
21 #include <array>
22 #include <map>
23 #include <cstddef>
24 #include <cstdlib>
25 #include <iostream>
26 #include <istream>
27 #include <mpi.h>
28 #include <string>
29 #include <vector>
30
31 #include "cluster.h"
32 #include "thread.h"
33 #include "tt.h"
34 #include "timeman.h"
35
36 namespace Stockfish {
37 namespace Cluster {
38
39 // Total number of ranks and rank within the communicator
40 static int world_rank = MPI_PROC_NULL;
41 static int world_size = 0;
42
43 // Signals between ranks exchange basic info using a dedicated communicator
44 static MPI_Comm signalsComm = MPI_COMM_NULL;
45 static MPI_Request reqSignals = MPI_REQUEST_NULL;
46 static uint64_t signalsCallCounter = 0;
47
48 // Signals are the number of nodes searched, stop, table base hits, transposition table saves
49 enum Signals : int { SIG_NODES = 0, SIG_STOP = 1, SIG_TB = 2, SIG_TTS = 3, SIG_NB = 4};
50 static uint64_t signalsSend[SIG_NB] = {};
51 static uint64_t signalsRecv[SIG_NB] = {};
52 static uint64_t nodesSearchedOthers = 0;
53 static uint64_t tbHitsOthers = 0;
54 static uint64_t TTsavesOthers = 0;
55 static uint64_t stopSignalsPosted = 0;
56
57 // The UCI threads of each rank exchange use a dedicated communicator
58 static MPI_Comm InputComm = MPI_COMM_NULL;
59
60 // bestMove requires MoveInfo communicators and data types
61 static MPI_Comm MoveComm = MPI_COMM_NULL;
62 static MPI_Datatype MIDatatype = MPI_DATATYPE_NULL;
63
64 // TT entries are communicated with a dedicated communicator.
65 // The receive buffer is used to gather information from all ranks.
66 // THe TTCacheCounter tracks the number of local elements that are ready to be sent.
67 static MPI_Comm TTComm = MPI_COMM_NULL;
68 static std::array<std::vector<KeyedTTEntry>, 2> TTSendRecvBuffs;
69 static std::array<MPI_Request, 2> reqsTTSendRecv = {MPI_REQUEST_NULL, MPI_REQUEST_NULL};
70 static uint64_t sendRecvPosted = 0;
71 static std::atomic<uint64_t> TTCacheCounter = {};
72
73 /// Initialize MPI and associated data types. Note that the MPI library must be configured
74 /// to support MPI_THREAD_MULTIPLE, since multiple threads access MPI simultaneously.
75 void init() {
76
77   int thread_support;
78   MPI_Init_thread(nullptr, nullptr, MPI_THREAD_MULTIPLE, &thread_support);
79   if (thread_support < MPI_THREAD_MULTIPLE)
80   {
81       std::cerr << "Stockfish requires support for MPI_THREAD_MULTIPLE."
82                 << std::endl;
83       std::exit(EXIT_FAILURE);
84   }
85
86   MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
87   MPI_Comm_size(MPI_COMM_WORLD, &world_size);
88
89   const std::array<MPI_Aint, 5> MIdisps = {offsetof(MoveInfo, move),
90                                            offsetof(MoveInfo, ponder),
91                                            offsetof(MoveInfo, depth),
92                                            offsetof(MoveInfo, score),
93                                            offsetof(MoveInfo, rank)};
94   MPI_Type_create_hindexed_block(5, 1, MIdisps.data(), MPI_INT, &MIDatatype);
95   MPI_Type_commit(&MIDatatype);
96
97   MPI_Comm_dup(MPI_COMM_WORLD, &InputComm);
98   MPI_Comm_dup(MPI_COMM_WORLD, &TTComm);
99   MPI_Comm_dup(MPI_COMM_WORLD, &MoveComm);
100   MPI_Comm_dup(MPI_COMM_WORLD, &signalsComm);
101 }
102
103 /// Finalize MPI and free the associated data types.
104 void finalize() {
105
106   MPI_Type_free(&MIDatatype);
107
108   MPI_Comm_free(&InputComm);
109   MPI_Comm_free(&TTComm);
110   MPI_Comm_free(&MoveComm);
111   MPI_Comm_free(&signalsComm);
112
113   MPI_Finalize();
114 }
115
116 /// Return the total number of ranks
117 int size() {
118
119   return world_size;
120 }
121
122 /// Return the rank (index) of the process
123 int rank() {
124
125   return world_rank;
126 }
127
128 /// The receive buffer depends on the number of MPI ranks and threads, resize as needed
129 void ttSendRecvBuff_resize(size_t nThreads) {
130
131   for (int i : {0, 1})
132   {
133      TTSendRecvBuffs[i].resize(TTCacheSize * world_size * nThreads);
134      std::fill(TTSendRecvBuffs[i].begin(), TTSendRecvBuffs[i].end(), KeyedTTEntry());
135   }
136 }
137
138 /// As input is only received by the root (rank 0) of the cluster, this input must be relayed
139 /// to the UCI threads of all ranks, in order to setup the position, etc. We do this with a
140 /// dedicated getline implementation, where the root broadcasts to all other ranks the received
141 /// information.
142 bool getline(std::istream& input, std::string& str) {
143
144   int size;
145   std::vector<char> vec;
146   int state;
147
148   if (is_root())
149   {
150       state = static_cast<bool>(std::getline(input, str));
151       vec.assign(str.begin(), str.end());
152       size = vec.size();
153   }
154
155   // Some MPI implementations use busy-wait polling, while we need yielding as otherwise
156   // the UCI thread on the non-root ranks would be consuming resources.
157   static MPI_Request reqInput = MPI_REQUEST_NULL;
158   MPI_Ibcast(&size, 1, MPI_INT, 0, InputComm, &reqInput);
159   if (is_root())
160       MPI_Wait(&reqInput, MPI_STATUS_IGNORE);
161   else
162   {
163       while (true)
164       {
165           int flag;
166           MPI_Test(&reqInput, &flag, MPI_STATUS_IGNORE);
167           if (flag)
168               break;
169           else
170               std::this_thread::sleep_for(std::chrono::milliseconds(10));
171       }
172   }
173
174   // Broadcast received string
175   if (!is_root())
176       vec.resize(size);
177   MPI_Bcast(vec.data(), size, MPI_CHAR, 0, InputComm);
178   if (!is_root())
179       str.assign(vec.begin(), vec.end());
180   MPI_Bcast(&state, 1, MPI_INT, 0, InputComm);
181
182   return state;
183 }
184
185 /// Sending part of the signal communication loop
186 void signals_send() {
187
188   signalsSend[SIG_NODES] = Threads.nodes_searched();
189   signalsSend[SIG_TB] = Threads.tb_hits();
190   signalsSend[SIG_TTS] = Threads.TT_saves();
191   signalsSend[SIG_STOP] = Threads.stop;
192   MPI_Iallreduce(signalsSend, signalsRecv, SIG_NB, MPI_UINT64_T,
193                  MPI_SUM, signalsComm, &reqSignals);
194   ++signalsCallCounter;
195 }
196
197 /// Processing part of the signal communication loop.
198 /// For some counters (e.g. nodes) we only keep their sum on the other nodes
199 /// allowing to add local counters at any time for more fine grained process,
200 /// which is useful to indicate progress during early iterations, and to have
201 /// node counts that exactly match the non-MPI code in the single rank case.
202 /// This call also propagates the stop signal between ranks.
203 void signals_process() {
204
205   nodesSearchedOthers = signalsRecv[SIG_NODES] - signalsSend[SIG_NODES];
206   tbHitsOthers = signalsRecv[SIG_TB] - signalsSend[SIG_TB];
207   TTsavesOthers = signalsRecv[SIG_TTS] - signalsSend[SIG_TTS];
208   stopSignalsPosted = signalsRecv[SIG_STOP];
209   if (signalsRecv[SIG_STOP] > 0)
210       Threads.stop = true;
211 }
212
213 void sendrecv_post() {
214
215    ++sendRecvPosted;
216    MPI_Irecv(TTSendRecvBuffs[sendRecvPosted       % 2].data(),
217              TTSendRecvBuffs[sendRecvPosted       % 2].size() * sizeof(KeyedTTEntry), MPI_BYTE,
218              (rank() + size() - 1) % size(), 42, TTComm, &reqsTTSendRecv[0]);
219    MPI_Isend(TTSendRecvBuffs[(sendRecvPosted + 1) % 2].data(),
220              TTSendRecvBuffs[(sendRecvPosted + 1) % 2].size() * sizeof(KeyedTTEntry), MPI_BYTE,
221              (rank() + 1         ) % size(), 42, TTComm, &reqsTTSendRecv[1]);
222 }
223
224 /// During search, most message passing is asynchronous, but at the end of
225 /// search it makes sense to bring them to a common, finalized state.
226 void signals_sync() {
227
228   while(stopSignalsPosted < uint64_t(size()))
229       signals_poll();
230
231   // Finalize outstanding messages of the signal loops.
232   // We might have issued one call less than needed on some ranks.
233   uint64_t globalCounter;
234   MPI_Allreduce(&signalsCallCounter, &globalCounter, 1, MPI_UINT64_T, MPI_MAX, MoveComm);
235   if (signalsCallCounter < globalCounter)
236   {
237       MPI_Wait(&reqSignals, MPI_STATUS_IGNORE);
238       signals_send();
239   }
240   assert(signalsCallCounter == globalCounter);
241   MPI_Wait(&reqSignals, MPI_STATUS_IGNORE);
242   signals_process();
243
244   // Finalize outstanding messages in the sendRecv loop
245   MPI_Allreduce(&sendRecvPosted, &globalCounter, 1, MPI_UINT64_T, MPI_MAX, MoveComm);
246   while (sendRecvPosted < globalCounter)
247   {
248       MPI_Waitall(reqsTTSendRecv.size(), reqsTTSendRecv.data(), MPI_STATUSES_IGNORE);
249       sendrecv_post();
250   }
251   assert(sendRecvPosted == globalCounter);
252   MPI_Waitall(reqsTTSendRecv.size(), reqsTTSendRecv.data(), MPI_STATUSES_IGNORE);
253
254 }
255
256 /// Initialize signal counters to zero.
257 void signals_init() {
258
259   stopSignalsPosted = tbHitsOthers = TTsavesOthers = nodesSearchedOthers = 0;
260
261   signalsSend[SIG_NODES] = signalsRecv[SIG_NODES] = 0;
262   signalsSend[SIG_TB] = signalsRecv[SIG_TB] = 0;
263   signalsSend[SIG_TTS] = signalsRecv[SIG_TTS] = 0;
264   signalsSend[SIG_STOP] = signalsRecv[SIG_STOP] = 0;
265
266 }
267
268 /// Poll the signal loop, and start next round as needed.
269 void signals_poll() {
270
271   int flag;
272   MPI_Test(&reqSignals, &flag, MPI_STATUS_IGNORE);
273   if (flag)
274   {
275      signals_process();
276      signals_send();
277   }
278 }
279
280 /// Provide basic info related the cluster performance, in particular, the number of signals send,
281 /// signals per sounds (sps), the number of gathers, the number of positions gathered (per node and per second, gpps)
282 /// The number of TT saves and TT saves per second. If gpps equals approximately TTSavesps the gather loop has enough bandwidth.
283 void cluster_info(Depth depth) {
284
285   TimePoint elapsed = Time.elapsed() + 1;
286   uint64_t TTSaves = TT_saves();
287
288   sync_cout << "info depth " << depth << " cluster "
289             << " signals " << signalsCallCounter << " sps " << signalsCallCounter * 1000 / elapsed
290             << " sendRecvs " << sendRecvPosted << " srpps " <<  TTSendRecvBuffs[0].size() * sendRecvPosted * 1000 / elapsed
291             << " TTSaves " << TTSaves << " TTSavesps " << TTSaves * 1000 / elapsed
292             << sync_endl;
293 }
294
295 /// When a TT entry is saved, additional steps are taken if the entry is of sufficient depth.
296 /// If sufficient entries has been collected, a communication is initiated.
297 /// If a communication has been completed, the received results are saved to the TT.
298 void save(Thread* thread, TTEntry* tte,
299           Key k, Value v, bool PvHit, Bound b, Depth d, Move m, Value ev) {
300
301   // Standard save to the TT
302   tte->save(k, v, PvHit, b, d, m, ev);
303
304   // If the entry is of sufficient depth to be worth communicating, take action.
305   if (d > 3)
306   {
307      // count the TTsaves to information: this should be relatively similar
308      // to the number of entries we can send/recv.
309      thread->TTsaves.fetch_add(1, std::memory_order_relaxed);
310
311      // Add to thread's send buffer, the locking here avoids races when the master thread
312      // prepares the send buffer.
313      {
314          std::lock_guard<std::mutex> lk(thread->ttCache.mutex);
315          thread->ttCache.buffer.replace(KeyedTTEntry(k,*tte));
316          ++TTCacheCounter;
317      }
318
319      size_t recvBuffPerRankSize = Threads.size() * TTCacheSize;
320
321      // Communicate on main search thread, as soon the threads combined have collected
322      // sufficient data to fill the send buffers.
323      if (thread == Threads.main() && TTCacheCounter > recvBuffPerRankSize)
324      {
325          // Test communication status
326          int flag;
327          MPI_Testall(reqsTTSendRecv.size(), reqsTTSendRecv.data(), &flag, MPI_STATUSES_IGNORE);
328
329          // Current communication is complete
330          if (flag)
331          {
332              // Save all received entries to TT, and store our TTCaches, ready for the next round of communication
333              for (size_t irank = 0; irank < size_t(size()) ; ++irank)
334              {
335                  if (irank == size_t(rank())) // this is our part, fill the part of the buffer for sending
336                  {
337                     // Copy from the thread caches to the right spot in the buffer
338                     size_t i = irank * recvBuffPerRankSize;
339                     for (auto&& th : Threads)
340                     {
341                         std::lock_guard<std::mutex> lk(th->ttCache.mutex);
342
343                         for (auto&& e : th->ttCache.buffer)
344                             TTSendRecvBuffs[sendRecvPosted % 2][i++] = e;
345
346                         // Reset thread's send buffer
347                         th->ttCache.buffer = {};
348                     }
349
350                     TTCacheCounter = 0;
351                  }
352                  else // process data received from the corresponding rank.
353                     for (size_t i = irank * recvBuffPerRankSize; i < (irank + 1) * recvBuffPerRankSize; ++i)
354                     {
355                         auto&& e = TTSendRecvBuffs[sendRecvPosted % 2][i];
356                         bool found;
357                         TTEntry* replace_tte;
358                         replace_tte = TT.probe(e.first, found);
359                         replace_tte->save(e.first, e.second.value(), e.second.is_pv(), e.second.bound(), e.second.depth(),
360                                           e.second.move(), e.second.eval());
361                     }
362              }
363
364              // Start next communication
365              sendrecv_post();
366
367              // Force check of time on the next occasion, the above actions might have taken some time.
368              static_cast<MainThread*>(thread)->callsCnt = 0;
369
370          }
371      }
372   }
373 }
374
375 /// Picks the bestMove across ranks, and send the associated info and PV to the root of the cluster.
376 /// Note that this bestMove and PV must be output by the root, the guarantee proper ordering of output.
377 /// TODO update to the scheme in master.. can this use aggregation of votes?
378 void pick_moves(MoveInfo& mi, std::string& PVLine) {
379
380   MoveInfo* pMoveInfo = NULL;
381   if (is_root())
382   {
383       pMoveInfo = (MoveInfo*)malloc(sizeof(MoveInfo) * size());
384   }
385   MPI_Gather(&mi, 1, MIDatatype, pMoveInfo, 1, MIDatatype, 0, MoveComm);
386
387   if (is_root())
388   {
389       std::map<int, int> votes;
390       int minScore = pMoveInfo[0].score;
391       for (int i = 0; i < size(); ++i)
392       {
393           minScore = std::min(minScore, pMoveInfo[i].score);
394           votes[pMoveInfo[i].move] = 0;
395       }
396       for (int i = 0; i < size(); ++i)
397       {
398           votes[pMoveInfo[i].move] += pMoveInfo[i].score - minScore + pMoveInfo[i].depth;
399       }
400       int bestVote = votes[pMoveInfo[0].move];
401       for (int i = 0; i < size(); ++i)
402       {
403           if (votes[pMoveInfo[i].move] > bestVote)
404           {
405               bestVote = votes[pMoveInfo[i].move];
406               mi = pMoveInfo[i];
407           }
408       }
409       free(pMoveInfo);
410   }
411
412   // Send around the final result
413   MPI_Bcast(&mi, 1, MIDatatype, 0, MoveComm);
414
415   // Send PV line to root as needed
416   if (mi.rank != 0 && mi.rank == rank()) {
417       int size;
418       std::vector<char> vec;
419       vec.assign(PVLine.begin(), PVLine.end());
420       size = vec.size();
421       MPI_Send(&size, 1, MPI_INT, 0, 42, MoveComm);
422       MPI_Send(vec.data(), size, MPI_CHAR, 0, 42, MoveComm);
423   }
424   if (mi.rank != 0 && is_root()) {
425       int size;
426       std::vector<char> vec;
427       MPI_Recv(&size, 1, MPI_INT, mi.rank, 42, MoveComm, MPI_STATUS_IGNORE);
428       vec.resize(size);
429       MPI_Recv(vec.data(), size, MPI_CHAR, mi.rank, 42, MoveComm, MPI_STATUS_IGNORE);
430       PVLine.assign(vec.begin(), vec.end());
431   }
432
433 }
434
435 /// Return nodes searched (lazily updated cluster wide in the signal loop)
436 uint64_t nodes_searched() {
437
438   return nodesSearchedOthers + Threads.nodes_searched();
439 }
440
441 /// Return table base hits (lazily updated cluster wide in the signal loop)
442 uint64_t tb_hits() {
443
444   return tbHitsOthers + Threads.tb_hits();
445 }
446
447 /// Return the number of saves to the TT buffers, (lazily updated cluster wide in the signal loop)
448 uint64_t TT_saves() {
449
450   return TTsavesOthers + Threads.TT_saves();
451 }
452
453
454 }
455 }
456
457 #else
458
459 #include "cluster.h"
460 #include "thread.h"
461
462 namespace Stockfish {
463 namespace Cluster {
464
465 uint64_t nodes_searched() {
466
467   return Threads.nodes_searched();
468 }
469
470 uint64_t tb_hits() {
471
472   return Threads.tb_hits();
473 }
474
475 uint64_t TT_saves() {
476
477   return Threads.TT_saves();
478 }
479
480 }
481 }
482
483 #endif // USE_MPI