Iteration through std containers in openmp

后端 未结 1 1762
没有蜡笔的小新
没有蜡笔的小新 2020-12-03 03:15

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:         


        
相关标签:
1条回答
  • 2020-12-03 03:58

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

    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.

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

    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.

    0 讨论(0)
提交回复
热议问题