# Collective operation

Collective operations are building blocks for interaction patterns, that are often used in SPMD algorithms in the parallel programming context. Hence, there is an interest in efficient realizations of these operations.

A realization of the collective operations is provided by the Message Passing Interface[1] (MPI).

## Definitions

In all asymptotic runtime functions, we denote the latency ${\displaystyle \alpha }$, the communication cost per word ${\displaystyle \beta }$, the number of processing units ${\displaystyle p}$ and the input size per node ${\displaystyle n}$. In cases where we have initial messages on more than one node we assume that all local messages are of the same size. To address individual processing units we use ${\displaystyle p_{i}\in \{p_{0},p_{1},\dots ,p_{p-1}\}}$.

If we do not have an equal distribution, i.e. node ${\displaystyle p_{i}}$ has a message of size ${\displaystyle n_{i}}$, we get an upper bound for the runtime by setting ${\displaystyle n=\max(n_{0},n_{1},\dots ,n_{p-1})}$.

A distributed memory model is assumed. The concepts are similar for the shared memory model. However, shared memory systems can provide hardware support for some operations like broadcast (§ Broadcast) for example, which allows convenient concurrent read.[2] Thus, new algorithmic possibilities can become available.

Information flow of Broadcast operation performed on three nodes.

The broadcast pattern is used to distribute data from one processing unit to all processing units, which is often needed in SPMD parallel programs to dispense input or global values. Broadcast can be interpreted as an inverse version of the reduce pattern (§ Reduce). Initially only root ${\displaystyle r}$ with ${\displaystyle id}$ ${\displaystyle 0}$ stores message ${\displaystyle m}$. During broadcast ${\displaystyle m}$ is sent to the remaining processing units, so that eventually ${\displaystyle m}$ is available to all processing units.

Since an implementation by means of a sequential for-loop with ${\displaystyle p-1}$ iterations becomes a bottleneck, divide-and-conquer approaches are common. One possibility is to utilize a binomial tree structure with the requirement that ${\displaystyle p}$ has to be a power of two. When a processing unit is responsible for sending ${\displaystyle m}$ to processing units ${\displaystyle i..j}$, it sends ${\displaystyle m}$ to processing unit ${\displaystyle \left\lceil (i+j)/2\right\rceil }$ and delegates responsibility for the processing units ${\displaystyle \left\lceil (i+j)/2\right\rceil ..\left\lceil (i+j)-1\right\rceil }$ to it, while its own responsibility is cut down to ${\displaystyle i..\left\lceil (i+j)/2\right\rceil -1}$.

Binomial trees have a problem with long messages ${\displaystyle m}$. The receiving unit of ${\displaystyle m}$ can only propagate the message to other units, after it received the whole message. In the meantime, the communication network is not utilized. Therefore pipelining on binary trees is used, where ${\displaystyle m}$ is split into an array of ${\displaystyle k}$ packets of size ${\displaystyle \left\lceil n/k\right\rceil }$. The packets are then broadcast one after another, so that data is distributed fast in the communication network.

Pipelined broadcast on balanced binary tree is possible in ${\displaystyle {\mathcal {O}}(\alpha \log p+\beta n)}$.

## Reduce [4]

Information flow of Reduce operation performed on three nodes. f is the associative operator and α is the result of the reduction.

The reduce pattern is used to collect data or partial results from different processing units and to combine them into a global result by a chosen operator. Given ${\displaystyle p}$ processing units, message ${\displaystyle m_{i}}$ is on processing unit ${\displaystyle p_{i}}$ initially. All ${\displaystyle m_{i}}$ are aggregated by ${\displaystyle \otimes }$ and the result is eventually stored on ${\displaystyle p_{0}}$. The reduction operator ${\displaystyle \otimes }$ must be associative at least. Some algorithms require a commutative operator with a neutral element. Operators like ${\displaystyle sum}$, ${\displaystyle min}$, ${\displaystyle max}$ are common.

Implementation considerations are similar to broadcast (§ Broadcast). For pipelining on binary trees the message must be representable as a vector of smaller object for component-wise reduction.

Pipelined reduce on a balanced binary tree is possible in ${\displaystyle {\mathcal {O}}(\alpha \log p+\beta n)}$.

## All-Reduce [5]

Information flow of All-Reduce operation performed on three nodes. f is the associative operator and α is the result of the reduction.

The all-reduce pattern (also called allreduce) is used if the result of a reduce operation (§ Reduce) must be distributed to all processing units. Given ${\displaystyle p}$ processing units, message ${\displaystyle m_{i}}$ is on processing unit ${\displaystyle p_{i}}$ initially. All ${\displaystyle m_{i}}$ are aggregated by an operator ${\displaystyle \otimes }$ and the result is eventually stored on all ${\displaystyle p_{i}}$. Analog to the reduce operation, the operator ${\displaystyle \otimes }$ must be at least associative.

All-reduce can be interpreted as a reduce operation with a subsequent broadcast (§ Broadcast). For long messages a corresponding implementation is suitable, whereas for short messages, the latency can be reduced by using a hypercube (Hypercube (communication pattern) § All-Gather/ All-Reduce) topology, if ${\displaystyle p}$ is a power of two.

All-reduce is possible in ${\displaystyle {\mathcal {O}}(\alpha \log p+\beta n)}$, since reduce and broadcast are possible in ${\displaystyle {\mathcal {O}}(\alpha \log p+\beta n)}$ with pipelining on balanced binary trees.

## Prefix-Sum/Scan [6]

Information flow of Prefix-Sum/Scan operation performed on three nodes. The operator + can be any associative operator.

The prefix-sum or scan operation is used to collect data or partial results from different processing units and to compute intermediate results by an operator, which are stored on those processing units. It can be seen as a generalization of the reduce operation (§ Reduce). Given ${\displaystyle p}$ processing units, message ${\displaystyle m_{i}}$ is on processing unit ${\displaystyle p_{i}}$. The operator ${\displaystyle \otimes }$ must be at least associative, whereas some algorithms require also a commutative operator and a neutral element. Common operators are ${\displaystyle sum}$, ${\displaystyle min}$ and ${\displaystyle max}$. Eventually processing unit ${\displaystyle p_{i}}$ stores the prefix sum ${\displaystyle \otimes _{i'<=i}}$${\displaystyle m_{i'}}$. In the case of the so-called exclusive prefix sum, processing unit ${\displaystyle p_{i}}$ stores the prefix sum ${\displaystyle \otimes _{i'${\displaystyle m_{i'}}$. Some algorithms require to store the overall sum at each processing unit in addition to the prefix sums.

For short messages, this can be achieved with a hypercube topology if ${\displaystyle p}$ is a power of two. For long messages, the hypercube (Hypercube (communication pattern) § Prefix sum, Prefix sum § Distributed memory: Hypercube algorithm) topology is not suitable, since all processing units are active in every step and therefore pipelining can't be used. A binary tree topology is better suited for arbitrary ${\displaystyle p}$ and long messages (Prefix sum § Large Message Sizes: Pipelined Binary Tree).

Prefix-sum on a binary tree can be implemented with an upward and downward phase. In the upward phase reduction is performed, while the downward phase is similar to broadcast, where the prefix sums are computed by sending different data to the left and right children. With this approach pipelining is possible, because the operations are equal to reduction (§ Reduce) and broadcast (§ Broadcast).

Pipelined prefix sum on a binary tree is possible in ${\displaystyle {\mathcal {O}}(\alpha \log p+\beta n)}$.

## Barrier [7]

The barrier as a collective operation is a generalization of the concept of a barrier, that can be used in distributed computing. When a processing unit calls barrier, it waits until all other processing units have called barrier as well. Barrier is thus used to achieve global synchronization in distributed computing.

One way to implement barrier is to call all-reduce (§ All-Reduce) with an empty/ dummy operand. We know the runtime of All-reduce is ${\displaystyle {\mathcal {O}}(\alpha \log p+\beta n)}$. Using a dummy operand reduces size ${\displaystyle n}$ to a constant factor and leads to a runtime of ${\displaystyle {\mathcal {O}}(\alpha \log p)}$.

## Gather [8]

Information flow of Gather operation performed on three nodes.

The gather communication pattern is used to store data from all processing units on a single processing unit. Given ${\displaystyle p}$ processing units, message ${\displaystyle m_{i}}$ on processing unit ${\displaystyle p_{i}}$. For a fixed processing unit ${\displaystyle p_{j}}$, we want to store the message ${\displaystyle m_{1}\cdot m_{2}\cdot \ldots \cdot m_{p}}$ on ${\displaystyle p_{j}}$. Gather can be thought of as a reduce operation (§ Reduce) that uses the concatenation operator. This works due to the fact that concatenation is associative. By using the same binomial tree reduction algorithm we get a runtime of ${\displaystyle {\mathcal {O}}(\alpha \log p+\beta pn)}$. We see that the asymptotic runtime is similar to the asymptotic runtime of reduce ${\displaystyle {\mathcal {O}}(\alpha \log p+\beta n)}$, but with the addition of a factor p to the term ${\displaystyle \beta n}$. This additional factor is due to the message size increasing in each step as messages get concatenated. Compare this to reduce where message size is a constant for operators like ${\displaystyle min}$.

## All-Gather [8]

Information flow of All-Gather operation performed on three nodes.

The all-gather communication pattern is used to collect data from all processing units and to store the collected data on all processing units. Given ${\displaystyle p}$ processing units ${\displaystyle p_{i}}$, message ${\displaystyle m_{i}}$ initially stored on ${\displaystyle p_{i}}$, we want to store the message ${\displaystyle m_{1}\cdot m_{2}\cdot \ldots \cdot m_{p}}$ on each ${\displaystyle p_{j}}$.

It can be thought of in multiple ways. The first is as an all-reduce operation (§ All-Reduce) with concatenation as the operator, in the same way that gather can be represented by reduce. The second is as a gather-operation followed by a broadcast of the new message of size ${\displaystyle pn}$. With this we see that all-gather in ${\displaystyle {\mathcal {O}}(\alpha \log p+\beta pn)}$ is possible.

## Scatter [9]

Information flow of Scatter operation performed on three nodes.

The scatter communication pattern is used to distribute data from one processing unit to all the processing units. It differs from broadcast, in that it does not send the same message to all processing units. Instead it splits the message and delivers one part of it to each processing unit.

Given ${\displaystyle p}$ processing units ${\displaystyle p_{i}}$, a fixed processing unit ${\displaystyle p_{j}}$ that holds the message ${\displaystyle m=m_{1}\cdot m_{2}\cdot \ldots \cdot m_{p}}$. We want to transport the message ${\displaystyle m_{i}}$ onto ${\displaystyle p_{i}}$. The same implementation concerns as for gather (§ Gather) apply. This leads to an optimal runtime in ${\displaystyle {\mathcal {O}}(\alpha \log p+\beta pn)}$.

## All-to-all [10]

All-to-all is the most general communication pattern. For ${\displaystyle 0\leq i, message ${\displaystyle m_{i,j}}$ is the message that is initially stored on node ${\displaystyle i}$ and has to be delivered to node ${\displaystyle j}$. We can express all communication primitives that do not use operators through all-to-all. For example, broadcast of message ${\displaystyle m}$ from node ${\displaystyle p_{k}}$ is emulated by setting ${\displaystyle m_{i,j}=m}$ for ${\displaystyle i=k}$ and setting ${\displaystyle m_{l,j}}$ empty for ${\displaystyle l\neq k}$.

Assuming we have a fully connected network, the best possible runtime for all-to-all is in ${\displaystyle {\mathcal {O}}(p(\alpha +\beta n))}$ . This is achieved through ${\displaystyle p}$ rounds of direct message exchange. For ${\displaystyle p}$ power of 2, in communication round ${\displaystyle k}$ , node ${\displaystyle p_{i}}$ exchanges messages with node ${\displaystyle p_{j},j=i\oplus k}$ .

If the message size is small and latency dominates the communication, a hypercube algorithm can be used to distribute the messages in time ${\displaystyle {\mathcal {O}}(\log p(\alpha +\beta pn))}$ .

Information flow of All-to-All operation performed on three nodes. Letters indicate nodes and numbers indicate information items.

## Runtime Overview [11]

This table gives an overview over the best known asymptotic runtimes, assuming we have free choice of network topology.

Example topologies we want for optimal runtime are binary tree, binomial tree, hypercube.

In practice, we have to adjust to the available physical topologies, e.g. dragonfly, fat tree, grid network (references other topologies, too).

For each operation, the optimal algorithm can depend on the input sizes ${\displaystyle n}$. For example, broadcast for short messages is best implemented using a binomial tree whereas for long messages a pipelined communication on a balanced binary tree is optimal.

The complexities stated in the table depend on the latency ${\displaystyle \alpha }$ and the communication cost per word ${\displaystyle \beta }$ in addition to the number of processing units ${\displaystyle p}$ and the input message size per node ${\displaystyle n}$. The # senders and # receivers columns represent the number of senders and receivers that are involved in the operation respectively. The # messages column lists the number of input messages and the Computations? column indicates if any computations are done on the messages or if the messages are just delivered without processing. Complexity gives the asymptotic runtime complexity of an optimal implementation under free choice of topology.

Name # senders # receivers # messages Computations? Complexity
Broadcast ${\displaystyle 1}$ ${\displaystyle p}$ ${\displaystyle 1}$ no ${\displaystyle {\mathcal {O}}(\alpha \log p+\beta n)}$
Reduce ${\displaystyle p}$ ${\displaystyle 1}$ ${\displaystyle p}$ yes ${\displaystyle {\mathcal {O}}(\alpha \log p+\beta n)}$
All-reduce ${\displaystyle p}$ ${\displaystyle p}$ ${\displaystyle p}$ yes ${\displaystyle {\mathcal {O}}(\alpha \log p+\beta n)}$
Prefix sum ${\displaystyle p}$ ${\displaystyle p}$ ${\displaystyle p}$ yes ${\displaystyle {\mathcal {O}}(\alpha \log p+\beta n)}$
Barrier ${\displaystyle p}$ ${\displaystyle p}$ ${\displaystyle 0}$ no ${\displaystyle {\mathcal {O}}(\alpha \log p)}$
Gather ${\displaystyle p}$ ${\displaystyle 1}$ ${\displaystyle p}$ no ${\displaystyle {\mathcal {O}}(\alpha \log p+\beta pn)}$
All-Gather ${\displaystyle p}$ ${\displaystyle p}$ ${\displaystyle p}$ no ${\displaystyle {\mathcal {O}}(\alpha \log p+\beta pn)}$
Scatter ${\displaystyle 1}$ ${\displaystyle p}$ ${\displaystyle p}$ no ${\displaystyle {\mathcal {O}}(\alpha \log p+\beta pn)}$
All-To-All ${\displaystyle p}$ ${\displaystyle p}$ ${\displaystyle p^{2}}$ no ${\displaystyle {\mathcal {O}}(\log p(\alpha +\beta pn))}$ or ${\displaystyle {\mathcal {O}}(p(\alpha +\beta n))}$

## Notes

1. ^ Intercommunicator Collective Operations. The Message Passing Interface (MPI) standard, chapter 7.3.1. Mathematics and Computer Science Division, Argonne National Laboratory.
2. ^ Sanders, Mehlhorn, Dietzfelbinger, Dementiev 2019, p. 395
3. ^ Sanders, Mehlhorn, Dietzfelbinger, Dementiev 2019, pp. 396-401
4. ^ Sanders, Mehlhorn, Dietzfelbinger, Dementiev 2019, pp. 402-403
5. ^ Sanders, Mehlhorn, Dietzfelbinger, Dementiev 2019, pp. 403-404
6. ^ Sanders, Mehlhorn, Dietzfelbinger, Dementiev 2019, pp. 404-406
7. ^ Sanders, Mehlhorn, Dietzfelbinger, Dementiev 2019, p. 408
8. ^ a b Sanders, Mehlhorn, Dietzfelbinger, Dementiev 2019, pp. 412-413
9. ^ Sanders, Mehlhorn, Dietzfelbinger, Dementiev 2019, p. 413
10. ^ Sanders, Mehlhorn, Dietzfelbinger, Dementiev 2019, pp. 413-418
11. ^ Sanders, Mehlhorn, Dietzfelbinger, Dementiev 2019, p. 394

## References

Sanders, Peter; Mehlhorn, Kurt; Dietzfelbinger, Martin; Dementiev, Roman (2019). Sequential and Parallel Algorithms and Data Structures - The Basic Toolbox. Springer Nature Switzerland AG. ISBN 978-3-030-25208-3.