OpenMP并行映射循环 [英] OpenMP Parallelizing for loop with map
问题描述
我正在尝试并行处理扫描std :: map的for循环.下面是我的玩具程序:
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;
}
编译命令:
icc -fopenmp foo.cpp
上面的代码块的输出是:
The output of the above code block is:
First for-loop with 1 threads
Second for-loop with 16 threads
为什么我不能并行化第一个for循环?
Why am I not able to parallelize the first for-loop?
推荐答案
std::map
不提供随机访问迭代器,仅提供常规的双向迭代器. OpenMP要求并行循环中的迭代器为随机访问类型.对于其他类型的迭代器,应改为使用显式任务:
std::map
does not provide random-access iterators, only the usual bi-directional iterator. OpenMP requires that the iterators in parallel loops are of random-access type. With other kind of iterators explicit tasks should be used instead:
#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开销将相对较高.
Note in that case a separate task is created for each member of the map. Since the task body is very computationally simple, the OpenMP overhead will be relatively high in that particular case.
这篇关于OpenMP并行映射循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!