Skip to content

Commit

Permalink
build: allow using compiler's OpenMP RT
Browse files Browse the repository at this point in the history
Pass `-DMKLDNN_THREADING=OMP:COMP` to cmake for that.
This is one of the steps required to address oneapi-src#312 issue.

By chance, this fixes oneapi-src#313.
The reason: we used ?undocumented? cmake flag [1] to prevent linking
with compilers OMP RT. Apparently this flag stopped working for recent
cmake versions. The new workaround is: `-Wl,--as-needed` for GCC and
`/full/path/to/libiomp5.so` for clang. How tricky... :(

[1] CMAKE_CXX_CREATE_SHARED_LIBRARY_FORBIDDEN_FLAGS

f: build: allow using compiler's OpenMP RT
  • Loading branch information
Fomenko, Evarist M committed Oct 3, 2018
1 parent 72dcfe8 commit a17db4a
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 19 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,13 @@ cmake options such as `CMAKE_INSTALL_PREFIX` or `CMAKE_BUILD_TYPE`,
user can also pass Intel MKL-DNN specific ones:

|Option | Possible Values (defaults in bold) | Description
|:--- |:--- | :---
|MKLDNN_LIBRARY_TYPE | **SHARED**, STATIC | Defines resulting library type
|MKLDNN_THREADING | **OMP**, TBB | Defines threading type
|WITH_EXAMPLE | **ON**, OFF | Controls building examples
|WITH_TEST | **ON**, OFF | Controls building tests
|ARCH_OPT_FLAGS (\*) | *compiler flags* | Specifies compiler optimization flags
|VTUNEROOT | *path* | Enables integration with Intel(R) Vtune(tm) Amplifier
|:--- |:--- | :---
|MKLDNN_LIBRARY_TYPE | **SHARED**, STATIC | Defines resulting library type
|MKLDNN_THREADING | **OMP**, OMP:INTEL, OMP:COMP, TBB | Defines threading type
|WITH_EXAMPLE | **ON**, OFF | Controls building examples
|WITH_TEST | **ON**, OFF | Controls building tests
|ARCH_OPT_FLAGS (\*) | *compiler flags* | Specifies compiler optimization flags
|VTUNEROOT | *path* | Enables integration with Intel(R) Vtune(tm) Amplifier

Please check [cmake/options.cmake](cmake/options.cmake) for more options
and details.
Expand Down
56 changes: 49 additions & 7 deletions cmake/OpenMP.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,53 @@ set(OpenMP_cmake_included true)

include("cmake/MKL.cmake")

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")
endif()


macro(forbid_link_compiler_omp_rt)
if (NOT WIN32)
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})
if (NOT APPLE)
set (CMAKE_SHARED_LINKER_FLAGS "-Wl,--as-needed")
endif()
endif()
endmacro()

macro(use_intel_omp_rt)
# fast return
if (CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
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.
if(HAVE_MKL AND NOT WIN32 AND NOT CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
if(HAVE_MKL)
forbid_link_compiler_omp_rt()
list(APPEND EXTRA_LIBS ${MKLIOMP5LIB})
if (UNIX AND NOT APPLE AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
# For some reasons Clang ignores `-fopenmp=libiomp5` switch and
# links against libomp.so anyways.
# The workaround is to set the full path to libiomp5.so
add_library(libiomp5 SHARED IMPORTED)
set_property(TARGET libiomp5 PROPERTY IMPORTED_LOCATION "${MKLIOMP5LIB}")
list(APPEND EXTRA_LIBS libiomp5)
else()
list(APPEND EXTRA_LIBS ${MKLIOMP5LIB})
endif()
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 "
"install Intel MKL / Intel MKL-ML (e.g. scripts/prepare_mkl.sh)")
endif()
endif()
endmacro()

Expand Down Expand Up @@ -66,12 +99,21 @@ else()
append_if(OpenMP_CXX_FOUND CMAKE_CXX_FLAGS "${OpenMP_CXX_FLAGS}")
endif()

if (MKLDNN_THREADING STREQUAL "OMP")
if (NOT OpenMP_CXX_FOUND)
message(FATAL_ERROR "OpenMP library could not be found")
if (MKLDNN_THREADING MATCHES "OMP")
if (OpenMP_CXX_FOUND)
add_definitions(-DMKLDNN_THR=MKLDNN_THR_OMP)
else()
message(${_omp_severity} "OpenMP library could not be found. "
"Proceeding might lead to highly sub-optimal performance.")
add_definitions(-DMKLDNN_THR=MKLDNN_THR_SEQ)
endif()

if (MKLDNN_THREADING STREQUAL "OMP:COMP")
set(MKLIOMP5LIB "")
set(MKLIOMP5DLL "")
else()
use_intel_omp_rt()
endif()
add_definitions(-DMKLDNN_THR=MKLDNN_THR_OMP)
use_intel_omp_rt()
else()
# Compilation happens with OpenMP to enable `#pragma omp simd`
# but during linkage OpenMP dependency should be avoided
Expand Down
17 changes: 14 additions & 3 deletions cmake/options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,20 @@ option(WITH_EXAMPLE "builds examples" ON)
option(WITH_TEST "builds tests" ON)

set(MKLDNN_THREADING "OMP" CACHE STRING
"specifies threading type; supports OMP (default), or TBB.
If Intel(R) Threading Building Blocks (Intel(R) TBB) one should also
set TBBROOT (either environement variable or CMake option) to the library
"specifies threading type; supports OMP (default), OMP:COMP, OMP:INTEL, or TBB.
When OpenMP is used a user can choose what runtime to use:
- native OpenMP runtime that comes with the compiler (OMP:COMP), or
- Intel OpenMP runtime that is compatible with all the compilers that
Intel MKL-DNN supports (OMP:INTEL). This option requires Intel MKL
be installed or Intel MKL-ML library be downloaded. This option doesn't
work with MSVC (w/o Intel Compiler).
The default option is OMP, which gives a preference to OMP:INTEL, but if
neither Intel MKL is installed nor Intel MKL-ML is available then fallback
to OMP:COMP.
To use Intel(R) Threading Building Blocks (Intel(R) TBB) one should also
set TBBROOT (either environment variable or CMake option) to the library
location")

# =============
Expand Down
2 changes: 1 addition & 1 deletion src/cpu/gemm_u8s8s32x_convolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ ::execute_forward_thr(const int ithr, const int nthr,
dst[o] = qz_a1b0<float, dst_data_t>()(d, rmode);
};

# if _OPENMP >= 201307
# if MKLDNN_THR == MKLDNN_THR_OMP && _OPENMP >= 201307
# pragma omp parallel for simd
for (int o = 0; o < jcp.os * jcp.oc; ++o) body(o);
# else
Expand Down
2 changes: 1 addition & 1 deletion src/cpu/ref_rnn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ void _ref_rnn_common_t<aprop>::gates_reduction(int n_gates, int dic, int wic, in
};

// @todo block k on simd-width
#if (_OPENMP >= 201307) \
#if MKLDNN_THR == MKLDNN_THR_OMP && _OPENMP >= 201307 \
/* icc 17.0 has a problem with simd collapse */ \
&& !((defined __INTEL_COMPILER) && (__INTEL_COMPILER == 1700))
#pragma omp parallel for simd collapse(2)
Expand Down

0 comments on commit a17db4a

Please sign in to comment.