Skip to content

Commit

Permalink
[cmake] cmake fix
Browse files Browse the repository at this point in the history
  -  Fix docker recipe for cmake
  -  add nvhpc support (module provided by nvidia)
  -  fix a spelling mistake in FindPlumed.cmake
  -  added an interface target for libraries that are common to all modules.
  -  use lists of target whenever cmake functions supports them otherwise use for loops for properties common to all targets
  -  use CMAKE_HIP_ARCHITECTURES and CMAKE_CUDA_ARCHITECTURES instead of custom variables
  -  update the full source file list.
  -  removed the all CMakeLists.txt except the file in the root and in src.
  • Loading branch information
mtaillefumier authored and oschuett committed Mar 1, 2023
1 parent 764ce72 commit f24923d
Show file tree
Hide file tree
Showing 13 changed files with 1,746 additions and 347 deletions.
38 changes: 29 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ endforeach()
# =================================================================================================
# OPTIONS

option(CP2K_ENABLE_REGTESTS
"Enable installation of the binaries for running regtests afterwards"
OFF)
option(CMAKE_POSITION_INDEPENDENT_CODE "Enable position independent code" ON)
option(CP2K_DEBUG_MODE "Enable several additional options for debugging cp2k."
OFF)
Expand Down Expand Up @@ -101,7 +104,7 @@ option(CP2K_USE_LIBXSMM "Use libxsmm for small gemms (supports x86 platforms)"
ON)

cmake_dependent_option(CP2K_ENABLE_ELPA_OPENMP_SUPPORT
"Enable elpa openmp support" OFF "CP2K_USE_ELPA" OFF)
"Enable elpa openmp support" ON "CP2K_USE_ELPA" OFF)
cmake_dependent_option(CP2K_ENABLE_FFTW3_OPENMP_SUPPORT
"Enable FFTW openmp support" ON "CP2K_USE_FFTW3" OFF)
cmake_dependent_option(CP2K_ENABLE_FFTW3_THREADS_SUPPORT
Expand Down Expand Up @@ -154,6 +157,9 @@ set(CP2K_USE_ACCEL
set_property(CACHE CP2K_USE_ACCEL
PROPERTY STRINGS ${CP2K_SUPPORTED_ACCELERATION_TARGETS})

cmake_dependent_option(CP2K_USE_NVHPC OFF "Enable Nvidia NVHPC kit"
"(NOT CP2K_USE_ACCEL MATCHES \"CUDA\")" OFF)

cmake_dependent_option(
CP2K_USE_SPLA_GEMM_OFFLOADING ON
"Enable SPLA dgemm offloading (only valid with gpu support on)"
Expand Down Expand Up @@ -330,7 +336,11 @@ if(CP2K_USE_ACCEL MATCHES "CUDA")
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -allow-unsupported-compiler")

enable_language(CUDA)
find_package(CUDAToolkit REQUIRED)
if(CP2K_USE_NVHPC)
find_package(NVHPC REQUIRED COMPONENTS CUDA MATH HOSTUTILS NCCL)
else()
find_package(CUDAToolkit REQUIRED)
endif()

list(FIND CP2K_SUPPORTED_CUDA_ARCHITECTURES ${CP2K_WITH_GPU}
CP2K_GPU_SUPPORTED)
Expand All @@ -342,13 +352,10 @@ if(CP2K_USE_ACCEL MATCHES "CUDA")
)
endif()

# set cuda architecture number and compilation flags. Taken from dbcsr
set(CP2K_ACC_ARCH_NUMBER ${CP2K_GPU_ARCH_NUMBER_${CP2K_WITH_GPU}})
set(CMAKE_CUDA_ARCHITECTURES ${CP2K_ACC_ARCH_NUMBER})
set(CUDA_ARCHITECTURES ${CP2K_ACC_ARCH_NUMBER})
set(CMAKE_CUDA_ARCHITECTURES ${CP2K_GPU_ARCH_NUMBER_${CP2K_WITH_GPU}})

message(STATUS "GPU target architecture: " ${CP2K_WITH_GPU})
message(STATUS "GPU architecture number: " ${CP2K_ACC_ARCH_NUMBER})
message(STATUS "GPU architecture number: " ${CMAKE_CUDA_ARCHITECTURES})
message(STATUS "GPU profiling enabled: " ${CP2K_WITH_CUDA_PROFILING})

if(WITH_CUDA_PROFILING)
Expand Down Expand Up @@ -379,7 +386,7 @@ elseif(CP2K_USE_ACCEL MATCHES "HIP")
set(HIP_RELEASE_OPTIONS "-O0 -g --std=c++11")
endif()

set(CP2K_ACC_ARCH_NUMBER ${CP2K_GPU_ARCH_NUMBER_${CP2K_WITH_GPU}})
set(CMAKE_HIP_ARCGITECTURES "${CP2K_GPU_ARCH_NUMBER_${CP2K_WITH_GPU}}")
set(CP2K_USE_HIP ON)
endif()

Expand Down Expand Up @@ -772,7 +779,7 @@ if(NOT CP2K_USE_ELPA)
endif()

if(NOT CP2K_USE_PLUMMED)
message(STATUS " - PLUMMED")
message(STATUS " - PLUMED")
endif()

if(NOT CP2K_USE_QUIP)
Expand Down Expand Up @@ -813,6 +820,19 @@ endif()

message(STATUS "\n\n")

message(
STATUS
"To run the regtests you need to run cmake with the additional flag -DCP2K_ENABLE_REGTESTS=ON"
)
message(
STATUS
"cmake will copy the necessary binaries to the right directory and then regtests can be ran as"
)
message(
STATUS
"usual. It is a temporary fix until the regtests properly integrate cmake build systems types."
)

# files needed for cmake

write_basic_package_version_file(
Expand Down
14 changes: 13 additions & 1 deletion README_cmake.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ tested with ROCM 5.1.x but this version shows performance regression and should
be avoided. The Jiting capabilities of ROCM 5.2.x do not work properly which
affects DBCSR. It is highly recommended to update ROCM to the latest version to
avoid all these issues. CP2K can be built with ROCM 5.2.x but GPU support in
DBCSR should be tunred off otherwise a crash should be expected.
DBCSR should be turned off otherwise a crash should be expected. ROCM 5.3.x and
later seems to work fine.

## Threading with blas and lapack

Expand Down Expand Up @@ -189,3 +190,14 @@ What is known to fail sometimes
present. `-DCP2k_BLAS_VENDOR=OpenBLAS` will also help CMake to find OpenBLAS if
it is used. Detecting the scalapack library might also fail if the user
environment is not properly set up.

- BLAS / LAPACK / SCALAPACK: It is possible to set up BLAS / LAPACK / SCALAPACK
libraries manually. The following command

```shell
cmake -DCP2K_BLAS_LINK_LIBRARIES=libmyblas.so -DCP2K_BLAS_VENDOR=CUSTOM
-DCP2K_LAPACK_LINK_LIBRARIES=libmylapack.so -DCP2K_SCALAPACK_VENDOR=GENERIC
-DCP2K_SCALAPACK_LIBRARIES=libscalapack.so
```

is an illustration of such functionality (it is still experimental).
72 changes: 40 additions & 32 deletions cmake/FindBlas.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,13 @@ set(CP2K_BLAS_VENDOR_LIST
"GenericBLAS"
"Armpl"
"FlexiBLAS"
"Atlas")
"Atlas"
"NVHPCBlas"
"CUSTOM")

set(__BLAS_VENDOR_LIST
"MKL"
"OpenBLAS"
"SCI"
"GenericBLAS"
"Armpl"
"FlexiBLAS"
"Atlas")
set(__BLAS_VENDOR_LIST ${CP2K_BLAS_VENDOR_LIST})
list(REMOVE_ITEM __BLAS_VENDOR_LIST "auto")
list(REMOVE_ITEM __BLAS_VENDOR_LIST "CUSTOM")

set(CP2K_BLAS_VENDOR
"auto"
Expand Down Expand Up @@ -76,7 +73,7 @@ set(CP2K_BLAS_FOUND FALSE)

# first check for a specific implementation if requested

if(NOT CP2K_BLAS_VENDOR MATCHES "auto")
if(NOT CP2K_BLAS_VENDOR MATCHES "auto|CUSTOM")
find_package(${CP2K_BLAS_VENDOR} REQUIRED)
if(TARGET CP2K::BLAS::${CP2K_BLAS_VENDOR}::blas)
get_target_property(
Expand All @@ -88,31 +85,42 @@ if(NOT CP2K_BLAS_VENDOR MATCHES "auto")
set(CP2K_BLAS_FOUND TRUE)
endif()
else()
# search for any blas implementation and exit imediately if one is found
foreach(_libs ${__BLAS_VENDOR_LIST})
# i exclude the first item of the list
find_package(${_libs})
if(TARGET CP2K::BLAS::${_libs}::blas)
get_target_property(CP2K_BLAS_INCLUDE_DIRS CP2K::BLAS::${_libs}::blas
INTERFACE_INCLUDE_DIRECTORIES)
get_target_property(CP2K_BLAS_LINK_LIBRARIES CP2K::BLAS::${_libs}::blas
INTERFACE_LINK_LIBRARIES)
set(CP2K_BLAS_VENDOR "${_libs}")
set(CP2K_BLAS_FOUND TRUE)
break()
endif()
endforeach()
endif()
if(CP2K_BLAS_VENDOR MATCHES "CUSTOM" AND NOT DEFINED CP2K_BLAS_LINK_LIBRARIES)
message(
FATAL_ERROR
"Setting CP2K_BLAS_VENDOR=CUSTOM imply setting CP2K_BLAS_LINK_LIBRARIES\n and CP2K_LAPACK_LINK_LIBRARIES to the right libraries. See the README_cmake.md for more details"
)
endif()

if(CP2K_BLAS_INCLUDE_DIRS)
find_package_handle_standard_args(
Blas REQUIRED_VARS CP2K_BLAS_LINK_LIBRARIES CP2K_BLAS_INCLUDE_DIRS
CP2K_BLAS_VENDOR)
else()
find_package_handle_standard_args(Blas REQUIRED_VARS CP2K_BLAS_LINK_LIBRARIES
CP2K_BLAS_VENDOR)
if(DEFINED CP2K_BLAS_LINK_LIBRARIES)
set(CP2K_BLAS_FOUND TRUE)
else()
# search for any blas implementation and exit immediately if one is found.
# we could also give a full list of found implementation and let the user
# choose which implementation to use
foreach(_libs ${__BLAS_VENDOR_LIST})
# I exclude the first item of the list
find_package(${_libs})
if(TARGET CP2K::BLAS::${_libs}::blas)
get_target_property(CP2K_BLAS_INCLUDE_DIRS CP2K::BLAS::${_libs}::blas
INTERFACE_INCLUDE_DIRECTORIES)
get_target_property(CP2K_BLAS_LINK_LIBRARIES CP2K::BLAS::${_libs}::blas
INTERFACE_LINK_LIBRARIES)
set(CP2K_BLAS_VENDOR "${_libs}")
set(CP2K_BLAS_FOUND TRUE)
break()
endif()
endforeach()
endif()
endif()

# we exclude the CP2K_BLAS_INCLUDE_DIRS from the list of mandatory variables as
# having the fortran interface is usually enough. C, C++ and others languages
# might require this information though

find_package_handle_standard_args(
Blas REQUIRED_VARS CP2K_BLAS_LINK_LIBRARIES CP2K_BLAS_VENDOR CP2K_BLAS_FOUND)

if(NOT TARGET CP2K::BLAS::blas)
add_library(CP2K::BLAS::blas INTERFACE IMPORTED)
endif()
Expand Down
34 changes: 15 additions & 19 deletions cmake/FindLapack.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,24 @@
include(FindPackageHandleStandardArgs)

# check for blas first. Most of the vendor libraries bundle lapack and blas in
# the same library. (MKL, and OPENBLAS do)
# the same library. If so the FindBlas.cmake module will contain this
# information already and the information will be included in the blas target
#
# This solution might not good enough though.

find_package(PkgConfig)
find_package(Blas REQUIRED)

if(CP2K_BLAS_FOUND)
# LAPACK in the Intel MKL 10+ library?
if(CP2K_BLAS_VENDOR MATCHES "MKL"
OR CP2K_BLAS_VENDOR MATCHES "OpenBLAS"
OR CP2K_BLAS_VENDOR MATCHES "Arm"
OR CP2K_BLAS_VENDOR MATCHES "SCI"
OR CP2K_BLAS_VENDOR MATCHES "FlexiBLAS")
if(CP2K_BLAS_VENDOR MATCHES "MKL|OpenBLAS|Armpl|SCI|FlexiBLAS|NVHPC")
# we just need to create the interface that's all
set(CP2K_LAPACK_FOUND TRUE)
get_target_property(CP2K_LAPACK_INCLUDE_DIRS CP2K::BLAS::blas
INTERFACE_INCLUDE_DIRECTORIES)
get_target_property(CP2K_LAPACK_LIBRARIES CP2K::BLAS::blas
INTERFACE_LINK_LIBRARIES)
else()

# we might get lucky to find a pkgconfig package for lapack (fedora provides
# one for instance)
if(PKG_CONFIG_FOUND)
Expand All @@ -52,18 +50,16 @@ endif()
# check if found
find_package_handle_standard_args(Lapack REQUIRED_VARS CP2K_LAPACK_LIBRARIES)

if(CP2K_LAPACK_FOUND)
if(NOT TARGET CP2K::LAPACK::lapack)
add_library(CP2K::LAPACK::lapack INTERFACE IMPORTED)
add_library(CP2K::LAPACK::LAPACK ALIAS CP2K::LAPACK::lapack)
endif()
set_property(TARGET CP2K::LAPACK::lapack PROPERTY INTERFACE_LINK_LIBRARIES
${CP2K_LAPACK_LIBRARIES})
if(CP2K_LAPACK_INCLUDE_DIRS)
set_property(
TARGET CP2K::LAPACK::lapack PROPERTY INTERFACE_INCLUDE_DIRECTORIES
${CP2K_LAPACK_INCLUDE_DIRS})
endif()
if(NOT TARGET CP2K::LAPACK::lapack)
add_library(CP2K::LAPACK::lapack INTERFACE IMPORTED)
add_library(CP2K::LAPACK::LAPACK ALIAS CP2K::LAPACK::lapack)
endif()
set_property(TARGET CP2K::LAPACK::lapack PROPERTY INTERFACE_LINK_LIBRARIES
${CP2K_LAPACK_LIBRARIES})
if(CP2K_LAPACK_INCLUDE_DIRS)
set_property(
TARGET CP2K::LAPACK::lapack PROPERTY INTERFACE_INCLUDE_DIRECTORIES
${CP2K_LAPACK_INCLUDE_DIRS})
endif()

# prevent clutter in cache
Expand Down
92 changes: 92 additions & 0 deletions cmake/FindNVHPCBlas.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!-------------------------------------------------------------------------------------------------!
#! CP2K: A general program to perform molecular dynamics simulations !
#! Copyright 2000-2023 CP2K developers group <https://cp2k.org> !
#! !
#! SPDX-License-Identifier: GPL-2.0-or-later !
#!-------------------------------------------------------------------------------------------------!

# Copyright (c) 2022- ETH Zurich
#
# authors : Mathieu Taillefumier

include(FindPackageHandleStandardArgs)
include(cp2k_utils)

cp2k_set_default_paths(CP2K_NVHPC "NVHPC")

find_library(
CP2K_NVHPC_BLAS_LP64
NAMES blas_lp64
PATHS "${CP2K_NVHPC_ROOT}"
PATH_SUFFIXES "lib" "lib64")

find_library(
CP2K_NVHPC_BLAS_ILP64
NAMES blas_ilp64
PATHS "${CP2K_NVHPC_ROOT}"
PATH_SUFFIXES "lib" "lib64")

find_library(
CP2K_NVHPC_LAPACK_LP64
NAMES lapack_lp64
PATHS "${CP2K_NVHPC_ROOT}"
PATH_SUFFIXES "lib" "lib64")

find_library(
CP2K_NVHPC_LAPACK_ILP64
NAMES lapack_ilp64
PATHS "${CP2K_NVHPC_ROOT}"
PATH_SUFFIXES "lib" "lib64")

find_path(
CP2K_NVHPC_BLAS_INCLUDE_DIRS_lp64
NAMES cblas.h
PATHS "${CP2K_NVHPC_ROOT}"
HINTS "${CP2K_NVHPC_ROOT}"
PATH_SUFFIXES "include" "include/lp64" "lp64")

find_path(
CP2K_NVHPC_BLAS_INCLUDE_DIRS_ilp64
NAMES cblas.h
PATHS "${CP2K_NVHPC_ROOT}"
HINTS "${CP2K_NVHPC_ROOT}"
PATH_SUFFIXES "include" "include/ilp64" "ilp64")

find_package_handle_standard_args(
NVHPCBlas
DEFAULT_MSG
CP2K_NVHPC_INCLUDE_DIRS_ipl64
CP2K_NVHPC_BLAS_INCLUDE_DIRS_lp64
CP2K_NVHPC_BLAS_ILP64
CP2K_NVHPC_BLAS_LP64
CP2K_NVHPC_LAPACK_ILP64
CP2K_NVHPC_LAPACK_LP64)

set(CP2K_BLAS_VENDOR "NVHPCBlas")
set(CP2K_NVHPCBLAS_FOUND "ON")

if(NOT TARGET CP2K::BLAS::NVHPCBlas::nvhpcblas)
add_library(CP2K::BLAS::NVHPCBlas::nvhpcblas INTERFACE IMPORTED)
add_library(CP2K::BLAS::NVHPCBlas::blas ALIAS
CP2K::BLAS::NVHPCBlas::nvhpcblas)
endif()

if(CP2K_BLAS_INTERFACE MATCHES "64bits")
set(CP2K_NVHPC_BLAS_LINK_LIBRARIES
"${CP2K_NVHPC_LAPACK_ILP64} ${CP2K_NVHPC_BLAS_ILP64})
set(CP2K_NVHPC_BLAS_INCLUDE_DIRS "${CP2K_NVHPC_INCLUDE_DIRS_ipl64}")
else()
set(CP2K_NVHPC_BLAS_LINK_LIBRARIES "${CP2K_NVHPC_LAPACK_LP64}
${CP2K_NVHPC_BLAS_LP64}")
set(CP2K_NVHPC_BLAS_INCLUDE_DIRS "${CP2K_NVHPC_INCLUDE_DIRS_pl64}")
endif()
set_target_properties(
CP2K::BLAS::NVHPCBlas::nvhpcblas
PROPERTIES INTERFACE_LINK_LIBRARIES "${CP2K_NVHPC_BLAS_LINK_LIBRARIES}"
INTERFACE_INCLUDE_DIRECTORIES "${CP2K_NVHPC_BLAS_INCLUDE_DIRS}
")
endif()
mark_as_advanced(CP2K_NVHPCBLAS_FOUND CP2K_NVHPC_BLAS_INCLUDE_DIRS
CP2K_NVHPC_BLAS_LINK_LIBRARIES CP2K_BLAS_VENDOR)
2 changes: 1 addition & 1 deletion cmake/FindPlumed.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ cp2k_set_default_paths(PLUMED "Plumed")
# First try with pkg
if(PKG_CONFIG_FOUND)
# plumed has a pkg-config module
pkg_check_module(CP2K_PLUMED IMPORTED_TARGET GLOBAL plumed plumedInternals)
pkg_search_modules(CP2K_PLUMED IMPORTED_TARGET GLOBAL plumed plumedInternals)
endif()

if(NOT ${CP2K_PLUMED_FOUND})
Expand Down
9 changes: 9 additions & 0 deletions cmake/cp2kConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ endif()
set(CP2K_BLAS_VENDOR @CP2K_BLAS_VENDOR@)
set(CP2K_SCALAPACK_VENDOR @CP2K_SCALAPACK_VENDOR@)

if (@CP2K_BLAS_VENDOR@ MATCHES "CUSTOM")
set(CP2K_BLAS_LINK_LIBRARIES @CP2K_BLAS_LINK_LIBRARIES@)
set(CP2K_LAPACK_LINK_LIBRARIES @CP2K_LAPACK_LINK_LIBRARIES@)
endif()

if (@CP2K_SCALAPACK_VENDOR@ MATCHES "CUSTOM")
set(CP2K_SCALAPACK_LINK_LIBRARIES @CP2K_SCALAPACK_LINK_LIBRARIES@)
endif()

find_package(Lapack REQUIRED)
find_package(DBCSR 2.4 REQUIRED)

Expand Down

0 comments on commit f24923d

Please sign in to comment.