【问题标题】:How to compile code with std::reduce and #include <execution_policy> using g++6?如何使用 g++6 编译带有 std::reduce 和 #include <execution_policy> 的代码?
【发布时间】:2016-12-10 23:13:31
【问题描述】:

谁能告诉我如何编译std::reduce 的示例代码,它位于cppreference 上?

#include <iostream>
#include <chrono>
#include <vector>
#include <numeric>
#include <execution_policy>

int main()
{
    std::vector<double> v(10'000'007, 0.5);

    {
        auto t1 = std::chrono::high_resolution_clock::now();
        double result = std::accumulate(v.begin(), v.end(), 0.0);
        auto t2 = std::chrono::high_resolution_clock::now();
        std::chrono::duration<double, std::milli> ms = t2 - t1;
        std::cout << std::fixed << "std::accumulate result " << result
                  << " took " << ms.count() << " ms\n";
    }

    {
        auto t1 = std::chrono::high_resolution_clock::now();
        double result = std::reduce(std::par, v.begin(), v.end());
        auto t2 = std::chrono::high_resolution_clock::now();
        std::chrono::duration<double, std::milli> ms = t2 - t1;
        std::cout << "std::reduce result "
                  << result << " took " << ms.count() << " ms\n";
    }
}

我在安装了新的 g++6.2 的 Mint18 下工作。在编译期间,我只使用 -std=c++17 标志。如果我想使用execution_policy#include &lt;execution_policy&gt;)怎么办?目前我的编译器告诉我reduce 不是命名空间std 的成员,并且没有execution_policy 这样的文件。

【问题讨论】:

  • 大多数库供应商尚未实现许多 C++17 功能。您不必等待太久。
  • 当你在等待 libstdc++ 赶上来时,已经有HPX了。
  • @ildjarn 这是我在编写该示例时使用的(然后发布了它应该在 C++ 中的外观。现在已编辑以匹配当前草稿)

标签: c++ stl c++17


【解决方案1】:

libstdc++(默认与 g++ 一起使用)和 libc++(默认与 clang 一起使用)都没有实现 execution_policy 支持。

this page 上跟踪了在 libstdc++ 中实现新功能的进展。跟踪 libc++ 进度here。如您所见,截至撰写本文时,这两个库尚未实现 P0024R2“并行 TS 应标准化”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-16
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-12
    • 2015-11-05
    • 2017-08-13
    相关资源
    最近更新 更多