Parallel Programming with C++

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.

Algorithm

Implementation

adjacent_difference

parallel, unsequenced

adjacent_find

parallel, unsequenced

all_of

parallel, unsequenced

any_of

parallel, unsequenced

copy

parallel, unsequenced

copy_if

parallel, unsequenced

copy_n

parallel, unsequenced

count

parallel, unsequenced

count_if

parallel, unsequenced

destroy

parallel, unsequenced

destroy_n

parallel, unsequenced

equal

parallel, unsequenced

exclusive_scan

parallel, unsequenced

fill

parallel, unsequenced

fill_n

parallel, unsequenced

find

parallel, unsequenced

find_end

parallel, unsequenced

find_first_of

parallel, unsequenced

find_if

parallel, unsequenced

find_if_not

parallel, unsequenced

for_each

parallel, unsequenced

for_each_n

parallel, unsequenced

generate

parallel, unsequenced

generate_n

parallel, unsequenced

includes

parallel

inclusive_scan

parallel, unsequenced

inplace_merge

parallel

is_heap

parallel, unsequenced

is_heap_until

parallel, unsequenced

is_partitioned

parallel, unsequenced

is_sorted

parallel, unsequenced

is_sorted_until

parallel, unsequenced

lexicographical_compare

parallel, unsequenced

max_element

parallel, unsequenced

merge

parallel

min_element

parallel, unsequenced

minmax_element

parallel, unsequenced

mismatch

parallel, unsequenced

move

parallel, unsequenced

none_of

parallel, unsequenced

nth_element

parallel

partial_sort

parallel

partial_sort_copy

parallel

partition

parallel

partition_copy

parallel, unsequenced

reduce

parallel, unsequenced

remove

parallel, unsequenced

remove_copy

parallel, unsequenced

remove_copy_if

parallel, unsequenced

remove_if

parallel, unsequenced

replace

parallel, unsequenced

replace_copy

parallel, unsequenced

replace_copy_if

parallel, unsequenced

replace_if

parallel, unsequenced

reverse

parallel, unsequenced

reverse_copy

parallel, unsequenced

rotate

parallel, unsequenced

rotate_copy

parallel, unsequenced

search

parallel, unsequenced

search_n

parallel, unsequenced

set_difference

 

set_intersection

 

set_symmetric_difference

 

set_union

 

sort

parallel

stable_sort

parallel

stable_partition

parallel

swap_ranges

parallel, unsequenced

transform

parallel, unsequenced

transform_exclusive_scan

parallel, unsequenced

transform_inclusive_scan

parallel, unsequenced

transform_reduce

parallel, unsequenced

uninitialized_copy

parallel, unsequenced

uninitialized_copy_n

parallel, unsequenced

uninitialized_default_construct

parallel, unsequenced

uninitialized_default_construct_n

parallel, unsequenced

uninitialized_fill

parallel, unsequenced

uninitialized_fill_n

parallel, unsequenced

uninitialized_move

parallel, unsequenced

uninitialized_move_n

parallel, unsequenced

uninitialized_value_construct

parallel, unsequenced

uninitialized_value_construct_n

parallel, unsequenced

unique

parallel

unique_copy

parallel, unsequenced

References

Another Techs


© 2022 Another Techs. All rights reserved.