C++17 并行算法已经实现了吗?



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

在查看并行算法的cpp偏好时,有一长串算法,声称

技术规范提供了以下69种算法的并行化版本,分别来自algorithmnumericmemory(...长长的清单...

这听起来像是算法已经"在纸上">准备好了,但还没有准备好使用?

在一年多前的这个SO问题中,答案声称这些功能尚未实现。但是到现在为止,我本来希望看到某种实现。我们已经可以使用什么了吗?

GCC 9 有它们,但你必须单独安装 TBB

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

  • GCC 9 是默认版本,也是 TBB 所需的最低版本
  • TBB(英特尔线程构建模块(为 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

并行算法和<execution>(需要线程构建块 2018 或更高版本(。

相关主题:

  • 如何在 Linux 上从源代码安装 TBB 并使其工作
  • 链接英特尔 TBB 库时遇到问题

乌班图18.04安装

Ubuntu 18.04 涉及更多:

  • GCC 9可以从值得信赖的PPA中获得,所以还不错
  • TBB 是 2017 版,它不起作用,我找不到值得信赖的 PPA。从源代码编译很容易,但是没有很烦人的安装目标......

以下是适用于 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

测试程序分析

我已经用这个程序测试了它来比较并行和串行分拣速度。

主.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 上,联想 ThinkPad P51 笔记本电脑与 CPU:英特尔酷睿 i7-7820HQ CPU(4 核/8 线程,2.90 GHz 基础,8 MB 缓存(,RAM:2x 三星 M471A2K43BB1-CRC(2x 16GiB,2400 Mbps(输入的典型输出,有 1 亿个数字要排序:

./main.out 100000000

是:

parallel 2.00886 s
serial 9.37583 s

所以并行版本快了大约 4.5 倍!另请参阅:"CPU 绑定"和"I/O 绑定"这两个术语是什么意思?

我们可以确认该进程正在生成具有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

这再次证实了该算法自真实用户以来

错误消息

嘿,谷歌,请索引这个。

如果未安装 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.

因此,我们看到<execution>依赖于卸载的TBB组件。

如果 TBB 太旧,例如默认的 Ubuntu 18.04,它会失败并显示:

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

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

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

  • https://github.com/intel/parallelstl

它正在并入海湾合作委员会。

Gcc 尚未实现并行 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 的支持。添加此支持 并不难:只需使用编译器标志编译应用程序即可 -福彭姆普。这将链接到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;
}

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

最新更新