Reduction operator explained
In computer science, the reduction operator[1] 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.[2] [3] [4] 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.
Theory
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:
- It can reduce an array to a single scalar value.
- The final result should be obtainable from the results of the partial tasks that were created.
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
can be applied in constant time on an input set
of
vectors with
elements each. The result
of the operation is the combination of the elements
and has to be stored at a specified root processor at the end of the execution. If the result
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
steps until only
is left. Sequential algorithms can not perform better than linear time, but parallel algorithms leave some space left to optimize.
Example
Suppose we have an array
. 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:
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
,
,
, and
. Then two cores can compute
and
, and lastly a single core computes
. So a total of 4 cores can be used to compute the sum in
steps instead of the
steps required for the serial version. This parallel binary tree technique computes
((2+3)+(5+1))+((7+6)+(8+4))
. 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.
Nonexample
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.
Algorithms
Binomial tree algorithms
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.
PRAM-algorithm
This algorithm represents a widely spread method to handle inputs where
is a power of two. The reverse procedure is often used for broadcasting elements.
[5] [6] [7] for
to
dofor
to
do in parallelif
is active thenif bit
of
is set thenset
to inactiveelse if
The binary operator for vectors is defined element-wise such that
\begin{pmatrix}
\ \vdots
\end{pmatrix} ⊕ \star\begin{pmatrix}
\ \vdots
\end{pmatrix}=\begin{pmatrix}
⊕
\ \vdots
⊕
\end{pmatrix}
. The algorithm further assumes that in the beginning
for all
and
is a power of two and uses the processing units
. 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
evaluates the given operator on the element
it is currently holding and
where
is the minimal index fulfilling
, so that
is becoming an inactive processor in the current step.
and
are not necessarily elements of the input set
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
to
is used. Each processor looks at its
-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
-th bit is not set. The underlying communication pattern of the algorithm is a binomial tree, hence the name of the algorithm.
Only
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
. Furthermore, the number
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.
[8] Runtime analysis
The main loop is executed
times, the time needed for the part done in parallel is in
as a processing unit either combines two vectors or becomes inactive. Thus the parallel time
for the PRAM is
. The strategy for handling read and write conflicts can be chosen as restrictive as an exclusive read and exclusive write (EREW). The speedup
of the algorithm is
and therefore the
efficiency is
. The efficiency suffers because half of the active processing units become inactive after each step, so
units are active in step
.
Distributed memory algorithm
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.
for
to
dofor
to
do in parallelif
is active thenif bit
of
is set thensend
to
set
to inactiveelse if
receive
The only difference between the distributed algorithm and the PRAM version is the inclusion of explicit communication primitives, the operating principle stays the same.
Runtime analysis
The communication between units leads to some overhead. A simple analysis for the algorithm uses the BSP-model and incorporates the time
needed to initiate communication and
the time needed to send a byte. Then the resulting runtime is
\Theta((Tstart+n ⋅ Tbyte) ⋅ log(p))
, as
elements of a vector are sent in each iteration and have size
in total.
Pipeline-algorithm
For distributed memory models, it can make sense to use pipelined communication. This is especially the case when
is small in comparison to
. 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:
[9] for
to
dofor
to
do in parallelif
send
to
if
receive
from
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
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.
Runtime analysis
The number of steps in the parallel execution are
, it takes
steps until the last processing unit receives its first element and additional
until all elements are received. Therefore, the runtime in the BSP-model is
T(n,p,m)=\left(Tstart+
⋅ Tbyte\right)(p+m-2)
, assuming that
is the total byte-size of a vector.
Although
has a fixed value, it is possible to logically group elements of a vector together and reduce
. 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
is halved, while the total byte-size
stays the same. The runtime
for this approach depends on the value of
, which can be optimized if
and
are known. It is optimal for
}, assuming that this results in a smaller
that divides the original one.
Applications
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.[10] 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.[11] [12]
Some parallel sorting algorithms use reductions to be able to handle very big data sets.[13]
See also
Notes and References
- Web site: Reduction Clause . www.dartmouth.edu . Dartmouth College . 26 September 2016 . en . 23 March 2009.
- Book: Solihin. Yan. Fundamentals of Parallel Multicore Architecture. 2016. CRC Press . 978-1-4822-1118-4 . 75.
- Book: Chandra. Rohit. Parallel Programming in OpenMP . limited . 2001 . Morgan Kaufmann . 1558606718 . 59–77.
- Cole. Murray. 2004. Bringing skeletons out of the closet: a pragmatic manifesto for skeletal parallel programming. Parallel Computing . 30 . 3. 393. 10.1016/j.parco.2003.12.002. 20.500.11820/8eb79d42-de83-4cfb-9faa-30d9ac3b3839. free.
- Bar-Noy. Amotz. Kipnis. Shlomo. Broadcasting multiple messages in simultaneous send/receive systems. Discrete Applied Mathematics. 55. 2. 95–105. 10.1016/0166-218x(94)90001-9. 1994.
- Santos. Eunice E.. Eunice Santos. Optimal and Efficient Algorithms for Summing and Prefix Summing on Parallel Machines. Journal of Parallel and Distributed Computing. 62. 4. 517–543. 10.1006/jpdc.2000.1698. 2002.
- Slater. P.. Cockayne. E.. Hedetniemi. S.. 1981-11-01. Information Dissemination in Trees. SIAM Journal on Computing. 10. 4. 692–701. 10.1137/0210052. 0097-5397.
- Book: Rabenseifner. Rolf. Träff. Jesper Larsson. Recent Advances in Parallel Virtual Machine and Message Passing Interface . More Efficient Reduction Algorithms for Non-Power-of-Two Number of Processors in Message-Passing Parallel Systems . 2004-09-19. 3241. Lecture Notes in Computer Science. en. Springer, Berlin, Heidelberg. 36–46. 10.1007/978-3-540-30218-6_13. 9783540231639.
- Bar-Noy. A.. Kipnis. S.. 1994-09-01. Designing broadcasting algorithms in the postal model for message-passing systems. Mathematical Systems Theory. en. 27. 5. 431–452. 10.1007/BF01184933. 0025-5661. 10.1.1.54.2543. 42798826.
- Pješivac-Grbović. Jelena. Angskun. Thara. Bosilca. George. Fagg. Graham E.. Gabriel. Edgar. Dongarra. Jack J.. 2007-06-01. Performance analysis of MPI collective operations. Cluster Computing. en. 10. 2. 127–143. 10.1007/s10586-007-0012-0. 2142998. 1386-7857. 10.1.1.80.3867.
- Lämmel. Ralf. Google's MapReduce programming model — Revisited. Science of Computer Programming. 70. 1. 1–30. 10.1016/j.scico.2007.07.001. 2008.
- Senger. Hermes. Gil-Costa. Veronica. Arantes. Luciana. Marcondes. Cesar A. C.. Marín. Mauricio. Sato. Liria M.. da Silva. Fabrício A.B.. 2016-06-10. BSP cost and scalability analysis for MapReduce operations. Concurrency and Computation: Practice and Experience. en. 28. 8. 2503–2527. 10.1002/cpe.3628. 1532-0634. 10533/147670. 33645927. free.
- Axtmann. Michael. Bingmann. Timo. Sanders. Peter. Schulz. Christian. 2014-10-24. Practical Massively Parallel Sorting. 1410.6754. cs.DS.