Search code examples
c++hpx

HPX transform_reduce


I've tried to use transform_reduce from hpx as given in the Answer https://stackoverflow.com/a/54481320/11008404 but I can't compile it. My code so far:

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

The compiler throws this errors:

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>’

Has someone a suggestion what is wrong or another solution for the question asked in Parallel reduce (e.g. sum) a vector of hpx::futures<double>?


Solution

  • If I change hkaiser's answer to

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

    the code compiles. It also compiles if you pass A by value or as a pointer.

    I don't know if this behaviour is intended so i opened an issue on HPX's github (https://github.com/STEllAR-GROUP/hpx/issues/3651)