Add CMake option CP2K_ENABLE_NATIVE_OPTIMIZATION

This commit is contained in:
Growl 2026-07-19 14:24:24 +08:00
parent 996af22233
commit d4e1ffa6ad
3 changed files with 32 additions and 14 deletions

View file

@ -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

View file

@ -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(
"$<$<COMPILE_LANG_AND_ID:C,GNU>:-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()

View file

@ -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`.