Execution Policy of STL Algorithms in Modern C++The Standard Template Library (STL) in Modern C++ offers extensive algorithms that function on elemental sequences, including vectors, arrays, and lists. These algorithms work with different iterators and are implemented as template functions. These algorithms are built with the fundamentals of generic programming in mind, and they make extensive use of iterators to abstract away the underlying data structures. In the context of STL algorithms, the execution policy describes how the algorithm can run in parallel. This was added in C++17 to enable programmers to use parallelism in specific algorithms when handling big datasets. The following are the three execution policies: 1. Sequential execution:This policy, std::execution::seq (Sequential Execution), makes sure of that. The default execution policy is used if none is provided. It ensures that the algorithm will run in a single thread and that the execution order will match the input iterators' sequence. Syntax:It has the following syntax: Example:Let us take an example to illustrate the sequential execution in C++: Output: 2. Standard::Execution::Par (Parallel Execution):This policy permits parallel algorithm execution. Whether or not to use threads, vectorization, or any other parallelization technique is up to the implementation; the C++ Standard Library does not mandate how parallelism is achieved. Syntax:It has the following syntax: Example:Let us take an example to illustrate the parallel execution in C++: Output: 3. Parallel Unsequenced Execution, or std::execution::par_unseq, is a policy that is comparable to std::execution::par but permits vectorization of the algorithm. It implies that in addition to being able to run in parallel, the algorithm may also be vectorized for improved performance on individual iterations. Syntax: It has the following syntax: Example: Let us take an example to illustrate the parallel unsequenced execution in C++: Output: It's crucial to remember that not all algorithms can benefit from parallel execution when utilizing these execution policies, and the amount of performance gain will vary depending on the algorithm's nature, the size of the data, and the hardware architecture. Before selecting a parallel execution policy, performance should be profiled and measured as synchronization, and other factors may cause overhead that may be introduced by parallel execution. Example:Here's a simple example demonstrating the use of std::execution::par with std::for_each: Output: Explanation: In this example, the lambda function is applied to each vector element in parallel, doubling each element. Performance Comparison Between Execution Policies:A simple C++ program can be used to compare the performance variations between the execution policies, as demonstrated below: Output: Explanation: As you can see, the quickest execution policies are the unsequenced_policy due to vectorization. Parallel_policy comes next, and parallel_unsequenced_policy comes after that. Ultimately, you carried out the execution technique in the planned order. It's important to keep in mind that not every algorithm supports every execution policy and that depending on the execution policy chosen, some algorithms may perform differently. It's crucial to select the execution strategy that best suits the task's requirements and the hardware at hand. You should also test various strategies to find the best one for a particular task. Benefits of Execution Policy of STL Algorithms in Modern C++Developers can now manage the parallel execution of specific algorithms due to the addition of execution policies in STL algorithms in C++17. This feature has several advantages:
It's important to remember that not all algorithms benefit equally from parallelization and that the efficiency of parallel execution varies depending on several variables, including the algorithm's nature, size, and hardware architecture. Using performance profiling and careful thought to determine the best use of execution policies in a particular situation is advised. Next TopicFeclearexcept in C++ |