Spack: A Multi-Platform Package Manager
Spack is a library for managing and installing C++ libraries. Spack is open-source on GitHub and has documentation here. The purpose of this article is to provide enough background to get started with Spack and to point to resources for more advanced users.
Spack can be viewed as the Conda for C++. Imagine you are writing one Python script that requires numpy 2.3.0 and another Python script that requires numpy 2.1.0. A package manager like Conda allows you to install both numpy 2.3.0 and numpy 2.1.0 in something called an environment. Then, depending on the script you are running, you can choose which environment you want to use - the one with numpy 2.3.0 or the one with numpy 2.1.0.
Spack does the same thing with C++ libraries. For example, Cabana (see the bootcamp article) is a package included in Spack. Imagine you want to build a C++ application that uses Cabana. You can either 1) Build, install, and link Cabana to your project manually, or 2) Have Spack build, install, and link Cabana for you (preferred). The following describes 1) How to install Spack on your Machine, 2) How to create a Spack environment that includes Cabana and 3) Use that environment to develop a C++ application.
Installing and Configuring Spack
- Clone the Spack Github repository somewhere that is accessible. It is recommended to clone Spack in a directory that has a lot of storage space.
Tip
If you are on a Livermore system, such as Tuolumne or Dane, we recommend cloning spack in /usr/workspace/{$USER}
- Add Spack commands to your command-line environment by running
source /path/to/spack/spack/share/spack/setup-env.sh. It is recommended to add this command to your.bashrcfile so it runs automatically at each login. - Verify Spack was correctly activated by running
which spackand you should see the path tosetup-env.shprinted.
Additional COMPASS-Specific Instructions
- Navigate to the spack-packages repository in COMPASS. Follow the instructions in the
README.mdto setup this repository on your system. This repository contains COMPASS-specific packages, or packages not ready for the main Spack. - Read the documentation for the python script provided by that library
- Run the
spack_setup_script.pyfrom that repository with the correct flags for your system.
Creating a Spack Environment that includes Cabana
- Create a directory where you want your environment stored. In this directory, create a file called
spack.yamland copy the following:
spack: specs: # Install from code in the master branch of Cabana on Github. Include Cabana::Grid headers and install with CUDA and MPI support. - cabana@master +grid +cuda +mpi - Activate this environment with the command
spack env activate /path/to/spack.yaml. - Concretize the environment with the command
spack concretize -f. During this step, Spack finds all the required libraries and non-conflicting versions of these libraries that must be installed in order to install Cabana.- For example, Cabana depends on Kokkos, so Spack will mark Kokkos as a library that must be installed.
- Since
Cabana +mpiis requested, Spack will mark an MPI library for installation, such as openmpi. If you want to use a specific MPI implementation, add the MPI library you want to use to the environment. - The
-fflag tells Spack to force a concretization of the environment even if it doesn't detect any changes to it. - When the command completes, all libraries that must be installed will be printed to the console indicating their versions and flags. A library that is a dependency for another library will be indented.
- Things can frequently go wrong in the concretization phase. Most commonly, two dependencies will require different versions of a library that are not compatible with each other. Expect to spend time debugging the concretization of complex Spack environments.
- Install the environment with the command
spack install. Again, things can frequently go wrong here. Spack will alert you to any build errors, and expect to spend time debugging the installation of complex Spack environment. Please ask team members for help!- Libraries are installed in the directory
path/to/spack/spack/opt/spack/[system architecture].
- Libraries are installed in the directory
- Once installation is completed, Cabana is ready to be used by your application.
- To deactivate a spack environment, run the command
spack env deactivate.
Using a Spack Environment to develop a C++ Application
Now that you have a spack environment that includes Cabana, add the line find_package(Cabana) to your project's CMake, and CMake will find Cabana. Spack automatically exposes the correct environment variables to CMake so that CMake knows where to find the library files for Cabana.