简体   繁体   English

C++17 并行算法和 CMake

[英]c++17 parallel algorithms and CMake

It looks like more and more standard libraries implementations are relying on TBB for their parallel algorithms.看起来越来越多的标准库实现依赖于 TBB 的并行算法。 This is a bit surprising to me, as I didn't think that standard libraries would have external dependencies (outside of stuff like pthread ), but that's a different question I imagine.这对我来说有点令人惊讶,因为我不认为标准库会有外部依赖项(在pthread之类的东西之外),但我想象这是一个不同的问题。

My issue is that I need to bake this into my CMakeLists.txt files now.我的问题是我现在需要将它烘焙到我的CMakeLists.txt文件中。

First bad news: There is no official CMake support for TBB, and TBB itself does not provide any FindTBB.cmake file.第一个坏消息:TBB 没有官方的 CMake 支持,而且 TBB 本身不提供任何FindTBB.cmake文件。 You can find it here and there on the web, but if standard libraries start relying on it, it would be nice to have it officially supported by CMake.你可以找到它在这里 没有在网络上,但如果标准库开始依赖它,它会是不错的它由CMake的正式支持。 Is this coming further down the line?这是进一步下降吗?

Then, I need to have some slightly convoluted code in my CMakeLists.txt file to find_package(TBB REQUIRED) and link the corresponding targets when required (depending on the standard library, version, etc.).然后,我需要在我的CMakeLists.txt文件中find_package(TBB REQUIRED)一些稍微复杂的代码到find_package(TBB REQUIRED)并在需要时链接相应的目标(取决于标准库、版本等)。 It looks like Conan is already offering a package that hides all that stuff from the user.看起来柯南已经提供了一个对用户隐藏所有这些东西的包。 You just get parallelstl and that's it.你只要得到parallelstl ,就是这样。 Will we have something similar in CMake in the future?将来我们会在 CMake 中有类似的东西吗?

We can already use these parallel algorithms in CMake today, but it would be great to make it easier to create such projects.我们今天已经可以在 CMake 中使用这些并行算法,但是如果能够更轻松地创建此类项目会很棒。

I have Ubuntu 21.10 with GCC and TBB ( libtbb-dev 2020.3-1 ) installed.我安装了带有 GCC 和 TBB ( libtbb-dev 2020.3-1 ) 的 Ubuntu 21.10。 Since TBB uses pkg-config what worked for me is this:由于 TBB 使用pkg-config对我有用的是:

# file CMakeLists.txt

find_package(PkgConfig REQUIRED)
pkg_search_module(TBB REQUIRED tbb)
link_libraries(PkgConfig::TBB)

Unfortunately, when I later installed intel-oneapi 2021.4.0 which includes its own TBB (in /opt/intel/oneapi ) this stopped working.不幸的是,当我后来安装了包含自己的 TBB(在/opt/intel/oneapi )的intel-oneapi 2021.4.0 ,它停止了工作。 This newer version of TBB cannot be used as a backed for GCC's (parallel) STL apparently (generates compilation errors in my system), so I did this in order to pick the system's TBB and not the /opt/intel version.这个较新版本的 TBB 显然不能用作 GCC(并行)STL 的支持(在我的系统中生成编译错误),所以我这样做是为了选择系统的 TBB 而不是/opt/intel版本。

link_libraries(-ltbb) #(PkgConfig::TBB)

This defeats the purpose of CMake, so I am also looking for a more robust solution.这违背了 CMake 的目的,所以我也在寻找更强大的解决方案。 My recommendation at the moment is do not install oneapi 's TBB if what you want is to make it work with system's GCC.我目前的建议是不要安装oneapi的 TBB,如果你想让它与系统的 GCC 一起工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM