Installation

StarSmasher is compiled against whatever MPI and CUDA your machine provides, so it is built from source rather than installed from a package.

What you need

  • Linux, or macOS on Apple Silicon

  • An MPI implementation providing mpif90. OpenMPI and MPICH both work

  • A Fortran compiler. Tested on gfortran 10 and later, and on ifort

  • Optionally, to write the snapshots in HDF5 as well (see HDF5 snapshots), the HDF5 library with its headers (libhdf5-dev on Debian and Ubuntu) and an MPI C compiler, mpicc

  • On Linux, optionally an NVIDIA card and CUDA. On Apple Silicon, the integrated GPU is supported through Metal. Without a GPU backend, make cpu builds a working executable

Apple Silicon users should follow the Apple Silicon installation guide. The build detects Darwin/arm64 and selects Metal automatically; CUDA is not required and no source or Makefile edits are needed.

The repository’s own documentation/installation.md covers setting these up, including installing CUDA and OpenMPI and dealing with module environments on clusters. What follows is the short version.

Getting the code

$ git clone https://github.com/jalombar/starsmasher.git

Clone rather than downloading an archive: the code is compiled against your own libraries, and a clone can be updated later.

Checking what the build will use

From parallel_bleeding_edge/src:

$ make config

which prints something like:

Platform
  OS               Linux
  architecture     x86_64
  GPU backend      cuda
MPI
  mpif90          /usr/lib64/openmpi/bin/mpif90
  wrapping        GNU
  fixed-form flag -ffixed-line-length-132 -fallow-argument-mismatch
CUDA (used only when GPU backend is cuda)
  nvcc            /usr/local/cuda/bin/nvcc
  CUDAPATH        /usr/local/cuda
  runtime libdir  /usr/local/cuda/lib64
Compilation
  FFLAGS          -O4 -mcmodel=medium
  code model      -mcmodel=medium
Output
  GPU executable  ../parallel_bleeding_edge_gpu_sph
  CPU executable  ../parallel_bleeding_edge_cpu_sph

Everything there is detected, not hardcoded. If a line is wrong, override it on the command line rather than editing the Makefile:

If this is wrong

Set

the mpif90 path, or the compiler behind it

make MPIF90=/full/path/to/mpif90

CUDAPATH, or the runtime library directory

make CUDAPATH=/opt/cuda

the optimisation flags

make OLEVEL=-O2

the compute capability, if nvidia-smi cannot report it

make COMPUTE_CAPABILITY=120

whether to build with OpenMP threads (the default is OPENMP=0)

make OPENMP=1

where HDF5 is, for make HDF5=1

make HDF5=1 HDF5ROOT=/path/to/hdf5, or HDF5_CFLAGS and HDF5_LIBS

Building

One command, from parallel_bleeding_edge/src:

$ make

That builds the gravity library too, so there is no need to build it separately. To rebuild only the library, which is occasionally useful when chasing a CUDA problem, use make -C SPHgrav_lib2.

On Apple Silicon the same command builds SPHgrav_lib_metal instead. The CPU-only target remains available with make cpu, but the Metal build is recommended for normal runs. Current M1 Pro measurements are about five times faster than CPU-only runs for the tested simulations. As a rough estimate for gravity-dominated work, Pro-class chips should be about five times faster with Metal than with CPU alone, and Max-class chips about ten times faster.

The library works out your card’s compute capability for itself, so there is normally nothing to set.

Setting the compute capability by hand

Where the machine you compile on differs from the machine you run on, such as a login node with no card in it or a cluster with mixed hardware, give the value explicitly:

$ make COMPUTE_CAPABILITY=120

Write it as the capability times ten, with no decimal point: 9.0 is 90, 12.0 is 120. The setting carries from src down into the gravity library, so it works on either make line.

If you are unsure of the number, __nvcc_device_query, which ships with CUDA, prints it. If you are unsure which of several cards to target, choose the lowest: code built for a lower capability still runs on a higher card, but not the other way round.

A successful build ends with:

***MADE VERSION THAT USES GPUS***  ->  ../parallel_bleeding_edge_gpu_sph

The executable is named after the directory containing src, and is copied one level up. make cpu produces ..._cpu_sph in the same place.

Note

nmax, nnmax and ntab are generated by the Makefile. Their defaults can be overridden without editing source files, for example with make NMAX=200000 NNMAX=128 NTAB=400000. Apple Silicon uses smaller defaults for nmax and nnmax, suitable for unified-memory Macs; other platforms keep the original values. ntab is the number of entries of the kernel tables, which are read with linear interpolation: the default of 200001 is more accurate than the former 107 entries read without interpolation, except for the cubic spline within 0.03h of a particle. kdm remains a compile-time constant in starsmasher.h.

Hybrid MPI x OpenMP

make OPENMP=1 adds -fopenmp to the Fortran flags and to the link line (make config shows OpenMP ON). The default, OPENMP=0, is the plain MPI build. With OpenMP the code starts MPI with MPI_THREAD_FUNNELED (only the master thread of each rank calls MPI and CUDA) and the loops that have been made parallel use the threads of each rank. Sums that are made in parallel use a fixed partition into blocks of 4096 terms, so that the results do not depend on the number of threads (they are the same bits for any OMP_NUM_THREADS at a fixed number of MPI ranks), and a build with one thread gives the same bits as the plain MPI build.

The number of threads of each rank is OMP_NUM_THREADS if it is set. If it is not set, a run with more than one MPI rank uses one thread per rank (so that the ranks do not all try to use the whole node), and a run with a single rank uses the OpenMP default, one thread per logical CPU. The first lines of log0.sph give the number of ranks, the number of threads and the cores of the threads of rank 0. Pin the threads, for example:

$ OMP_NUM_THREADS=8 OMP_PLACES=cores OMP_PROC_BIND=close \
    mpirun --map-by ppr:1:l3cache:pe=8 --bind-to core -np 8 ./parallel_bleeding_edge_gpu_sph

and check the placement with mpirun --report-bindings and OMP_DISPLAY_AFFINITY=true.

The layout of the ranks and threads depends on the number of GPUs. With one GPU, use one rank that has all the cores (-np 1, OMP_NUM_THREADS equal to the number of cores, ngravprocs = 1); one rank per core is not a good choice with the threaded build. With several GPUs, set ngravprocs to the number of GPUs and run one rank per GPU, each with the threads of its share of the cores, placed next to its GPU (nvidia-smi topo -m shows which cores are close to which card). For 8 ranks of 7 threads on a node with 63 usable cores and 8 cores per L3 cache, the rankfile that places rank r on the cores 8r..8r+6 is:

rank 0=+n0 slot=0-6
rank 1=+n0 slot=8-14
...
rank 7=+n0 slot=56-62

and the run is OMP_NUM_THREADS=7 OMP_PLACES=cores OMP_PROC_BIND=close mpirun --rankfile rankfile -np 8 -x OMP_NUM_THREADS -x OMP_PLACES -x OMP_PROC_BIND ./parallel_bleeding_edge_gpu_sph.

Building for the processor

By default the code is compiled for a generic x86-64 processor; the tree gravity (gravtree.f90 and gravpair.f90) has flags of its own, TREEFLAGS, which need AVX2. make MARCH=native adds -march=native to the flags of every Fortran, C and C++ file, the host code of the CUDA library included, and replaces the -mavx2 -mfma of TREEFLAGS, so that the compiler can use everything the processor has (AVX-512, fused multiply-add). MARCH can also be the name of an architecture (MARCH=znver5), which is what to use to build on one machine for another; make config prints it. The bits of the results change with MARCH (fused multiply-add changes the rounding, and wider vectors the order of some sums in the compiler’s hands) but, as with the default, they do not depend on the number of threads. Change MARCH and the next make rebuilds every file.

HDF5 snapshots (optional)

The snapshots are out*.sph files, a format of the code’s own. To have out*.h5 files as well, which Python reads directly (see HDF5 snapshots), build with HDF5=1:

$ make HDF5=1            # the GPU version
$ make cpu HDF5=1        # the CPU version

and set nhdf5=1 in sph.input. Without HDF5=1 the build is the same as before and needs nothing new, not even a C compiler. With it you need the HDF5 library with its headers, and mpicc, which compiles the one C file.

Installing HDF5. Any HDF5 from 1.10 on will do, in its serial form or with MPI. The writer calls only the C interface of the library, not the Fortran modules, whose .mod files depend on the version of the Fortran compiler, so the HDF5 on your machine does not have to have been built with yours.

Where

How

Debian, Ubuntu

sudo apt install libhdf5-dev pkg-config (or libhdf5-openmpi-dev)

macOS, Homebrew

brew install hdf5 pkg-config

A cluster

module load hdf5, or spack load hdf5, if your site has one; then give the directory with HDF5ROOT, below

Python, to read the files

pip install h5py numpy matplotlib (pandas too, for to_dataframe)

Where the build looks. In this order:

  1. HDF5ROOT: the directory that holds include and lib, as in make HDF5=1 HDF5ROOT=/opt/hdf5.

  2. HDF5_CFLAGS and HDF5_LIBS, if you give them yourself:

    $ make HDF5=1 HDF5_CFLAGS='-I/opt/hdf5/include' HDF5_LIBS='-L/opt/hdf5/lib -lhdf5'
    
  3. Otherwise pkg-config, asked for hdf5-serial, then hdf5, then hdf5-openmpi, and the first that exists is used. Without pkg-config the link line gets a plain -lhdf5.

Checking it. make config HDF5=1 prints mpicc, HDF5_CFLAGS and HDF5_LIBS, which is what the build is going to use; if one is wrong, set it as above. The link line of the build then ends with the HDF5 library. A run that asks for nhdf5 above 0 with a build made without HDF5=1 stops at the start with init: nhdf5>0 needs a build with make HDF5=1, so a build that lacks the writer cannot be mistaken for one that has it. The test suite has a test for the writer, python3 tests/run_tests.py --hdf5 hdf5_dump, which needs h5py; without HDF5 in the build, or without h5py, it is skipped with a message.

Checking that it works

From the top of the repository, not from src:

$ cd ../..
$ python3 tests/run_tests.py

The suite builds its own copy of the code and runs seven short calculations, each checking something that has to be true rather than comparing against stored output. It relaxes a polytrope and checks it against the virial theorem, verifies energy conservation, confirms the star stays put once relaxed, checks that the answer does not depend on the number of MPI ranks or on whether the GPU or CPU build is used, and confirms that two input guards still work.

Expect about seven minutes on a GPU, and longer without one. For a quicker check, --quick uses a smaller star and finishes in well under a minute:

$ python3 tests/run_tests.py --quick

Nothing about what is asserted changes, only the size of the model. Use --list to see the individual tests, and name one to run it alone.