From d4e1ffa6adde4402e28c4e9f4270b34d0618a8f7 Mon Sep 17 00:00:00 2001 From: Growl Date: Sun, 19 Jul 2026 14:24:24 +0800 Subject: [PATCH] Add CMake option CP2K_ENABLE_NATIVE_OPTIMIZATION --- CMakeLists.txt | 8 ++++++ cmake/CompilerConfiguration.cmake | 32 ++++++++++++++--------- docs/getting-started/build-from-source.md | 6 ++++- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e5e5b846e5..15eafb38a8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -112,6 +112,14 @@ set(CMAKE_INSTALL_LIBDIR # ================================================================================================= # OPTIONS + +# Native CPU optimization +string(TOUPPER "${CMAKE_BUILD_TYPE}" _CP2K_BUILD_TYPE) +cmake_dependent_option( + CP2K_ENABLE_NATIVE_OPTIMIZATION "Optimize for the build host CPU" ON + "NOT _CP2K_BUILD_TYPE STREQUAL GENERIC" OFF) +unset(_CP2K_BUILD_TYPE) + option(CMAKE_POSITION_INDEPENDENT_CODE "Enable position independent code" ON) option(CP2K_ENABLE_CONSISTENCY_CHECKS diff --git a/cmake/CompilerConfiguration.cmake b/cmake/CompilerConfiguration.cmake index 2d27dcb2a7..fd652a265e 100644 --- a/cmake/CompilerConfiguration.cmake +++ b/cmake/CompilerConfiguration.cmake @@ -9,6 +9,8 @@ set(CP2K_C_COMPILER_LIST "GNU;Intel;IntelLLVM;NAG;Cray;PGI;NVHPC;Clang;AppleClang") set(CP2K_Fortran_COMPILER_LIST "GNU;Intel;IntelLLVM;NAG;Cray;PGI;NVHPC") +include(CheckCompilerFlag) + if(NOT CMAKE_C_COMPILER_ID IN_LIST CP2K_C_COMPILER_LIST) message( WARNING @@ -50,20 +52,24 @@ add_compile_options( "$<$:-Wno-deprecated-declarations;-Wno-vla-parameter>" ) -# -- Apple Silicon + GCC: -march=native expands internally to -march=apple-m1 -# (invalid). Use -mcpu=native instead. -set(_CP2K_GNU_NATIVE_TUNE "-march=native;-mtune=native") -set(_CP2K_GNU_GENERIC_TUNE "-mtune=generic") -if(APPLE - AND CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64" - AND (CMAKE_Fortran_COMPILER_ID STREQUAL "GNU" - OR CMAKE_C_COMPILER_ID STREQUAL "GNU" - OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU")) - set(_CP2K_GNU_NATIVE_TUNE "-mcpu=native") -elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "ppc64|ppc64le") - set(_CP2K_GNU_NATIVE_TUNE "-mcpu=native") - set(_CP2K_GNU_GENERIC_TUNE "") +# Detect CPU native/generic tunes +set(_CP2K_GNU_NATIVE_TUNE "") +set(_CP2K_GNU_GENERIC_TUNE "") +check_compiler_flag(Fortran "-mtune=generic" CP2K_HAS_MTUNE_GENERIC) +if(CP2K_HAS_MTUNE_GENERIC) + set(_CP2K_GNU_GENERIC_TUNE "-mtune=generic") endif() +if(CP2K_ENABLE_NATIVE_OPTIMIZATION AND NOT CMAKE_CROSSCOMPILING) + foreach(_flag IN ITEMS -march=native -mcpu=native) + string(MAKE_C_IDENTIFIER "CP2K_HAS_${_flag}" _var) + check_compiler_flag(Fortran "${_flag}" ${_var}) + if(${_var}) + set(_CP2K_GNU_NATIVE_TUNE "${_flag}") + break() + endif() + endforeach() +endif() + if(APPLE) add_definitions(-D__MACOSX) endif() diff --git a/docs/getting-started/build-from-source.md b/docs/getting-started/build-from-source.md index cd22c0e327..8d5495164d 100644 --- a/docs/getting-started/build-from-source.md +++ b/docs/getting-started/build-from-source.md @@ -216,7 +216,8 @@ Here are some other important general options you may want to know: generator, which is also used by `make_cp2k.sh`; in this case, please ensure that Ninja is installed on your host system. - `-DCMAKE_BUILD_TYPE` Valid vaules are `Release` (default) and `Debug` (enables debug settings and - generates `pdbg` or `sdbg` instead of `psmp` or `ssmp`; recommended for development). + generates `pdbg` or `sdbg` instead of `psmp` or `ssmp`; recommended for development). CP2K also + supports setting `Generic` and `Coverage`. - `-DCMAKE_INSTALL_PREFIX` Specifies the installation path of CP2K. Assuming it is set to `/path/to/installation`, there will be several subdirectories: `bin` for binaries like `cp2k.psmp`, `include` for module files and headers, `lib` or `lib64` for libraries, and `share` @@ -227,6 +228,9 @@ Here are some other important general options you may want to know: Along with some options with CP2K: +- `-DCP2K_ENABLE_NATIVE_OPTIMIZATION` Decide if native flag (`-march=native` or `-mcpu-native`) is + used to trigger native optimization for the build host CPU. Only available when `CMAKE_BUILD_TYPE` + is not `Generic`. - `-DCP2K_USE_EVERYTHING` Enables all dependencies or not. - `-DCP2K_DATA_DIR` Specifies the location of the data of basis and potentials. Default is `/path/to/installation/share/cp2k/data`.