Skip to content
Merged
104 changes: 104 additions & 0 deletions src/building.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,107 @@ git clone https://github.com/spack/spack.git
cd spack
./bin/spack install -v mfem
```

## Building MFEM with CMake
To build a serial version of MFEM with CMake first create a build directory. For example, using a build directory named `build` inside the MFEM source directory:
```sh
mkdir build
cd build
```

Run the CMake configuration on the MFEM source directory.
```sh
cmake ..
```

Run the build command associated with the CMake configuration, specifying the number of parallel build tasks with the `-j` flag (4 tasks in this case).
```sh
cmake --build . -j 4
```
### Parallel build using CMake
To build a parallel version of MFEM with CMake first build METIS and Hypre as described above.
From the MFEM source directory, create a build directory. For example, using a build directory named `build` inside the MFEM source directory:
```sh
mkdir build
cd build
```

Run the CMake configuration on the MFEM source directory using the `MFEM_USE_MPI` CMake variable to enable MPI.
This will automatically search for the system MPI implementation, the METIS installation (in `<mfem-source-dir>/../metis-4.0`), and Hypre installation (in `<mfem-source-dir/../hypre`).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the disclaimer that I'm a bit biased here as the main author of the CMake TPL fetching feature, I'll suggest the option to have only the -DMFEM_FETCH_TPLS=YES approach in this documentation to avoid needing to talk about where CMake looks for Hypre and METIS (which obviously includes more than the relative paths used by the pure Makefile system)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noting that GPU builds are not yet supported with TPL fetching, my suggestion above is moot (i.e., I think the documentation should note both approaches to TPLs)

```sh
cmake .. -DMFEM_USE_MPI=YES
```
Alternatively, run the CMake configuration also using the `MFEM_FETCH_TPLS` CMake variable to enable fetching of Hypre and METIS.
This will automatically download, configure, and build Hypre and METIS alongside MFEM (note that this option is **not** currently supported for GPU builds).
```sh
cmake .. -DMFEM_USE_MPI=YES -DMFEM_FETCH_TPLS=YES
```
For either CMake configuration approach, now run the build command associated with the configuration, specifying the number of parallel build tasks with the `-j` flag (4 tasks in this case).
```sh
cmake --build . -j 4
```

### Advanced configuration steps
To build with CUDA:
```sh
cmake .. -DMFEM_USE_CUDA=YES
```
To specify what CUDA architecture to target:
```sh
cmake .. -DCUDA_ARCH="sm_70"
```
The CUDA architecture is formatted as `sm_{CC}`, or just `{CC}`, where CC is the GPU compute capability of the target GPU without the decimal point. A list of NVIDIA GPU compute capabilities can be found in [the NVIDIA developers documentation](https://developer.nvidia.com/cuda-gpus). Multiple CUDA architectures can be targeted with a comma or semicolon separated list.
```sh
cmake .. -DCUDA_ARCH="{ARCH1},{ARCH2},{ARCH3}"
```
or
```sh
cmake .. -DCUDA_ARCH="{ARCH1};{ARCH2};{ARCH3}"
```
Other accepted architecture identifies are `"all"` which targets all CUDA architectures,
`"all-major"` which targets all major versions `sm_{*0}`, and `"native"` which targets the visible GPUs on the system.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
To specify what CUDA architecture to target:
```sh
cmake .. -DCUDA_ARCH="sm_70"
```
The CUDA architecture is formatted as `sm_{CC}`, where CC is the GPU compute capability of the target GPU without the decimal point. A list of Nvidia GPU compute capabilities can be found in [the Nividia developers documentation](https://developer.nvidia.com/cuda-gpus).

TODO: currently I think MFEM's CMake setup doesn't allow targeting multiple CUDA architectures at once. I think we should fix this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a CMake 3.18 target property CUDA_ARCHITECTURES that allows semicolon separated lists https://cmake.org/cmake/help/latest/prop_tgt/CUDA_ARCHITECTURES.html

Right now it looks like mfem manually sets the -arch flag. From the nvcc compiler documentation it seems like it might take comma separated lists if the format is the same as the --gpu-code flag. I don't have access to cuda enabled machines to try this out right now.

The all, all-major, and native options for the architecture may be worth mentioning.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expanded support for all, all-major, and native as well as multiple specific cuda architectures in this PR: pr4561.

Supported formats now include:
-DCUDA_ARCH="all"
-DCUDA_ARCH="all-major"
-DCUDA_ARCH="native"
-DCUDA_ARCH="{ARCH1},{ARCH2},..."
-DCUDA_ARCH="{ARCH1};{ARCH2};..."
where ARCHN can be either just the CC number (70, 86, etc.), or optionally prefixed with sm_ (sm_70, sm_86, etc.).

This should work even for CMake older than 3.18 (the current CMakeLists minimum version is 3.8).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great, added some documentation for this!

To build with METIS 5, after following the instructions to build METIS 5 above:
```sh
cmake .. -DMFEM_USE_MPI=YES -DMFEM_USE_METIS_5=YES -DMETIS_DIR=../../metis-5.1.0
```
To build with HIP:
```sh
cmake .. -DMFEM_USE_HIP=YES
```

To specify what HIP architecture(s) to target:
```sh
cmake .. -DCMAKE_HIP_ARCHITECTURES="gfx942;gfx90a"
```
Multiple architectures can be targeted using a semi-colon separated list.
The HIP architecture for different GPU models can be found in [the LLVM documentation](https://llvm.org/docs/AMDGPUUsage.html#processors)

When building for GPUs, it is recommended to enable Umpire with `-DMFEM_USE_UMPIRE=ON`; if it is not automatically found by CMake the installation directory can be specified with `-DUMPIRE_DIR=<path-to-umpire-installation-dir>`. Umpire can be downloaded from

- [https://github.com/LLNL/Umpire/tags](https://github.com/LLNL/Umpire/tags)

### Advanced build steps
Different targets can be built with the --target flag in the build step
```sh
cmake --build . -j 4 --target <target-name>
```
To build the examples use the `examples` target (the executables will be in the `build/examples` directory).
```sh
cmake --build . -j 4 --target examples
```

To quickly check if the code is successfully built using example 1/1p use the `check` target.
```sh
cmake --build . -j 4 --target check
```

To build the miniapps use the `miniapps` target
```sh
cmake --build . -j 4 --target miniapps
```

To build everything use the `exec` target
```sh
cmake --build . -j 4 --target exec
```