Compiling and running GROMACS natively on Windows

Shoubhik R Maiti
CodeX
Published in
13 min readJul 19, 2021

--

I have been working for a few months on a project on molecular dynamics (MD) simulations. I was not particularly familiar with MD simulations before this, but I had to learn to use NAMD and GROMACS — two very popular softwares that can run various type of calculations using force-fields (i.e. the atoms are considered as classical particles obeying Newtonian dynamics and the forces between them are expressed by parameterised equations). There are other softwares for MD simulations, such as OpenMM, but my guess is that GROMACS and NAMD claim the lion’s share of papers published about MD.

NAMD provides precompiled binaries for Windows, but GROMACS does not, so it needs to be compiled. On Linux or POSIX-like systems compiling is quite easy. On Windows, it can be compiled, but the procedure is a bit difficult.

TL, DR: If you only want the compiled binaries, and do not want to read further, then please visit this github repository where I have put precompiled Windows binaries of GROMACS.

Here’s how to compile GROMACS for Windows natively (i.e. no Cygwin or WSL):

Requirements —

  1. Windows 10 (Windows 8 and 7 should also work but haven’t been tested)
  2. Microsoft Visual Studio 2019 (2017 also works, older versions may work)
  3. [Intel oneAPI base toolkit (for MKL)] OR [FFTW3 library]
  4. (optional) meson and ninja

Installing Visual Studio 2019

The community edition of Visual Studio 2019 (or 2017) can be downloaded for free from Microsoft website. The community edition is sufficient for our purpose. (Download page: latest version(VS2019) , VS2017 and older)

Download and run the installer. When the installer asks for which components should be installed, make sure to select “Desktop development with C++”. The other options are not necessary for installing GROMACS, so they can be left unchecked if you wish.

Visual Studio 2019 Installer

I recommend not changing any of the optional installations on the right side. The “C++ CMake tools for Windows” are required for building GROMACS. Click Install to download and install Visual Studio.

We will use the command line compilation. For this the compiler command line environment has to be brought up. Please check that you have the option “x64 Native Tools Command Prompt for VS2019” from the start menu like this:

Open the compiler command prompt and check that running cl gives an output like this: (cl.exe is the MSVC++ compiler)

Testing for the presence of cl.exe (MSVC++ compiler) on the command line

Installing Intel oneAPI for MKL (alternative to FFTW3)

GROMACS needs fast fourier transform libraries for many simulation options, such as particle mesh ewald electrostatics. These libraries can be provided by Intel MKL (Math Kernel Library). Intel MKL is quite fast, especially on Intel chips. (I am not sure if it is as fast on other chips like AMD; there were reports some time ago about Intel inserting code into MKL that checks if the processor is Intel, and then runs the slower code if it isn’t. But in the 2020 update 3, Intel claims to have fixed them.)

If you choose MKL for GROMACS, MKL will also be used for linear algebra (BLAS and LAPACK). Intel MKL is now freely available as a part of the Intel oneAPI base toolkit (Download page: base toolkit). Select Windows as Operating system. I would recommend downloading the online installer, as the local installer has the full oneAPI base kit which is not necessary. Then click download, the website will prompt you to sign in — click download as guest if you don’t have an account.

Run the installer once downloaded. Make sure to select only the “Intel oneAPI Math Kernel Library” option because the others are not required.

Intel oneAPI installer 2021 (it shows installed next to MKL as I already have it installed)

Note the installation location at the bottom left corner. Make sure that you can find the libraries mkl_intel_lp64.lib, mkl_sequential.liband mkl_core.lib from the installation directory. If the default is kept, then those libraries should be in C:\Program Files (x86)\Intel\oneAPI\mkl\latest\lib\intel64\ .

Using FFTW3 library (alternative to MKL)

If you are using Intel chips, MKL will give you the best performance. MKL is also works on AMD chips, but it is possible that other libraries will give faster performance there. You would have to run benchmarks and check for yourself. FFTW3 is one such open-source library which provides fast fourier transform functionality.

To use FFTW3 download the library from https://www.fftw.org/install/windows.html. Unfortunately, FFTW3 provides source code by default and no official windows precompiled libraries. The page linked provides libraries for windows that have been compiled by other users. Note that the windows libraries are of an older version (3.3.5) not the latest (3.3.9). If you wish to use FFTW3, download the 64-bit version zip and extract into a folder.

For the usual build of GROMACS, the single precision FFTW3 library is required. As MSVC++ compiler is being used here, the library needs to be present in import library format (.lib), which is not provided in the archive.

To make the .lib files, open the compiler command prompt for MSVC++ (“x64 Native Tools …”). Navigate to the folder where the FFTW3 files are present, then run lib /machine:x64 /def:libfftw3f-3.def . This will generate a libfftw3f-f.lib file. This is the single precision FFTW3 import library that we will need while compiling GROMACS.

(Optional) Meson and Ninja

Ninja is a build system that can take build files generated from CMake and carry out the compiling and linking to prepare the final executable software. Ninja and meson are usually distributed together for Windows in an installer. (we actually don’t have any need for meson)

Do we need to use ninja for building GROMACS? No, because Visual Studio already has a build system (nmake) which can do the same job, and is installed with Visual Studio by default. However, ninja can use all of the cores of your PC to run parallel compile jobs, whereas nmake only runs compilations sequentially, so it takes a little longer. For the purposes of this, the time difference isn’t much, so you can use nmake or ninja , whichever you wish.

Downloading GROMACS

GROMACS releases source codes at very frequent intervals. The source codes of different versions can be downloaded from this page. As they change the source code slightly in each version, it is possible that future versions may cause errors during compiling. I am using the 2021.1 release for this guide.

Download the tar.gz archive, and then extract it into a folder.

Some modifications

For some versions, modifications might be required.

For the 2021.1 version, one filename needs to be changed. Go to the folder where the extracted source files are, and find the file src\gromacs\nbnxm\kernels_simd_2xmm\kernel_ElecQSTabTwinCut_VdwLJEwCombGeom_VgrpF.cpp0000666. The extension of this file should be just .cpp, so remove those numbers. (I am not really sure why the file has a weird extension, but without the file, I am getting Unresolved Externals error during linking.) This problem is not present in 2021.2 or more recent versions.

For the 2021 version, no changes are required. I believe the same applies for older versions.

Running CMake to generate build files

CMake is bundled with Visual Studio and can be run from the x64 Native Tools command prompt. From the compiler command prompt, navigate to the folder where GROMACS source files have been extracted.

Run the following commands in order (if using Intel MKL):

set CC=cl
set CXX=cl
cmake -G"NMake Makefiles" -S. -B./build -DGMX_GPU=off -DGMX_FFT_LIBRARY=mkl -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=off -DMKL_INCLUDE_DIR="C:/Program Files (x86)/Intel/oneAPI/mkl/latest/include" -DMKL_LIBRARIES="C:/Program Files (x86)/Intel/oneAPI/mkl/latest/lib/intel64/mkl_intel_lp64.lib;C:/Program Files (x86)/Intel/oneAPI/mkl/latest/lib/intel64/mkl_sequential.lib;C:/Program Files (x86)/Intel/oneAPI/mkl/latest/lib/intel64/mkl_core.lib"

The first few lines set a few environment variables (which are not always necessary, but I am setting them just to be safe). The CC and CXX variables store the name of the C and C++ compiler that CMake will use respectively (here MSVC++ is used for both).

(Note: the cmake command is one whole command i.e. not broken into lines)

There are several things to notice here. The -G option determines to build system used for compilation. Here the argument “NMake Makefiles” specifies nmake. If you wish to use Ninja, use the argument Ninja (without quotes i.e. -GNinja ). The -S option tells cmake that the current directory (.) is the source directory. The -B option tells it to create a folder ./build and place all the configured build files there. -DGMX_GPU=off means GPU acceleration is not used (as I do not currently have a GPU; if I get one, I will update this with a GPU accelerated build guide).

Then the DGMX_FFT_LIBRARY=mkl chooses Intel MKL as the library for Fourier-transform. -DCMAKE_BUILD_TYPE=Release means highest optimization is used, without any debugging facility.-DBUILD_SHARED_LIBS=off means the GROMACS binary is statically linked to the gromacs library (gromacs.lib) and is more portable.

As we have specified the Intel MKL as the fast Fourier-transform library, we need to specify its location. The -DMKL_INLCUDE_DIR gives the location of the include files (e.g. headers such as mkl.h). The default installation directory for Intel oneAPI is C:/Program Files (x86)/Intel/oneAPI/mkl/latest/include . If you installed it in a different location, make sure to change the path accordingly.

The -DMKL_LIBRARIES gives the location of the libraries. Three libraries are required for MKL static link: mkl_intel_lp64.lib , mkl_sequential.lib and mkl_core.lib . The full paths to each of these libraries have to given, separated by a semicolon. As before, if you installed oneAPI in a different location, change accordingly.

Note that each path argument has to be inside a pair of double quotes (“ ” ) because they contain spaces. Also notice the use of frontslash (/) in the paths instead of the Windows style backslash (\). This is because CMake takes paths with frontslashes, as it treats backslash as escape character. You must use frontslash, otherwise CMake won’t be able to find the files.

After you run this command, CMake will test out a lot of different options and will try to choose the best possible compilation options on your PC. Don’t worry if there are a few “ — Failed” messages. At the end, it should show you the message “Generating done” and “Build files have been written to…”. This means CMake configuration is successful.

Command line output when CMake compilation is finished successfully

When using FFTW3 library, run these commands:

set CC=cl
set CXX=cl
cmake -G"Nmake Makefiles" -S. -B./build -DGMX_GPU=off -DGMX_FFT_LIBRARY=fftw3 -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=off -DFFTWF_INCLUDE_DIR="G:/Installer/gromacs-2021.1/fftw/fftw-3.3.5-dll64" -DFFTWF_LIBRARY="G:/Installer/gromacs-2021.1/fftw/fftw-3.3.5-dll64/libfftw3f-3.lib"

Here, the -DFFTWF_INCLUDE_DIR should point to the directory where the inlcude file fftw3.h is present. The -DFFTWF_LIBRARY should point to the library file for single precision FFTW3 (libfftw3f-3.lib)

Again, CMake should generate the build files successfully.

Final compilation and linking

The final compilation and linking can be done either by nmake or ninja . NMake can be run from the x64 Native Tools command prompt. If you used the meson-ninja installer for Windows, then ninja is automatically added to the PATH during installation, so ninja can be run from any command prompt.

Navigate to the build folder in the GROMACS source folder, and then run NMake:

cd build
nmake

For Ninja, run:

cd build
ninja -j 4

Here, -j 4 means Ninja will use 4 cores to run parallel compilation processes. If you have more cores in your chip, then change the value.

The compilation will take some time (~20 minutes). You will see a long output, and there might be a few compiler warnings — they are ok to ignore. Unless there is any error message, at the end you will see the message “Linking gmx.exe” or something similar to that. If the linking succeeds, the binary gmx.exe can be found in the build folder.

If you used Ninja, the gmx.exe is generally in the build/bin folder. If you used NMake, the gmx.exe is probably in the build folder itself.

(Optional) MPI parallel builds

The default build of GROMACS can use OpenMP parallelisation, and/or it’s own thread-based parallelisation (called thread-MPI). Both are quite efficient. However, if there are a very high number of processors, or you are running it on multiple processors (nodes) then MPI-parallelisation is better. It is unlikely that you will get any advantage with MPI based builds on a laptop or home desktop. Nevertheless, if you want to use a MPI parallelised GROMACS binary, first install MS-MPI from here. Then add the argument DGMX_MPI=on to the CMake invocation command. CMake will automatically detect the location of MS-MPI and compile with it.

Note that turning on MPI will automatically turn off thread-MPI. You will still have OpenMP support. Also note that the exectuable will be named gmx_mpi.exe after compilation finishes.

Setting up GROMACS for run

First, choose a directory where you will keep GROMACS for running. For example, you could keep it in “C:\gromacs”. Make sure that the directory name hasn’t got any spaces in it.

Create a folder “bin” inside this folder, and then put gmx.exe insider the “bin” sub-folder.

Then go to the folder where you extracted the gromacs source files. Find the folder “share”. Copy the “share” folder into your GROMACS directory. Then inside the share folder create a new folder named “gromacs” and put the template and top directories into that. (Look at the directory tree below if it’s confusing).

GROMACS uses OpenMP parallelisation and the Microsoft Visual C/C++ runtime. These are provided by .dll files (dynamic link libraries). I am not entirely sure whether Visual Studio automatically installs them or not. If they are not installed, the program will not run. You can also install only the dll files from the Visual C++ Redistributable Package here.

If you used Intel MKL:

The GROMACS executable was linked statically to Intel MKL, so no dll is necessary.

If you used FFTW3:

Find the libfftw3f-3.dll file in the directory where FFTW3 was extracted. Copy this file into the “bin” sub-folder where gmx.exe is present.

(Optional) Copy the gromacs library

There should be a gromacs.lib or gmx.lib library present inside the “lib” folder insider build folder when you finish compilation. This is required only if you are going to use GROMACS in another program. You can copy these files to a “lib” sub-folder inside the GROMACS install directory, but I don’t believe they are necessary.

(Note: if you didn’t use -DBUILD_SHARED_LIBS=off during compilation then a gromacs.dll or gmx.dll file can be created. You have to copy those files into the “bin” folder probably.)

At the end, your GROMACS directory tree should look like this:

C:\gromacs\
├───bin
│ ├───gmx.exe
│ ├───vcomp140.dll
│ └───[libfftw3f-3.dll] (only if you used FFTW3)
├───lib
│ └───gromacs.lib

└───share
└───gromacs
├───template
│ └───cmake
└───top
├───amber03.ff
├───amber94.ff
├───amber96.ff
├───amber99.ff
├───amber99sb-ildn.ff
├───amber99sb.ff
├───amberGS.ff
├───charmm27.ff
├───gromos43a1.ff
├───gromos43a2.ff
├───gromos45a3.ff
├───gromos53a5.ff
├───gromos53a6.ff
├───gromos54a7.ff
└───oplsaa.ff

Once all the files are in place, we need to create a way to access the GROMACS binary and those files from command line.

For this, create a file named “GMXRC.bat” with the following contents:

@set "PATH=C:\Gromacs\bin;%PATH%"
@set GMXDATA=C:\Gromacs\share\gromacs
@cmd

Change the paths if you are using a different location. Save this .bat file somewhere convenient.

Whenever, you need to run GROMACS, just double click the bat file and it will open a command prompt window where you can run GROMACS. Type gmx and press enter; you should see something like this:

GROMACS is running!

For MPI-parallel builds, you will have to use the MS-MPI mpiexec command to kick off the binary. So, if you have 4 processors on your computer you would use mpiexec -np 4 gmx_mpi.exe <commands> . The mpiexec command is automatically installed into your PATH when you install MS-MPI.

Regression tests

You can test whether you compiled your GROMACS correctly by running regression tests. These test can be downloaded from the same webpage as the GROMACS source codes (e.g. for v2021.1 download here).

You will also need Perl to run the tests. The tests do not take long — at most 3 minutes. So, I would recommend running the tests once. If everything is right, then at the end you would see the message “All 7 essential dynamic tests PASSED”.

If any of the tests failed, there was probably an error in compilation.

Well that’s it! You have GROMACS up and running on your Windows PC.

Is it worth it?

As you can see, compiling on Windows is quite a bit of pain, because you need to explicitly specify the paths to the libraries and include files etc.

Is it worth going through all of this trouble? When getting into computational modelling I was regularly told “why don’t you get Linux?” when faced with softwares that won’t run on Windows. My laptop came with Windows loaded, and I have all of my files and utilities that I need regularly. It was too much of risk to try to install a dual boot Linux. Despite Linux being quite a robust and old OS, a lot of utilities still do not exist on Linux. For example, I need to use a VPN for my university work, and that VPN client only installs on Windows or Mac.

Admitted, for any major production calculations you will need to use a HPC (i.e. a supercomputer). But a lot of the testing and preparatory work has to be done on my own PC, and small production runs can even be done if your PC is strong enough. So I think it is worth it, to compile GROMACS or other softwares on Windows if they can run on it.

Many people mention Cygwin and WSL whenever compiling source code (targeted at Linux) on Windows is mentioned. But they are both a Linux compatibility layers, and very slow especially if file I/O is involved. Using them should be last resort, not the first option.

As mentioned at the beginning I have also placed my compiled GROMACS executables in my github repo here. These are precompiled binaries but the redistributables for Visual C++ still has to be installed (because the binary relies on the Visual C/C++ OpenMP runtime libraries). Instructions are given in the readme file in the repository.

All of the above guide was written considering only 64-bit machines (x64). For 32-bit machines, there would be some changes.

--

--

Shoubhik R Maiti
CodeX

PhD student in computational chemistry. Interested in theoretical chemistry, programming and data science.