Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
Fix MKL static link & default to static link on unix (#17751)
Browse files Browse the repository at this point in the history
* Fix MKL static link & default to static link on unix

Fixes #17641

* Test cmake MKL build on CI
  • Loading branch information
leezu committed Mar 4, 2020
1 parent 5cffa74 commit 6368977
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 43 deletions.
11 changes: 6 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -425,15 +425,13 @@ if(USE_OPENMP)
$<$<CXX_COMPILER_ID:MSVC>:/w>)
endfunction()

# This should build on Windows, but there's some problem and I don't have a Windows box, so
# could a Windows user please fix?
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/openmp/CMakeLists.txt
AND SYSTEM_ARCHITECTURE STREQUAL "x86_64"
AND NOT CMAKE_BUILD_TYPE STREQUAL "Distribution"
AND NOT BLAS STREQUAL "MKL"
AND NOT MSVC
AND NOT CMAKE_CROSSCOMPILING)
load_omp()
list(REMOVE_ITEM mxnet_LINKER_LIBS iomp5)
list(APPEND mxnet_LINKER_LIBS omp)
if(UNIX)
list(APPEND mxnet_LINKER_LIBS pthread)
Expand All @@ -446,8 +444,11 @@ if(USE_OPENMP)
if(OPENMP_FOUND)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
if(NOT BLAS STREQUAL "MKL")
# Linker flags for Intel OMP are already set in case MKL is used. Only set if not MKL
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif()
add_definitions(-DMXNET_USE_OPENMP=1)
endif()
endif()
Expand Down
42 changes: 20 additions & 22 deletions ci/docker/runtime_functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -453,18 +453,16 @@ build_ubuntu_cpu_openblas_make() {

build_ubuntu_cpu_mkl() {
set -ex
export CC="ccache gcc"
export CXX="ccache g++"
make \
DEV=1 \
USE_CPP_PACKAGE=1 \
USE_BLAS=mkl \
USE_TVM_OP=1 \
USE_MKLDNN=0 \
USE_INTEL_PATH=/opt/intel \
USE_DIST_KVSTORE=1 \
USE_SIGNAL_HANDLER=1 \
-j$(nproc)
cd /work/build
cmake \
-DCMAKE_BUILD_TYPE="RelWithDebInfo" \
-DUSE_MKLDNN=OFF \
-DUSE_CUDA=OFF \
-DUSE_TVM_OP=ON \
-DUSE_MKL_IF_AVAILABLE=ON \
-DUSE_BLAS=MKL \
-GNinja /work/mxnet
ninja
}

build_ubuntu_cpu_cmake_debug() {
Expand Down Expand Up @@ -644,16 +642,16 @@ build_ubuntu_cpu_mkldnn() {

build_ubuntu_cpu_mkldnn_mkl() {
set -ex
build_ccache_wrappers

make \
DEV=1 \
USE_CPP_PACKAGE=1 \
USE_TVM_OP=1 \
USE_BLAS=mkl \
USE_SIGNAL_HANDLER=1 \
USE_INTEL_PATH=/opt/intel/ \
-j$(nproc)
cd /work/build
cmake \
-DCMAKE_BUILD_TYPE="RelWithDebInfo" \
-DUSE_MKLDNN=ON \
-DUSE_CUDA=OFF \
-DUSE_TVM_OP=ON \
-DUSE_MKL_IF_AVAILABLE=ON \
-DUSE_BLAS=MKL \
-GNinja /work/mxnet
ninja
}

build_ubuntu_gpu() {
Expand Down
16 changes: 8 additions & 8 deletions ci/jenkins/Jenkins_steps.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,13 @@ def compile_unix_int64_gpu() {
}

def compile_unix_mkl_cpu() {
return ['CPU: MKL Makefile': {
return ['CPU: MKL': {
node(NODE_LINUX_CPU) {
ws('workspace/build-cpu-mkl') {
timeout(time: max_time, unit: 'MINUTES') {
utils.init_git()
utils.docker_run('ubuntu_cpu', 'build_ubuntu_cpu_mkl', false)
utils.pack_lib('cpu_mkl', mx_mkldnn_lib_make)
utils.pack_lib('cpu_mkl', mx_lib)
}
}
}
Expand Down Expand Up @@ -217,13 +217,13 @@ def compile_unix_mkldnn_cpu_make() {
}

def compile_unix_mkldnn_mkl_cpu() {
return ['CPU: MKLDNN_MKL Makefile': {
return ['CPU: MKLDNN_MKL': {
node(NODE_LINUX_CPU) {
ws('workspace/build-mkldnn-cpu') {
timeout(time: max_time, unit: 'MINUTES') {
utils.init_git()
utils.docker_run('ubuntu_cpu', 'build_ubuntu_cpu_mkldnn_mkl', false)
utils.pack_lib('mkldnn_mkl_cpu', mx_mkldnn_lib_make)
utils.pack_lib('mkldnn_mkl_cpu', mx_mkldnn_lib)
}
}
}
Expand Down Expand Up @@ -791,11 +791,11 @@ def test_unix_python3_cpu() {
}

def test_unix_python3_mkl_cpu() {
return ['Python3: MKL-CPU Makefile': {
return ['Python3: MKL-CPU': {
node(NODE_LINUX_CPU) {
ws('workspace/ut-python3-cpu') {
try {
utils.unpack_and_init('cpu_mkl', mx_lib_make)
utils.unpack_and_init('cpu_mkl', mx_lib)
python3_ut('ubuntu_cpu')
utils.publish_test_coverage()
} finally {
Expand Down Expand Up @@ -907,11 +907,11 @@ def test_unix_python3_mkldnn_cpu() {
}

def test_unix_python3_mkldnn_mkl_cpu() {
return ['Python3: MKLDNN-MKL-CPU Makefile': {
return ['Python3: MKLDNN-MKL-CPU': {
node(NODE_LINUX_CPU) {
ws('workspace/ut-python3-mkldnn-mkl-cpu') {
try {
utils.unpack_and_init('mkldnn_mkl_cpu', mx_mkldnn_lib_make)
utils.unpack_and_init('mkldnn_mkl_cpu', mx_lib)
python3_ut_mkldnn('ubuntu_cpu')
utils.publish_test_coverage()
} finally {
Expand Down
22 changes: 14 additions & 8 deletions cmake/Modules/FindMKL.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
#
# Options:
#
# USE_MKLDNN : Search for MKL:ML library variant
#
# MKL_USE_SINGLE_DYNAMIC_LIBRARY : use single dynamic library interface
# MKL_USE_STATIC_LIBS : use static libraries
# MKL_MULTI_THREADED : use multi-threading
Expand All @@ -45,8 +43,14 @@ set(INTEL_ROOT "/opt/intel" CACHE PATH "Folder contains intel libs")


# ---[ Options
option(MKL_USE_SINGLE_DYNAMIC_LIBRARY "Use single dynamic library interface" ON)
cmake_dependent_option(MKL_USE_STATIC_LIBS "Use static libraries" OFF "NOT MKL_USE_SINGLE_DYNAMIC_LIBRARY" OFF)
if(UNIX)
# Single dynamic library interface leads to conflicts between intel omp and llvm omp
# https://github.com/apache/incubator-mxnet/issues/17641
option(MKL_USE_SINGLE_DYNAMIC_LIBRARY "Use single dynamic library interface" OFF)
else()
option(MKL_USE_SINGLE_DYNAMIC_LIBRARY "Use single dynamic library interface" ON)
endif()
cmake_dependent_option(MKL_USE_STATIC_LIBS "Use static libraries" ON "NOT MKL_USE_SINGLE_DYNAMIC_LIBRARY" OFF)
cmake_dependent_option(MKL_MULTI_THREADED "Use multi-threading" ON "NOT MKL_USE_SINGLE_DYNAMIC_LIBRARY" OFF)
option(MKL_USE_ILP64 "Use ilp64 data model" OFF)
cmake_dependent_option(MKL_USE_CLUSTER "Use cluster functions" OFF "CMAKE_SIZEOF_VOID_P EQUAL 4" OFF)
Expand Down Expand Up @@ -122,7 +126,6 @@ set(INTEL_ROOT "/opt/intel" CACHE PATH "Folder contains intel libs")
list(APPEND MKL_LIBRARIES ${${__mkl_lib_upper}_LIBRARY})
endforeach()


if(NOT MKL_USE_SINGLE_DYNAMIC_LIBRARY)
if (MKL_USE_STATIC_LIBS)
set(__iomp5_libs iomp5 libiomp5mt.lib)
Expand All @@ -135,15 +138,18 @@ set(INTEL_ROOT "/opt/intel" CACHE PATH "Folder contains intel libs")
list(APPEND __looked_for INTEL_INCLUDE_DIR)
endif()

find_library(MKL_RTL_LIBRARY ${__iomp5_libs}
find_library(IOMP_LIBRARY ${__iomp5_libs}
PATHS ${INTEL_RTL_ROOT} ${INTEL_ROOT}/compiler ${MKL_ROOT}/.. ${MKL_ROOT}/../compiler
PATH_SUFFIXES ${__path_suffixes}
DOC "Path to Path to OpenMP runtime library")

list(APPEND __looked_for MKL_RTL_LIBRARY)
list(APPEND MKL_LIBRARIES ${MKL_RTL_LIBRARY})
list(APPEND __looked_for IOMP_LIBRARY)
list(APPEND MKL_LIBRARIES ${IOMP_LIBRARY})
endif()

if(MKL_USE_STATIC_LIBS AND UNIX)
set(MKL_LIBRARIES -Wl,--start-group "${MKL_LIBRARIES}" -Wl,--end-group)
endif()


include(FindPackageHandleStandardArgs)
Expand Down

0 comments on commit 6368977

Please sign in to comment.