OpenMP.cmake 4.96 KB
Newer Older
openvino-pushbot's avatar
openvino-pushbot committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#===============================================================================
# Copyright 2017-2018 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#===============================================================================

# Manage OpenMP-related compiler flags
#===============================================================================

if(OpenMP_cmake_included)
    return()
endif()
set(OpenMP_cmake_included true)
24 25 26
include("cmake/Threading.cmake")
include("cmake/MKL.cmake")

27 28
set(MKLDNN_USES_INTEL_OPENMP FALSE)

29 30 31 32 33 34
if (APPLE AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
    # OSX Clang doesn't have OpenMP by default.
    # But we still want to build the library.
    set(_omp_severity "WARNING")
else()
    set(_omp_severity "FATAL_ERROR")
Alexey Suhov's avatar
Alexey Suhov committed
35 36
endif()

37 38
macro(forbid_link_compiler_omp_rt)
    if (NOT WIN32)
39 40 41 42 43 44
        set_if(OpenMP_C_FOUND
            CMAKE_C_CREATE_SHARED_LIBRARY_FORBIDDEN_FLAGS
            "${OpenMP_C_FLAGS}")
        set_if(OpenMP_CXX_FOUND
            CMAKE_CXX_CREATE_SHARED_LIBRARY_FORBIDDEN_FLAGS
            "${OpenMP_CXX_FLAGS}")
45
        if (NOT APPLE)
46
            append(CMAKE_SHARED_LINKER_FLAGS "-Wl,--as-needed")
47 48 49 50 51 52 53
        endif()
    endif()
endmacro()

macro(use_intel_omp_rt)
    # fast return
    if (CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
54
        set(MKLDNN_USES_INTEL_OPENMP TRUE)
55 56 57 58 59 60
        return()
    endif()

    # Do not link with compiler-native OpenMP library if Intel MKL is present.
    # Rationale: Intel MKL comes with Intel OpenMP library which is compatible
    # with all libraries shipped with compilers that Intel MKL-DNN supports.
61 62 63 64 65
    get_filename_component(MKLIOMP5LIB "${MKLIOMP5LIB}" PATH)
    find_library(IOMP5LIB
                NAMES "iomp5" "iomp5md" "libiomp5" "libiomp5md"
                HINTS  ${MKLIOMP5LIB} )
    if(IOMP5LIB)
66
        forbid_link_compiler_omp_rt()
67 68 69 70 71
        if (WIN32)
            get_filename_component(MKLIOMP5DLL "${MKLIOMP5DLL}" PATH)
            find_file(IOMP5DLL
                NAMES "libiomp5.dll" "libiomp5md.dll"
                HINTS ${MKLIOMP5DLL})
72
        endif()
73
        list(APPEND EXTRA_SHARED_LIBS ${IOMP5LIB})
74 75 76 77 78
    else()
        if (MKLDNN_THREADING STREQUAL "OMP:INTEL")
            message(${_omp_severity} "Intel OpenMP runtime could not be found. "
                "Please either use OpenMP runtime that comes with the compiler "
                "(via -DMKLDNN_THREADING={OMP,OMP:COMP}), or "
79 80
                "explicitely provide the path to libiomp with the "
                "-DCMAKE_LIBRARY_PATH option")
81 82 83
        endif()
    endif()
endmacro()
openvino-pushbot's avatar
openvino-pushbot committed
84 85 86 87

if(WIN32 AND ${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
    add_definitions(/Qpar)
    add_definitions(/openmp)
88 89 90 91 92
    set(OpenMP_CXX_FOUND true)
elseif(MSVC AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
    append(CMAKE_C_FLAGS "-Xclang -fopenmp")
    append(CMAKE_CXX_FLAGS "-Xclang -fopenmp")
    set(OpenMP_CXX_FOUND true)
93
    list(APPEND EXTRA_SHARED_LIBS ${IOMP5LIB})
openvino-pushbot's avatar
openvino-pushbot committed
94 95 96 97 98 99 100 101 102 103 104 105
else()
    find_package(OpenMP)
    #newer version for findOpenMP (>= v. 3.9)
    if(CMAKE_VERSION VERSION_LESS "3.9" AND OPENMP_FOUND)
        if(${CMAKE_MAJOR_VERSION} VERSION_LESS "3" AND ${CMAKE_CXX_COMPILER_ID} STREQUAL "Intel")
            # Override FindOpenMP flags for Intel Compiler (otherwise deprecated)
            set(OpenMP_CXX_FLAGS "-fopenmp")
            set(OpenMP_C_FLAGS "-fopenmp")
        endif()
        set(OpenMP_C_FOUND true)
        set(OpenMP_CXX_FOUND true)
    endif()
106
    append_if(OpenMP_C_FOUND CMAKE_SRC_CCXX_FLAGS "${OpenMP_C_FLAGS}")
openvino-pushbot's avatar
openvino-pushbot committed
107 108
endif()

109 110 111
if (MKLDNN_THREADING MATCHES "OMP")
    if (OpenMP_CXX_FOUND)
        set_threading("OMP")
112 113
        append(CMAKE_TEST_CCXX_FLAGS "${OpenMP_CXX_FLAGS}")
        append(CMAKE_EXAMPLE_CCXX_FLAGS "${OpenMP_CXX_FLAGS}")
114 115 116
    else()
        message(${_omp_severity} "OpenMP library could not be found. "
            "Proceeding might lead to highly sub-optimal performance.")
openvino-pushbot's avatar
openvino-pushbot committed
117
    endif()
118 119

    if (MKLDNN_THREADING STREQUAL "OMP:COMP")
120 121
        set(IOMP5LIB "")
        set(IOMP5DLL "")
122 123
    else()
        use_intel_omp_rt()
openvino-pushbot's avatar
openvino-pushbot committed
124
    endif()
125 126 127 128

    if(MKLIOMP5LIB)
        set(MKLDNN_USES_INTEL_OPENMP TRUE)
    endif()
129 130 131 132 133
else()
    # Compilation happens with OpenMP to enable `#pragma omp simd`
    # but during linkage OpenMP dependency should be avoided
    forbid_link_compiler_omp_rt()
    return()
openvino-pushbot's avatar
openvino-pushbot committed
134 135
endif()

136
set_ternary(_omp_lib_msg IOMP5LIB "${IOMP5LIB}" "provided by compiler")
137 138
message(STATUS "OpenMP lib: ${_omp_lib_msg}")
if(WIN32)
139
    set_ternary(_omp_dll_msg IOMP5DLL "${IOMP5LIB}" "provided by compiler")
140
    message(STATUS "OpenMP dll: ${_omp_dll_msg}")
Alexey Suhov's avatar
Alexey Suhov committed
141
endif()