macOS: add support for OpenMP, making smoke/fluid/cloth simulations faster.
authorArto Kitula <arto.kitula@gmail.com>
Sat, 26 Jan 2019 13:14:51 +0000 (14:14 +0100)
committerBrecht Van Lommel <brechtvanlommel@gmail.com>
Sat, 26 Jan 2019 14:48:56 +0000 (15:48 +0100)
This bring macOS on par with Windows and Linux. It uses the OpenMP library
added to our precompiled libraries.

Custom flags are set because FindOpenMP from CMake below 3.12 does not support
AppleClang, and more recent versions do not work with our custom directory
location either.

Differential Revision: https://developer.blender.org/D4257

CMakeLists.txt
build_files/build_environment/cmake/harvest.cmake
build_files/build_environment/cmake/openmp.cmake
build_files/cmake/platform/platform_apple.cmake
source/creator/CMakeLists.txt

index 2341a53560350534f1d0be18592b8eb9e74e5f8e..a71677c030824f6157b2afc3dfeb470828b0e297 100644 (file)
@@ -1177,7 +1177,10 @@ endif()
 #-----------------------------------------------------------------------------
 # Configure OpenMP.
 if(WITH_OPENMP)
-       find_package(OpenMP)
+       if(NOT OPENMP_CUSTOM)
+               find_package(OpenMP)
+       endif()
+
        if(OPENMP_FOUND)
                if(NOT WITH_OPENMP_STATIC)
                        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
index d79d91f2b90c4697eafee17ad7ae56b6e02ed8a3..a281eb900e3e62facf1c3ccf92f7e190b92d41ff 100644 (file)
@@ -131,6 +131,10 @@ harvest(lame/lib ffmpeg/lib "*.a")
 harvest(clang/bin llvm/bin "clang-format")
 harvest(llvm/bin llvm/bin "llvm-config")
 harvest(llvm/lib llvm/lib "libLLVM*.a")
+if(APPLE)
+       harvest(openmp/lib openmp/lib "*")
+       harvest(openmp/include openmp/include "*.h")
+endif()
 harvest(ogg/lib ffmpeg/lib "*.a")
 harvest(openal/include openal/include "*.h")
 if(UNIX AND NOT APPLE)
index ba8e6248126b39bc25b384c88699145a0cc41072..d8224020ecfa7399fd17ff9d0060a9ff0a84ce16 100644 (file)
@@ -23,7 +23,8 @@ ExternalProject_Add(external_openmp
        URL_HASH MD5=${OPENMP_HASH}
        PREFIX ${BUILD_DIR}/openmp
        CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${LIBDIR}/openmp ${DEFAULT_CMAKE_FLAGS}
-       INSTALL_DIR ${LIBDIR}/clang
+       INSTALL_COMMAND cd ${BUILD_DIR}/openmp/src/external_openmp-build && install_name_tool -id '@executable_path/../Resources/lib/libomp.dylib' runtime/src/libomp.dylib && make install
+       INSTALL_PATH ${LIBDIR}/openmp
 )
 
 add_dependencies(
index 4e78737fd9efa5076b6449188928a6dc697562b1..1b3b844642eff0581edb2f649469c9c2f6f6f481 100644 (file)
@@ -383,13 +383,22 @@ if(WITH_CYCLES_EMBREE)
        set(PLATFORM_LINKFLAGS "${PLATFORM_LINKFLAGS} -Xlinker -stack_size -Xlinker 0x100000")
 endif()
 
+# CMake FindOpenMP doesn't know about AppleClang before 3.12, so provide custom flags.
 if(WITH_OPENMP)
-       execute_process(COMMAND ${CMAKE_C_COMPILER} --version OUTPUT_VARIABLE COMPILER_VENDOR)
-       string(SUBSTRING "${COMPILER_VENDOR}" 0 5 VENDOR_NAME) # truncate output
-       if(${VENDOR_NAME} MATCHES "Apple") # Apple does not support OpenMP reliable with gcc and not with clang
-               set(WITH_OPENMP OFF)
-       else() # vanilla gcc or clang_omp support OpenMP
-               message(STATUS "Using special OpenMP enabled compiler !") # letting find_package(OpenMP) module work for gcc
+       if(CMAKE_C_COMPILER_ID MATCHES "AppleClang" AND CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "7.0")
+               # Use OpenMP from our precompiled libraries.
+               message(STATUS "Using ${LIBDIR}/openmp for OpenMP")
+               set(OPENMP_CUSTOM ON)
+               set(OPENMP_FOUND ON)
+               set(OpenMP_C_FLAGS "-Xclang -fopenmp -I${LIBDIR}/openmp/include")
+               set(OpenMP_CXX_FLAGS "-Xclang -fopenmp -I${LIBDIR}/openmp/include")
+               set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L${LIBDIR}/openmp/lib -lomp")
+
+               # Copy libomp.dylib to allow executables like datatoc to work.
+               execute_process(
+                       COMMAND mkdir -p ${CMAKE_BINARY_DIR}/Resources/lib
+                       COMMAND cp -p ${LIBDIR}/openmp/lib/libomp.dylib ${CMAKE_BINARY_DIR}/Resources/lib/libomp.dylib
+               )
        endif()
 endif()
 
index a553b8a569b0a846126bd818b8e9d2621dc33298..d2255bbd89409cf9df14504d390575ef316815fb 100644 (file)
@@ -914,6 +914,13 @@ elseif(APPLE)
                blender.app/Contents/
        )
 
+       if(WITH_OPENMP AND OPENMP_CUSTOM)
+               install(
+                       FILES ${LIBDIR}/openmp/lib/libomp.dylib
+                       DESTINATION blender.app/Contents/Resources/lib
+               )
+       endif()
+
        if(WITH_LLVM AND NOT LLVM_STATIC)
                install(
                        FILES ${LIBDIR}/llvm/lib/libLLVM-3.4.dylib