Fork–join Model
   HOME

TheInfoList



OR:

In parallel computing, the fork–join model is a way of setting up and executing parallel programs, such that execution branches off in parallel at designated points in the program, to "join" (merge) at a subsequent point and resume sequential execution. Parallel sections may fork
recursively Recursion (adjective: ''recursive'') occurs when a thing is defined in terms of itself or of its type. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in mathematic ...
until a certain task granularity is reached. Fork–join can be considered a parallel
design pattern A design pattern is the re-usable form of a solution to a design problem. The idea was introduced by the architect Christopher Alexander and has been adapted for various other disciplines, particularly software engineering. The " Gang of Four" b ...
. It was formulated as early as 1963. By nesting fork–join computations recursively, one obtains a parallel version of the
divide and conquer Divide and rule policy ( la, divide et impera), or divide and conquer, in politics and sociology is gaining and maintaining power divisively. Historically, this strategy was used in many different ways by empires seeking to expand their terr ...
paradigm, expressed by the following generic
pseudocode In computer science, pseudocode is a plain language description of the steps in an algorithm or another system. Pseudocode often uses structural conventions of a normal programming language, but is intended for human reading rather than machine re ...
: solve(problem): if problem is small enough: solve problem directly (sequential algorithm) else: for part in subdivide(problem) fork subtask to solve(part) join all subtasks spawned in previous loop return combined results


Examples

The simple parallel
merge sort In computer science, merge sort (also commonly spelled as mergesort) is an efficient, general-purpose, and comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the order of equal elements is the same ...
of CLRS is a fork–join algorithm. mergesort(A, lo, hi): if lo < hi: ''// at least one element of input'' mid = ⌊lo + (hi - lo) / 2⌋ fork mergesort(A, lo, mid) ''// process (potentially) in parallel with main task'' mergesort(A, mid, hi) ''// main task handles second recursion'' join merge(A, lo, mid, hi) The first recursive call is "forked off", meaning that its execution may run in parallel (in a separate thread) with the following part of the function, up to the that causes all threads to synchronize. While the may look like a barrier, it is different because the threads will continue to work after a barrier, while after a only one thread continues. The second recursive call is not a fork in the pseudocode above; this is intentional, as forking tasks may come at an expense. If both recursive calls were set up as subtasks, the main task would not have any additional work to perform before being blocked at the .


Implementations

Implementations of the fork–join model will typically fork ''tasks'', ''fibers'' or ''lightweight threads'', not operating-system-level "heavyweight"
threads Thread may refer to: Objects * Thread (yarn), a kind of thin yarn used for sewing ** Thread (unit of measurement), a cotton yarn measure * Screw thread, a helical ridge on a cylindrical fastener Arts and entertainment * ''Thread'' (film), 2016 ...
or processes, and use a
thread pool In computer programming, a thread pool is a software design pattern for achieving concurrency of execution in a computer program. Often also called a replicated workers or worker-crew model, a thread pool maintains multiple threads waiting for ...
to execute these tasks: the fork primitive allows the programmer to specify ''potential'' parallelism, which the implementation then maps onto actual parallel execution. The reason for this design is that creating new threads tends to result in too much overhead. The lightweight threads used in fork–join programming will typically have their own scheduler (typically a work stealing one) that maps them onto the underlying thread pool. This scheduler can be much simpler than a fully featured, preemptive operating system scheduler: general-purpose thread schedulers must deal with blocking for locks, but in the fork–join paradigm, threads only block at the join point. Fork–join is the main model of parallel execution in the
OpenMP OpenMP (Open Multi-Processing) is an application programming interface (API) that supports multi-platform shared-memory multiprocessing programming in C, C++, and Fortran, on many platforms, instruction-set architectures and operating sy ...
framework, although OpenMP implementations may or may not support nesting of parallel sections. It is also supported by the Java concurrency framework, the Task Parallel Library for .NET, and Intel's Threading Building Blocks (TBB). The
Cilk Cilk, Cilk++, Cilk Plus and OpenCilk are general-purpose programming languages designed for multithreaded parallel computing. They are based on the C and C++ programming languages, which they extend with constructs to express parallel loo ...
programming language has language-level support for fork and join, in the form of the spawn and sync keywords, or cilk_spawn and cilk_sync in Cilk Plus.


See also

*
MapReduce MapReduce is a programming model and an associated implementation for processing and generating big data sets with a parallel, distributed algorithm on a cluster. A MapReduce program is composed of a ''map'' procedure, which performs filteri ...
*
Task parallelism Task parallelism (also known as function parallelism and control parallelism) is a form of parallelization of computer code across multiple processors in parallel computing environments. Task parallelism focuses on distributing tasks—concurre ...
* Work stealing


References


External links


A Primer on Scheduling Fork–Join Parallelism with Work Stealing


{{DEFAULTSORT:Fork-join Model Parallel computing