I am trying to parallelize a for-loop which scans std::map. Below is my toy program:
#include <iostream>
#include <cstdio>
#include <map>
#include <string>
#include <cassert>
#include <omp.h>
#define NUM 100000
using namespace std;
int main()
{
omp_set_num_threads(16);
int realThreads = 0;
string arr[] = {"0", "1", "2"};
std::map<int, string> myMap;
for(int i=0; i<NUM; ++i)
myMap[i] = arr[i % 3];
string is[NUM];
#pragma omp parallel for
for(map<int, string>::iterator it = myMap.begin(); it != myMap.end(); it++)
{
is[it->first] = it->second;
if(omp_get_thread_num() == 0)
realThreads = omp_get_num_threads();
}
printf("First for-loop with %d threads\n", realThreads);
realThreads = 0;
#pragma omp parallel for
for(int i=0; i<NUM; ++i)
{
assert(is[i] == arr[i % 3]);
if(omp_get_thread_num() == 0)
realThreads = omp_get_num_threads();
}
printf("Second for-loop with %d threads\n", realThreads);
return 0;
}
Compilation command:
icc -fopenmp foo.cpp
The output of the above code block is:
First for-loop with 1 threads
Second for-loop with 16 threads
Why am I not able to parallelize the first for-loop?