Why does this code not parallelize std::for_each() when it works perfectly fine with std::sort()?
How do I fix it?
g++ -fopenmp -D_GLIBCXX_PARALLEL=1 -o p p.cc && time ./p sort
GCC 4.3 on Linux.
#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);
}
}