diff --git a/pkgs/by-name/cp/cp2k/package.nix b/pkgs/by-name/cp/cp2k/package.nix index 64229039b8ca..9a4b913e29a5 100644 --- a/pkgs/by-name/cp/cp2k/package.nix +++ b/pkgs/by-name/cp/cp2k/package.nix @@ -3,18 +3,19 @@ stdenv, fetchFromGitHub, mpiCheckPhaseHook, + cmake, python3, gfortran, blas, lapack, + dbcsr, fftw, libint, libvori, libxc, dftd4, - mctc-lib, - mstore, - multicharge, + simple-dftd3, + tblite, mpi, gsl, scalapack, @@ -30,6 +31,10 @@ libvdwxc, spla, spfft, + trexio, + toml-f, + greenx, + gmp, enableElpa ? false, elpa, cudaPackages, @@ -44,10 +49,9 @@ "none" ), # Change to a value suitable for your target GPU. - # For AMD values see https://github.com/cp2k/cp2k/blob/master/INSTALL.md#2v-rocmhip-support-for-amd-gpu - # and for Nvidia see https://github.com/cp2k/cp2k/blob/master/INSTALL.md#2i-cuda-optional-improved-performance-on-gpu-systems - gpuVersion ? (if gpuBackend == "cuda" then "A100" else "Mi100"), - gpuArch ? (if gpuBackend == "cuda" then "sm_80" else "gfx908"), + # see https://github.com/cp2k/cp2k/blob/master/CMakeLists.txt#L433 + hipTarget ? "gfx908", + cudaTarget ? "80", }: assert builtins.elem gpuBackend [ @@ -56,20 +60,15 @@ assert builtins.elem gpuBackend [ "rocm" ]; -let - cp2kVersion = "psmp"; - arch = "Linux-x86-64-gfortran"; - -in stdenv.mkDerivation rec { pname = "cp2k"; - version = "2025.1"; + version = "2025.2"; src = fetchFromGitHub { owner = "cp2k"; repo = "cp2k"; - tag = "v${version}"; - hash = "sha256-04AFiEuv+EYubZVoYErQDdr9zipKlF7Gqy8DrUaYUMk="; + rev = "v${version}"; + hash = "sha256-vfl5rCoFeGtYuZ7LcsVsESjKxFbN5IYDvBSzOqsd64w="; fetchSubmodules = true; }; @@ -77,10 +76,14 @@ stdenv.mkDerivation rec { # Remove the build command line from the source. # This avoids dependencies to .dev inputs ./remove-compiler-options.patch + + # Fix pkg-config path generation + ./pkgconfig.patch ]; nativeBuildInputs = [ python3 + cmake which makeWrapper pkg-config @@ -95,15 +98,15 @@ stdenv.mkDerivation rec { libvori libxc dftd4 - mctc-lib - mstore - multicharge + simple-dftd3 + tblite libxsmm mpi spglib scalapack blas lapack + dbcsr plumed zlib hdf5-fortran @@ -111,6 +114,10 @@ stdenv.mkDerivation rec { spla spfft libvdwxc + trexio + toml-f + greenx + gmp ] ++ lib.optional enableElpa elpa ++ lib.optionals (gpuBackend == "cuda") [ @@ -129,15 +136,6 @@ stdenv.mkDerivation rec { propagatedBuildInputs = [ (lib.getBin mpi) ]; propagatedUserEnvPkgs = [ mpi ]; - makeFlags = [ - "ARCH=${arch}" - "VERSION=${cp2kVersion}" - ]; - - doCheck = gpuBackend == "none"; - - enableParallelBuilding = true; - postPatch = '' patchShebangs tools exts/dbcsr/tools/build_utils exts/dbcsr/.cp2k substituteInPlace exts/build_dbcsr/Makefile \ @@ -145,121 +143,66 @@ stdenv.mkDerivation rec { --replace 'SHELL = /bin/sh' 'SHELL = bash' ''; - configurePhase = '' - runHook preConfigure - - cat > arch/${arch}.${cp2kVersion} << EOF - CC = mpicc - CPP = - FC = mpif90 - LD = mpif90 - AR = ar -r - ${lib.strings.optionalString (gpuBackend == "cuda") '' - OFFLOAD_CC = nvcc - OFFLOAD_FLAGS = -O3 -g -w --std=c++11 -arch ${gpuArch} - OFFLOAD_TARGET = cuda - GPUVER = ${gpuVersion} - CXX = mpicxx - CXXFLAGS = -std=c++11 -fopenmp - ''} - ${lib.strings.optionalString (gpuBackend == "rocm") '' - GPUVER = ${gpuVersion} - OFFLOAD_CC = hipcc - OFFLOAD_FLAGS = -fopenmp -m64 -pthread -fPIC -D__GRID_HIP -O2 --offload-arch=${gpuArch} --rocm-path=${rocmPackages.rocm-core} - OFFLOAD_TARGET = hip - CXX = mpicxx - CXXFLAGS = -std=c++11 -fopenmp -D__HIP_PLATFORM_AMD__ - ''} - DFLAGS = -D__FFTW3 -D__LIBXC -D__LIBINT -D__parallel -D__SCALAPACK \ - -D__MPI_VERSION=3 -D__F2008 -D__LIBXSMM -D__SPGLIB \ - -D__MAX_CONTR=4 -D__LIBVORI ${lib.optionalString enableElpa "-D__ELPA"} \ - -D__PLUMED2 -D__HDF5 -D__GSL -D__SIRIUS -D__LIBVDWXC -D__SPFFT -D__SPLA \ - -D__DFTD4 \ - ${ - lib.strings.optionalString ( - gpuBackend == "cuda" - ) "-D__OFFLOAD_CUDA -D__ACC -D__DBCSR_ACC -D__NO_OFFLOAD_PW" - } \ - ${lib.strings.optionalString ( - gpuBackend == "rocm" - ) "-D__OFFLOAD_HIP -D__DBCSR_ACC -D__NO_OFFLOAD_PW"} - CFLAGS = -fopenmp - FCFLAGS = \$(DFLAGS) -O2 -ffree-form -ffree-line-length-none \ - -ftree-vectorize -funroll-loops -msse2 \ - -std=f2008 \ - -fopenmp -ftree-vectorize -funroll-loops \ - ${lib.optionalString enableElpa "$(pkg-config --variable=fcflags elpa)"} \ - -I${lib.getDev libint}/include \ - -I${lib.getDev sirius}/include/sirius \ - -I${lib.getDev libxc}/include \ - -I${lib.getDev dftd4}/include/dftd4 \ - -I${lib.getDev libxsmm}/include \ - -I${lib.getDev hdf5-fortran}/include \ - -fallow-argument-mismatch - LIBS = -lfftw3 -lfftw3_threads \ - -lscalapack -lblas -llapack \ - -lxcf03 -lxc -lxsmmf -lxsmm -lsymspg \ - -lint2 -lstdc++ -lvori \ - -lgomp -lpthread -lm \ - -fopenmp ${lib.optionalString enableElpa "$(pkg-config --libs elpa)"} \ - -lz -ldl ${lib.optionalString (mpi.pname == "openmpi") "$(mpicxx --showme:link)"} \ - -lplumed -lhdf5_fortran -lhdf5_hl -lhdf5 -lgsl -lsirius -lspla -lspfft -lvdwxc \ - -ldftd4 -lmstore -lmulticharge -lmctc-lib \ - ${ - lib.strings.optionalString (gpuBackend == "cuda") '' - -L${cudaPackages.cuda_cudart}/lib/stubs/ \ - -lcudart -lnvrtc -lcuda -lcublas - '' - } \ - ${lib.strings.optionalString ( - gpuBackend == "rocm" - ) "-lamdhip64 -lhipfft -lhipblas -lrocblas"} - LDFLAGS = \$(FCFLAGS) \$(LIBS) - include ${plumed}/lib/plumed/src/lib/Plumed.inc - EOF - - runHook postConfigure - ''; + cmakeFlags = [ + (lib.strings.cmakeBool "CP2K_USE_DFTD4" true) + (lib.strings.cmakeBool "CP2K_USE_TBLITE" true) + (lib.strings.cmakeBool "CP2K_USE_FFTW3" true) + (lib.strings.cmakeBool "CP2K_USE_HDF5" true) + (lib.strings.cmakeBool "CP2K_USE_LIBINT2" true) + (lib.strings.cmakeBool "CP2K_USE_LIBXC" true) + (lib.strings.cmakeBool "CP2K_USE_MPI" true) + (lib.strings.cmakeBool "CP2K_USE_VORI" true) + (lib.strings.cmakeBool "CP2K_USE_TREXIO" true) + (lib.strings.cmakeBool "CP2K_USE_SPGLIB" true) + (lib.strings.cmakeBool "CP2K_USE_SPLA" true) + (lib.strings.cmakeBool "CP2K_USE_LIBXSMM" true) + (lib.strings.cmakeBool "CP2K_USE_SIRIUS" true) + (lib.strings.cmakeBool "CP2K_USE_LIBVDWXC" true) + (lib.strings.cmakeBool "CP2K_USE_PLUMED" true) + (lib.strings.cmakeBool "CP2K_USE_GREENX" true) + (lib.strings.cmakeBool "CP2K_USE_ELPA" enableElpa) + (lib.strings.cmakeBool "BUILD_SHARED_LIBS" (!stdenv.hostPlatform.isStatic)) + ] + ++ lib.optionals (gpuBackend == "rocm") [ + (lib.strings.cmakeFeature "CP2K_USE_ACCEL" "HIP") + (lib.strings.cmakeFeature "CMAKE_HIP_ARCHITECTURES" hipTarget) + ] + ++ lib.optionals (gpuBackend == "cuda") [ + (lib.strings.cmakeFeature "CP2K_USE_ACCEL" "CUDA") + (lib.strings.cmakeFeature "CMAKE_CUDA_ARCHITECTURES" cudaTarget) + ]; nativeCheckInputs = [ mpiCheckPhaseHook ]; - checkPhase = '' - runHook preCheck - - export CP2K_DATA_DIR=data - mpirun -np 2 exe/${arch}/libcp2k_unittest.${cp2kVersion} - - runHook postCheck - ''; - - installPhase = '' - runHook preInstall - - mkdir -p $out/bin $out/share/cp2k - - cp exe/${arch}/* $out/bin - rm $out/bin/*_unittest.* - - for i in cp2k cp2k_shell graph; do - wrapProgram $out/bin/$i.${cp2kVersion} \ - --set-default CP2K_DATA_DIR $out/share/cp2k - done - - wrapProgram $out/bin/cp2k.popt \ - --set-default CP2K_DATA_DIR $out/share/cp2k \ - --set OMP_NUM_THREADS 1 - - cp -r data/* $out/share/cp2k - - runHook postInstall - ''; - passthru = { inherit mpi; }; + postInstall = '' + mkdir -p $out/share/cp2k + cp -r ../data/* $out/share/cp2k + + for i in $out/bin/*; do + wrapProgram $i \ + --set-default CP2K_DATA_DIR $out/share/cp2k \ + --set-default OMP_NUM_THREADS 1 + done + ''; + + doInstallCheck = gpuBackend == "none"; + + installCheckPhase = '' + runHook preInstallCheck + + for TEST in $out/bin/{dbt_tas,dbt,libcp2k,parallel_rng_types,gx_ac}_unittest.psmp; do + mpirun -n 2 $TEST + done + + runHook postInstallCheck + ''; + meta = { description = "Quantum chemistry and solid state physics program"; homepage = "https://www.cp2k.org"; diff --git a/pkgs/by-name/cp/cp2k/pkgconfig.patch b/pkgs/by-name/cp/cp2k/pkgconfig.patch new file mode 100644 index 000000000000..2194d3a97d01 --- /dev/null +++ b/pkgs/by-name/cp/cp2k/pkgconfig.patch @@ -0,0 +1,14 @@ +diff --git a/cmake/libcp2k.pc.in b/cmake/libcp2k.pc.in +index 618af55e28..8d08a51a0c 100644 +--- a/cmake/libcp2k.pc.in ++++ b/cmake/libcp2k.pc.in +@@ -1,7 +1,7 @@ + prefix="@CMAKE_INSTALL_PREFIX@" + exec_prefix="${prefix}" +-libdir="${prefix}/@CMAKE_INSTALL_LIBDIR@" +-includedir="${prefix}/@CMAKE_INSTALL_INCLUDEDIR@" ++libdir=CMAKE_INSTALL_FULL_LIBDIR@" ++includedir="@CMAKE_INSTALL_FULL_INCLUDEDIR@" + + Name: @PROJECT_NAME@ + Description: @CMAKE_PROJECT_DESCRIPTION@ diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 31feafa9b7b4..bdeed6c21a21 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -14545,6 +14545,75 @@ with pkgs; siesta-mpi = callPackage ../applications/science/chemistry/siesta { useMpi = true; }; + cp2k = + # CP2K requires all dependencies from the Grimme ecosystem to be build with + # CMake instead of Meson. Unfortunately most other consumers require meson + let + grimmeCmake = lib.makeScope pkgs.newScope (self: { + mctc-lib = pkgs.mctc-lib.override { + buildType = "cmake"; + inherit (self) jonquil toml-f; + }; + + toml-f = pkgs.toml-f.override { + buildType = "cmake"; + inherit (self) test-drive; + }; + + dftd4 = pkgs.dftd4.override { + buildType = "cmake"; + inherit (self) mstore mctc-lib multicharge; + }; + + jonquil = pkgs.jonquil.override { + buildType = "cmake"; + inherit (self) toml-f test-drive; + }; + + mstore = pkgs.mstore.override { + buildType = "cmake"; + inherit (self) mctc-lib; + }; + + multicharge = pkgs.multicharge.override { + buildType = "cmake"; + inherit (self) mctc-lib mstore; + }; + + test-drive = pkgs.test-drive.override { buildType = "cmake"; }; + + simple-dftd3 = pkgs.simple-dftd3.override { + buildType = "cmake"; + inherit (self) mctc-lib mstore toml-f; + }; + + tblite = pkgs.tblite.override { + buildType = "cmake"; + inherit (self) + mctc-lib + mstore + toml-f + multicharge + dftd4 + simple-dftd3 + ; + }; + + sirius = pkgs.sirius.override { + inherit (self) + mctc-lib + toml-f + multicharge + dftd4 + simple-dftd3 + ; + }; + }); + in + grimmeCmake.callPackage ../by-name/cp/cp2k/package.nix { + libxc = pkgs.libxc_7; + }; + ### SCIENCE/GEOMETRY ### SCIENCE/BENCHMARK