HPX transform_reduce

时间:2019-02-03 14:47:18

标签: c++ hpx

我已经尝试使用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>中提出的问题有什么建议吗?或者其他解决方案?

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

我想补充一下,并行STL已通过-std=c++17进入 gcc 9 ,只需要与-ltbb(即{{3} },例如可以使用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;
}