3
爲什麼此代碼不能並行std :: for_each()時,它完全正常工作與std :: sort()?並行for_each使用openmp
我該如何解決?
g++ -fopenmp -D_GLIBCXX_PARALLEL=1 -o p p.cc && time ./p sort
Linux上的GCC 4.3。
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
void delay()
{
for(int c = 0; c < 1000000; c++) {
}
}
int lt(int a, int b)
{
delay();
return a < b;
}
void noop(int a)
{
delay();
}
int main(int argc, char **argv)
{
if (argc < 2) {
printf("%s <sort | for_each>\n", argv[0]);
return 1;
}
std::vector<int> foo(10000);
if (!strcmp(argv[1], "sort")) {
std::sort(foo.begin(), foo.end(), lt);
} else if (!strcmp(argv[1], "for_each")) {
std::for_each(foo.begin(), foo.end(), noop);
}
}
我知道Big-Oh,它只是讓它足夠慢而能看到「top」和「time」。 – Thomas 2010-01-14 21:34:28
您的配置和調整鏈接幫助。這使得for_each()並行: std :: for_each(foo.begin(),foo.end(),noop,_gnu_parallel :: parallel_balanced); – Thomas 2010-01-14 21:35:31