[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]
The Parallel Patterns Library (PPL) provides algorithms that concurrently perform work on collections of data. These algorithms resemble those provided by the Standard Template Library (STL).
The parallel algorithms are composed from existing functionality in the Concurrency Runtime. For example, the parallel_for algorithm uses a structured_task_group object to perform the parallel loop iterations. The parallel_for algorithm partitions work in an optimal way given the available number of computing resources.
This topic describes the following parallel algorithms in detail:
The parallel_for algorithm repeatedly performs the same task in parallel. The parallel_for algorithm partitions tasks in an optimum way for parallel execution. It also uses a work-stealing algorithm to balance these partitions when workloads are unbalanced. This algorithm is useful when you have a loop body that does not share resources among iterations of that loop.
The parallel_for algorithm has two overloaded versions. The first version takes a start value, an end value, and a work function (a lambda expression, function object, or function pointer). The second version takes a start value, an end value, a value by which to step, and a work function. The first version of this function uses 1 as the step value.
You can convert many for loops to use parallel_for. However, the parallel_for algorithm differs from the for statement in the following ways:
The parallel_for algorithm does not support arbitrary termination conditions. The parallel_for algorithm stops when the current value of the iteration variable is one less than _Last.
The _Index_type type parameter must be an integral type.
The loop iteration must be forward. The parallel_for algorithm throws an exception of type invalid_argument if the _Step parameter is less than 1.
Although the parallel_for algorithm does not support arbitrary termination conditions, you can use cancellation to stop all tasks. For more information about cancellation, see Cancellation in the PPL.
Like other parallel algorithms, parallel_for does not execute the tasks in a specific order.
Example
parallel_for_each Algorithm
The parallel_for_each algorithm performs tasks on an iterative container, such as those provided by the STL, in parallel. It uses the same partitioning logic that the parallel_for algorithm uses.
The parallel_for_each algorithm resembles the STL for_each algorithm, except that the parallel_for_each algorithm executes the tasks concurrently. Like other parallel algorithms, parallel_for_each does not execute the tasks in a specific order.
Although the parallel_for_each algorithm works on both forward iterators and random access iterators, it performs better with random access iterators.
Example
parallel_invoke Algorithm
The parallel_invoke algorithm executes a set of tasks in parallel. It does not return until each task has been completed. This algorithm is useful when you have several independent tasks that you want to execute at the same time.
The parallel_invoke algorithm takes as its parameters a series of work functions (lambda functions, function objects, or function pointers). The parallel_invoke algorithm is overloaded to take between two and ten parameters. Every function that you pass to parallel_invoke must take zero parameters.
Like other parallel algorithms, parallel_invoke does not execute the tasks in a specific order.
Example
- How to: Write a parallel_for Loop
Shows how to use the parallel_for algorithm to perform matrix multiplication.
- How to: Write a parallel_for_each Loop
Shows how to use the parallel_for_each algorithm with an STL container.
- How to: Use parallel_invoke to Call Functions in Parallel
Shows how to use the parallel_invoke algorithm to call multiple routines at the same time.
- Parallel Patterns Library (PPL)
Describes the PPL, which provides an imperative programming model that promotes scalability and ease-of-use for developing concurrent applications.
- Cancellation in the PPL
Explains the role of cancellation in the PPL, how to cancel parallel work, and how to determine when a task group is canceled.
parallel_for Function
parallel_for_each Function
parallel_invoke Function