|
ADS
Algorithms and Data Structures
|
The headers of include/ADS/distributed realize, beyond the book, the algorithms whose data are spread over the processes of MPI, each with a memory of its own, which exchange them only by messages; the area holds the sort by regular sampling of DistributedSort.h, whose processes may also run on the threads of an executor (see Theory of parallelism), and the runtime of the processes as workers: their executor, their mailbox with the detection of the termination, and the minimum of a value over them (MPIExecutor.h and MPIMailbox.h). This page collects what the headers take for granted: the model of the cost of the messages and its notation (see Definitions and notation), the property on which the sort rests, with the argument behind its bound (see Sorting by regular sampling), and the detection of the termination by the four counters of Mattern (see Messages, owners and termination). The pseudocode of each algorithm is instead on the page of its header, in the comment of its function, together with the paper that it follows.
A distributed computation runs on \( P \) processes, numbered by their ranks \( 0 , \ldots , P - 1 \) in a communicator, each with its own memory and its own part of the input; a process reads the data of another only by receiving a message. A collective operation is one that all the processes of the communicator call, each with its part, e.g., the gathering of a value of each process in all of them (MPI_Allgather) or the exchange in which each process sends a piece of its data to every other one (MPI_Alltoall, and MPI_Alltoallv for pieces of different sizes). The cost of a message of \( b \) bytes is taken as \( \alpha + \beta b \), \( \alpha \) the latency and \( \beta \) the time per byte, and the cost of an algorithm is its local work, its number of messages and its volume of communication, all per process, i.e., along its slowest process.
| Symbol | Meaning |
|---|---|
| \( P \) | the processes of the communicator comm |
| \( n_i \), \( n \) | the items of the process \( i \), and \( n = \sum_i n_i \) the items of all |
| \( \alpha \), \( \beta \) | the latency of a message and the time per byte |
| \( s_1 , \ldots , s_{P - 1} \) | the splitters of the sort, which cut the keys into \( P \) intervals, one per process |
| \( w \) | the items of a process between two of its samples, \( n_i / P \) |
| \( s_i \), \( r_i \) | the messages sent and received by the process \( i \) in a computation of the mailbox |
| \( S_k \), \( R_k \) | the sums of \( s_i \) and of \( r_i \) read by the wave \( k \) |
A sort of the items spread over the processes leaves in the process of rank \( j \) a sorted vector, whose items are not smaller than those of the ranks before \( j \), so that the vectors in the order of the ranks are the sorted items of all. Every distributed sort moves most items to another process, since a process holds, before the sort, items of every interval: the volume of communication is at least about \( n / P \) per process, and the aim is to send each item once, in one exchange, with parts of about \( n / P \) items at the end.
The sort of Shi and Schaeffer (sort() of DistributedSort.h) sorts the items of each process, takes from them \( P - 1 \) samples at the ranks \( \lfloor ( j + 1 ) n_i / P \rfloor \), i.e., one every \( w \) items, gathers all the \( P ( P - 1 ) \) samples in every process and sorts them, and takes as splitters every \( ( P - 1 ) \)-th of them. Each process then cuts its sorted items at the splitters by binary searches, sends to the process \( j \) its items in \( ( s_j , s_{j+1} ] \), and merges the \( P \) sorted pieces that it receives. Since every process cuts at the same splitters, the items received by the process \( j \) are exactly those of all the processes in its interval, and their merge is sorted; hence the vectors in the order of the ranks are the sorted items.
Lemma 1 (regular sampling). With \( n / P \) items in each process and distinct keys, each process receives
\[ \text{fewer than } 2 n / P \text{ items.} \tag{1} \]
Indeed, the interval of a process holds \( P - 1 \) samples, those between two consecutive splitters, and let \( t_i \) be those of the process \( i \), so that \( \sum_i t_i \le P - 1 \). The items of the process \( i \) in the interval lie between its sample before the first of its \( t_i \) samples and its sample after the last one, i.e., in at most \( t_i + 1 \) of its gaps of \( w = n / P^2 \) items; hence the interval holds at most \( \sum_i ( t_i + 1 ) w \le ( 2 P - 1 ) n / P^2 < 2 n / P \) items, which is (1). The bound is tight up to a constant, and in practice the parts are far closer to \( n / P \). With repeated keys (1) may fail, since all the items of a key go to one interval; sort() therefore cuts the items equal to a run of \( k \) equal splitters into \( k + 1 \) parts whose sizes differ by at most one, so that a key repeated many times is spread over as many processes as it fills, and the merge keeps the result sorted since the parts are of equal items.
The cost of the sort is the local sort, \( O( ( n / P ) \log( n / P ) ) \), the sort of the samples, \( O( P^2 \log P ) \), the \( P - 1 \) binary searches, and the merge of \( P \) pieces of \( O( n / P ) \) items in all, \( O( ( n / P ) \log P ) \), i.e., \( O( ( n / P ) \log n ) \) work per process when \( n / P \ge P^2 \). The communication is three collective operations: the gathering of the samples, \( P^2 \) items to each process; the exchange of the sizes of the pieces, one integer for each pair of processes; and the exchange of the pieces, each item sent once, which by (1) costs each process \( O( n / P ) \) items. With a direct all-to-all, in which each process sends \( P - 1 \) messages, the time of the communication is therefore
\[ O( \alpha P + \beta ( n / P + P^2 ) ) , \tag{2} \]
which is \( O( \alpha P + \beta n / P ) \) when \( n / P \ge P^2 \). Beyond some thousands of processes the latency \( \alpha P \) and the \( P^2 \) samples dominate (2); the multi-level sorts of Axtmann, Bingmann, Sanders and Schulz then group the processes and exchange the items in \( O( \log P ) \) levels, each item sent once per level, which trades the \( P \) messages of each process for \( O( P^{1 / \ell} ) \) messages per level over \( \ell \) levels.
The processes of a communicator are the workers of a distributed computation (MPIExecutor of MPIExecutor.h, whose run( f ) makes the call f( rank ) in each process): each owns a part of the data, the items whose keys hash to its rank (ownerOf( k , P ) of Mailbox.h, the hash of \( k \) modulo \( P \)), and sends to the owner what concerns them, e.g., the nodes of a search that the other processes generate. The mailbox of MPIMailbox.h gathers the messages of a process for each rank into an outbox and sends it as one message of MPI when it holds a batch, by MPI_Isend, which does not block: the buffer stays in a list of the sends in flight until MPI has completed it, which MPI_Testsome finds at each poll, so that a process never waits for a receiver; the batches that have come are found by MPI_Iprobe from any source and received by MPI_Recv. The batches trade the latency \( \alpha \) of a message for its volume: \( k \) messages of \( b \) bytes cost \( \alpha + \beta k b \) instead of \( k ( \alpha + \beta b ) \), and the latency of a single message grows by the time its batch takes to fill, which flush() cuts. This is the asynchronous mailbox of the library YGM of Priest, Steil, Sanders and Pearce; the exchange of NBX of Hoefler, Siebert and Lumsdaine solves instead a single round of sparse messages, by synchronous sends and a barrier that does not block.
A batch is an array of messages when they are trivially copyable, which MPI then copies as bytes (several kinds of them travel as one std::variant, itself trivially copyable). Messages of other kinds, or of lengths that vary (the nodes of a search with a state of variable size), are records of MPIByteMailbox: each a tag of 32 bits, a length and the bytes, padded to 8, packed into a batch until it holds batch bytes, and MPIMailbox of a type that is not trivially copyable is such a mailbox whose bytes are those of MPICodec, the customization point that encodes and decodes a message. Whatever their kind, the messages are counted one by one, so that the waves below see all of them.
A process is active while it has work and passive otherwise, becomes active only by receiving, and sends only while active; the computation has terminated when all the processes are passive and no message is on its way. Each process counts the messages it has sent, \( s_i \), and those it has received, \( r_i \), both of which only grow. A wave reads the counters of all the processes and sums them, \( S = \sum_i s_i \) and \( R = \sum_i r_i \); here it is a reduction that does not block (MPI_Iallreduce), to which each process contributes the pair \( ( s_i , r_i ) \) copied when it is passive, i.e., from quiescent(), while it keeps receiving and working until the wave completes, and a process starts a wave only after the previous one has completed in it, which happens only after all the processes have contributed to it.
Lemma 2 (Mattern's four counters). Let the wave \( k - 1 \) read the receipts \( R_{k - 1} \) and the next wave \( k \) the sends \( S_k \). If
\[ S_k = R_{k - 1} , \tag{3} \]
the computation had terminated when the last process contributed to the wave \( k - 1 \).
Indeed, let \( T \) be the moment of the last contribution to the wave \( k - 1 \), which precedes every contribution to the wave \( k \), and \( S( T ) \), \( R( T ) \) the messages sent and received by all at \( T \). The counters only grow, the receipts of the wave \( k - 1 \) were read at or before \( T \) and the sends of the wave \( k \) at or after it, and a message is received after it is sent; hence
\[ R_{k - 1} \le R( T ) \le S( T ) \le S_k , \]
and (3) makes them all equal. Thus at \( T \) no message was on its way (\( R( T ) = S( T ) \)), and no process had received a message after it contributed to the wave \( k - 1 \) (\( R_{k - 1} = R( T ) \)): each process was passive when it contributed, and a passive process becomes active only by receiving, so that all were passive at \( T \), which is the termination. Conversely, once the computation has terminated, every process eventually finds no work and contributes to every wave, the counters stop growing, and two waves after the termination \( S_k = R_{k - 1} \) holds; since every process gets the same sums, all the processes stop at the same wave. A wave costs a reduction of three integers, \( O( \alpha \log P ) \) time, so that the termination is known \( O( \alpha \log P ) \) after it happens, with no message per message sent (the acknowledgments of Dijkstra and Scholten) and no process that coordinates the waves.
A process may also stop the whole computation before its termination (a limit on the work, a bound that closes the search), by stop(): it sends a note, an empty batch, to each other process, and from then on posts and receives nothing; a process whose poll finds a note stops in the same way, and leaves its work for quiescent(). A stopped process contributes to the waves the triple \( ( s_i , r_i , 1 ) \), the others \( ( s_i , r_i , 0 ) \), and the third sum \( \Sigma \) of a wave counts the processes stopped when they contributed to it.
Lemma 3 (the stop). If a process calls stop(), every process sees the end at the same wave, at most two waves after the last one has received the note; if \( \Sigma_k = P \) for the wave \( k \), all the processes had stopped when they contributed to it.
Indeed, the notes are messages of MPI, which delivers them; a process that does not stop by itself polls while it works, or in quiescent() while it waits, and stops when it receives its note. Once stopped, a process stays stopped, and since it receives nothing more its work does not grow, so that it calls quiescent() until the end; the first wave started by all after the last of them has stopped has \( \Sigma = P \), and every process, which has the same sums, sees the end at that wave. The waves are started by each process only after the previous one has completed, so that they are the same collective operations in the same order on all the processes, also when the end comes from (3) instead of from \( \Sigma = P \): a computation whose processes have all stopped can also meet (3), if nothing is on its way, and it is then over in both senses.
After the end, leftovers( f ) hands to \( f \) the messages that no poll has handed: those still on their way to the process, those to itself and those of its outboxes. The first are known by counting: each process keeps the messages \( m_{i , q} \) and the notes \( n_{i , q} \) that it has sent by MPI to each process \( q \), and one reduction that scatters its sums (MPI_Reduce_scatter_block) gives each process \( q \) the totals
\[ M_q = \sum_i m_{i , q} , \qquad N_q = \sum_i n_{i , q} \tag{4} \]
of what was sent to it; the process then receives, by blocking calls, until it has received \( M_q \) messages and \( N_q \) notes in all, and waits for its own sends, which its receivers are receiving in the same way.
Lemma 4 (each message once). After leftovers() on every process, each message given to send() on any process has been handed to a function exactly once, by a poll() or by leftovers().
Indeed, a message given to send() is in an outbox until its batch is posted, then either among the messages of the process to itself or on its way by MPI, and finally handed by the poll that receives it. A stopped process posts nothing, so that what it holds in its outboxes or for itself is handed by its leftovers(); what was posted by MPI to \( q \) is counted in \( M_q \) of (4), and \( q \) receives until it has received \( M_q \) messages, those of its polls included, which are exactly those of the batches posted to it, since MPI delivers each message once. The cost is one reduction of \( 2 P \) integers and the messages left, i.e., \( O( \alpha \log P + \beta P ) \) time besides them, paid once at the end, and nothing during the computation but a load of a flag per send; the destructor makes the same exchange, dropping the messages, if leftovers() was not called, so that no send stays in flight.
The bound of a search that every process improves (the value of the best solution found so far) is shared the same way: DistributedMin starts a reduction of the minimum that does not block and tests it while the process works, so that a process learns the bounds of the others at most one reduction late, without a message for each improvement.
The sort of Sorting by regular sampling runs on the processes themselves (the communicator of an MPIExecutor, comm()), each with the threads of an executor of its own inside: no part of it simulates the processes.