2

I wish to iterate through all the elements in a std::multimap in parallel using OpenMP.

I tried to compile the following code using g++-7 (7.2.0) and icpc (18.0.0 20170811), but both failed.

Is this possible? If so, how can I parallelize a for-loop through a C++ std::multimap using OpenMP?

#include <map>
#include <omp.h>

int main() {

    std::multimap<int,int> myMultimap;
    for (int i = 0; i < 10; ++i){
        myMultimap.insert(std::make_pair(i,i+1)); // create dummy contents
    }

    std::multimap<int, int>::const_iterator itr;

#pragma omp parallel for private (itr)
    for (itr = myMultimap.cbegin(); itr != myMultimap.cend(); ++itr) {
        // do something here
    }

    return 0;
}
Pengin
  • 771
  • 1
  • 10
  • 16
  • 1
    Possible duplicate of [How do I parallelize a for loop through a C++ std::list using OpenMP?](https://stackoverflow.com/questions/8691459/how-do-i-parallelize-a-for-loop-through-a-c-stdlist-using-openmp) – Daniel Illescas Oct 01 '17 at 12:15
  • 1
    Are you sure you need a multimap? What do you do, that a sorted vector isn't an option? – ead Oct 01 '17 at 12:26
  • 1
    The standard forbids from using iterators that are not random access, so you can't use `multimap` iterators in an OpenMP for loop. – Massimiliano Oct 01 '17 at 16:21
  • Given that the correct answer is *"you can't"*, please provide more detail on the actual contents and operations if you want a helpful answer. – Zulan Oct 02 '17 at 08:06
  • Also, why OpenMP? Other techniques such as TBB https://www.threadingbuildingblocks.org/ are more C++ friendly. – Jim Cownie Oct 02 '17 at 08:42

0 Answers0