Skip to content

Commit

Permalink
Update OpenMP cmake setting for xcode 9 compiler(AppleClang 9.0) (pyt…
Browse files Browse the repository at this point in the history
…orch#14473)

Summary:
Original PR: pytorch#11563
Pull Request resolved: pytorch#14473

Differential Revision: D13234208

Pulled By: ezyang

fbshipit-source-id: 7d874c63659e93728af239ecdfb85547613e52ad
  • Loading branch information
JerryShih authored and facebook-github-bot committed Nov 28, 2018
1 parent 302caef commit 8901935
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pytorch_linux_test_defaults: &pytorch_linux_test_defaults
<<: *setup_ci_environment
- run:
name: Test
no_output_timeout: "1h"
no_output_timeout: "90m"
command: |
set -e
export COMMIT_DOCKER_IMAGE=${DOCKER_IMAGE}-${CIRCLE_SHA1}
Expand Down
12 changes: 11 additions & 1 deletion .jenkins/pytorch/build-asan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,18 @@ clang --version
# symbolize=1: Gives us much better errors when things go wrong
export ASAN_OPTIONS=detect_leaks=0:symbolize=1

# FIXME: Remove the hardcoded "-pthread" option.
# With asan build, the cmake thread CMAKE_HAVE_LIBC_CREATE[1] checking will
# succeed because "pthread_create" is in libasan.so. However, libasan doesn't
# have the full pthread implementation. Other advanced pthread functions doesn't
# exist in libasan.so[2]. If we need some pthread advanced functions, we still
# need to link the pthread library.
# [1] https://github.com/Kitware/CMake/blob/8cabaaf054a16ea9c8332ce8e9291bd026b38c62/Modules/FindThreads.cmake#L135
# [2] https://wiki.gentoo.org/wiki/AddressSanitizer/Problems
#
# TODO: Make the ASAN flags a more unified env var
CC="clang" CXX="clang++" LDSHARED="clang --shared" \
CFLAGS="-fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all -shared-libasan" \
CFLAGS="-fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all -shared-libasan -pthread" \
CXX_FLAGS="-pthread" \
NO_CUDA=1 USE_MKLDNN=0 \
python setup.py install
10 changes: 9 additions & 1 deletion caffe2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,16 @@ if(NOT BUILD_ATEN_ONLY)
else()
target_link_libraries(caffe2 PUBLIC protobuf::libprotobuf)
endif()
endif()

#cmake only check for separate OpenMP library on AppleClang 7+
#https://github.com/Kitware/CMake/blob/42212f7539040139ecec092547b7d58ef12a4d72/Modules/FindOpenMP.cmake#L252
if (WITH_OPENMP AND CMAKE_CXX_COMPILER_ID MATCHES "AppleClang")
if (CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL "7.0" OR
CMAKE_CXX_COMPILER_VERSION VERSION_GREATER "7.0")
target_link_libraries(caffe2 PRIVATE ${OpenMP_libomp_LIBRARY})
endif()
endif()
endif()
target_link_libraries(caffe2 PUBLIC c10)
target_link_libraries(caffe2 PUBLIC ${Caffe2_PUBLIC_DEPENDENCY_LIBS})
target_link_libraries(caffe2 PRIVATE ${Caffe2_DEPENDENCY_LIBS})
Expand Down
65 changes: 27 additions & 38 deletions cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -627,12 +627,37 @@ endif()

# ---[ OpenMP
if(USE_OPENMP)
find_package(OpenMP)
set(WITH_OPENMP ON CACHE BOOL "OpenMP support if available?")
if(APPLE AND CMAKE_COMPILER_IS_GNUCC)
exec_program(uname ARGS -v OUTPUT_VARIABLE DARWIN_VERSION)
string(REGEX MATCH "[0-9]+" DARWIN_VERSION ${DARWIN_VERSION})
message(STATUS "MAC OS Darwin Version: ${DARWIN_VERSION}")
if(DARWIN_VERSION GREATER 9)
set(APPLE_OPENMP_SUCKS 1)
endif(DARWIN_VERSION GREATER 9)
execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
OUTPUT_VARIABLE GCC_VERSION)
if(APPLE_OPENMP_SUCKS AND GCC_VERSION VERSION_LESS 4.6.2)
message(STATUS "Warning: Disabling OpenMP (unstable with this version of GCC)")
message(STATUS " Install GCC >= 4.6.2 or change your OS to enable OpenMP")
add_compile_options(-Wno-unknown-pragmas)
set(WITH_OPENMP OFF CACHE BOOL "OpenMP support if available?" FORCE)
endif()
endif()

if(WITH_OPENMP AND NOT CHECKED_OPENMP)
find_package(OpenMP)
set(CHECKED_OPENMP ON CACHE BOOL "already checked for OpenMP")

# OPENMP_FOUND is not cached in FindOpenMP.cmake (all other variables are cached)
# see https://github.com/Kitware/CMake/blob/master/Modules/FindOpenMP.cmake
set(OPENMP_FOUND ${OPENMP_FOUND} CACHE BOOL "OpenMP Support found")
endif()

if(OPENMP_FOUND)
message(STATUS "Adding " ${OpenMP_CXX_FLAGS})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
else()
message(WARNING "Not compiling with OpenMP. Suppress this warning with -DUSE_OPENMP=OFF")
caffe2_update_option(USE_OPENMP OFF)
Expand Down Expand Up @@ -1085,42 +1110,6 @@ if (NOT BUILD_ATEN_MOBILE)
STRING(REPLACE "-DNDEBUG" "" CMAKE_CXX_FLAGS_DEBUG "" ${CMAKE_CXX_FLAGS_DEBUG})
STRING(REPLACE "-DNDEBUG" "" CMAKE_CXX_FLAGS_RELEASE "" ${CMAKE_CXX_FLAGS_RELEASE})
ENDIF()

# OpenMP support?
SET(WITH_OPENMP ON CACHE BOOL "OpenMP support if available?")
IF (APPLE AND CMAKE_COMPILER_IS_GNUCC)
EXEC_PROGRAM (uname ARGS -v OUTPUT_VARIABLE DARWIN_VERSION)
STRING (REGEX MATCH "[0-9]+" DARWIN_VERSION ${DARWIN_VERSION})
MESSAGE (STATUS "MAC OS Darwin Version: ${DARWIN_VERSION}")
IF (DARWIN_VERSION GREATER 9)
SET(APPLE_OPENMP_SUCKS 1)
ENDIF (DARWIN_VERSION GREATER 9)
EXECUTE_PROCESS (COMMAND ${CMAKE_C_COMPILER} -dumpversion
OUTPUT_VARIABLE GCC_VERSION)
IF (APPLE_OPENMP_SUCKS AND GCC_VERSION VERSION_LESS 4.6.2)
MESSAGE(STATUS "Warning: Disabling OpenMP (unstable with this version of GCC)")
MESSAGE(STATUS " Install GCC >= 4.6.2 or change your OS to enable OpenMP")
add_compile_options(-Wno-unknown-pragmas)
SET(WITH_OPENMP OFF CACHE BOOL "OpenMP support if available?" FORCE)
ENDIF()
ENDIF()

IF (WITH_OPENMP AND NOT CHECKED_OPENMP)
FIND_PACKAGE(OpenMP)
SET(CHECKED_OPENMP ON CACHE BOOL "already checked for OpenMP")

# OPENMP_FOUND is not cached in FindOpenMP.cmake (all other variables are cached)
# see https://github.com/Kitware/CMake/blob/master/Modules/FindOpenMP.cmake
SET(OPENMP_FOUND ${OPENMP_FOUND} CACHE BOOL "OpenMP Support found")
ENDIF()

IF (OPENMP_FOUND)
MESSAGE(STATUS "Compiling with OpenMP support")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
ENDIF()


SET(CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE OFF)

FIND_PACKAGE(MAGMA)
Expand Down
13 changes: 11 additions & 2 deletions modules/detectron/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ file(GLOB Detectron_CPU_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/*.cc)
file(GLOB Detectron_GPU_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/*.cu)

if (BUILD_CAFFE2_OPS)
#cmake only check for separate OpenMP library on AppleClang 7+
#https://github.com/Kitware/CMake/blob/42212f7539040139ecec092547b7d58ef12a4d72/Modules/FindOpenMP.cmake#L252
if (WITH_OPENMP AND CMAKE_CXX_COMPILER_ID MATCHES "AppleClang")
if (CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL "7.0" OR
CMAKE_CXX_COMPILER_VERSION VERSION_GREATER "7.0")
Set(OpenMP_link ${OpenMP_libomp_LIBRARY})
endif()
endif()

# Note(ilijar): Since Detectron ops currently have no
# CPU implementation, we only build GPU ops for now.
if (USE_CUDA)
Expand All @@ -10,11 +19,11 @@ if (BUILD_CAFFE2_OPS)
${Detectron_CPU_SRCS}
${Detectron_GPU_SRCS})

target_link_libraries(caffe2_detectron_ops_gpu caffe2_gpu)
target_link_libraries(caffe2_detectron_ops_gpu caffe2_gpu ${OpenMP_link})
install(TARGETS caffe2_detectron_ops_gpu DESTINATION lib)
elseif(NOT IOS_PLATFORM)
add_library(caffe2_detectron_ops SHARED ${Detectron_CPU_SRCS})
target_link_libraries(caffe2_detectron_ops caffe2)
target_link_libraries(caffe2_detectron_ops caffe2 ${OpenMP_link})
install(TARGETS caffe2_detectron_ops DESTINATION lib)
endif()
endif()
13 changes: 10 additions & 3 deletions torch/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ if (CAFFE2_CMAKE_BUILDING_WITH_MAIN_REPO)
return()
endif()
else()
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(torch CXX C)
find_package(Caffe2 REQUIRED)
option(USE_CUDA "Use CUDA" ON)
Expand Down Expand Up @@ -333,8 +333,15 @@ if(OPENMP_FOUND)
if (VERBOSE)
message(STATUS "Compiling with OpenMP")
endif()
target_compile_options(torch INTERFACE -fopenmp)
target_link_libraries(torch -fopenmp)
target_compile_options(torch INTERFACE ${OpenMP_CXX_FLAGS})
#cmake only check for separate OpenMP library on AppleClang 7+
#https://github.com/Kitware/CMake/blob/42212f7539040139ecec092547b7d58ef12a4d72/Modules/FindOpenMP.cmake#L252
if (CMAKE_CXX_COMPILER_ID MATCHES "AppleClang")
if (CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL "7.0" OR
CMAKE_CXX_COMPILER_VERSION VERSION_GREATER "7.0")
target_link_libraries(torch ${OpenMP_libomp_LIBRARY})
endif()
endif()
endif()

if (NOT NO_API AND NOT USE_ROCM)
Expand Down

0 comments on commit 8901935

Please sign in to comment.