FindOpenMP¶
Finds Open Multi-Processing (OpenMP) support in a compiler:
find_package(OpenMP [<version>] [COMPONENTS <langs>...] [...])
If the compiler supports OpenMP, the flags required to compile with OpenMP support are returned in variables for the different languages. The variables may be empty if the compiler does not need a special flag to support OpenMP.
Added in version 3.5: Clang support.
Components¶
This module supports components that can be specified using the standard syntax:
find_package(OpenMP [COMPONENTS <langs>...])
Each of these components controls the various languages to search OpenMP support for. The following components are exposed:
CAdded in version 3.10.
CXXAdded in version 3.10.
FortranAdded in version 3.10.
CUDAAdded in version 3.31.
The
CUDAlanguage component is supported when using a CUDA compiler that supports OpenMP on the host.
If no components are specified, module checks for all of them automatically, depending on the enabled languages in the project.
Imported Targets¶
This module provides the following Imported Targets:
OpenMP::OpenMP_<lang>Added in version 3.9.
Target encapsulating the OpenMP usage requirements for language
<lang>.OpenMP::OpenMP_<lang>_OffloadAdded in version 4.6.
Target for code whose OpenMP
targetregions may be offloaded to a device, for<lang>ofC,CXX, orFortran. It linksOpenMP::OpenMP_<lang>and, when offload is enabled with theOpenMP_<lang>_OFFLOADorOpenMP_<lang>_OFFLOAD_FLAGSvariables, adds the offload flags when compiling<lang>sources. It also adds them, together with the OpenMP flags, when the link is done by a compiler with the sameCMAKE_<LANG>_COMPILER_ID, because compilers need both in order to link device code. When offload is not enabled, it is equivalent toOpenMP::OpenMP_<lang>.With NVHPC, an executable linked by NVHPC must also link the offload target when it uses a shared library with offloaded code, for example through
$<LINK_ONLY:OpenMP::OpenMP_<lang>_Offload>in the library'sINTERFACElink libraries.Linking through a separate linker, as with Visual Studio generators or
clang-cl, is not supported.
Input Variables¶
The following variables may be set to influence this module's behavior:
OpenMP_RUNTIME_MSVCAdded in version 3.30.
Specify the OpenMP Runtime when compiling with the MSVC compiler (
cl). If set to a non-empty value, such asexperimentalorllvm, it will be passed as the value of the-openmp:flag. This has no effect with other compilers, includingclang-cl.
OpenMP_<lang>_OFFLOADAdded in version 4.6.
Enables OpenMP
targetoffload for targets that linkOpenMP::OpenMP_<lang>_Offload. If not defined, offload is enabled whenOpenMP_<lang>_OFFLOAD_FLAGSis non-empty. If defined, its boolean value decides, soOFFdisables offload even when flags are set.The offload flags are
OpenMP_<lang>_OFFLOAD_FLAGSif non-empty, and otherwise a default for the compiler:GNU: none. GCC offloads to the devices whose offload compilers are installed, such as
gcc-offload-nvptxon Debian and Ubuntu.NVHPC:
-mp=gpu.IntelLLVM with a GNU-like command line (
icx,icpx,ifx):-fopenmp-targets=spir64.Cray, CrayClang: none. The device comes from the loaded
craype-accel-*module.
Other compilers, including Clang, have no default. Enabling offload for them without
OpenMP_<lang>_OFFLOAD_FLAGSis an error, even if the package isQUIETor the component is optional.Targets linking only
OpenMP::OpenMP_<lang>are not affected. Projects using the result variables must add the device flags for compiling and linking themselves. Disabling offload only removes what the offload target would add; it does not undo offload enabled by other compiler flags or by the environment.OpenMP_<lang>_FOUNDandOpenMP_<lang>_VERSIONdescribe host OpenMP support only; the module does not check that offload works.OpenMP_<lang>_OFFLOAD_FLAGSAdded in version 4.6.
Compiler flags selecting the device(s) to offload OpenMP
targetregions to, replacing the compiler's default described above. For example,--offload-arch=sm_80for Clang,-foffload=nvptx-nonefor GCC, or-mp=gpu -gpu=cc80for NVHPC. The value is a command-line string, split into arguments as by theSHELL:prefix oftarget_compile_options(). An empty or whitespace-only value counts as not set.
Result Variables¶
This module defines the following variables:
OpenMP_FOUNDAdded in version 3.10.
Boolean indicating that OpenMP flags for all requested languages were found. If no components are specified, this variable is set to boolean true if OpenMP settings for all enabled languages were detected.
OpenMP_VERSIONAdded in version 4.2.
Minimal version of the OpenMP standard detected among the requested languages, or all enabled languages if no components were specified.
This module will set the following variables per language in the
project, where <lang> is one of C, CXX, CUDA, or Fortran:
OpenMP_<lang>_FOUNDAdded in version 3.9.
Boolean indicating whether the (requested version of) OpenMP support for
<lang>was detected.OpenMP_<lang>_VERSIONAdded in version 3.9.
OpenMP version implemented by the
<lang>compiler, if available.OpenMP_<lang>_VERSION_MAJORAdded in version 3.9.
Major version of OpenMP implemented by the
<lang>compiler, if available.OpenMP_<lang>_VERSION_MINORAdded in version 3.9.
Minor version of OpenMP implemented by the
<lang>compiler, if available.OpenMP_<lang>_SPEC_DATEAdded in version 3.7.
Date of the OpenMP specification implemented by the
<lang>compiler, if available.The specification date is formatted as given in the OpenMP standard:
yyyymmwhereyyyyandmmrepresents the year and month of the OpenMP specification implemented by the<lang>compiler.OpenMP_<lang>_FLAGSOpenMP compiler flags for
<lang>, separated by spaces.OpenMP_<lang>_INCLUDE_DIRSAdded in version 3.16.
Directories that must be added to the header search path for
<lang>when using OpenMP.
For linking with OpenMP code written in <lang>, the following
variables are provided:
OpenMP_<lang>_LIB_NAMESAdded in version 3.9.
A semicolon-separated list of OpenMP programs libraries for
<lang>.OpenMP_<libname>_LIBRARYAdded in version 3.9.
Location of the individual libraries needed for OpenMP support in
<lang>. The<libname>names are stored in theOpenMP_<lang>_LIB_NAMESlist.OpenMP_<lang>_LIBRARIESAdded in version 3.9.
A list of libraries needed to link with OpenMP code written in
<lang>.
Specifically for Fortran, the module sets the following variables:
OpenMP_Fortran_HAVE_OMPLIB_HEADERAdded in version 3.9.
Boolean indicating whether OpenMP is accessible through the
omp_lib.hFortran header.OpenMP_Fortran_HAVE_OMPLIB_MODULEAdded in version 3.9.
Boolean indicating whether OpenMP is accessible through the
omp_libFortran module.
For some compilers, it may be necessary to add a header search path to find
the relevant OpenMP headers. This location may be language-specific. Where
this is needed, the module may attempt to find the location, but it can be
provided directly by setting the OpenMP_<lang>_INCLUDE_DIR cache variable.
Note that this variable is an input control to the module. Project code
should use the OpenMP_<lang>_INCLUDE_DIRS output variable if it needs
to know what include directories are needed, or preferably the
OpenMP::OpenMP_<lang> imported target.
For Clang targeting the MSVC ABI, the module searches for the OpenMP runtime
library, preferring the one shipped with the compiler. A different library
can be provided directly by setting the OpenMP_libomp_LIBRARY cache
variable. Programs linked with it need the matching runtime DLL, such as
libomp.dll, next to them or on PATH when they run.
Changed in version 4.6: In previous CMake versions, the first libomp found through the LIB
environment variable was used, such as the one provided with MSVC for its
-openmp:llvm option.
Examples¶
Finding OpenMP support and linking the imported target to a project target using the C language component:
find_package(OpenMP)
target_link_libraries(project_target PRIVATE OpenMP::OpenMP_C)
Linking a library whose target regions may be offloaded to a GPU:
find_package(OpenMP REQUIRED)
target_link_libraries(kernels PRIVATE OpenMP::OpenMP_CXX_Offload)
and enabling offload when configuring the build, e.g. with Clang:
$ cmake -B build -DOpenMP_CXX_OFFLOAD_FLAGS=--offload-arch=sm_80