STL算法和并发编程

发布于 2024-08-27 11:37:38 字数 143 浏览 11 评论 0 原文

如果我为编译器启用 OpenMP,是否可以并行执行任何 STL 算法/容器操作(例如 std::fillstd::transform)?我目前正在使用 MSVC 2008。 或者也许还有其他方法可以使其并发?

谢谢。

Can any of STL algorithms/container operations like std::fill, std::transform be executed in parallel if I enable OpenMP for my compiler? I am working with MSVC 2008 at the moment.
Or maybe there are other ways to make it concurrent?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

恬淡成诗 2024-09-03 11:37:39

有许多项目旨在拥有并行 STL 类型库:

  1. OpenMP 多线程模板库
  2. < a href="http://gcc.gnu.org/onlinedocs/libstdc++/manual/parallel_mode.html" rel="noreferrer">libstdc++ 并行
  3. HPC++ 并行标准模板库
  4. 并行模式库(无耻地借用了 AshleysBrain 的答案)

There are a number of projects that aim at having parallel STL type libraries:

  1. OpenMP Multi-Threaded Template Library
  2. libstdc++ parallel
  3. HPC++ Parallel Standard Template Library
  4. Parallel Patterns Library (shamelessly borrowed from AshleysBrain's answer)
放赐 2024-09-03 11:37:39

为了保证 std::transform 和 std::fill 是并行安全的,您必须编写自己的版本。这些函数的常见实现是顺序执行。

让我们以 std::fill 为例。在转换为并行时,您需要将函数分解为更小的函数,这些函数可以异步执行而无需任何相互依赖。例如,一个子功能可以填充前半部分,第二个子功能可以填充后半部分。父函数必须委托(分叉)两个子函数,并等待它们完成(加入)。

更大的问题是,并行执行的运行时准备所花费的开销是否能够弥补实际的并行执行时间。大填充比小填充具有更高的合理性。

也许更好的想法是制作这些函数的线程安全版本并让线程并行执行,而不是拆分函数。

在将事物拆分为多个线程之前,首先尝试参考数据进行优化。在网络上搜索面向数据的设计。文章表明,通过优化执行来减少处理器缓存未命中,程序可以运行得更快。

To guarantee std::transform and std::fill to be parallel-safe, you will have to write your own version. The common implementation of these functions is for sequential execution.

Let us take std::fill as a simple example. In converting to parallel, you will need to break up the function into smaller functions that can be executed asynchronously without any inter-dependencies. For example, one sub-function could fill the first half and the second sub-function could fill the second half. The parent function would have to delegate (fork) the two sub-functions, and wait for them to finish (join).

The bigger question is whether or not the overhead spent in run-time preparation for parallel execution can make up for the actual parallel execution time. Large fills would have a higher justification than smaller fills.

Perhaps a better idea is to make thread-safe versions of these functions and have the threads execute in parallel, rather than splitting up the functions.

Before splitting things into multiple threads, first try optimizing in reference to the data. Search the web for Data Oriented Design. Articles have shown that by optimizing execution to reduce processor cache misses, a program can run significantly faster.

扛刀软妹 2024-09-03 11:37:39

当前的 C++ 标准根本不讨论线程,所以没有。 这里或多或少是关于STL线程安全的原始声明。

编辑:

看一下 std::fill 的一种常见 (GCC) 实现:

template<typename _ForwardIter, typename _Tp>
  void
  fill(_ForwardIter __first, _ForwardIter __last, const _Tp& __value)
  {
      for ( ; __first != __last; ++__first)
          *__first = __value; 
  }

很明显,并行执行不安全(这需要专门的实现。)

这是 并行模式

Current C++ standards don't talk about threads at all, so no. Here is more or less original statement about STL thread safety.

Edit:

Look at one common (GCC) implementation of std::fill:

template<typename _ForwardIter, typename _Tp>
  void
  fill(_ForwardIter __first, _ForwardIter __last, const _Tp& __value)
  {
      for ( ; __first != __last; ++__first)
          *__first = __value; 
  }

It's obvious that it's not safe for parallel execution (which would require specialized implementation.)

And here is GCC extension for Parallel Mode.

辞旧 2024-09-03 11:37:39

Visual Studio 2010 提供并行模式库它具有并行执行的 STL 风格算法。当然,这是 Microsoft 特定于 VS2010(我猜是更高版本)的。

Visual Studio 2010 provides the Parallel Patterns Library which has STL style algorithms which execute in parallel. Of course, this is Microsoft-specific to VS2010 (and up, I guess).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文