执行策略与何时使用它们之间的区别 [英] Difference between execution policies and when to use them

查看:126
本文介绍了执行策略与何时使用它们之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到< algorithm> 中的大多数(如果不是全部)函数都出现了一个或多个额外的重载。所有这些额外的重载都会添加一个特定的新参数,例如, std :: for_each 来自:

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 );

至:

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

这笔额外的 ExecutionPolicy 有什么影响这些功能?

What effect does this extra ExecutionPolicy have on these functions?

之间有什么区别?


  • std :: execution :: seq

  • std :: execution :: par

  • std :: execution :: par_unseq

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

什么时候使用其中一个?

And when to use one or the other?

推荐答案

seq 意味着顺序执行,与没有执行策略的版本完全相同。

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

par 的意思是执行并行,这允许实现在多个线程上并行执行。您有责任确保在 f 之内没有发生数据争夺。

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 意味着除了允许在多个线程中执行之外,该实现还允许在单个线程内交错各个循环迭代,即加载多个元素并执行 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.

这篇关于执行策略与何时使用它们之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆