我尝试使用来自hpx的transform_reduce,如答案https://stackoverflow.com/a/54481320/11008404所示,但我无法编译它。到目前为止我的代码是:
#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的向量::期货中提出的问题有什么建议或另一种解决办法吗?
发布于 2019-02-04 08:54:07
如果我把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按值传递或作为指针传递,它也会编译。
我不知道这种行为是否有意,所以我在HPX的github (https://github.com/STEllAR-GROUP/hpx/issues/3651)上打开了一个问题。
发布于 2019-02-03 23:56:05
在transform_reduce的标准化过程中,它的签名发生了几次变化(请参阅这里的实际标准化: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;
}发布于 2019-08-26 09:16:59
我想补充的是,并行的STL已经进入了gcc 9和-std=c++17,只需要与-ltbb链接(即英特尔的线程构建模块,易于在Linux上安装,例如使用apt)。
#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;
}https://stackoverflow.com/questions/54504021
复制相似问题