简体   繁体   English

无法在 Windows 11、VIsual Studio 2019 中使用 OpenMP 并行运行超过 1 个线程

[英]Cannot run more than 1 thread in parallel using OpenMP in Windows 11, VIsual Studio 2019

I created an empty C++ project using Visual Studio 2019 in Windows 11.我在 Windows 11 中使用 Visual Studio 2019 创建了一个空的 C++ 项目。

Then I ran this minimal piece of code to test multi-threading, using OpenMP:然后我运行这段最小的代码来测试多线程,使用 OpenMP:

#include <iostream>
#include <omp.h>


int main() {
    
    omp_set_dynamic(0); // ensure that the number of threads doesn't change due to system demands
    omp_set_num_threads(16);
    std::cout << "Max threads: " << omp_get_max_threads() << "\n";

    int id;
#pragma omp parallel private(id)
    {
        std::cout << "Number of threads running in parallel: " << omp_get_num_threads() << "\n";
        std::cout << "Thread ID in parallel region: " << omp_get_thread_num() << "\n";
    }

    return 0;
}

This is the output I am getting:这是我得到的 output:

Max threads: 16最大线程数:16
Number of threads running in parallel: 1并行运行的线程数:1
Thread ID in parallel region: 0并行区域中的线程 ID:0

I first tried multithreading in a CMake project (used CMake 3.22 and the modern way of including OpenMP in a CMake project ) and attempted using both the Visual Studio 2019 and 2022 compilers.我首先在 CMake 项目中尝试了多线程(使用 CMake 3.22 和将OpenMP 包含在 CMake 项目中的现代方法)并尝试使用 202 编译器 Studio201 项目。 The result was the same, I couldn't get more than 1 thread to run in parallel.结果是一样的,我不能让超过 1 个线程并行运行。 So, then I tried this simple example in Visual Studio 2019 and ran into the same issue.所以,然后我在 Visual Studio 2019 中尝试了这个简单的示例并遇到了同样的问题。

I'm wondering if there is some Windows rule/setting that restricts the number of threads I can use.我想知道是否有一些 Windows 规则/设置限制了我可以使用的线程数。 Btw, using Matlab 2021b and the Parallel Computing toolbox, I was able to use multithreading successfully (8/16 threads).顺便说一句,使用 Matlab 2021b 和并行计算工具箱,我能够成功使用多线程(8/16 线程)。

If you need any other information about the issue/my system, don't hesitate to ask it from me.如果您需要有关问题/我的系统的任何其他信息,请随时向我询问。

Thank you.谢谢你。

Edit:编辑:

I get the following output from CMake我从 CMake 得到以下 output

"C:\Program Files\JetBrains\CLion 2022.1.1\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug "-DCMAKE_MAKE_PROGRAM=C:/Program Files/JetBrains/CLion 2022.1.1/bin/ninja/win/ninja.exe" -G Ninja -S C:\Users\parvanitis\Documents\MBI_code\cpp\ImagingFocus -B C:\Users\parvanitis\Documents\MBI_code\cpp\ImagingFocus\cmake-build-debug "C:\Program Files\JetBrains\CLion 2022.1.1\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug "-DCMAKE_MAKE_PROGRAM=C:/Program Files/JetBrains/CLion 2022.1.1/bin/ninja /win/ninja.exe" -G Ninja -S C:\Users\parvanitis\Documents\MBI_code\cpp\ImagingFocus -B C:\Users\parvanitis\Documents\MBI_c\make-build-de-demagingocus

  • The CXX compiler identification is MSVC 19.32.31332.0 CXX 编译器标识为 MSVC 19.32.31332.0
  • The CUDA compiler identification is NVIDIA 11.6.124 CUDA 编译器标识为 NVIDIA 11.6.124
  • Detecting CXX compiler ABI info检测 CXX 编译器 ABI 信息
  • Detecting CXX compiler ABI info - done检测 CXX 编译器 ABI 信息 - 完成
  • Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.32.31326/bin/Hostx64/x64/cl.exe检查工作的 CXX 编译器:C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.32.31326/bin/Hostx64/x64/cl.exe
  • skipped跳过
  • Detecting CXX compile features检测 CXX 编译特性
  • Detecting CXX compile features - done检测 CXX 编译功能 - 完成
  • Detecting CUDA compiler ABI info检测 CUDA 编译器 ABI 信息
  • Detecting CUDA compiler ABI info - done检测 CUDA 编译器 ABI 信息 - 完成
  • Check for working CUDA compiler: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.6/bin/nvcc.exe - skipped检查工作 CUDA 编译器:C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.6/bin/nvcc.exe - 已跳过
  • Detecting CUDA compile features检测 CUDA 编译特征
  • Detecting CUDA compile features - done检测 CUDA 编译功能 - 完成
  • Found Matlab: C:/Program Files/MATLAB/R2022a/extern/include (found version "9.12") found components: MAT_LIBRARY找到 Matlab: C:/Program Files/MATLAB/R2022a/extern/include(找到版本“9.12”)找到组件:MAT_LIBRARY
  • Found Git: C:/Program Files/Git/cmd/git.exe (found version "2.36.1.windows.1")找到 Git: C:/Program Files/Git/cmd/git.exe (找到版本“2.36.1.windows.1”)
  • Found OpenMP_CXX: -openmp (found version "2.0")找到 OpenMP_CXX:-openmp(找到版本“2.0”)
  • Found OpenMP: TRUE (found version "2.0")找到 OpenMP:TRUE(找到版本“2.0”)
  • Configuring done配置完成
  • Generating done生成完成
  • Build files have been written to: C:/Users/parvanitis/Documents/MBI_code/cpp/ImagingFocus/cmake-build-debug构建文件已写入:C:/Users/parvanitis/Documents/MBI_code/cpp/ImagingFocus/cmake-build-debug

[Finished] [完成的]

OK, real rookie mistake(s) here.好的,这里是真正的菜鸟错误。

First of all, I didn't add the /openmp flag to the CMAKE_CXX_FLAGS which is necessary in Windows (I thought find_package(OpenMP) was enough, while adding -fopenmp, as in Linux, was not recognized, so I skipped it).首先,我没有将 /openmp 标志添加到 Windows 中必需的 CMAKE_CXX_FLAGS 中(我认为 find_package(OpenMP) 就足够了,而在 Linux 中添加 -fopenmp 时无法识别,所以我跳过了它)。

Even after I did, it didn't work.即使在我这样做之后,它也没有用。 The reason was that the project I was building was also using CUDA and I was calling the OpenMP-multithreaded code from a.cu file, which is of course compiled using NVCC, which is ignorant of the CXX flags, and most likely does not have any reason to support them awyway.原因是我正在构建的项目也使用 CUDA 并且我从 a.cu 文件调用 OpenMP 多线程代码,该文件当然是使用 NVCC 编译的,它不知道 CXX 标志,很可能没有有任何理由支持他们。 So, I needed to move the OpenMP-multithreaded code to a.cpp file in order for it to work.因此,我需要将 OpenMP 多线程代码移动到 a.cpp 文件中才能正常工作。 I didn't mention my project was also using CUDA in the first place because I didn't think that would cause a problem (silly me).我没有提到我的项目首先也在使用 CUDA 因为我不认为这会导致问题(我很傻)。

Thanks for your help anyway Jérôme.无论如何,感谢您的帮助杰罗姆。

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

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