gpt4 book ai didi

c++ - HPX transform_reduce

转载 作者:行者123 更新时间:2023-11-30 03:18:40 27 4
gpt4 key购买 nike

我已经尝试使用来自 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>’

对于 Parallel reduce (e.g. sum) a vector of hpx::futures<double> 中提出的问题,是否有人提出了错误的建议或其他解决方案? ?

最佳答案

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;
}

关于c++ - HPX transform_reduce,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54504021/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com