1000/1000
Hot
Most Recent
In computer science, the reduction operator is a type of operator that is commonly used in parallel programming to reduce the elements of an array into a single result. Reduction operators are associative and often (but not necessarily) commutative. The reduction of sets of elements is an integral part of programming models such as Map Reduce, where a reduction operator is applied (mapped) to all elements before they are reduced. Other parallel algorithms use reduction operators as primary operations to solve more complex problems. Many reduction operators can be used for broadcasting to distribute data to all processors.
A reduction operator can help break down a task into various partial tasks by calculating partial results which can be used to obtain a final result. It allows certain serial operations to be performed in parallel and the number of steps required for those operations to be reduced. A reduction operator stores the result of the partial tasks into a private copy of the variable. These private copies are then merged into a shared copy at the end.
An operator is a reduction operator if:
These two requirements are satisfied for commutative and associative operators that are applied to all array elements.
Some operators which satisfy these requirements are addition, multiplication, and some logical operators (and, or, etc.).
A reduction operator [math]\displaystyle{ \oplus }[/math] can be applied in constant time on an input set [math]\displaystyle{ V = \{v_0 = \begin{pmatrix} e_0^0 \\ \vdots \\ e_0^{m-1}\end{pmatrix}, v_1 = \begin{pmatrix} e_1^0 \\ \vdots \\ e_1^{m-1}\end{pmatrix}, \dots, v_{p-1} = \begin{pmatrix} e_{p-1}^0 \\ \vdots \\ e_{p-1}^{m-1}\end{pmatrix}\} }[/math]of [math]\displaystyle{ p }[/math] vectors with [math]\displaystyle{ m }[/math] elements each. The result [math]\displaystyle{ r }[/math] of the operation is the combination of the elements [math]\displaystyle{ r = \begin{pmatrix} e_0^0 \oplus e_1^0 \oplus \dots \oplus e_{p-1}^0 \\ \vdots \\ e_0^{m-1} \oplus e_1^{m-1} \oplus \dots \oplus e_{p-1}^{m-1}\end{pmatrix} = \begin{pmatrix} \bigoplus_{i=0}^{p-1} e_i^0 \\ \vdots \\ \bigoplus_{i=0}^{p-1} e_i^{m-1} \end{pmatrix} }[/math] and has to be stored at a specified root processor at the end of the execution. If the result [math]\displaystyle{ r }[/math] has to be available at every processor after the computation has finished, it is often called Allreduce. An optimal sequential linear-time algorithm for reduction can apply the operator successively from front to back, always replacing two vectors with the result of the operation applied to all its elements, thus creating an instance that has one vector less. It needs [math]\displaystyle{ (p-1)\cdot m }[/math] steps until only [math]\displaystyle{ r }[/math] is left. Sequential algorithms can not perform better than linear time, but parallel algorithms leave some space left to optimize.
Suppose we have an array [math]\displaystyle{ [2, 3, 5, 1, 7, 6, 8, 4] }[/math]. The sum of this array can be computed serially by sequentially reducing the array into a single sum using the '+' operator. Starting the summation from the beginning of the array yields:
[math]\displaystyle{ \Bigg( \bigg( \Big( \big(\, (\, (2 + 3) + 5 ) + 1 \big) + 7\Big) + 6 \bigg) + 8\Bigg) + 4 = 36 }[/math]
Since '+' is both commutative and associative, it is a reduction operator. Therefore this reduction can be performed in parallel using several cores, where each core computes the sum of a subset of the array, and the reduction operator merges the results. Using a binary tree reduction would allow 4 cores to compute [math]\displaystyle{ (2 + 3) }[/math], [math]\displaystyle{ (5 + 1) }[/math], [math]\displaystyle{ (7 + 6) }[/math], and [math]\displaystyle{ (8 + 4) }[/math]. Then two cores can compute [math]\displaystyle{ (5 + 6) }[/math] and [math]\displaystyle{ (13 + 12) }[/math], and lastly a single core computes [math]\displaystyle{ (11 + 25) = 36 }[/math]. So a total of 4 cores can be used to compute the sum in [math]\displaystyle{ \log_{2}8 = 3 }[/math] steps instead of the [math]\displaystyle{ 7 }[/math] steps required for the serial version. This parallel binary tree technique computes [math]\displaystyle{ \big(\,(2 + 3) + (5 + 1)\,\big) + \big(\,(7 + 6) + (8 + 4)\,\big) }[/math]. Of course the result is the same, but only because of the associativity of the reduction operator. The commutativity of the reduction operator would be important if there were a master core distributing work to several processors, since then the results could arrive back to the master processor in any order. The property of commutativity guarantees that the result will be the same.
Matrix multiplication is not a reduction operator since the operation is not commutative. If processes were allowed to return their matrix multiplication results in any order to the master process, the final result that the master computes will likely be incorrect if the results arrived out of order. However, note that matrix multiplication is associative, and therefore the result would be correct as long as the proper ordering were enforced, as in the binary tree reduction technique.
Regarding parallel algorithms, there are two main models of parallel computation, the parallel random access machine as an extension of the RAM with shared memory between processing units and the bulk synchronous parallel computer which takes communication and synchronization into account. Both models have different implications for the time-complexity, therefore two algorithms will be shown.
This algorithm represents a widely spread method to handle inputs where [math]\displaystyle{ p }[/math] is a power of two. The reverse procedure is often used for broadcasting elements.[2][3][4]
The binary operator for vectors is defined element-wise such that [math]\displaystyle{ \begin{pmatrix} e_i^0 \\ \vdots \\ e_i^{m-1}\end{pmatrix} \oplus^\star \begin{pmatrix} e_j^0 \\ \vdots \\ e_j^{m-1}\end{pmatrix} = \begin{pmatrix} e_i^0 \oplus e_j^0 \\ \vdots \\ e_i^{m-1} \oplus e_j^{m-1} \end{pmatrix} }[/math]. The algorithm further assumes that in the beginning [math]\displaystyle{ x_i = v_i }[/math] for all [math]\displaystyle{ i }[/math] and [math]\displaystyle{ p }[/math] is a power of two and uses the processing units [math]\displaystyle{ p_0, p_1,\dots p_{n-1} }[/math]. In every iteration, half of the processing units become inactive and do not contribute to further computations. The figure shows a visualization of the algorithm using addition as the operator. Vertical lines represent the processing units where the computation of the elements on that line take place. The eight input elements are located on the bottom and every animation step corresponds to one parallel step in the execution of the algorithm. An active processor [math]\displaystyle{ p_i }[/math] evaluates the given operator on the element [math]\displaystyle{ x_i }[/math] it is currently holding and [math]\displaystyle{ x_j }[/math] where [math]\displaystyle{ j }[/math] is the minimal index fulfilling [math]\displaystyle{ j \gt i }[/math], so that [math]\displaystyle{ p_j }[/math] is becoming an inactive processor in the current step. [math]\displaystyle{ x_i }[/math] and [math]\displaystyle{ x_j }[/math] are not necessarily elements of the input set [math]\displaystyle{ X }[/math] as the fields are overwritten and reused for previously evaluated expressions. To coordinate the roles of the processing units in each step without causing additional communication between them, the fact that the processing units are indexed with numbers from [math]\displaystyle{ 0 }[/math] to [math]\displaystyle{ p-1 }[/math] is used. Each processor looks at its [math]\displaystyle{ k }[/math]-th least significant bit and decides whether to get inactive or compute the operator on its own element and the element with the index where the [math]\displaystyle{ k }[/math]-th bit is not set. The underlying communication pattern of the algorithm is a binomial tree, hence the name of the algorithm.
Only [math]\displaystyle{ p_0 }[/math] holds the result in the end, therefore it is the root processor. For an Allreduce-operation the result has to be distributed, which can be done by appending a broadcast from [math]\displaystyle{ p_0 }[/math]. Furthermore, the number [math]\displaystyle{ p }[/math] of processors is restricted to be a power of two. This can be lifted by padding the number of processors to the next power of two. There are also algorithms that are more tailored for this use-case.[5]
The main loop is executed [math]\displaystyle{ \lceil\log_2 p\rceil }[/math] times, the time needed for the part done in parallel is in [math]\displaystyle{ \mathcal{O}(m) }[/math] as a processing unit either combines two vectors or becomes inactive. Thus the parallel time [math]\displaystyle{ T(p, m) }[/math] for the PRAM is [math]\displaystyle{ T(p, m) = \mathcal{O}(\log(p) \cdot m) }[/math]. The strategy for handling read and write conflicts can be chosen as restrictive as an exclusive read and exclusive write (EREW). The speedup [math]\displaystyle{ S(p, m) }[/math] of the algorithm is [math]\displaystyle{ S(p, m) \in \mathcal{O}\left(\frac{T_{seq}}{T(p, m)}\right) = \mathcal{O}\left(\frac{p}{\log(p)}\right) }[/math] and therefore the efficiency is [math]\displaystyle{ E(p, m) \in \mathcal{O}\left(\frac{S(p, m)}{p}\right) = \mathcal{O}\left(\frac{1}{\log(p)}\right) }[/math]. The efficiency suffers because of the fact that half of the active processing units become inactive after each step, so [math]\displaystyle{ \frac{p}{2^i} }[/math] units are active in step [math]\displaystyle{ i }[/math].
In contrast to the PRAM-algorithm, in the distributed memory model, memory is not shared between processing units and data has to be exchanged explicitly between processing units. Therefore, data has to be exchanged explicitly between units, as can be seen in the following algorithm.
The only difference between the distributed algorithm and the PRAM version is the inclusion of explicit communication primitives, the operating principle stays the same.
The communication between units leads to some overhead. A simple analysis for the algorithm uses the BSP-model and incorporates the time [math]\displaystyle{ T_{start} }[/math] needed to initiate communication and [math]\displaystyle{ T_{byte} }[/math] the time needed to send a byte. Then the resulting runtime is [math]\displaystyle{ \Theta((T_{start} + n \cdot T_{byte})\cdot log(p)) }[/math], as [math]\displaystyle{ m }[/math] elements of a vector are sent in each iteration and have size [math]\displaystyle{ n }[/math] in total.
For distributed memory models, it can make sense to use pipelined communication. This is especially the case when [math]\displaystyle{ T_{start} }[/math] is small in comparison to [math]\displaystyle{ T_{byte} }[/math]. Usually, linear pipelines split data or a tasks into smaller pieces and process them in stages. In contrast to the binomial tree algorithms, the pipelined algorithm uses the fact that the vectors are not inseparable, but the operator can be evaluated for single elements:[6]
It is important to note that the send and receive operations have to be executed concurrently for the algorithm to work. The result vector is stored at [math]\displaystyle{ p_{p-1} }[/math] at the end. The associated animation shows an execution of the algorithm on vectors of size four with five processing units. Two steps of the animation visualize one parallel execution step.
The number of steps in the parallel execution are [math]\displaystyle{ p + m -2 }[/math], it takes [math]\displaystyle{ p-1 }[/math] steps until the last processing unit receives its first element and additional [math]\displaystyle{ m-1 }[/math] until all elements are received. Therefore, the runtime in the BSP-model is [math]\displaystyle{ T(n, p, m) = \left(T_{start} + \frac{n}{m}\cdot T_{byte}\right)(p+m-2) }[/math], assuming that [math]\displaystyle{ n }[/math] is the total byte-size of a vector.
Although [math]\displaystyle{ m }[/math] has a fixed value, it is possible to logically group elements of a vector together and reduce [math]\displaystyle{ m }[/math]. For example, a problem instance with vectors of size four can be handled by splitting the vectors into the first two and last two elements, which are always transmitted and computed together. In this case, double the volume is sent each step, but the number of steps has roughly halved. It means that the parameter [math]\displaystyle{ m }[/math] is halved, while the total byte-size [math]\displaystyle{ n }[/math] stays the same. The runtime [math]\displaystyle{ T(p) }[/math] for this approach depends on the value of [math]\displaystyle{ m }[/math], which can be optimized if [math]\displaystyle{ T_{start} }[/math] and [math]\displaystyle{ T_{byte} }[/math] are known. It is optimal for [math]\displaystyle{ m = \sqrt{\frac{n \cdot (p-2)\cdot T_{byte}}{T_{start}}} }[/math], assuming that this results in a smaller [math]\displaystyle{ m }[/math] that divides the original one.
Reduction is one of the main collective operations implemented in the Message Passing Interface, where performance of the used algorithm is important and evaluated constantly for different use cases.[7] Operators can be used as parameters for MPI_Reduce
and MPI_Allreduce
, with the difference that the result is available at one (root) processing unit or all of them. MapReduce relies heavily on efficient reduction algorithms to process big data sets, even on huge clusters.[8][9]
Some parallel sorting algorithms use reductions to be able to handle very big data sets.[10]