From 09afc419bc0f2e821b528d1e45e7a09c2a0c943d Mon Sep 17 00:00:00 2001 From: Matthias Hochsteger Date: Fri, 20 Dec 2019 18:07:54 +0100 Subject: [PATCH 1/3] [cmake] option USE_NATIVE_ARCH for Windows --- CMakeLists.txt | 58 ++++++++++++++++++++++++++++++++----- cmake/NetgenConfig.cmake.in | 1 + 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ee1b532a..2e3ccc70 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,9 +9,7 @@ else(WIN32) cmake_minimum_required(VERSION 3.8) endif(WIN32) -if(NOT WIN32) - option( USE_NATIVE_ARCH "build which -march=native" ON) -endif(NOT WIN32) +option( USE_NATIVE_ARCH "build for native cpu architecture" ON) option( USE_GUI "don't build netgen with GUI" ON ) option( USE_PYTHON "build with python interface" ON ) @@ -94,11 +92,6 @@ if(USE_CCACHE) endif(CCACHE_FOUND) endif(USE_CCACHE) -####################################################################### -if(USE_NATIVE_ARCH) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native") -endif(USE_NATIVE_ARCH) - ####################################################################### if(INTEL_MIC) set(MKL_ARCH "mic") @@ -411,6 +404,55 @@ if (USE_PYTHON) endif (USE_PYTHON) add_subdirectory(tests) +####################################################################### +if(USE_NATIVE_ARCH) + if(WIN32) + include(CheckCXXSourceRuns) + check_cxx_source_runs(" + #include + int main() + { + __m256d a{1.,2.,3.,4.}; + __m256d b{2.,0.,3.,5.}; + __m256d c = _mm256_mul_pd(a,b); + return 0; + } " NG_HAVE_AVX) + check_cxx_source_runs(" + #include + int main() + { + __m256i a{1,2,3,4}; + __m256i b{2,0,3,5}; + __m256i c = _mm256_cmpgt_epi64 (a,b); + return 0; + } " NG_HAVE_AVX2) + check_cxx_source_runs(" + #include + int main() + { + __m512d a{1.,2.,3.,4.}; + __m512d b{5.,6.,7.,8.}; + __m512d c = _mm512_mul_pd(a,b); + return 0; + } " NG_HAVE_AVX512) + + if(NG_HAVE_AVX512) + target_compile_options(ngcore PUBLIC "/arch:AVX512") + message(STATUS "Build for AVX512 CPU") + elseif(NG_HAVE_AVX2) + target_compile_options(ngcore PUBLIC "/arch:AVX2") + message(STATUS "Build for AVX2 CPU") + elseif(NG_HAVE_AVX) + target_compile_options(ngcore PUBLIC "/arch:AVX") + message(STATUS "Build for AVX CPU") + else() + message(STATUS "Build for generic CPU") + endif() + else() + target_compile_options(ngcore PUBLIC "-march=native") + endif(WIN32) +endif(USE_NATIVE_ARCH) + ####################################################################### # Debian packager diff --git a/cmake/NetgenConfig.cmake.in b/cmake/NetgenConfig.cmake.in index 0606332b..e8262666 100644 --- a/cmake/NetgenConfig.cmake.in +++ b/cmake/NetgenConfig.cmake.in @@ -54,6 +54,7 @@ set(NETGEN_USE_MPEG @USE_MPEG@) set(NETGEN_INTEL_MIC @INTEL_MIC@) set(NETGEN_INSTALL_PROFILES @INSTALL_PROFILES@) set(NETGEN_USE_CCACHE @USE_CCACHE@) +set(NETGEN_USE_NATIVE_ARCH @USE_NATIVE_ARCH@) set(NETGEN_PYTHON_RPATH "@NETGEN_PYTHON_RPATH@") set(NETGEN_RPATH_TOKEN "@NG_RPATH_TOKEN@") From ce90bd9e83d1e140f8bd12cb07371a4c16a882db Mon Sep 17 00:00:00 2001 From: Matthias Hochsteger Date: Thu, 16 Jan 2020 14:58:56 -0800 Subject: [PATCH 2/3] [cmake] Simplify compile flag handling on Windows Add options/define flags to ngcore, so they are automatically passed on to dependencies --- CMakeLists.txt | 4 ---- cmake/SuperBuild.cmake | 12 ------------ libsrc/core/CMakeLists.txt | 18 +++++++++--------- libsrc/geom2d/CMakeLists.txt | 3 ++- libsrc/occ/CMakeLists.txt | 1 + 5 files changed, 12 insertions(+), 26 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2e3ccc70..a4344075 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -215,13 +215,9 @@ endmacro() set(CMAKE_CXX_STANDARD 17) if(WIN32) - get_WIN32_WINNT(ver) - add_definitions(-D_WIN32_WINNT=${ver} -DWNT -DWNT_WINDOW -DNOMINMAX) set(CMAKE_MFC_FLAG 0) - add_definitions(-DMSVC_EXPRESS -D_CRT_SECURE_NO_WARNINGS -DHAVE_STRUCT_TIMESPEC) # build convenience (aka object) libraries in windows) set(NG_LIB_TYPE OBJECT) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP") else(WIN32) # build shared libraries set(NG_LIB_TYPE SHARED) diff --git a/cmake/SuperBuild.cmake b/cmake/SuperBuild.cmake index 36ec058f..a04e4164 100644 --- a/cmake/SuperBuild.cmake +++ b/cmake/SuperBuild.cmake @@ -20,18 +20,6 @@ if(WIN32) set (OCC_DOWNLOAD_URL_WIN "${DEPS_DOWNLOAD_URL}/occ_win64.zip" CACHE STRING INTERNAL) set (TCLTK_DOWNLOAD_URL_WIN "${DEPS_DOWNLOAD_URL}/tcltk_win64.zip" CACHE STRING INTERNAL) set (ZLIB_DOWNLOAD_URL_WIN "${DEPS_DOWNLOAD_URL}/zlib_win64.zip" CACHE STRING INTERNAL) - if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Intel") - string(REGEX REPLACE "/W[0-4]" "/W0" CMAKE_CXX_FLAGS_NEW ${CMAKE_CXX_FLAGS}) - set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS_NEW} CACHE STRING "compile flags" FORCE) - string(REGEX REPLACE "/W[0-4]" "/W0" CMAKE_CXX_FLAGS_NEW ${CMAKE_CXX_FLAGS_RELEASE}) - set(CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_NEW} CACHE STRING "compile flags" FORCE) - - string(REGEX REPLACE "/W[0-4]" "/W0" CMAKE_SHARED_LINKER_FLAGS_NEW ${CMAKE_SHARED_LINKER_FLAGS}) - set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS_NEW} /IGNORE:4217,4049" CACHE STRING "compile flags" FORCE) - string(REGEX REPLACE "/W[0-4]" "/W0" CMAKE_EXE_LINKER_FLAGS_NEW ${CMAKE_EXE_LINKER_FLAGS}) - set(CMAKE_EXE_LINKER_FLAGS"${CMAKE_EXE_LINKER_FLAGS_NEW}/IGNORE:4217,4049" CACHE STRING "compile flags" FORCE) - - endif(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Intel") endif(WIN32) if(UNIX) diff --git a/libsrc/core/CMakeLists.txt b/libsrc/core/CMakeLists.txt index 5c58e7e6..cb5eb36c 100644 --- a/libsrc/core/CMakeLists.txt +++ b/libsrc/core/CMakeLists.txt @@ -23,21 +23,21 @@ if(USE_PYTHON) target_compile_definitions(ngcore PUBLIC NETGEN_PYTHON NG_PYTHON) endif(USE_PYTHON) -target_compile_definitions(ngcore PRIVATE NGCORE_EXPORTS) -if(NOT WIN32) - target_compile_options(ngcore PRIVATE -fvisibility=hidden) -endif(NOT WIN32) - if(WIN32) - target_compile_options(ngcore PUBLIC /bigobj) + target_compile_options(ngcore PUBLIC /bigobj /MP /W1 /wd4068) + get_WIN32_WINNT(ver) + target_compile_definitions(ngcore PUBLIC _WIN32_WINNT=${ver} WNT WNT_WINDOW NOMINMAX MSVC_EXPRESS _CRT_SECURE_NO_WARNINGS HAVE_STRUCT_TIMESPEC) + target_link_options(ngcore PUBLIC /ignore:4273 /ignore:4217 /ignore:4049) +else(WIN32) + target_compile_options(ngcore PRIVATE -fvisibility=hidden) endif(WIN32) -target_compile_definitions(ngcore PUBLIC $<$:NETGEN_ENABLE_CHECK_RANGE>) +target_compile_definitions(ngcore PRIVATE NGCORE_EXPORTS) target_include_directories(ngcore INTERFACE $ $) -if(CHECK_RANGE) +if(CHECK_RANGE OR CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "DEBUG") target_compile_definitions(ngcore PUBLIC NETGEN_ENABLE_CHECK_RANGE) -endif(CHECK_RANGE) +endif(CHECK_RANGE OR CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "DEBUG") if(USE_SPDLOG) include_directories(${SPDLOG_INCLUDE_DIR}) diff --git a/libsrc/geom2d/CMakeLists.txt b/libsrc/geom2d/CMakeLists.txt index ab292315..f496448d 100644 --- a/libsrc/geom2d/CMakeLists.txt +++ b/libsrc/geom2d/CMakeLists.txt @@ -11,8 +11,9 @@ endif(NOT WIN32) if(USE_GUI) add_library(geom2dvis ${NG_LIB_TYPE} vsgeom2d.cpp) + target_link_libraries(geom2dvis PUBLIC ngcore) if(NOT WIN32) - target_link_libraries(geom2dvis geom2d) + target_link_libraries(geom2dvis PUBLIC geom2d) install( TARGETS geom2dvis ${NG_INSTALL_DIR}) endif(NOT WIN32) endif(USE_GUI) diff --git a/libsrc/occ/CMakeLists.txt b/libsrc/occ/CMakeLists.txt index 39da5a89..7e7a2a65 100644 --- a/libsrc/occ/CMakeLists.txt +++ b/libsrc/occ/CMakeLists.txt @@ -6,6 +6,7 @@ add_library(occ ${NG_LIB_TYPE} ) if(USE_GUI) add_library(occvis ${NG_LIB_TYPE} vsocc.cpp) + target_link_libraries(occvis PUBLIC ngcore) endif(USE_GUI) target_link_libraries(occ PUBLIC ngcore PRIVATE netgen_python) From a5ffcb73c15e4400b4404886f9341f00b837d05b Mon Sep 17 00:00:00 2001 From: Matthias Hochsteger Date: Fri, 17 Jan 2020 13:09:44 +0100 Subject: [PATCH 3/3] Build with '-fno-stack-check' on MacOS if USE_NATIVE_ACH=ON XCode 11.3 introduced a bug with stack alignment if AVX is activated: https://forums.developer.apple.com/thread/121887 According to the link, '-fno-stack-check' circumvents this problem --- CMakeLists.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index a4344075..009660aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -447,6 +447,10 @@ if(USE_NATIVE_ARCH) else() target_compile_options(ngcore PUBLIC "-march=native") endif(WIN32) + if(APPLE) + # work-around for bug in Xcode 11.3: https://forums.developer.apple.com/thread/121887 + target_compile_options(ngcore PUBLIC "-fno-stack-check") + endif(APPLE) endif(USE_NATIVE_ARCH)