• <i id='SQoUd'><tr id='SQoUd'><dt id='SQoUd'><q id='SQoUd'><span id='SQoUd'><b id='SQoUd'><form id='SQoUd'><ins id='SQoUd'></ins><ul id='SQoUd'></ul><sub id='SQoUd'></sub></form><legend id='SQoUd'></legend><bdo id='SQoUd'><pre id='SQoUd'><center id='SQoUd'></center></pre></bdo></b><th id='SQoUd'></th></span></q></dt></tr></i><div id='SQoUd'><tfoot id='SQoUd'></tfoot><dl id='SQoUd'><fieldset id='SQoUd'></fieldset></dl></div>
    • <bdo id='SQoUd'></bdo><ul id='SQoUd'></ul>
  • <legend id='SQoUd'><style id='SQoUd'><dir id='SQoUd'><q id='SQoUd'></q></dir></style></legend>

    <small id='SQoUd'></small><noframes id='SQoUd'>

    <tfoot id='SQoUd'></tfoot>

        在 openmp 中迭代 std 容器

        时间:2024-05-11
          <tbody id='7FO5a'></tbody>

        <small id='7FO5a'></small><noframes id='7FO5a'>

      1. <i id='7FO5a'><tr id='7FO5a'><dt id='7FO5a'><q id='7FO5a'><span id='7FO5a'><b id='7FO5a'><form id='7FO5a'><ins id='7FO5a'></ins><ul id='7FO5a'></ul><sub id='7FO5a'></sub></form><legend id='7FO5a'></legend><bdo id='7FO5a'><pre id='7FO5a'><center id='7FO5a'></center></pre></bdo></b><th id='7FO5a'></th></span></q></dt></tr></i><div id='7FO5a'><tfoot id='7FO5a'></tfoot><dl id='7FO5a'><fieldset id='7FO5a'></fieldset></dl></div>

          • <legend id='7FO5a'><style id='7FO5a'><dir id='7FO5a'><q id='7FO5a'></q></dir></style></legend>
              <bdo id='7FO5a'></bdo><ul id='7FO5a'></ul>
                <tfoot id='7FO5a'></tfoot>
                • 本文介绍了在 openmp 中迭代 std 容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 openmp 通过 std::set 对循环进行多线程处理.当我编写以下代码时 -

                  I'm trying to use openmp to multithread a loop through std::set. When I write the following code -

                      #pragma omp parallel for
                      for (std::set<A>::const_iterator i = s.begin(); i != s.end(); ++i) {
                              const A a = *i;
                              operate(a);
                      }
                  

                  我收到此错误:

                  error: invalid type for iteration variable 'i'
                  error: invalid controlling predicate
                  error: invalid increment expression.
                  

                  是否有另一种正确的方法来使用 openmp 迭代 std 容器?我知道我可以使用 int i 并从 0 迭代到 s.size() 和迭代器或 operator[] 在循环体中,但这看起来不那么干净.

                  Is there an another, correct way to iterate through std containers using openmp? I know I can use int i and iterate from 0 to s.size() and an iterator or operator[] in the loop body, but this looks much less clean.

                  推荐答案

                  stl 迭代器的循环并行化仅适用于 OpenMP 3.0 以来,并且仅适用于随机访问迭代器(例如 vectordeque).你应该能够做这样的事情:

                  Loop parallelization for stl iterators only works since OpenMP 3.0, and only for random access iterators (e.g. vector and deque). You should be able to do something like this:

                  #pragma omp parallel {
                     for (std::set<A>::const_iterator i = s.begin(); i != s.end(); ++i) {
                        #pragma omp single nowait {
                           operate(*i);
                        }
                     }
                  }
                  

                  开销相当大,因为每个线程遍历整个序列(但只对其中的一些执行operate).您使用 int i 的方法效率更高.

                  Overhead is quite big though because each thread iterates over the whole sequence (but only executes operate on some of it). Your method using an int i is more efficient.

                  作为替代,看看 GCC 对 std 的并行实现::for_each.看我的评论.

                  As an alternative take a look at GCC's parallel implementation of std::for_each. See my comment.

                  编辑:STL Parallism TS,它将很可能是 C++17 的一部分,将来可能是迭代标准容器的不错选择.

                  EDIT: The STL Parallism TS, which will most likely be part of C++17, might be a good option in the future for iterating over standard containers.

                  这篇关于在 openmp 中迭代 std 容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  • <bdo id='YG05S'></bdo><ul id='YG05S'></ul>
                  • <i id='YG05S'><tr id='YG05S'><dt id='YG05S'><q id='YG05S'><span id='YG05S'><b id='YG05S'><form id='YG05S'><ins id='YG05S'></ins><ul id='YG05S'></ul><sub id='YG05S'></sub></form><legend id='YG05S'></legend><bdo id='YG05S'><pre id='YG05S'><center id='YG05S'></center></pre></bdo></b><th id='YG05S'></th></span></q></dt></tr></i><div id='YG05S'><tfoot id='YG05S'></tfoot><dl id='YG05S'><fieldset id='YG05S'></fieldset></dl></div>

                      <small id='YG05S'></small><noframes id='YG05S'>

                          <tbody id='YG05S'></tbody>
                      • <legend id='YG05S'><style id='YG05S'><dir id='YG05S'><q id='YG05S'></q></dir></style></legend>
                        <tfoot id='YG05S'></tfoot>