c++ - 带有 std::execution::par_unseq 的 std::for_each 不适用于 GCC 但适用于 MSVC

标签 c++ gcc visual-c++ parallel-processing c++20

我想并行化一个 for 循环并发现了 std::for_each 以及它的 execution policies .令人惊讶的是它在使用 GCC 时没有并行化:

#include <iostream>
#include <algorithm>
#include <execution>
#include <chrono>
#include <thread>
#include <random>

int main() {
    std::vector<int> foo;
    foo.reserve(1000);
    for (int i = 0; i < 1000; i++) {
        foo.push_back(i);
    }

    std::for_each(std::execution::par_unseq,
                  foo.begin(), foo.end(),
                  [](auto &&item) {
                      std::cout << item << std::endl;
                      std::random_device dev;
                      std::mt19937 rng(dev());
                      std::uniform_int_distribution<std::mt19937::result_type> dist6(10, 100);
                      std::this_thread::sleep_for(std::chrono::milliseconds(dist6(rng)));
                      std::cout << "Thread ID: " << std::this_thread::get_id() << std::endl;
                  });
}
此代码仍按顺序运行。
使用 MSVC代码被并行化并且完成得更快。GCC :
$ gcc --version
gcc (Ubuntu 10.1.0-2ubuntu1~18.04) 10.1.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
MSVC :
>cl.exe
Microsoft (R) C/C++ Optimizing Compiler Version 19.27.29112 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

usage: cl [ option... ] filename... [ /link linkoption... ]
CMakeLists.txt :
cmake_minimum_required(VERSION 3.17)
project(ParallelTesting)

set(CMAKE_CXX_STANDARD 20)

add_executable(ParallelTesting main.cpp)
我需要做些什么来启用与 GCC 的并行化吗?还有?ldd我的二进制文件的输出:
$ ldd my_binary
    linux-vdso.so.1 (0x00007ffe9e6b9000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f79efaa0000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f79ef881000)
    libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f79ef4ad000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f79ef295000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f79eeea4000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f79f041a000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f79eeb06000)
debugrelease二进制整体版本相同ldd输出。

最佳答案

我通过首先升级我的 WSL 来解决它Ubuntu从版本分发 18.0420.04自运行后 sudo apt install gcc libtbb-dev安装 TBB我仍然收到以下错误:#error Intel(R) Threading Building Blocks 2018 is required; older versions are not supported.这是由 TBB 引起的太老了。
现在与 TBB 2002.1-2安装它按预期工作:

$ sudo apt install libtbb-dev
[sudo] password for ubuntu:
Reading package lists... Done
Building dependency tree
Reading state information... Done
libtbb-dev is already the newest version (2020.1-2).
0 upgraded, 0 newly installed, 0 to remove and 10 not upgraded.
This answer 很好地描述了所有细节。
因为我正在使用 CMake我还必须将以下行添加到我的 CMakeLists.txt :
# Link against the dependency of Intel TBB (for parallel C++17 algorithms)
target_link_libraries(${PROJECT_NAME} tbb)

关于c++ - 带有 std::execution::par_unseq 的 std::for_each 不适用于 GCC 但适用于 MSVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65496175/

相关文章:

c++ - 将 new 运算符与对象的非默认构造函数一起使用

c++ - 如何根据编号(从键盘输入)创建可编辑字段?

c++ - 无法理解的 с++ 编译器行为

c++ - dsym 文件 UUID 与 xx 中的不匹配,Eclipse CDT 中未加载符号表

c++ - 仅当首先复制到临时变量中时,传递对字符串的引用才有效

python - 痛饮/ python : object does not support indexing

c - 内存对齐和填充 - 32 位和 64 位之间的差异

c++ - 以编程方式将原始数据发送到打印机

c++ - 正则表达式慢

c++ - 如何结合opencv和多线程?