From aa993e95c96c839f483af14de83371df77dbd21a Mon Sep 17 00:00:00 2001 From: Yulong Wang Date: Thu, 13 Aug 2020 15:56:42 -0700 Subject: [PATCH] enable build flag '--use_openmp' on MacOS (#4774) * enable build flag '--use_openmp' on MacOS * cmake 3.16.1 to enable find_package(OpenMP) on mac --- BUILD.md | 8 ++++---- README.md | 1 + cmake/CMakeLists.txt | 6 ++++++ cmake/onnxruntime_unittests.cmake | 3 +++ .../github/azure-pipelines/templates/mac-ci.yml | 1 + .../github/linux/docker/scripts/install_deps.sh | 10 +++++----- 6 files changed, 20 insertions(+), 9 deletions(-) diff --git a/BUILD.md b/BUILD.md index c6cfe5f39d..b97ef6fe43 100644 --- a/BUILD.md +++ b/BUILD.md @@ -584,7 +584,7 @@ The Vitis-AI execution provider is only supported on Linux. .\build.bat --use_openmp ``` -##### Linux +##### Linux/Mac OS X ``` ./build.sh --use_openmp @@ -866,10 +866,10 @@ pip3 install numpy # Build the latest cmake mkdir /code cd /code -wget https://cmake.org/files/v3.13/cmake-3.13.5.tar.gz; -tar zxf cmake-3.13.5.tar.gz +wget https://cmake.org/files/v3.13/cmake-3.16.1.tar.gz; +tar zxf cmake-3.16.1.tar.gz -cd /code/cmake-3.13.5 +cd /code/cmake-3.16.1 ./configure --system-curl make sudo make install diff --git a/README.md b/README.md index 7226f0005e..3fa4e599c6 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,7 @@ The following are required for usage of the official published packages. * For Windows, **OpenMP** support comes as part of VC runtime. It is also available as redist packages: [vc_redist.x64.exe](https://aka.ms/vs/16/release/vc_redist.x64.exe) and [vc_redist.x86.exe](https://aka.ms/vs/16/release/vc_redist.x86.exe) * For Linux, the system must have **libgomp.so.1** which can be installed using `apt-get install libgomp1`. + * For Mac OS X, the system must have **libomp.dylib** which can be installed using `brew install libomp`. * Default GPU (CUDA) * The default GPU build requires CUDA runtime libraries being installed on the system: diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 6390b5a429..f0a07dfb93 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -175,6 +175,7 @@ if(onnxruntime_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}") + include_directories(${OpenMP_CXX_INCLUDE_DIR}) # MKLML and NGraph depend on their own OpenMP library that may be different with the compiler's. # Disable the options to build mklml/NGraph and OpenMP together. if(onnxruntime_USE_MKLML) @@ -184,6 +185,7 @@ if(onnxruntime_USE_OPENMP) message(FATAL_ERROR "Please use only one of onnxruntime_USE_NGRAPH, onnxruntime_USE_OPENMP") endif() else() + message(WARNING "Flag --use_openmp is specified, but OpenMP is not found in current build environment. Setting it to OFF.") set(onnxruntime_USE_OPENMP OFF) endif() endif() @@ -1112,6 +1114,10 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Android") list(APPEND onnxruntime_EXTERNAL_LIBRARIES log) endif() +if (onnxruntime_USE_OPENMP) + list(APPEND onnxruntime_EXTERNAL_LIBRARIES OpenMP::OpenMP_CXX) +endif() + if(WIN32) list(APPEND onnxruntime_EXTERNAL_LIBRARIES ${SYS_PATH_LIB} Shlwapi) list(APPEND onnxruntime_EXTERNAL_LIBRARIES debug Dbghelp) diff --git a/cmake/onnxruntime_unittests.cmake b/cmake/onnxruntime_unittests.cmake index 68e2cfe0a8..fcc40dd950 100644 --- a/cmake/onnxruntime_unittests.cmake +++ b/cmake/onnxruntime_unittests.cmake @@ -835,6 +835,9 @@ set(onnxruntime_mlas_test_libs onnxruntime_mlas onnxruntime_common) if(NOT WIN32) list(APPEND onnxruntime_mlas_test_libs nsync_cpp ${CMAKE_DL_LIBS}) endif() +if (onnxruntime_USE_OPENMP) + list(APPEND onnxruntime_mlas_test_libs OpenMP::OpenMP_CXX) +endif() list(APPEND onnxruntime_mlas_test_libs Threads::Threads) target_link_libraries(onnxruntime_mlas_test PRIVATE ${onnxruntime_mlas_test_libs}) if(WIN32) diff --git a/tools/ci_build/github/azure-pipelines/templates/mac-ci.yml b/tools/ci_build/github/azure-pipelines/templates/mac-ci.yml index c99e70b606..6ce79b3f3d 100644 --- a/tools/ci_build/github/azure-pipelines/templates/mac-ci.yml +++ b/tools/ci_build/github/azure-pipelines/templates/mac-ci.yml @@ -28,6 +28,7 @@ jobs: sudo python3 -m pip install -r '$(Build.SourcesDirectory)/tools/ci_build/github/linux/docker/scripts/requirements.txt' sudo xcode-select --switch /Applications/Xcode_10.app/Contents/Developer + brew install libomp ${{ parameters.BuildCommand }} displayName: 'Build and Test OnnxRuntime lib for MacOS' - task: PublishTestResults@2 diff --git a/tools/ci_build/github/linux/docker/scripts/install_deps.sh b/tools/ci_build/github/linux/docker/scripts/install_deps.sh index 5aaba20d7e..53f5ffb029 100755 --- a/tools/ci_build/github/linux/docker/scripts/install_deps.sh +++ b/tools/ci_build/github/linux/docker/scripts/install_deps.sh @@ -80,17 +80,17 @@ if [[ $SYS_LONG_BIT = "64" && "$GLIBC_VERSION" -gt "9" ]]; then tar --strip 1 -xf /tmp/azcopy/azcopy.tar.gz -C /tmp/azcopy cp /tmp/azcopy/azcopy /usr/bin echo "Installing cmake" - GetFile https://github.com/Kitware/CMake/releases/download/v3.13.5/cmake-3.13.5-Linux-x86_64.tar.gz /tmp/src/cmake-3.13.5-Linux-x86_64.tar.gz - tar -zxf /tmp/src/cmake-3.13.5-Linux-x86_64.tar.gz --strip=1 -C /usr + GetFile https://github.com/Kitware/CMake/releases/download/v3.16.1/cmake-3.16.1-Linux-x86_64.tar.gz /tmp/src/cmake-3.16.1-Linux-x86_64.tar.gz + tar -zxf /tmp/src/cmake-3.16.1-Linux-x86_64.tar.gz --strip=1 -C /usr echo "Installing Node.js" GetFile https://nodejs.org/dist/v12.16.3/node-v12.16.3-linux-x64.tar.xz /tmp/src/node-v12.16.3-linux-x64.tar.xz tar -xf /tmp/src/node-v12.16.3-linux-x64.tar.xz --strip=1 -C /usr else echo "Installing cmake" - GetFile https://github.com/Kitware/CMake/releases/download/v3.13.5/cmake-3.13.5.tar.gz /tmp/src/cmake-3.13.5.tar.gz - tar -xf /tmp/src/cmake-3.13.5.tar.gz -C /tmp/src + GetFile https://github.com/Kitware/CMake/releases/download/v3.16.1/cmake-3.16.1.tar.gz /tmp/src/cmake-3.16.1.tar.gz + tar -xf /tmp/src/cmake-3.16.1.tar.gz -C /tmp/src pushd . - cd /tmp/src/cmake-3.13.5 + cd /tmp/src/cmake-3.16.1 ./bootstrap --prefix=/usr --parallel=$(getconf _NPROCESSORS_ONLN) --system-bzip2 --system-curl --system-zlib --system-expat make -j$(getconf _NPROCESSORS_ONLN) make install