【问题标题】:Are C++17 Parallel Algorithms implemented already?是否已经实现了 C++17 并行算法?
【发布时间】:2018-12-04 11:26:54
【问题描述】:

我试图尝试使用 C++17 标准中提出的新并行库功能,但我无法让它工作。我尝试使用g++ 8.1.1clang++-6.0-std=c++17 的最新版本进行编译,但似乎都不支持#include <execution>std::execution::par 或类似的东西。

当查看cppreference 的并行算法时,有一长串算法,声称

技术规范提供了来自algorithmnumericmemory 的以下 69 种算法的并行版本:(...长列表...)

听起来算法已经准备好'在纸上',但还没有准备好使用?

在一年多前的this SO question 中,答案声称这些功能尚未实现。但到现在为止,我预计会看到某种实现。有什么我们可以使用的吗?

【问题讨论】:

  • 似乎MSVC是唯一支持这些功能的主要编译器,见here
  • 我也在寻找 g++ 的这些功能,但似乎还没有计划......

标签: c++ parallel-processing g++ c++17 clang++


【解决方案1】:

GCC 9 有,但您必须单独安装 TBB

在 Ubuntu 19.10 中,所有组件终于对齐:

  • GCC 9 is the default one,以及 TBB 所需的最低版本
  • TBB(Intel Thread Building Blocks)为 2019~U8-1,因此满足 2018 年的最低要求

所以你可以简单地做:

sudo apt install gcc libtbb-dev
g++ -ggdb3 -O3 -std=c++17 -Wall -Wextra -pedantic -o main.out main.cpp -ltbb
./main.out

并用作:

#include <execution>
#include <algorithm>

std::sort(std::execution::par_unseq, input.begin(), input.end());

另请参阅下面的完整可运行基准。

GCC 9 和 TBB 2018 是第一个工作的版本,如发行说明中所述:https://gcc.gnu.org/gcc-9/changes.html

并行算法和&lt;execution&gt;(需要 Thread Building Blocks 2018 或更高版本)。

相关话题:

Ubuntu 18.04 安装

Ubuntu 18.04 涉及更多:

以下是针对 Ubuntu 18.04 的全自动测试命令:

# Install GCC 9
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-9 g++-9

# Compile libtbb from source.
sudo apt-get build-dep libtbb-dev
git clone https://github.com/intel/tbb
cd tbb
git checkout 2019_U9
make -j `nproc`
TBB="$(pwd)"
TBB_RELEASE="${TBB}/build/linux_intel64_gcc_cc7.4.0_libc2.27_kernel4.15.0_release"

# Use them to compile our test program.
g++-9 -ggdb3 -O3 -std=c++17 -Wall -Wextra -pedantic -I "${TBB}/include" -L 
"${TBB_RELEASE}" -Wl,-rpath,"${TBB_RELEASE}" -o main.out main.cpp -ltbb
./main.out

测试程序分析

我已经用这个比较并行和串行排序速度的程序进行了测试。

main.cpp

#include <algorithm>
#include <cassert>
#include <chrono>
#include <execution>
#include <random>
#include <iostream>
#include <vector>

int main(int argc, char **argv) {
    using clk = std::chrono::high_resolution_clock;
    decltype(clk::now()) start, end;
    std::vector<unsigned long long> input_parallel, input_serial;
    unsigned int seed;
    unsigned long long n;

    // CLI arguments;
    std::uniform_int_distribution<uint64_t> zero_ull_max(0);
    if (argc > 1) {
        n = std::strtoll(argv[1], NULL, 0);
    } else {
        n = 10;
    }
    if (argc > 2) {
        seed = std::stoi(argv[2]);
    } else {
        seed = std::random_device()();
    }

    std::mt19937 prng(seed);
    for (unsigned long long i = 0; i < n; ++i) {
        input_parallel.push_back(zero_ull_max(prng));
    }
    input_serial = input_parallel;

    // Sort and time parallel.
    start = clk::now();
    std::sort(std::execution::par_unseq, input_parallel.begin(), input_parallel.end());
    end = clk::now();
    std::cout << "parallel " << std::chrono::duration<float>(end - start).count() << " s" << std::endl;

    // Sort and time serial.
    start = clk::now();
    std::sort(std::execution::seq, input_serial.begin(), input_serial.end());
    end = clk::now();
    std::cout << "serial " << std::chrono::duration<float>(end - start).count() << " s" << std::endl;

    assert(input_parallel == input_serial);
}

在 Ubuntu 19.10 上,带有 CPU 的 Lenovo ThinkPad P51 笔记本电脑:Intel Core i7-7820HQ CPU(4 核/8 线程,2.90 GHz 基础,8 MB 缓存),RAM:2x Samsung M471A2K43BB1-CRC(2x 16GiB,2400 Mbps)具有 1 亿个待排序数字的输入的典型输出:

./main.out 100000000

曾经:

parallel 2.00886 s
serial 9.37583 s

所以并行版本快了大约 4.5 倍!另见:What do the terms "CPU bound" and "I/O bound" mean?

我们可以用strace 确认进程正在生成线程:

strace -f -s999 -v ./main.out 100000000 |& grep -E 'clone'

其中显示了几行类型:

[pid 25774] clone(strace: Process 25788 attached
[pid 25774] <... clone resumed> child_stack=0x7fd8c57f4fb0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0x7fd8c57f59d0, tls=0x7fd8c57f5700, child_tidptr=0x7fd8c57f59d0) = 25788

另外,如果我注释掉串行版本并运行:

time ./main.out 100000000

我明白了:

real    0m5.135s
user    0m17.824s
sys     0m0.902s

which confirms again that the algorithm was parallelized since real < user,并介绍了它在我的系统中的并行化效率(大约 3.5 倍,8 核)。

错误消息

Google,请将此索引。

如果你没有安装tbb,错误是:

In file included from /usr/include/c++/9/pstl/parallel_backend.h:14,
                 from /usr/include/c++/9/pstl/algorithm_impl.h:25,
                 from /usr/include/c++/9/pstl/glue_execution_defs.h:52,
                 from /usr/include/c++/9/execution:32,
                 from parallel_sort.cpp:4:
/usr/include/c++/9/pstl/parallel_backend_tbb.h:19:10: fatal error: tbb/blocked_range.h: No such file or directory
   19 | #include <tbb/blocked_range.h>
      |          ^~~~~~~~~~~~~~~~~~~~~
compilation terminated.

所以我们看到&lt;execution&gt; 依赖于一个已卸载的 TBB 组件。

如果 TBB 太旧,例如默认的 Ubuntu 18.04 之一,它失败了:

#error Intel(R) Threading Building Blocks 2018 is required; older versions are not supported.

【讨论】:

  • 此处指定:gcc.gnu.org/onlinedocs/gcc-9.1.0/libstdc++/manual/manual/…注 3:并行算法对 Intel TBB 2018 或更高版本有外部依赖性。如果包含&lt;execution&gt; 标头,则必须使用-ltbb 链接到TBB。 似乎并行算法是由英特尔的Parallel STL 实现的:github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/…,它本身需要TBB。
  • @DanielLangr 谢谢!我的主要问题(现在懒得研究;-))是:TBB in-tree,如果是,我需要用任何额外的标志配置 GCC 构建,否则如何在 Ubuntu 上安装它。我不知道-ltbb,所以这已经是一个好的开始了:-) 然后我要对一些东西进行基准测试并在这里给出一个图表,这会很有趣!
  • 并行 STL 标头现在是 libstdc++ 的一部分。他们定义了一个并行后端the only one supported is TBB。 TBB 后端标头requires TBB headers。但是,我找不到 TBB 标头是 libstdc++ 的一部分。因此,这意味着不仅需要链接 TBB 库,还需要提供 TBB 头文件。在线demo支持:wandbox.org/permlink/VSIcdvWCtTRko43Q.
  • 顺便说一句,我更喜欢并行 STL 算法的独立实现,而不是 TBB 中的那些。我广泛使用并行排序并进行了大量实验,这些实验通常表明从 libstdc++ 并行模式 (PM) 实现并行快速排序和归并排序优于 TBB。不幸的是,PM 是建立在 OpenMP 之上的,并且通过 C++11 线程重新实现它绝非易事。
  • @DanielLangr 感谢您提供此信息!你一定在那边做一些有趣的事情,呵呵 ;-) 我稍后会尝试玩这个。如果您有任何开源基准代码,请分享链接。
【解决方案2】:

您可以参考https://en.cppreference.com/w/cpp/compiler_support 查看所有C++ 功能实现状态。对于您的情况,只需搜索“Standardization of Parallelism TS”,您会发现现在只有 MSVCIntel C++ 编译器支持此功能。

【讨论】:

【解决方案3】:

英特尔发布了一个遵循 C++17 标准的并行 STL 库:

being merged into GCC

【讨论】:

    【解决方案4】:

    Gcc 尚未实现 Parallelism TS(请参阅 https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2017

    但是 libstdc++(带有 gcc)对于一些等效的并行算法有一个实验模式。见https://gcc.gnu.org/onlinedocs/libstdc++/manual/parallel_mode.html

    让它工作:

    任何并行功能的使用都需要额外的编译器和 运行时支持,特别是对 OpenMP 的支持。添加此支持 并不难:只需使用编译器标志编译您的应用程序 -fopenmp。这将链接到 libgomp,即 GNU 卸载和多处理运行时库,它的存在是强制性的。

    代码示例

    #include <vector>
    #include <parallel/algorithm>
    
    int main()
    {
      std::vector<int> v(100);
    
      // ...
    
      // Explicitly force a call to parallel sort.
      __gnu_parallel::sort(v.begin(), v.end());
      return 0;
    }
    

    【讨论】:

      【解决方案5】:

      Gcc 现在支持执行标头,但不支持来自 https://apt.llvm.org 的标准 clang 构建

      【讨论】:

        猜你喜欢
        • 2011-02-22
        • 2021-03-04
        • 2019-05-16
        • 1970-01-01
        • 1970-01-01
        • 2023-03-13
        • 1970-01-01
        • 2016-08-03
        • 1970-01-01
        相关资源
        最近更新 更多