并行编程实战——TBB的安装
一、安装前的准备
在并行编程中,intel TBB还是一个比较强大的框架。可能对大多数数的开发者来说它还是比较陌生的,因为一般很少有公司会直接用到并行框架的水平。比之于OpenMP之类的比较容易直接使用的相关并行库,intel TBB还是一个相对来说比较难的框架。这不光在于开发过程,也因为它的安装过程。
早期的intel TBB安装还是比较复杂的,但随着技术的升级和系统版本的迭代,到现在基本已经没有什么难度可言了。这里会把安装intel TBB的过程详细的说明一下,并给出一个简单例子来证明框架安装成功与否。
二、TBB的安装
TBB在intel官网上已经正式命名为intel oneapi。所以在搜索相关的技术支持及相关功能文档时,请务必注意。intel oneAPI的安装在Ubuntu22.04中(Debian相同)的安装过程如下:
1、sudo apt-get update
2、sudo apt-get upgrade
3、sudo apt install libtbb-dev或sudo apt install libtbb2 #只安装运行库
4、sudo apt install clang #如果需要,否则可不安装
查看安装状态:
$ dpkg -l | grep libtbb
ii libtbb12:amd64 2021.5.0-7ubuntu2 amd64 parallelism library for C++ - runtime files
ii libtbb2:amd64 2020.3-1ubuntu3 amd64 parallelism library for C++ - runtime files
ii libtbb2-dev:amd64 2020.3-1ubuntu3 amd64 parallelism library for C++ - development files
ii libtbbmalloc2:amd64 2021.5.0-7ubuntu2 amd64 parallelism helper library for C++ - runtime files
或者使用
apt show libtbb*
或
apt search libtbb
可能显示:
Package: libtbb12
Version: 2021.5.0-7ubuntu2
Priority: optional
Section: universe/libs
Source: onetbb
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian Science Maintainers <debian-science-maintainers@lists.alioth.debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 266 kB
Depends: libtbbmalloc2 (= 2021.5.0-7ubuntu2), libc6 (>= 2.34), libgcc-s1 (>= 3.3.1), libstdc++6 (>= 11)
Homepage: https://www.threadingbuildingblocks.org/
Download-Size: 84.8 kB
APT-Manual-Installed: no
APT-Sources: https://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy/universe amd64 Packages
如果升级版本则可能需要进行删除操作:
1、sudo apt-get update
2、sudo apt-get upgrade
3、sudo apt-get -y autoremove libtbb2(或sudo apt-remove libtbb-dev)
请注意下面删除方式的不同,根据自己的实际场景进行选择:
sudo apt-remove libtbb-dev #基本的删除动作,删除开发库
sudo apt-get purge libtbb-dev #同时删除与libtbb相关的配置文件
sudo apt-get remove libtbb2 #只删除运行时库
sudo apt-get autoremove libtbb2 #同时删除依赖和不再需要的包
说明:执行任何卸载操作之前请备份重要数据,libtbb-dev为开发库,如只想卸载运行时库,请确保只卸载libtbb2而不是libtbb-dev。
三、验证
从网上摘抄了一段代码,进行编译测试:
#include <algorithm>
#include <execution>
#include <iostream>
#include <random>
#include <vector>
bool Odd(int n) { return n % 2; }
int main() {
std::vector<int> numbers(500);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dis(0, 100000);
auto rand_num([&dis, &gen]() mutable { return dis(gen); });
std::generate(std::execution::seq, std::begin(numbers), std::end(numbers),
rand_num);
std::sort(std::execution::par, std::begin(numbers), std::end(numbers));
std::reverse(std::execution::par, std::begin(numbers), std::end(numbers));
auto odds(std::count_if(std::execution::par, std::begin(numbers),
std::end(numbers), Odd));
std::cout << "Reverse sorted numbers: " << std::endl;
size_t i = 1;
for (const auto& number : numbers) {
std::cout << number << ", ";
if (i % 10 == 0) {
std::cout << std::endl;
}
++i;
}
std::cout << std::endl;
std::cout << (100.0 * odds / numbers.size()) << "% of the numbers are odd.\n";
return 0;
}
编译并运行:
g++ -O2 -DNDEBUG -o tbb3 tbb3.cpp -ltbb -lrt -std=c++17
./tbb3
Reverse sorted numbers:
99987, 99785, 99382, 99220, 98992, 98963, 98961, 98895, 98479, 98302,
98121, 97899, 97842, 97775, 97706, 97605, 97578, 97486, 97117, 96981,
96893, 96812, 96782, 96556, 96030, 96010, 95870, 95746, 95524, 95211,
95073, 94961, 94928, 94727, 94192, 93885, 93852, 93219, 93115, 93067,
92489, 92423, 92313, 91973, 91937, 91842, 91789, 91612, 91517, 91511,
91488, 91479, 91418, 91355, 91055, 90589, 90497, 90322, 90320, 89691,
89554, 88831, 88746, 88724, 88691, 88660, 88396, 87977, 87853, 87589,
87477, 87441, 87410, 86996, 86795, 86546, 86074, 85996, 85857, 85477,
85367, 85269, 85151, 84957, 84937, 84764, 84759, 84741, 84206, 83855,
83739, 83714, 83458, 82728, 82407, 82240, 82199, 82008, 81912, 81538,
81364, 81287, 81137, 80983, 80777, 80333, 80264, 79874, 79801, 79152,
79016, 78988, 78650, 78598, 78414, 78257, 78186, 78164, 77799, 77546,
77523, 77379, 77279, 77208, 77133, 76873, 76872, 76863, 76816, 76761,
76563, 76282, 75798, 75454, 75323, 75056, 74953, 74905, 74747, 74613,
74499, 73627, 73572, 73509, 73404, 72586, 72553, 72392, 71742, 71527,
71378, 70840, 70811, 70656, 70277, 69771, 69541, 69464, 69263, 69240,
69022, 69000, 68876, 68858, 68760, 68636, 68562, 68503, 68327, 68043,
67912, 67490, 67104, 67026, 66515, 66371, 66346, 65929, 65894, 65770,
65321, 65313, 65250, 65203, 65079, 64935, 64756, 64729, 64453, 64216,
64113, 63890, 63860, 63686, 63593, 63561, 63493, 63102, 62704, 62522,
62353, 62283, 62272, 62088, 61969, 61487, 61399, 60905, 60710, 60516,
60226, 59815, 59613, 59606, 59362, 59246, 58711, 58438, 58416, 58405,
58317, 58091, 58015, 57875, 57768, 57464, 57300, 57213, 57156, 57052,
56961, 56807, 56714, 56304, 56284, 56206, 55756, 55655, 55395, 55368,
55330, 55066, 54561, 54009, 53954, 53626, 53619, 53237, 52954, 52479,
52303, 52088, 51877, 51875, 51233, 51226, 51147, 50558, 50423, 50316,
49971, 49857, 49720, 49112, 49064, 48844, 48747, 48621, 48549, 48541,
48530, 48442, 48417, 48353, 48305, 48183, 47956, 47630, 47574, 47296,
47158, 47149, 47139, 47036, 46876, 46837, 46557, 46259, 46232, 45823,
45793, 45701, 45543, 45223, 45179, 44890, 44760, 44395, 43934, 43911,
43876, 43820, 43603, 43366, 43275, 43063, 43055, 43025, 42902, 42621,
42447, 41394, 41299, 41089, 40096, 40002, 39846, 38302, 38276, 38272,
38231, 37808, 37768, 36821, 36536, 35537, 35536, 35514, 35228, 35168,
35049, 34281, 34108, 33869, 33846, 33682, 33013, 32945, 32821, 32784,
32387, 31895, 31856, 31716, 31661, 31162, 30867, 30578, 29630, 29507,
29416, 28799, 28697, 28037, 28013, 27981, 27633, 27610, 27528, 27478,
27315, 27230, 26715, 26687, 26677, 26644, 26057, 25968, 25601, 25466,
25323, 24796, 24642, 24553, 24463, 24438, 24403, 24368, 24180, 24071,
24030, 23976, 23878, 23868, 23723, 23612, 23594, 23160, 22988, 22852,
22666, 22541, 22357, 22330, 22178, 22141, 21716, 21607, 21580, 21381,
21237, 20864, 20676, 20108, 19815, 19770, 19769, 19476, 19431, 19161,
19118, 19099, 18692, 18617, 18490, 18463, 18210, 18021, 17970, 17861,
17722, 17695, 17446, 17125, 16852, 16702, 16427, 16372, 16353, 16314,
15529, 15358, 14237, 14047, 13950, 13931, 13868, 13835, 13805, 13767,
13757, 13701, 13693, 13663, 13244, 12697, 12512, 12438, 12384, 12155,
11998, 11986, 11499, 11453, 11349, 11269, 11155, 11085, 10718, 10663,
9806, 9441, 9328, 8253, 8173, 7953, 7791, 7343, 7277, 6763,
6593, 6450, 6269, 5260, 5138, 5101, 4687, 4649, 4626, 4556,
4422, 3791, 3719, 3451, 3209, 2745, 2332, 2291, 2282, 2253,
1814, 1794, 1313, 1275, 885, 320, 99, 97, 38, 27,
52.6% of the numbers are odd.
在最初的编译过程中指定了相关的库路径,如下:
g++ -O2 -DNDEBUG -I /usr/local/include -L /usr/local/lib64 -o tbb3 tbb3.cpp -ltbb -lrt -std=c++17
结果无法编译通过,后来去查找相关的库路径,发现安装并未在这个路径下,而在/usr/lib/x86_64-linux-gnu路径下。所以一般勿需直接指定,程序可以自行查找相关库的路径并引用成功。另外需要说明的是,如果安装有不同的版本,可能头文件有所不同,一定要认真的对比,防止交叉使用。
四、总结
一个好的框架,必然是容易使用。不光是接口让开发者感觉良好,环境安装也要相对简单。否则除了一些技术极客对大多数开者来说,可能安装就被劝退了。这种情况还是比较常见的。比如早期的Eclipse就被后来的idea给抢占了大半的江山。而在Windows平台上,Vistual Studio一直是一座绕不过去的大山。就是因为这两者都是容易安装和使用,一般不会有什么需要具体的配置才能进行正确的进行开发。
简单是永远是方向,至于简单到何种地步,就见仁见智了。