windows openMP 入门。安装在窗户上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11079586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:36:30  来源:igfitidea点击:

Getting started with openMP. install on windows

windowsinstallopenmp

提问by Nurlan

I want to write parallel program in C++ using OpenMP, so I am getting started with OpenMP. On the other words I am a beginner and I need good OpenMP guide telling how to install it. Does someone know how to install OpenMP on Windows, then compile and run the program?

我想使用 OpenMP 用 C++ 编写并行程序,所以我开始使用 OpenMP。换句话说,我是一个初学者,我需要很好的 OpenMP 指南来告诉我如何安装它。有人知道如何在 Windows 上安装 OpenMP,然后编译和运行程序吗?

回答by Hristo Iliev

OpenMP is not something that you install. It comes with your compiler. You just need a decent compiler that supports OpenMP and you need to know how to enable OpenMP support since it is usually disabled by default.

OpenMP 不是您安装的东西。它随您的编译器一起提供。您只需要一个支持 OpenMP 的不错的编译器,并且您需要知道如何启用 OpenMP 支持,因为默认情况下它通常是禁用的。

The standard compiler for Windows comes from Microsoft and it is the Microsoft Visual C/C++ compiler from Visual Studio. Unfortunately its OpenMP support is a bit outdated - even the latest and greatest Visual Studio only supports OpenMP 2.0 (an outdated standard version from 2002). See herefor more information on how to use OpenMP in Visual Studio. There are other compilers available as well - both Intel C/C++ Compiler (commercial license required) and GCC (freely available) support newer OpenMP versions and other compilers are available too.

Windows 的标准编译器来自 Microsoft,它是来自 Visual Studio 的 Microsoft Visual C/C++ 编译器。不幸的是,它的 OpenMP 支持有点过时——即使是最新最好的 Visual Studio 也只支持 OpenMP 2.0(2002 年的过时标准版本)。有关如何在 Visual Studio 中使用 OpenMP 的更多信息,请参见此处。还有其他可用的编译器——英特尔 C/C++ 编译器(需要商业许可)和 GCC(免费提供)都支持较新的 OpenMP 版本,其他编译器也可用。

You can start learning OpenMP by visiting the OpenMP web site here. Also there is a great tutorial on OpenMP from Lawrence Livermore National Laboratory available here.

您可以通过访问此处的 OpenMP 网站开始学习 OpenMP 。也有对OpenMP的一个伟大的教程由劳伦斯·利弗莫尔国家实验室提供在这里

2020 Update:Microsoft now ships Clang for Windows with Visual Studio. Although it is a bit convoluted, one can (ab)use the Clang-cl toolset to produce working 32-bitOpenMP programs. A number of steps are necessary:

2020 年更新:Microsoft 现在随 Visual Studio 一起提供适用于 Windows 的 Clang。尽管有点复杂,但可以(ab)使用 Clang-cl 工具集来生成可运行的32 位OpenMP 程序。需要几个步骤:

  1. If not already installed, add Clang and Clang-cl using the Visual Studio 2019 Installer.
  2. Set the project's platform toolset (project Properties -> General -> Platform Toolset) to "LLVM (clang-cl)".
  3. Enable Clang OpenMP support by adding -Xclang -fopenmpto the compiler options in project Properties -> C/C++ -> All Options -> Additional Options.
    Important:make sure that OpenMP support is disabled beforeswitching the platform toolset (this is the default for new C++ projects). It seems that VS remembers the setting and still passes /openmpeven though the language configuration for Clang has no option for OpenMP. If clang-cl.exethrows error MSB8055 (unsupported /openmpoption) during build, set the platform toolset back to "Visual Studio 2019 (vXXX)" and disable the OpenMP support in Properties -> C/C++ -> Language -> Open MP Support, then switch the platform toolset again to "LLVM (Clang-cl)".
  4. Add libomp.libto the additional libraries in project Properties -> Linker -> Input -> Additional Dependencies.
  5. Add the path to libomp.libto the linker search path by adding a new entry with value $(LLVMInstallDir)\libin project Properties -> Linker -> General -> Additional Library Directories.
  6. Add a post-build action that copies LLVM's libomp.dllto the project output directory (without this step, running the executable will fail unless libomp.dllis in the DLL search path). In project Properties -> Build Events -> Post-Build Event -> Command Line:

    xcopy /y "$(LLVMInstallDir)\bin\libomp.dll" "$(SolutionDir)$(Configuration)"

  7. Build and run the project.

  1. 如果尚未安装,请使用 Visual Studio 2019 安装程序添加 Clang 和 Clang-cl。
  2. 将项目的平台工具集(项目属性 -> 常规 -> 平台工具集)设置为“LLVM (clang-cl)”。
  3. 通过-Xclang -fopenmp在项目Properties -> C/C++ -> All Options -> Additional Options 中添加编译器选项来启用 Clang OpenMP 支持。
    重要提示:确保切换平台工具集之前禁用 OpenMP 支持(这是新 C++ 项目的默认设置)。/openmp即使 Clang 的语言配置没有 OpenMP 的选项,VS 似乎记住了设置并且仍然通过。如果在构建期间clang-cl.exe抛出错误 MSB8055(不支持的/openmp选项),请将平台工具集设置回“Visual Studio 2019 (vXXX)”并在Properties -> C/C++ -> Language -> Open MP Support 中禁用 OpenMP支持,然后切换平台工具集再次设置为“LLVM(Clang-cl)”。
  4. 添加libomp.lib到项目Properties -> Linker -> Input -> Additional Dependencies 中的附加库。
  5. 路径添加到libomp.lib通过添加具有值的新条目链接的搜索路径$(LLVMInstallDir)\lib项目>链接- - >常规- >附加库目录属性
  6. 添加将 LLVM 复制libomp.dll到项目输出目录的构建后操作(如果没有此步骤,运行可执行文件将失败,除非libomp.dll位于 DLL 搜索路径中)。在项目属性 -> 构建事件 -> 构建后事件 -> 命令行中

    xcopy /y "$(LLVMInstallDir)\bin\libomp.dll" "$(SolutionDir)$(Configuration)"

  7. 构建并运行项目。

Note: this is very much likely still unsupported by Microsoft and it only works for x86 projects since the LLVM libraries shipped with VS are 32-bit only.

注意:这很可能仍然不受 Microsoft 支持,并且它仅适用于 x86 项目,因为 VS 附带的 LLVM 库仅为 32 位。

回答by FrickeFresh

So here is what I did to finally get OpenMP working on my Windows 10 PC:

因此,为了最终让 OpenMP 在我的 Windows 10 PC 上运行,我做了以下工作:

  1. Get MinGW - Download and grab what you need to get the basic gcc compiler and the g++ pakage (its really easy to do). You can always run g++ -vto make sure it is up and running
  2. Run mingw-get upgrade --recursive "gcc<4.7.*" "gcc-g++<4.7.*"This is the "Fun" part. Because at this time there was no libgomp library supported in their 4.9.* version my gcc wasn't able to recognize <omp.h>the last support version was 4.7.2 so with this I finally was able to run my openMP
  1. 获取 MinGW - 下载并获取获取基本 gcc 编译器和 g++ 包所需的内容(这真的很容易做到)。您可以随时运行g++ -v以确保它已启动并正在运行
  2. 运行mingw-get upgrade --recursive "gcc<4.7.*" "gcc-g++<4.7.*"这是“有趣”的部分。因为此时在他们的 4.9.* 版本中不支持 libgomp 库,我的 gcc 无法识别<omp.h>最后一个支持版本是 4.7.2,所以我终于能够运行我的 openMP

To compile run g++ -fopenmp myOpenMPFile.cpp -o myOpenMPand it will all work from there

编译运行g++ -fopenmp myOpenMPFile.cpp -o myOpenMP,它将从那里开始工作

gcc -fopenmp myOpenMPFile.cpp -o myOpenMPwill also work for C code

gcc -fopenmp myOpenMPFile.cpp -o myOpenMP也适用于 C 代码

回答by nae9on

I would like to share what I did to get OpenMP working on my Windows 10 PC (things have got even simpler in 2019)

我想分享我为让 OpenMP 在我的 Windows 10 PC 上运行所做的工作(2019 年的事情变得更加简单)

  1. I installed MinGW distribution from herewith GCC 8.2.0 compiler. The maintainer of the distribution has already added winpthreads and OpenMP support to GCC.

  2. I compiled my code with -fopenmpflag as follows: g++ -fopenmp main.cpp -o exec

  1. 我从这里使用 GCC 8.2.0 编译器安装了 MinGW 发行版。该发行版的维护者已经向 GCC 添加了 winpthreads 和 OpenMP 支持。

  2. 我用-fopenmp标志编译了我的代码,如下所示:g++ -fopenmp main.cpp -o exec

Note: the MinGW distribution provides support for many useful libraries (such as Boost 1.69.0) and other utilities. I found it to be very useful.

注意:MinGW 发行版支持许多有用的库(例如 Boost 1.69.0)和其他实用程序。我发现它非常有用。