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?
Set the project's platform toolset (project Properties -> General -> Platform Toolset) to "LLVM (clang-cl)". Enable Clang OpenMP support by adding -Xclang -fopenmp to the compiler options in project Properties -> C/C++ -> All Options -> Additional Options.
OpenMP (Open Multi-Processing) is an application programming interface (API) that supports multi-platform shared-memory multiprocessing programming in C, C++, and Fortran, on many platforms, instruction-set architectures and operating systems, including Solaris, AIX, FreeBSD, HP-UX, Linux, macOS, and Windows.
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.
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 here for 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.
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.
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-bit OpenMP programs. A number of steps are necessary:
-Xclang -fopenmp
to the compiler options in project Properties -> C/C++ -> All Options -> Additional Options./openmp
even though the language configuration for Clang has no option for OpenMP. If clang-cl.exe
throws error MSB8055 (unsupported /openmp
option) 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)".libomp.lib
to the additional libraries in project Properties -> Linker -> Input -> Additional Dependencies.libomp.lib
to the linker search path by adding a new entry with value $(LLVMInstallDir)\lib
in project Properties -> Linker -> General -> Additional Library Directories.Add a post-build action that copies LLVM's libomp.dll
to the project output directory (without this step, running the executable will fail unless libomp.dll
is in the DLL search path). In project Properties -> Build Events -> Post-Build Event -> Command Line:
xcopy /y "$(LLVMInstallDir)\bin\libomp.dll" "$(SolutionDir)$(Configuration)"
Build and run the project.
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.
So here is what I did to finally get OpenMP working on my Windows 10 PC:
g++ -v
to make sure it is up and runningmingw-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 openMPTo compile run g++ -fopenmp myOpenMPFile.cpp -o myOpenMP
and it will all work from there
gcc -fopenmp myOpenMPFile.cpp -o myOpenMP
will also work for C code
I would like to share what I did to get OpenMP working on my Windows 10 PC (things have got even simpler in 2019)
I installed MinGW distribution from here with GCC 8.2.0 compiler. The maintainer of the distribution has already added winpthreads and OpenMP support to GCC.
I compiled my code with -fopenmp
flag as follows: 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With