Table of Contents

Compile CP2K on macOS

This page describes how CP2K can be installed under macOS (Monterey, Ventura, Sonoma, and Sequoia). This howto has last been tested on an Apple M1 under macOS Sequoia 15.1 (Darwin Kernel Version 24.1.0, Homebrew 4.4.3). For further details check the corresponding Darwin-gnu-arm64 arch file and regression tester. This howto assumes that your default shell is bash.

1. Install Homebrew

Open a Terminal and run the command

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

which will install the latest Homebrew version. This howto was tested with Homebrew 3.6.11 as well as with newer versions up to 4.4.3.

a) The easy way

If you are only interested in testing CP2K under macOS, then you can install a pre-compiled CP2K binary providing all basic features via

brew install cp2k

using this Homebrew formula. You can ignore the rest of this how-to, in case you are already happy with that CP2K installation.

b) Compile CP2K under macOS from scratch

For best performance or code development, it is recommended to install CP2K from scratch. If you installed already CP2K with brew as described in the previous section, then make sure that you first uninstall cp2k and unlink its dependencies open-mpi and scalapack, e.g. with

brew uninstall cp2k
brew unlink open-mpi scalapack 

before you continue to avoid any interference with these Homebrew packages during the installation of the CP2K toolchain.

Create the following links

ln -s /opt/homebrew/bin/gcc-14 /opt/homebrew/bin/gcc
ln -s /opt/homebrew/bin/g++-14 /opt/homebrew/bin/g++
ln -s /opt/homebrew/bin/gfortran-14 /opt/homebrew/bin/gfortran

to make Homebrew's latest gcc and g++ compilers the default instead of the clang versions in /usr/bin. Check with

gcc -v

if that is the case.

2. Obtain CP2K

Checkout the latest CP2K (master) version available from the CP2K GitHub repository using

git clone --recursive https://github.com/cp2k/cp2k.git cp2k

or the latest CP2K release version

git clone --recursive -b support/v2024.3 https://github.com/cp2k/cp2k.git cp2k

3. Build the CP2K toolchain

Change to the new folder cp2k

cd cp2k

and run the command

source arch/Darwin-gnu-arm64.ssmp

to build the toolchain for a serial CP2K binary. Alternatively, you can build the toolchain for an MPI/OpenMP parallel CP2K binary with

source arch/Darwin-gnu-arm64.psmp

4. Compile CP2K

Check the output from the toolchain build in the previous step and if there is no error, run

make -j ARCH=Darwin-gnu-arm64 VERSION=ssmp

or

make -j ARCH=Darwin-gnu-arm64 VERSION=psmp

as suggested depending on the selected toolchain build.

5. Test the CP2K binary

As a final check, you can run a CP2K regression test with

make -j ARCH=Darwin-arm64 VERSION=psmp test

to validate the generated CP2K binary. This will run more than 4000 test cases. At the end of that test, a summary is printed which should indicate that there are no FAILED or WRONG tests. Instead or running all tests, you can also restrict the testing to certain test folders in tests by passing TESTOPTS with the make command like

make -j ARCH=Darwin-arm64 VERSION=psmp TESTOPTS="--restrictdir QS/regtest-gpw-1" test

which will only run the test cases in the folder tests/QS/regtest-1. You can list all available TESTOPTS with

tests/do_regtest.py -h

All data generated by test can be removed with

make ARCH=Darwin-arm64 VERSION=psmp testclean

whereas

make ARCH=Darwin-arm64 VERSION=psmp realclean

and

make distclean

will remove all data from make ARCH=local VERSION=ssmp and from any make, respectively.

Last but not least

Before using CP2K, do not forget to source always the setup file with

source tools/toolchain/install/setup

It is also suggested to increase the OMP_STACKSIZE to at least 16 MB

export OMP_STACKSIZE=16M
ulimit -s 65000

and the stacksize to 65 MB (check with ulimit -a).

Adding the cp2k/exe/local folder to your binary search PATH

export PATH=$PATH:$HOME/github/cp2k/cp2k/exe/local

will allow for running CP2K just with

cp2k.ssmp

and likewise with cp2k.sopt.

CP2K MPI/OpenMP parallel runs are launched with mpirun or mpiexec, e.g.

mpiexec -n 4 -genv OMP_NUM_THREADS=2 cp2k.psmp H2O-32.inp

will use 4 MPI ranks with 2 OpenMP threads each (i.e. it will consume 8 CPU cores) to run the input file H2O-32.inp which can be found in the cp2k folder benchmarks/QS.

The cp2k.popt binary is only MPI parallel and will run just one thread per MPI rank automatically (like cp2k.sopt) which is usually the most efficient usage of CP2K performance-wise if no memory limitation per MPI rank come into play. Therefore, the following commands

mpiexec -n 4 -genv OMP_NUM_THREADS=1 cp2k.psmp H2O-32.inp
mpiexec -n 4                         cp2k.popt H2O-32.inp

are basically equivalent.

Enjoy CP2K under macOS!