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

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

最佳答案

如果我将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)

08-16 02:14