首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HPX transform_reduce

HPX transform_reduce
EN

Stack Overflow用户
提问于 2019-02-03 14:47:18
回答 3查看 267关注 0票数 1

我尝试使用来自hpx的transform_reduce,如答案https://stackoverflow.com/a/54481320/11008404所示,但我无法编译它。到目前为止我的代码是:

代码语言:javascript
复制
#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;
}

编译器抛出此错误:

代码语言:javascript
复制
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的向量::期货中提出的问题有什么建议或另一种解决办法吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-02-04 08:54:07

如果我把hkaiser的答案改为

代码语言:javascript
复制
#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)上打开了一个问题。

票数 1
EN

Stack Overflow用户

发布于 2019-02-03 23:56:05

transform_reduce的标准化过程中,它的签名发生了几次变化(请参阅这里的实际标准化:reduce)。我认为,为了编译,您只需要正确地确定参数序列:

代码语言:javascript
复制
#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;
}
票数 2
EN

Stack Overflow用户

发布于 2019-08-26 09:16:59

我想补充的是,并行的STL已经进入了gcc 9-std=c++17,只需要与-ltbb链接(即英特尔的线程构建模块,易于在Linux上安装,例如使用apt)。

代码语言:javascript
复制
#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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54504021

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档