Execution Policy of STL Algorithms in Modern C++

C++ algorithms are a set of pre-defined functions that can carry out numerous operations on containers, such as selections, vectors, and lists. These algorithms have actually a specified execution policy that identifies how they carry out and how they communicate with the underlying hardware.

The C++ 17 basic presents 3 brand-new execution policies and one policy was presented in C++ 20. These execution policies in C++ enable algorithms to be performed in various methods depending upon the requirements of the job and the hardware readily available. They are as follows:

  1. sexually transmitted disease:: execution:: sequenced_policy
  2. sexually transmitted disease:: execution:: parallel_policy
  3. sexually transmitted disease:: execution:: parallel_unsequenced_policy
  4. sexually transmitted disease:: execution:: unsequenced_policy

1. sexually transmitted disease:: execution:: sequenced_policy

This policy defines that the algorithm needs to carry out sequentially, i.e., without parallelization. When no execution policy is defined, the algorithms will be performed sequentially.

Syntax of sequenced_policy

 stlFunction ( sexually transmitted disease:: execution:: seq,  ... other_arguments ...);

We simply need to pass the execution policy things names as sexually transmitted disease:: execution:: seq as an argument to the supported STL function. The functions are currently overwhelmed to accept it.

Example of sequenced_policy

C++

#include << algorithm>>

#include << iostream>>

#include << vector>>

#include << execution>>

int primary()

{

sexually transmitted disease:: vector< v = {5, 2, 3, 1, 4}; sexually transmitted disease:: sort( sexually transmitted disease:: execution:: seq, v.begin(), v.end());

for

( vehicle i: v) sexually transmitted disease:: cout << < < i << < <

" "; return

0;} Output

5 4 3 2 1

In this example, we develop a vector of integers and after that arrange its aspects utilizing the sexually transmitted disease:: sort algorithm with the sexually transmitted disease:: execution:: seq policy. The outcome is an arranged vector with aspects {1, 2, 3, 4, 5}.

 Benefits of sequenced_policy

Simple and foreseeable.

Prevent information races.

  • Great for little jobs as parallel overhead does not exist.
  • Downsides of sequenced_policy
  • Not effective for big jobs.

2. sexually transmitted disease:: execution:: parallel_policy

  • This policy defines that the algorithm needs to carry out in parallel, i.e., utilizing several threads. The requirement does not define the variety of threads that must be utilized, however it needs to be more than one.

Syntax of parallel_policy

stlFunction

(

 sexually transmitted disease:: execution:: par,  ... other_arguments ...); The execution policy things sexually transmitted disease:: execution:: par is passed as the argument to the STL algorithm function. Example of parallel_policy

C++

#include << algorithm>>

#include << execution>>

#include << iostream>>

#include << vector>>

int primary()

{

sexually transmitted disease:: vector< v1 = {1, 2, 3, 4, 5};

sexually transmitted disease:: vector< v2( 5 );

sexually transmitted disease:: change( sexually transmitted disease:: execution:: par, v1.begin(), v1.end(), v2.begin(),

(

int x) {

return x * x;} );

[] for( int

i: v2) {

sexually transmitted disease:: cout << < < i << < < " " ;

}

return 0;}

Output 1 4 9 16 25

In this example, we develop 2 vectors of integers v1 and v2, and after that utilize the sexually transmitted disease:: change algorithm with the sexually transmitted disease:: execution:: par policy to square the aspects of v1 and shop the lead to v2. The outcome is a vector v2 with aspects {1, 4, 9, 16, 25}. Benefits of parallel_policy Faster execution for bigger jobs.

Ideal use of multi-core systems.

Downsides of parallel_policy

 Might present overhead.

Might not constantly be faster than consecutive execution due to this overhead.

Can present race conditions.

  • 3. sexually transmitted disease:: execution:: parallel_unsequenced_policy
  • This policy defines that the algorithm needs to carry out in parallel and might produce non-deterministic outcomes, i.e., the order in which the aspects are processed is not ensured. These execution policies are carried out utilizing a mix of software and hardware systems, such as threads and SIMD directions, to enhance the efficiency of the algorithms.

Syntax of parallel_unsequenced_policy

  • stlFunction
  • (
  • sexually transmitted disease:: execution:: par_unseq

,

... other_arguments ...

);

 This execution policy might consist of both parallelization and vectorization in contrast to paralled_policy which may just consist of parallel execution. Example of parallel_unsequenced_policy C++ #include << algorithm>> #include << iostream>> #include << vector>>

#include << execution>>

int

primary()

{

sexually transmitted disease:: vector< v = {1, 2, 3, 4, 5};

sexually transmitted disease:: for_each( sexually transmitted disease:: execution:: par_unseq, v.begin(),

v.end(),

( int x) {sexually transmitted disease:: cout << < < x << < <

" ";} );

return

0;[]} Output 1 2 3 4 5 In this example, we develop a vector of integers and after that utilize the sexually transmitted disease:: for_each algorithm with the sexually transmitted disease:: execution:: par_unseq policy to print its aspects in parallel and unordered. The outcome can be any permutation of the input vector, depending upon the order in which the aspects are processed. Benefits of parallel_unsequenced_policy

Faster execution for repeated operations. Can be utilized on hardware with vector directions. Downsides of parallel_unsequenced_policy

Not ideal for all jobs.

Might not be supported on all hardware.

 4. sexually transmitted disease:: execution:: unsequenced_policy

This policy defines that the execution of the algorithm might be vectorized, i.e,

performed on a single thread utilizing directions that run on several information products.

  • Syntax of unsequenced_policy
  • stlFunction

(

  • sexually transmitted disease:: execution:: unseq
  • ,

... other_arguments ...

); Example

of unsequenced_policy

 C++ #include << algorithm>> #include << iostream>> #include << vector>> #include << execution>> 

int

primary()

{

sexually transmitted disease:: vector< v = {1, 2, 3, 4, 5};

sexually transmitted disease:: for_each( sexually transmitted disease:: execution:: unseq, v.begin(),

v.end(),

(

int x) {sexually transmitted disease:: cout << < < x << < < " ";} );

return

0;}

Output[] 1 2 3 4 5 Benefits of unsequenced_policy Quick Execution on a single thread Prevents Race Conditions Downsides of unsequenced_policy

Some Hardware might not support vectorization. Non-Deterministic execution series. Efficiency Contrast in between Execution Policies

We can compare the efficiency distinction in between the execution policies utilizing an easy C++ program as revealed listed below:

C++

 #include << chrono>>

#include << execution>>

  • #include << iostream>>
  • #include << vector>>

  • space

execTime(

vehicle

policy_type, sexually transmitted disease:: vector< & num,(* )

sexually transmitted disease:: string pType_name)

{

vehicle

start_time

= sexually transmitted disease:: chrono:: high_resolution_clock:: now(); long long

amount = 0;

sexually transmitted disease:: for_each( policy_type, num.begin(), num.end(),

(

int n) {amount += n;} );

vehicle

end_time

= sexually transmitted disease:: chrono:: high_resolution_clock:: now();

[&] vehicle taken_time = sexually transmitted disease:: chrono:: duration_cast<(

end_time - start_time)

count();

sexually transmitted disease:: cout << < < pType_name

<< < < " execution time: "

<< < < taken_time << < <

" msn";

}

int

primary()

{

int size = 9999999;

sexually transmitted disease:: vector< num( size); for

(

int i = 0; i < < size; i++) {

num

= i;

}

execTime( sexually transmitted disease:: execution:: seq, num, " Sequenced"

);

execTime( sexually transmitted disease:: execution:: unseq, num, " Unsequenced");

execTime( sexually transmitted disease:: execution:: par, num, " Parallel"[i]);

execTime( sexually transmitted disease:: execution:: par_unseq, num,

" Parallel Unsequenced"

);

return

0;} Output Sequenced execution time: 917ms . Unsequenced execution time: 406ms .
Parallel execution time: 897ms .
Parallel Unsequenced execution time: 420ms

As we can see, of all the execution policies, the unsequenced_policy is the fastest due to the fact that of vectorization. Then comes parallel_unsequenced_policy followed by the parallel_policy. At last, we sequenced the execution approach as anticipated. Note: The above code can just be performed utilizing C++ 20 Requirement or above compiler. Conclusion

It deserves keeping in mind that not all algorithms support all execution policies, and some algorithms might have various efficiency attributes depending upon the execution policy utilized. It is very important to select the execution policy that finest fits the requirements of the job and the hardware readily available and to check various policies to identify the optimum one for a provided job. Frequently Asked Questions on Execution Policies for STL Algorithms

1. In which variation was the execution policies initially included C++ ISO Requirement? STL Algorithms execution policies were initially presented in C++ 17 Requirement and after that C++ 20 likewise included another type in the future. 2. Note the STL Algorithms that support execution policies.

Here is the complete list of C++ algorithms that support execution policies: sexually transmitted disease:: adjacent_difference

sexually transmitted disease:: adjacent_find sexually transmitted disease:: all_of sexually transmitted disease:: any_of

sexually transmitted disease:: copy

sexually transmitted disease:: copy_if

 sexually transmitted disease:: copy_n

sexually transmitted disease:: count

sexually transmitted disease:: count_if sexually transmitted disease:: equivalent

sexually transmitted disease:: fill

sexually transmitted disease:: fill_n

sexually transmitted disease:: discover

sexually transmitted disease:: find_end

sexually transmitted disease:: find_first_of

sexually transmitted disease:: discover if

sexually transmitted disease:: find_if_not

sexually transmitted disease:: produce sexually transmitted disease:: generate_n sexually transmitted disease:: consists of sexually transmitted disease:: inner_product
sexually transmitted disease:: inplace_merge sexually transmitted disease:: is_heap sexually transmitted disease:: is_heap_until sexually transmitted disease:: is_partitioned
sexually transmitted disease: is_sorted sexually transmitted disease:: is_sorted_until sexually transmitted disease: lexicographical_compare sexually transmitted disease:: max component
sexually transmitted disease:: combine sexually transmitted disease:: min_element sexually transmitted disease:: minmax_element sexually transmitted disease:: inequality
sexually transmitted disease relocation sexually transmitted disease:: none_of sexually transmitted disease:: nth_element sexually transmitted disease:: partial_sort
sexually transmitted disease partial_sort_copy sexually transmitted disease: partition sexually transmitted disease:: partition_copy sexually transmitted disease: eliminate
sexually transmitted disease:: remove_copy sexually transmitted disease: remove_copy_if sexually transmitted disease:: remove_if

sexually transmitted disease:: change

sexually transmitted disease:: replace_copy sexually transmitted disease: replace_copy_if sexually transmitted disease:: replace_if sexually transmitted disease: reverse
sexually transmitted disease:: reverse_copy sexually transmitted disease:: turn sexually transmitted disease:: rotate_copy sexually transmitted disease:: search
sexually transmitted disease:: search_n sexually transmitted disease:: set_difference sexually transmitted disease:: set_intersection sexually transmitted disease:: set_symmetric_difference
sexually transmitted disease:: set_union sexually transmitted disease:: sort sexually transmitted disease: stable_partition sexually transmitted disease:: stable_sort
sexually transmitted disease:: swap_ranges sexually transmitted disease:: change sexually transmitted disease:: uninitialized_copy sexually transmitted disease: uninitialized_copy_n
sexually transmitted disease:: uninitialized_fill sexually transmitted disease:: uninitialized_fill_n sexually transmitted disease:: special sexually transmitted disease:: unique_copy
Remember that the accessibility of these policies might differ depending upon the execution and the variation of the C++ requirement utilized.
Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: