我的并行算法图数据结构具有以下迭代器方法:
/**
* Iterate in parallel over all nodes of the graph and call handler (lambda closure).
*/
void Graph::parallelForNodes(std::function<void(node)> handle) {
#pragma omp parallel for
for (node v = 0; v < z; ++v) {
// call here
handle(v);
}
}
我本可以将该handle
函数声明为模板参数,但我认为 C++11 中的首选方式是使用std::function
.
现在我想使用带有这样一个迭代器的 OpenMP 执行并行缩减。每次调用的返回值handle
都减少为一个总和。使用函数模板,这看起来像:
template<typename L>
inline double Graph::parallelSumForNodes(L handle) {
double sum = 0.0;
#pragma omp parallel for reduction(+:sum)
for (node v = 0; v < z; ++v) {
// call here
if (exists[v]) {
sum += handle(v);
}
}
return sum;
}
using 的等价物是什么std::function
?我可以定义handle
返回 double 或 int 的类型吗(因为函数体同时适用于两者)。