我尝试使用答案 https://stackoverflow.com/a/54481320/11008404 中给出的 hpx transform_reduce,但我无法编译它。到目前为止我的代码:
#include <hpx/hpx_main.hpp>
#include <hpx/hpx.hpp>
#include <hpx/include/parallel_transform_reduce.hpp>
#include <hpx/include/iostreams.hpp>
#include <vector>
class A {
public:
double residual() {
// Calculate actual local residual
double i = 1.0;
return i;
}
};
int main() {
std::vector<A> vec(300);
double res = hpx::parallel::transform_reduce(hpx::parallel::execution::par,
vec.begin(), vec.end(), // (1)
[](A& a_ref){ return a_ref.residual(); }, // (2)
0, [](double a, double b){ return a + b; }); // (3)
hpx::cout << "residual: " << res << hpx::endl;
return 0;
}
编译器会引发此错误:
hpx.cpp:23:65: error: no matching function for call to ‘transform_reduce(const hpx::parallel::execution::parallel_policy&, std::vector<A>::iterator, std::vector<A>::iterator, main()::<lambda(A&)>, int, main()::<lambda(double, double)>)’
0, [](double a, double b){ return a + b; }); // (3)
.../include/hpx/parallel/algorithms/transform_reduce.hpp:255:22: error: no type named ‘type’ in ‘struct hpx::util::invoke_result<main()::<lambda(double, double)>, A>’
是否有人建议什么是错误的或针对并行中提出的问题的另一种解决方案,减少(例如求和(hpx::futures
transform_reduce
的签名在其标准化过程中发生了多次更改(请参阅此处了解实际标准化的内容:https://en.cppreference.com/w/cpp/algorithm/transform_reduce(。我认为为了编译,你只需要正确处理参数的顺序:
#include <hpx/hpx_main.hpp>
#include <hpx/hpx.hpp>
#include <hpx/include/parallel_transform_reduce.hpp>
#include <hpx/include/iostreams.hpp>
#include <vector>
class A {
public:
double residual() {
// Calculate actual local residual
double i = 1.0;
return i;
}
};
int main() {
std::vector<A> vec(300);
double res = hpx::parallel::transform_reduce(hpx::parallel::execution::par,
vec.begin(), vec.end(),
0.,
[](double a, double b){ return a + b; },
[](A& a_ref){ return a_ref.residual(); });
hpx::cout << "residual: " << res << hpx::endl;
return 0;
}
如果我将 hkaiser 的答案改为
#include <hpx/hpx_main.hpp>
#include <hpx/hpx.hpp>
#include <hpx/include/parallel_transform_reduce.hpp>
#include <hpx/include/iostreams.hpp>
#include <vector>
class A {
public:
double residual() const {
// Calculate actual local residual
double i = 1.0;
return i;
}
};
int main() {
std::vector<A> vec(300);
double res = hpx::parallel::transform_reduce(hpx::parallel::execution::par,
vec.begin(), vec.end(),
0.,
[](double a, double b){ return a + b; },
[](const A& a_ref){ return a_ref.residual(); }); // note: const!
hpx::cout << "residual: " << res << hpx::endl;
return 0;
}
代码编译。如果按值传递 A 或作为指针传递 A,它也会进行编译。
我不知道这种行为是否有意,所以我在HPX的github上打开了一个问题(https://github.com/STEllAR-GROUP/hpx/issues/3651(
补充一点,并行 STL 已经通过 -std=c++17
进入了 gcc 9,只需要与 -ltbb
链接(即英特尔的线程构建块,例如使用 apt
即可轻松安装在 Linux 上(。
#include <numeric>
#include <execution>
#include <vector>
class A {
public:
double residual() {
// Calculate actual local residual
double i = 1.0;
return i;
}
};
int main() {
std::vector<A> vec(300);
double res = std::transform_reduce(std::execution::par,
vec.begin(), vec.end(),
0.,
[](double a, double b){ return a + b; },
[](A& a_ref){ return a_ref.residual(); });
std::cout << "residual: " << res << std::endl;
return 0;
}