C ++ 2011:std :: thread:简单的例子来并行化一个循环?
C ++ 2011包含了非常酷的新功能,但是我找不到很多并行化for循环的例子。
所以我非常幼稚的问题是:你如何并行化一个简单的for循环(如使用omp parallel for)与std :: thread? (我搜索一个例子)。
C++ 2011 includes very cool new features, but I can't find a lot of example to parallelize a for-loop. So my very naive question is : how do you parallelize a simple for loop (like using "omp parallel for") with std::thread ? (I search for an example).
非常感谢。
std :: thread
不一定意味着将环路解析。它的意思是低层抽象构建像一个parallel_for算法的结构。如果你想将你的循环并置,你应该自己编写一个parallel_for算法或使用现有的库提供基于任务的parallism。
std::thread
is not necessarily meant to parallize loops. It is meant to be the lowlevel abstraction to build constructs like a parallel_for algorithm. If you want to parallize your loops, you should either wirte a parallel_for algorithm yourself or use existing libraires which offer task based parallism.
以下示例显示了如何将一个简单循环并行化,但另一方面也显示了缺点,如缺少负载平衡和简单循环的复杂性。
The following example shows how you could parallize a simple loop but on the other side also shows the disadvantages, like the missing load-balancing and the complexity for a simple loop.
typedef std::vector<int> container;
typedef container::iterator iter;
container v(100, 1);
auto worker = [] (iter begin, iter end) {
for(auto it = begin; it != end; ++it) {
*it *= 2;
}
};
// serial
worker(std::begin(v), std::end(v));
std::cout << std::accumulate(std::begin(v), std::end(v), 0) << std::endl; // 200
// parallel
std::vector<std::thread> threads(8);
const int grainsize = v.size() / 8;
auto work_iter = std::begin(v);
for(auto it = std::begin(threads); it != std::end(threads) - 1; ++it) {
*it = std::thread(worker, work_iter, work_iter + grainsize);
work_iter += grainsize;
}
threads.back() = std::thread(worker, work_iter, std::end(v));
for(auto&& i : threads) {
i.join();
}
std::cout << std::accumulate(std::begin(v), std::end(v), 0) << std::endl; // 400
使用库提供 parallel_for
template,可以简化为
Using a library which offers a parallel_for
template, it can be simplified to
parallel_for(std::begin(v), std::end(v), worker);