我正在尝试并行化一个扫描std :: map的for循环。以下是我的玩具程序:
#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;
}
编译命令:
icc -fopenmp foo.cpp
上述代码块的输出为:
First for-loop with 1 threads
Second for-loop with 16 threads
为什么我无法并行化第一个for-loop?
答案 0 :(得分:3)
std::map
不提供随机访问迭代器,只提供通常的双向迭代器。 OpenMP要求并行循环中的迭代器是随机访问类型。对于其他类型的迭代器,应该使用显式任务:
#pragma omp parallel
{
#pragma omp master
realThreads = omp_get_num_threads();
#pragma omp single
for(map<int, string>::iterator it = myMap.begin(); it != myMap.end(); it++)
{
#pragma omp task
is[it->first] = it->second;
}
}
请注意,在这种情况下,会为地图的每个成员创建一个单独的任务。由于任务主体在计算上非常简单,因此在特定情况下OpenMP开销相对较高。