Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
130 views
in Technique[技术] by (71.8m points)

c++ - Difference between execution policies and when to use them

I noticed that a majority (if not all) functions in <algorithm> are getting one or more extra overloads. All of these extra overloads add a specific new parameter, for example, std::for_each goes from:

template< class InputIt, class UnaryFunction >
UnaryFunction for_each( InputIt first, InputIt last, UnaryFunction f );

to:

template< class ExecutionPolicy, class InputIt, class UnaryFunction2 >
void for_each( ExecutionPolicy&& policy, InputIt first, InputIt last, UnaryFunction2 f );

What effect does this extra ExecutionPolicy have on these functions?

What are the differences between:

  • std::execution::seq
  • std::execution::par
  • std::execution::par_unseq

And when to use one or the other?

question from:https://stackoverflow.com/questions/39954678/difference-between-execution-policies-and-when-to-use-them

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

seq means "execute sequentially" and is the exact same thing as the version without an execution policy.

par means "execute in parallel", which permits the implementation to execute on multiple threads in parallel. You are responsible for making sure that no data races happen within f.

par_unseq means that in addition to being allowed to execute in multiple threads, the implementation is also allowed to interleave individual loop iterations within a single thread, i.e. load multiple elements and execute f on all of them only afterwards. This is required to permit a vectorized implementation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...