Parallel Programming with C++

/images/cpp-parallel-stl.jpg

C++17 - New Parallel Algorithm of STL

C++17 introduces parallel algorithms. However, there aren’t many implementations where the additional functionalities can be used.

The concept is straightforward. More than 100 algorithms are included in the Standard Template (STL) for searching, counting, and manipulating ranges and their constituents. 69 of them are overloaded in C++17, and a few new ones are added. A so-called execution policy can be used to invoke the overloaded and new algorithms. You can specify whether the method should run sequentially, parallelly, or parallel and vectorized by using the execution policy.

C++ Parallel Algorithm of STL

There are almost 100 algorithms in the Standard Template Library for finding, counting, and manipulating ranges and their elements. C++17 adds new overloads to 69 of them and adds new ones to others. A so-called execution policy can be used to launch overloaded and new algorithms. You can indicate whether the method should run sequentially, in parallel, or in parallel with vectorization using an execution policy. You must add the header <execution> if you want to use the execution policy.

Execution Policy of Parallel Algorithm

Three execution policies are defined in the C++17 standard:

  • std::execution::sequenced_policy : sequential execution
  • std::execution::parallel_policy : Parallel execution
  • std::execution::parallel_unsequenced_policy : Parallel and unsequenced execution

The important thing to remember is that the execution policies are permissions rather than obligations. Each library implementation may decide what and how much can be parallelized.

To use parallel algorithms, you need at least forward iterators.

Here is code snippet of sort algorithm which applies all execution policies.

#include <vector> //for vector
#include <algorithm> // for sort
#include <execution> // for parallel execution


int main() {

std::vector<int> vec  = {21,34,53,98,22,7,244,52,60,72,89,44,57};

//standart sequential sort
std::sort(vec.begin(),vec.end());

// sequential execution
std::sort(std::execution::seq,vec.begin(),vec.end());

// permittin parallel execution
std::sort(std::execution::par,vec.begin(),vec.end());

// permitting parallel and vectorized execution
std::sort(std::execution::par_unseq,vec.begin(),vec.end());

}

The example demonstrates that the classic variant of std::sort can still be used . Furthermore, in C++17, you can specify whether you want to utilise the sequential , parallel , or parallel and vectorized versions.

Exception

If an exception occurs while using an algorithm with an execution policy, the function std::terminate is invoked. The installed std::terminate::handler is called by std::terminate. As a result, the std::abort function is invoked by default, resulting in abnormal programme termination. The handling of exceptions distinguishes between the invocation of an algorithm without an execution policy and the invocation of an algorithm with a sequential std::execution::seq execution policy. The exception is propagated when the algorithm is invoked without an execution policy, and so the exception can be handled.

With C++17, 69 STL algorithms received new overloads, and new algorithms were introduced.

Algorithm

The table below shows whether parallel and unsequenced execution are supported by each of the C++17 algorithms that accept execution policies. The use of an unsupported algorithm and execution policy will result in sequential execution.

References


Written By

Hatim

Tech enthusiast and founder of InsightfulScript, passionately navigating the worlds of programming and machine learning