Gromacs 2023.3 on Apple M3 chip

GROMACS version: 2023.3
GROMACS modification: No

Hi folks

I know that there have been several threads on running Gromacs with Apple M1 and M1 or M2 chips (e.g. Error compiling Gromacs 2023's checks on Mac M2), but I recently got a MacBook Pro with the M3 chip, so I was interested to see how it would perform. I am starting this thread in case other people have concerns or suggestions or just need a starting point.

Here are the details:

My MacBook Pro is the 14" version with 18GB of RAM and an M3 Pro chip (12 core CPU, 18 core GPU). OS is Sonoma 14.1.2
I am comparing it to a Linux PC running Ubuntu 20.04 with 32GB RAM, a 24-core i9 processor and a 3080ti card with CUDA 11.2.
Test runs were run on HIV-1 protease (1ajx.pdb) with an inhibitor and a 1.5nm solvent jacket (11,137 atoms total).

Note: Mac and PC are approximately equally fast running ML in Keras, with the Mac using the Tensorflow metal plugin and the PC using the CUDA code.

Installation

per Error compiling Gromacs 2023's checks on Mac M2 - #5 by hess etc

Install homebrew etc

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

brew install wget
brew install cmake
brew install hwloc
brew install subversion
brew install gcc
brew install libomp
brew install opencl-headers

wget https://ftp.gromacs.org/regressiontests/regressiontests-2023.3.tar.gz

cd gromacs-2023.3

Build, with OpenMP

Note: Always start builds in a new build directory

rm -rf build ; mkdir build ; cd build
cmake … -DGMX_BUILD_OWN_FFTW=ON -DREGRESSIONTEST_DOWNLOAD=OFF
-DCMAKE_C_COMPILER=/opt/homebrew/bin/gcc-13
-DCMAKE_CXX_COMPILER=/opt/homebrew/bin/g+±13
-DGMX_MPI=no
-DGMX_OPENMP=ON
-DOpenMP_{C,CXX}FLAGS=“-Xpreprocessor -fopenmp -I/opt/homebrew/opt/libomp/include”
-DOpenMP
{C,CXX}_LIB_NAMES=omp
-DOpenMP_omp_LIBRARY=/opt/homebrew/opt/libomp/lib/libomp.dylib
-DREGRESSIONTEST_PATH=//Users/gvigers/Work/programs/gromacs/regressiontests-2023.3

Nope:

CMake Warning at cmake/gmxDetectCpu.cmake:100 (message):
Did not detect build CPU features - detection program did not compile.
Please file a bug report if this is a common platform.
Call Stack (most recent call first):
cmake/gmxDetectSimd.cmake:69 (gmx_run_cpu_detection)
cmake/gmxDetectSimd.cmake:155 (gmx_suggest_simd)
cmake/gmxManageSimd.cmake:91 (gmx_detect_simd)
CMakeLists.txt:650 (gmx_manage_simd)

Try with Apple clang compiler instead

cd … ; rm -rf build ; mkdir build ; cd build
cmake … -DGMX_BUILD_OWN_FFTW=ON -DREGRESSIONTEST_DOWNLOAD=OFF
-DCMAKE_C_COMPILER=/usr/bin/clang
-DCMAKE_CXX_COMPILER=/usr/bin/clang++
-DGMX_MPI=no
-DGMX_OPENMP=ON
-DOpenMP_{C,CXX}FLAGS=“-Xpreprocessor -fopenmp -I/opt/homebrew/opt/libomp/include”
-DOpenMP
{C,CXX}_LIB_NAMES=omp
-DOpenMP_omp_LIBRARY=/opt/homebrew/opt/libomp/lib/libomp.dylib
-DREGRESSIONTEST_PATH=/Users/gvigers/Work/programs/gromacs/regressiontests-2023.3

make check

100% tests passed, 0 tests failed out of 83

sudo make install

source /usr/local/gromacs/bin/GMXRC

See below for test results.

Build again with no shared libraries and add GPUs and hwloc (still using clang)

cd … ; rm -rf build ; mkdir build ; cd build
cmake … -DGMX_BUILD_OWN_FFTW=ON -DREGRESSIONTEST_DOWNLOAD=OFF
-DCMAKE_C_COMPILER=/usr/bin/clang
-DCMAKE_CXX_COMPILER=/usr/bin/clang++
-DGMX_MPI=no
-DGMX_OPENMP=ON
-DGMX_GPU=OpenCL
-DBUILD_SHARED_LIBS=off
-DGMX_HWLOC=ON
-DOpenMP_{C,CXX}FLAGS=“-Xpreprocessor -fopenmp -I/opt/homebrew/opt/libomp/include”
-DOpenMP
{C,CXX}_LIB_NAMES=omp
-DOpenMP_omp_LIBRARY=/opt/homebrew/opt/libomp/lib/libomp.dylib
-DREGRESSIONTEST_PATH=/Users/gvigers/Work/programs/gromacs/regressiontests-2023.3

make check

sudo make install

source /usr/local/gromacs/bin/GMXRC

Looks good:

gmx --version
:-) GROMACS - gmx, 2023.3 (-:

Executable: /usr/local/gromacs/bin/gmx
Data prefix: /usr/local/gromacs
Working dir: /Users/gvigers/Work/programs/gromacs
Command line:
gmx --version

GROMACS version: 2023.3
Precision: mixed
Memory model: 64 bit
MPI library: thread_mpi
OpenMP support: enabled (GMX_OPENMP_MAX_THREADS = 128)
GPU support: OpenCL
NB cluster size: 8
SIMD instructions: ARM_NEON_ASIMD
CPU FFT library: fftw-3.3.8
GPU FFT library: VkFFT internal (1.2.26-b15cb0ca3e884bdb6c901a12d87aa8aadf7637d8) with OpenCL backend
Multi-GPU FFT: none
TNG support: enabled
Hwloc support: disabled
Tracing support: disabled
C compiler: /usr/bin/clang AppleClang 12.0.5.12050022
C compiler flags: -Wno-missing-field-initializers -fno-stack-check -fno-stack-check -O3 -DNDEBUG
C++ compiler: /usr/bin/clang++ AppleClang 12.0.5.12050022
C++ compiler flags: -Wno-missing-field-initializers -fno-stack-check -fno-stack-check -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-return-std-move-in-c++11 -Wno-source-uses-openmp -Wno-c++17-extensions -Wno-documentation-unknown-command -Wno-covered-switch-default -Wno-switch-enum -Wno-extra-semi-stmt -Wno-weak-vtables -Wno-shadow -Wno-padded -Wno-reserved-id-macro -Wno-double-promotion -Wno-exit-time-destructors -Wno-global-constructors -Wno-documentation -Wno-format-nonliteral -Wno-used-but-marked-unused -Wno-float-equal -Wno-conditional-uninitialized -Wno-conversion -Wno-disabled-macro-expansion -Wno-unused-macros -Wno-unused-parameter -Wno-unused-variable -Wno-newline-eof -Wno-old-style-cast -Wno-zero-as-null-pointer-constant -Wno-sign-compare SHELL:-Xpreprocessor -fopenmp -I/opt/homebrew/opt/libomp/include -O3 -DNDEBUG
BLAS library: External - detected on the system
LAPACK library: External - detected on the system
OpenCL include dir: /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/System/Library/Frameworks/OpenCL.framework
OpenCL library: /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/System/Library/Frameworks/OpenCL.framework
OpenCL version: 1.2

Timings on 1ajx.pdb:

Mac, Steepest-descents minimization:

Steepest Descents converged to Fmax < 500 in 1932 steps
Potential Energy = -5.4745700e+05
Maximum force = 4.9502078e+02 on atom 3131
Norm of force = 1.1838092e+01
real 0m9.929s

PC, Steepest-descents minimization:

Steepest Descents converged to Fmax < 500 in 1913 steps
Potential Energy = -5.4743575e+05
Maximum force = 4.8873819e+02 on atom 3131
Norm of force = 1.1845837e+01
real 0m4.175s

Mac, 500ps MD run, CPUs only:

           Core t (s)   Wall t (s)        (%)
   Time:    27530.204     2294.186     1200.0
                     38:14
             (ns/day)    (hour/ns)

Performance: 18.830 1.275
real 38m21.783s

Mac, 500ps MD run, Add GPUs:

           Core t (s)   Wall t (s)        (%)
   Time:     6834.195      569.527     1200.0
             (ns/day)    (hour/ns)

Performance: 75.853 0.316
real 9m35.661s

PC, 500ps MD run:

          Core t (s)   Wall t (s)        (%)
   Time:     1802.629       75.117     2399.8
             (ns/day)    (hour/ns)

Performance: 575.103 0.042
real 1m21.512s

Mac, Calculating interaction energies (CPU-only task)

real 10m6.758s

PC, Calculating interaction energies (CPU-only task)

real 13m2.150s

Conclusions:
The Mac is suprisingly capable, given it’s energy consumption :)
I couldn’t get the g+±13 compiler to work, but the Apple compiler looks good.
Compiling with GPU support on the M3 chip gives ~4x boost in speed.
My PC is ~7.5x as fast for MD runs, slightly slower for CPU-only tasks.
Minimizations have worked well on ~450 test cases (data not shown). I have not tested MD runs extensively.

Side note: I tried the same tests on a Mac Studio with the M2 ultra chip and got similar results, except that compiling with GPUs made it 2x slower! I did not pursue this very far.

Questions:
Any suggestions for improvements?
Are there other tests that folks would like to see?

Guy Vigers

P.S. Thanks to the developers for a great program, as always!

Hello! Thank you for instructions, i also have same MacBook as you, but i have issues with installation.

When i do cmake in the same way as you did, i have a error that says: "CMake Error: Unknown argument -I/opt/homebrew/opt/libomp/include”

How i can solve this issue? I don’t have installed xcode, maybe it causes these kind of problems

UPD: i have installed Xcode and it worked, but now after make i have a error: “/src/gromacs/applied_forces/qmmm/qmmmtopologypreprocessor.cpp:277:17: error: no viable overloaded ‘operator->’
while (atoms->globalAtomNumber() < mtop->natoms)”

Newest UPD: i found that this is global issue with newer version of boost library in brew according to Build failure with Boost 1.83.0: no viable overloaded 'operator->' (#4909) · Issues · GROMACS / GROMACS · GitLab and System-wide installed version of Boost gets picked up during build instead of GROMACS bundled one and results in build failure with GMX_GPU=OpenCL (#4893) · Issues · GROMACS / GROMACS · GitLab

The script is ok.
Just remove a dot after cmake (only two dots)
and replace the last four lines with these:
-DGMX_OPENMP=ON
-DOpenMP_{C,CXX}FLAGS=“-Xpreprocessor -fopenmp -I/opt/homebrew/opt/libomp/include”
-DOpenMP
{C,CXX}_LIB_NAMES=omp
-DOpenMP_omp_LIBRARY=/opt/homebrew/opt/libomp/lib/libomp.dylib

You can also do :
-DREGRESSIONTEST_DOWNLOAD=ON and remove the last line.
I suggest you to copy in the terminal each command, with a space : -DGMX_BUILD_OWN_FFTW=ON -DREGRESSIONTEST_DOWNLOAD=OFF and so on, then press enter

Hello! Thanks for the script. I am trying to install it on m3 MAX and it seems that I have a problem with the C,C++ flags. I get the error

CMake Error: Unknown argument -I/opt/homebrew/opt/libomp/include”it seems that the argument -I is unknown. What is the argument -I supposed to do? parse the path? DO you have an idea why this is unknown?

Thank you and best wishes

Hi Stefan

I’m sorry - I’m not an expert on this stuff at all, but it looks to me as if the -I flag is being passed to the openmp preprocessor. Do you have all of the software (particularly libomp) installed in the correct places?

My commands seem to have got modified a bit in the forum formatting, so here they are again in plain text, just in case that helps:

/opt/homebrew/bin/cmake … -DGMX_BUILD_OWN_FFTW=ON -DREGRESSIONTEST_DOWNLOAD=OFF
-DCMAKE_C_COMPILER=/opt/homebrew/bin/gcc-13
-DCMAKE_CXX_COMPILER=/opt/homebrew/bin/g+±13
-DGMX_MPI=no
-DGMX_OPENMP=ON
-DOpenMP_{C,CXX}FLAGS=“-Xpreprocessor -fopenmp -I/opt/homebrew/opt/libomp/include”
-DOpenMP
{C,CXX}_LIB_NAMES=omp
-DOpenMP_omp_LIBRARY=/opt/homebrew/opt/libomp/lib/libomp.dylib
-DREGRESSIONTEST_PATH=/Volumes/VMs/scratch/Work/programs/gromacs/regressiontests-2023.3

make check

Good luck!

Guy

Hi Vigers,

Thank you for your great post. It is really helpful. But I still have troubles.
I have the same mac as you, but I am trying to install Gromacs-constantph (Gromacs constant ph / Constantph · GitLab).

I tried many different combination for the commands and the one that seems working is as follows:

cmake … -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DGMX_FFT_LIBRARY=fftw3 -DGMX_BUILD_OWN_FFTW=ON -DREGRESSIONTEST_DOWNLOAD=ON -DGMX_OPENMP=ON -DOpenMP_{C,CXX}FLAGS=“-Xpreprocessor -fopenmp -I/opt/homebrew/opt/libomp/include” -DOpenMP{C,CXX}_LIB_NAMES=omp -DOpenMP_omp_LIBRARY=/opt/homebrew/opt/libomp/lib/libomp.dylib

But then I get this error:

In file included from /Users/sbim/Documents/Gromacs/constantph/gromacs-constantph/src/gromacs/mdrun/simulatorbuilder.cpp:44:
/Users/sbim/Documents/Gromacs/constantph/gromacs-constantph/src/gromacs/mdrun/simulatorbuilder.h:155:18: error: unknown type name ‘FILE’
SimulatorEnv(FILE* fplog,
^
/Users/sbim/Documents/Gromacs/constantph/gromacs-constantph/src/gromacs/mdrun/simulatorbuilder.h:169:5: error: unknown type name ‘FILE’
FILE* fplog_;
^
2 errors generated.
make[2]: *** [src/gromacs/CMakeFiles/libgromacs.dir/mdrun/simulatorbuilder.cpp.o] Error 1
make[1]: *** [src/gromacs/CMakeFiles/libgromacs.dir/all] Error 2
make: *** [all] Error 2

I followed every steps you have done. I am not sure if this is problem with Gromacs-constantph. Could maybe look into it please?

Thank you

Seong

Dear Seong

I’m sorry but you are trying to do things that are far beyond me! However, I have 2 thoughts:

Have you managed to get standard Gromacs to compile and run? I was using version 2023.3, so you might want to test that first.

The M3 mac works very well for CPU-limited tasks like minimizations or energy evaluations, but it is very slow for GPU-bound tasks. If you are planning to do serious MD runs you are almost certainly going to need to use an Intel or AMD CPU and an Nvidia GPU under Linux. So putting in more work on the Mac may be a waste of your time.

Good Luck and I’m sorry that I can’t be of more help!

Guy Vigers

Thank you for the reply!
I tried installing the “original” Gromacs and installation is smooth and has no problem. I guess the problem is some compatibility issue with the Gromacs-constantph.

At least I know one Gromacs can run with my mac.
Thank you.

Seong

Which version of clang do you have?

On my MacBook with M2 (but the processor shouldn’t matter here) the constant-pH code compiler with when configured with
cmake …/… -DGMX_BUILD_OWN_FFTW=ON

That then uses Apple clang version 14.0.3.

Note that I prefer gcc as that supports OpenMP.

I get:

/usr/bin/clang --version

Apple clang version 12.0.5 (clang-1205.0.22.9)

Target: arm64-apple-darwin23.1.0

Thread model: posix

InstalledDir: /Library/Developer/CommandLineTools/usr/bin

gmx built wth this clang 12.0.5 does appear to support openMP:

gmx --version

:-) GROMACS - gmx, 2023.3 (-:

Executable: /usr/local/gromacs/bin/gmx

Data prefix: /usr/local/gromacs

Working dir: /Users/gvigers/Work/programs/gromacs

Command line:

gmx --version

GROMACS version: 2023.3

Precision: mixed

Memory model: 64 bit

MPI library: thread_mpi

OpenMP support: enabled (GMX_OPENMP_MAX_THREADS = 128)

GPU support: OpenCL

NB cluster size: 8

SIMD instructions: ARM_NEON_ASIMD

CPU FFT library: fftw-3.3.8

GPU FFT library: VkFFT internal (1.2.26-b15cb0ca3e884bdb6c901a12d87aa8aadf7637d8) with OpenCL backend

Multi-GPU FFT: none

TNG support: enabled

Hwloc support: disabled

Tracing support: disabled

C compiler: /usr/bin/clang AppleClang 12.0.5.12050022

C compiler flags: -Wno-missing-field-initializers -fno-stack-check -fno-stack-check -O3 -DNDEBUG

C++ compiler: /usr/bin/clang++ AppleClang 12.0.5.12050022

C++ compiler flags: -Wno-missing-field-initializers -fno-stack-check -fno-stack-check -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-return-std-move-in-c++11 -Wno-source-uses-openmp -Wno-c++17-extensions -Wno-documentation-unknown-command -Wno-covered-switch-default -Wno-switch-enum -Wno-extra-semi-stmt -Wno-weak-vtables -Wno-shadow -Wno-padded -Wno-reserved-id-macro -Wno-double-promotion -Wno-exit-time-destructors -Wno-global-constructors -Wno-documentation -Wno-format-nonliteral -Wno-used-but-marked-unused -Wno-float-equal -Wno-conditional-uninitialized -Wno-conversion -Wno-disabled-macro-expansion -Wno-unused-macros -Wno-unused-parameter -Wno-unused-variable -Wno-newline-eof -Wno-old-style-cast -Wno-zero-as-null-pointer-constant -Wno-sign-compare SHELL:-Xpreprocessor -fopenmp -I/opt/homebrew/opt/libomp/include -O3 -DNDEBUG

BLAS library: External - detected on the system

LAPACK library: External - detected on the system

OpenCL include dir: /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/System/Library/Frameworks/OpenCL.framework

OpenCL library: /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/System/Library/Frameworks/OpenCL.framework

OpenCL version: 1.2

There is a version of gcc at /usr/bin/gcc

gcc --version

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1

Apple clang version 12.0.5 (clang-1205.0.22.9)

Target: arm64-apple-darwin23.1.0

Thread model: posix

InstalledDir: /Library/Developer/CommandLineTools/usr/bin

Is this the one that you use?

I did also try with homebrew/gcc-13, but I got:

cmake … -DGMX_BUILD_OWN_FFTW=ON -DREGRESSIONTEST_DOWNLOAD=OFF
-DCMAKE_C_COMPILER=/opt/homebrew/bin/gcc-13
-DCMAKE_CXX_COMPILER=/opt/homebrew/bin/g+±13
-DGMX_MPI=no
-DGMX_OPENMP=ON
-DOpenMP_{C,CXX}FLAGS=“-Xpreprocessor -fopenmp -I/opt/homebrew/opt/libomp/include”
-DOpenMP
{C,CXX}_LIB_NAMES=omp
-DOpenMP_omp_LIBRARY=/opt/homebrew/opt/libomp/lib/libomp.dylib
-DREGRESSIONTEST_PATH=//Users/gvigers/Work/programs/gromacs/regressiontests-2023.3

CMake Warning at cmake/gmxDetectCpu.cmake:100 (message):
Did not detect build CPU features - detection program did not compile.
Please file a bug report if this is a common platform.
Call Stack (most recent call first):
cmake/gmxDetectSimd.cmake:69 (gmx_run_cpu_detection)
cmake/gmxDetectSimd.cmake:155 (gmx_suggest_simd)
cmake/gmxManageSimd.cmake:91 (gmx_detect_simd)
CMakeLists.txt:650 (gmx_manage_simd)

/opt/homebrew/bin/gcc-13 --version

gcc-13 (Homebrew GCC 13.2.0) 13.2.0

Copyright (C) 2023 Free Software Foundation, Inc.

This is free software; see the source for copying conditions. There is NO

warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

I’m not at all an expert on any of this, but I’m happy to run any tests that you’d like to suggest!

Best Wishes

Guy Vigers

It looks like you have some issues with your compiler setups. I am not an expert on installing compilers on Mac. I use macports and that works for me.

Where did you get your clang compiler from? Version 12 sounds old. My Mac has version 14. Maybe you need to upgrade X-code? Strange though as an M3 machine is so new that it should come with new software as well.

I’m not sure it’s an issue as such - the /usr/bin/gcc compiler is working really well :) I was just pointing out that the /homebrew/bin/gcc compiler doesn’t work.

I did try to install the macports version but got:

sudo port install gcc13

Portfile for gcc13 changed since last build; discarding previous state.

—> Computing dependencies for gcc13

llvm-10 is known to fail. Try to install anyway? [y/N]: n

Error: llvm-10 is known to fail

My Mac OS is 14.1 (because it was working well and there are known problems with 14.4). I have only installed the xcode command line tools but they are the correct version for Sonoma 14.1

I’m very happy with the results of the /usr/bin/gcc and clang compilers - but I will be happy to update the OS (and Xcode) once there is a fix for 14.4 and repeat these tests.

I think that this discussion started with Seong’s attempt to compile the more abstruse constant pH version of gromacs, which I haven’t attempted.

Guy

I was trying to resolve the constant-ph complation issues. But the reported error does not seem to be specific to the constant-ph branch, at least I can’t find differences in the file or files included there. And I don’t have issues compiler constant-ph (apart from the tests, where there are known issues).

I’ve just tried to compile the constant-pH code with my compiler as follows:

cmake … \

-DGMX_BUILD_OWN_FFTW=ON
-DCMAKE_C_COMPILER=/usr/bin/gcc
-DCMAKE_CXX_COMPILER=/usr/bin/g++
-DGMX_MPI=no
-DGMX_OPENMP=ON
-DGMX_GPU=OpenCL
-DBUILD_SHARED_LIBS=off
-DGMX_HWLOC=ON
-DOpenMP_{C,CXX}FLAGS=“-Xpreprocessor -fopenmp -I/opt/homebrew/opt/libomp/include”
-DOpenMP
{C,CXX}_LIB_NAMES=omp
-DOpenMP_omp_LIBRARY=/opt/homebrew/opt/libomp/lib/libomp.dylib
-DGMX_INSTALL_NBLIB_API=OFF
-DGMXAPI=OFF

make

Dies with the error:

/Users/gvigers/Work/programs/gromacs/cph/constantph/gromacs-constantph/src/gromacs/hardware/device_management_ocl.cpp:79:14: error: use of undeclared identifier ‘CTL_KERN’
mib[0] = CTL_KERN;

So I’m sure that you are right and the problems are compiler-specific. As I’ve said, I’ll repeat all the tests once OS 14.5 is safe to upgrade to on the assumption that this will also upgrade gcc/clang/llvm for the M3 chip.

Thank you!

Guy

Hi Folks

I just wanted to let you know that I’ve upgraded to OSX 14.4.1, but clang remains the same at 12.0.5 (on M3 chip). And so, of course, regular gromacs still compiles fine and constant-pH still gives the same error message as before.

However, I’ve just checked my wife’s MacBook Air with the M2 chip, also running 14.4, and it’s clang version is 15.0.0 So there really are some major and unexpected differences here!

Best Wishes

Guy

Hi,
yet another variant. I did the compilation on the new MacBook M3 Pro with gcc-13/g+±13 and it works fine, it loads properly the support for OpenMP and OpenCL. HOWEVER, the mpi is not thread-mpi, therefore MPI gets disabled at runtime.
Instead, I compiled fine with Apple clang, it gets support for thread-MPI, but pulls off OMP and OCL…
Couldn’t get how to have BOTH MPI and OCL/OMP functioning at the same time…
Has anybody else observed such a conundrum…?
cheers
Fabri