GCC 9已经拥有它们,但您需要单独安装TBB
在Ubuntu 19.10中,所有组件终于对齐了:
- GCC 9 是默认的, 并且是TBB所需的最低版本
- TBB(Intel线程构建块)为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>
(需要Thread Building Blocks 2018或更新版本)。
相关主题:
Ubuntu 18.04安装
Ubuntu 18.04有点复杂:
以下是Ubuntu 18.04的完全自动化测试命令:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-9 g++-9
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"
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;
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;
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;
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:Intel Core i7-7820HQ CPU(4核心/8线程,2.90 GHz基频,8 MB缓存),内存:2x Samsung M471A2K43BB1-CRC(2x 16GiB,2400 Mbps)的Lenovo ThinkPad P51笔记本电脑,对于要排序的1亿个数字输入,典型输出如下:
./main.out 100000000
现在是:
parallel 2.00886 s
serial 9.37583 s
因此,并行版本的速度大约快了4.5倍!另请参阅:“CPU bound”和“I/O bound”这些术语是什么意思?
我们可以使用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
这 再次确认算法并行化,因为real < user,并且给出了在我的系统中它可以有效地并行化的想法(对于8个核心约为3.5倍)。
错误信息
嘿,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 |
| ^~~~~~~~~~~~~~~~~~~~~
compilation terminated.
因此我们可以看到,<execution>
依赖于未安装的 TBB 组件。
如果 TBB 版本过旧,例如默认的 Ubuntu 18.04 版本,则会出现以下错误: