Tutorial setup

If you have not done the prior sections, you’ll need to start the docker image:

docker run -it ghcr.io/spack/tutorial:sc23

and then set Spack up like this:

git clone --depth=100 --branch=releases/v0.21 https://github.com/spack/spack
. spack/share/spack/setup-env.sh
spack tutorial -y
spack bootstrap now
spack compiler find

See the Basic Installation Tutorial for full details on setup. For more help, join us in the #tutorial channel on Slack – get an invitation at slack.spack.io

Module Files Tutorial

This tutorial illustrates how Spack can be used to generate module files for the software that has been installed. Both hierarchical and non-hierarchical deployments will be discussed in details and we will show how to customize the content and naming of each module file.

At the end of the tutorial readers should have a clear understanding of:

  • What module files are and how they are used on HPC clusters

  • How Spack generates module files for the software it installs

  • Which Spack commands can be used to manage module files

  • How module files generated by Spack can be customized

and be confident that Spack can deal with all of the common use cases that occur when maintaining software installations on HPC clusters.

Setup for the Tutorial

To prepare for this tutorial we are going to install a small but representative set of software that includes different configurations of the same packages and some external packages. To keep the installations manageable, let’s start by uninstalling everything from earlier in the tutorial:

$ spack uninstall -ay

and by enabling tcl module files, which are disabled by default since Spack v0.20:

$ spack config add "modules:default:enable:[tcl]"

Build a module tool

The first thing that we need is the module tool itself. In the tutorial we will use lmod because it can work with both hierarchical and non-hierarchical layouts.

$ spack install lmod

Once the module tool is installed we need to have it available in the current shell. Installation directories in Spack’s store are definitely not easy to remember, but they can be retrieved with the spack location command:

$ . $(spack location -i lmod)/lmod/lmod/init/bash

Now we can re-source the setup file and Spack modules will be put in our module path.

$ . spack/share/spack/setup-env.sh

Add a new compiler

The second step is to build a recent compiler. On first use, Spack scans the environment and automatically locates the compiler(s) already available on the system. For this tutorial, however, we want to use gcc@12.3.0.

$ spack install gcc@12.3.0

You can get this in your environment using spack load gcc@12.3.0:

$ spack load gcc@12
$ which gcc
/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-11.4.0/gcc-12.3.0-chmemdiqoycjlxz2myvdxqzt5don54uw/bin/gcc

Now, gcc is in your PATH. You can add it to the list of compilers with spack compiler add:

$ spack compiler add
==> Added 1 new compiler to /home/spack/.spack/linux/compilers.yaml
    gcc@12.3.0
==> Compilers are defined in the following files:
    /home/spack/.spack/linux/compilers.yaml

To check which compilers are available you can use spack compiler list:

$ spack compiler list
==> Available compilers
-- clang ubuntu22.04-x86_64 -------------------------------------
clang@14.0.0

-- gcc ubuntu22.04-x86_64 ---------------------------------------
gcc@12.3.0  gcc@11.4.0	gcc@10.5.0

Finally, when you confirmed gcc@12.3.0 is properly registered, clean the environment with spack unload:

$ spack unload --all

Build the software that will be used in the tutorial

Finally, we will use Spack to install the packages used in the examples:

$ spack install netlib-scalapack ^openmpi ^openblas
$ spack install netlib-scalapack ^mpich ^openblas
$ spack install netlib-scalapack ^openmpi ^netlib-lapack
$ spack install netlib-scalapack ^mpich ^netlib-lapack
$ spack install py-scipy ^openblas

What are Module Files?

Module files are an easy way to modify your environment in a controlled manner during a shell session. In general, they contain the information needed to run an application or use a library. The module command is used to interpret and execute module files. For example, module show tells you what a module will do when loaded:

$ module show gcc
------------------------------------------------------------------------------------------
   /home/spack/spack/share/spack/modules/linux-ubuntu22.04-x86_64_v3/gcc/12.3.0-gcc-11.4.0-chmemdi:
------------------------------------------------------------------------------------------
whatis("The GNU Compiler Collection includes front ends for C, C++, Objective-C, Fortran, Ada, and Go, as well as libraries for these languages.")
depends_on("gmp/6.2.1-gcc-11.4.0-2fyiqrp")
depends_on("mpc/1.3.1-gcc-11.4.0-jueol5k")
depends_on("mpfr/4.2.0-gcc-11.4.0-qpadvjw")
depends_on("zlib-ng/2.1.4-gcc-11.4.0-5xcetrv")
depends_on("zstd/1.5.5-gcc-11.4.0-jkznmrm")
prepend_path("PATH","/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-11.4.0/gcc-12.3.0-chmemdiqoycjlxz2myvdxqzt5don54uw/bin")
prepend_path("MANPATH","/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-11.4.0/gcc-12.3.0-chmemdiqoycjlxz2myvdxqzt5don54uw/share/man")
prepend_path("CMAKE_PREFIX_PATH","/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-11.4.0/gcc-12.3.0-chmemdiqoycjlxz2myvdxqzt5don54uw/.")
setenv("CC","/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-11.4.0/gcc-12.3.0-chmemdiqoycjlxz2myvdxqzt5don54uw/bin/gcc")
setenv("CXX","/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-11.4.0/gcc-12.3.0-chmemdiqoycjlxz2myvdxqzt5don54uw/bin/g++")
setenv("FC","/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-11.4.0/gcc-12.3.0-chmemdiqoycjlxz2myvdxqzt5don54uw/bin/gfortran")
setenv("F77","/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-11.4.0/gcc-12.3.0-chmemdiqoycjlxz2myvdxqzt5don54uw/bin/gfortran")
append_path("MANPATH","")
help([[Name   : gcc
Version: 12.3.0
Target : x86_64_v3

The GNU Compiler Collection includes front ends for C, C++, Objective-C,
Fortran, Ada, and Go, as well as libraries for these languages.
]])

module load will execute all of the changes shown above:

$ which gcc
/usr/bin/gcc
$ module load gcc
$ which gcc
/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-11.4.0/gcc-12.3.0-chmemdiqoycjlxz2myvdxqzt5don54uw/bin/gcc

and to undo the modifications, you can use module unload:

$ module unload gcc
$ which gcc
/usr/bin/gcc

Module Systems

There are two main module systems used in HPC, both installable by Spack. In this tutorial we will be working with lmod and be showing examples with both Tcl and Lua.

Environment Modules

This is the original modules tool. It can be installed with Spack using the following command:

$ spack install environment-modules

It was first coded in C in the early 1990s and was later rewritten entirely in Tcl. Long stagnant, the project has been revived in the past few years by Xavier Delaruelle at CEA, and it is now very actively developed. For further details we refer to its documentation.

Lmod

Lmod is a module system written in Lua, originally created at the “Texas Advanced Computing Center” (TACC) by Robert McLay. You can get it with:

$ spack install lmod

as shown in the Setup for the Tutorial section. It is a drop-in replacement for Environment Modules, and it works with both Tcl and Lua module files. It is fully compatible with Environment Modules, but it also has many distinguishing features of its own. The main one is the module hierarchy, which simplifies the module UI by only showing modules built with the currently loaded compiler and/or MPI. There are also some unique safety features.

How does Spack generate module files?

Before we dive into the hands-on sections it’s worth explaining how module files are generated by Spack. The following diagram provides a high-level view of the process:

_images/module_file_generation.png

Modules in Spack are generated using configuration files (config.yaml and modules.yaml), information from Spack’s package recipes, and Jinja2 templates. Spack comes with Jinja2, an external template engine, so you do not need to install it yourself.

Modules vs spack load

You may have noticed that we used spack load in the Setup for the Tutorial section above. This is a built-in mechanism of Spack’s – it’s designed so that users on a cluster or a laptop can quickly get a package into their path, and it understands Spack’s spec syntax. It does not require modules, as Spack needs to work regardless of whether modules are set up on the system.

As you might expect, you can see what is loaded via spack load using spack find:

$ spack find --loaded
-- linux-ubuntu22.04-x86_64_v3 / gcc@11.4.0 ---------------------
gcc@12.3.0
==> 1 loaded package

Because Spack is designed to be run on HPC systems, it also generates a module file for every installed package. This allows users unfamiliar with Spack’s interface to see things through the module system they’re used to. To see this, try:

$ module avail

------------- /home/spack/spack/share/spack/modules/linux-ubuntu22.04-x86_64_v3 --------------
   autoconf-archive/2023.02.20-gcc-11.4.0-4wbogd6
   autoconf/2.69-gcc-11.4.0-mnfnoa5
   automake/1.16.5-gcc-11.4.0-d3cncgl
   bc/1.07.1-gcc-11.4.0-ra6efl5
   berkeley-db/18.1.40-gcc-11.4.0-ku7makq
   bzip2/1.0.8-gcc-11.4.0-4oz3kpf
   ca-certificates-mozilla/2023-05-30-gcc-11.4.0-ct4al4u
   curl/8.4.0-gcc-11.4.0-ijsmc3j
   diffutils/3.9-gcc-11.4.0-ueheij3
   ed/1.4-gcc-11.4.0-3kggq53
   gawk/5.2.2-gcc-11.4.0-t4xydm4
   gcc/12.3.0-gcc-11.4.0-chmemdi
   gdbm/1.23-gcc-11.4.0-m626hzw
   gettext/0.22.3-gcc-11.4.0-y26lmlo
   gmake/4.4.1-gcc-11.4.0-znvoani
   gmp/6.2.1-gcc-11.4.0-2fyiqrp
   libiconv/1.17-gcc-11.4.0-ivn4eq4
   libsigsegv/2.14-gcc-11.4.0-zuopqri
   libtool/2.4.7-gcc-11.4.0-rgag55h
   libxml2/2.10.3-gcc-11.4.0-67qoxbv
   lmod/8.7.18-gcc-11.4.0-y47nb2g
   lua-luafilesystem/1.8.0-gcc-11.4.0-jw4gyrw
   lua-luaposix/36.1-gcc-11.4.0-6rkzyvr
   lua/5.4.4-gcc-11.4.0-paqrr2m
   m4/1.4.19-gcc-11.4.0-jnv5nut
   mpc/1.3.1-gcc-11.4.0-jueol5k
   mpfr/4.2.0-gcc-11.4.0-qpadvjw
   ncurses/6.4-gcc-11.4.0-qqlh6as
   nghttp2/1.57.0-gcc-11.4.0-my64owh
   openssl/3.1.3-gcc-11.4.0-35j7wvr
   perl/5.38.0-gcc-11.4.0-dg34i2a
   pigz/2.7-gcc-11.4.0-catlxmo
   pkgconf/1.9.5-gcc-11.4.0-zjgtpdo
   readline/8.2-gcc-11.4.0-xxgqlmj
   tar/1.34-gcc-11.4.0-vdb3ozo
   tcl/8.6.12-gcc-11.4.0-tt2hv6r
   texinfo/7.0.3-gcc-11.4.0-km6pqxp
   unzip/6.0-gcc-11.4.0-uy5dhue
   xz/5.4.1-gcc-11.4.0-axxqoeq
   zlib-ng/2.1.4-gcc-11.4.0-5xcetrv
   zstd/1.5.5-gcc-11.4.0-jkznmrm

If the avail list is too long consider trying:

"module --default avail" or "ml -d av" to just list the default modules.
"module overview" or "ml ov" to display the number of modules for each name.

Use "module spider" to find all possible modules and extensions.
Use "module keyword key1 key2 ..." to search for all possible modules matching any of the
"keys".

You can module load any of these. By default, Spack generates modules named by package-version-compiler-version-hash, which is a bit hard to read. We’ll show you how to customize this in the following sections.

Non-hierarchical Module Files

If you arrived to this point you should have an environment that looks similar to:

$ module avail

------------- /home/spack/spack/share/spack/modules/linux-ubuntu22.04-x86_64_v3 --------------
   amdblis/4.1-gcc-12.3.0-ywpdccr
   autoconf-archive/2023.02.20-gcc-11.4.0-4wbogd6
   autoconf/2.69-gcc-11.4.0-mnfnoa5
   autoconf/2.69-gcc-12.3.0-kroqjku			 (D)
   automake/1.16.5-gcc-11.4.0-d3cncgl
   automake/1.16.5-gcc-12.3.0-dajnwux			 (D)
   bc/1.07.1-gcc-11.4.0-ra6efl5
   berkeley-db/18.1.40-gcc-11.4.0-ku7makq
   berkeley-db/18.1.40-gcc-12.3.0-krlqpve		 (D)
   bison/3.8.2-gcc-12.3.0-tr45sog
   bzip2/1.0.8-gcc-11.4.0-4oz3kpf
   bzip2/1.0.8-gcc-12.3.0-bbwtdnr			 (D)
   ca-certificates-mozilla/2023-05-30-gcc-11.4.0-ct4al4u
   ca-certificates-mozilla/2023-05-30-gcc-12.3.0-l7zpjxt (D)
   cmake/3.27.7-gcc-12.3.0-dukasmm
   curl/8.4.0-gcc-11.4.0-ijsmc3j
   curl/8.4.0-gcc-12.3.0-u2ni6an			 (D)
   diffutils/3.9-gcc-11.4.0-ueheij3
   diffutils/3.9-gcc-12.3.0-sbfcnap			 (D)
   ed/1.4-gcc-11.4.0-3kggq53
   expat/2.5.0-gcc-12.3.0-grrt7ig
   findutils/4.9.0-gcc-12.3.0-pdopgfr
   gawk/5.2.2-gcc-11.4.0-t4xydm4
   gcc/12.3.0-gcc-11.4.0-chmemdi
   gdbm/1.23-gcc-11.4.0-m626hzw
   gdbm/1.23-gcc-12.3.0-w66nich				 (D)
   gettext/0.22.3-gcc-11.4.0-y26lmlo
   gettext/0.22.3-gcc-12.3.0-y7ty4lo			 (D)
   gmake/4.4.1-gcc-11.4.0-znvoani
   gmake/4.4.1-gcc-12.3.0-6qiak7n			 (D)
   gmp/6.2.1-gcc-11.4.0-2fyiqrp
   hwloc/2.9.1-gcc-12.3.0-rvotk5a
   krb5/1.20.1-gcc-12.3.0-5d6b7ng
   libbsd/0.11.7-gcc-12.3.0-sygavnw
   libedit/3.1-20210216-gcc-12.3.0-fm2rgwy
   libevent/2.1.12-gcc-12.3.0-3rudtaf
   libfabric/1.19.0-gcc-12.3.0-46fsov7
   libffi/3.4.4-gcc-12.3.0-ecpriyn
   libiconv/1.17-gcc-11.4.0-ivn4eq4
   libiconv/1.17-gcc-12.3.0-a34xpad			 (D)
   libmd/1.0.4-gcc-12.3.0-ivvykht
   libpciaccess/0.17-gcc-12.3.0-el7pkf4
   libsigsegv/2.14-gcc-11.4.0-zuopqri
   libsigsegv/2.14-gcc-12.3.0-46vv5f3			 (D)
   libtool/2.4.7-gcc-11.4.0-rgag55h
   libtool/2.4.7-gcc-12.3.0-hvedpuf			 (D)
   libxcrypt/4.4.35-gcc-12.3.0-guszc5m
   libxml2/2.10.3-gcc-11.4.0-67qoxbv
   libxml2/2.10.3-gcc-12.3.0-damyeos			 (D)
   lmod/8.7.18-gcc-11.4.0-y47nb2g
   lua-luafilesystem/1.8.0-gcc-11.4.0-jw4gyrw
   lua-luaposix/36.1-gcc-11.4.0-6rkzyvr
   lua/5.4.4-gcc-11.4.0-paqrr2m
   m4/1.4.19-gcc-11.4.0-jnv5nut
   m4/1.4.19-gcc-12.3.0-oebiztm				 (D)
   meson/1.2.2-gcc-12.3.0-htbkkj2
   mpc/1.3.1-gcc-11.4.0-jueol5k
   mpfr/4.2.0-gcc-11.4.0-qpadvjw
   mpich/4.1.2-gcc-12.3.0-cxezwh7
   ncurses/6.4-gcc-11.4.0-qqlh6as
   ncurses/6.4-gcc-12.3.0-glwymee			 (D)
   netlib-lapack/3.11.0-gcc-12.3.0-4uab534
   netlib-scalapack/2.2.0-gcc-12.3.0-f4bc72j
   netlib-scalapack/2.2.0-gcc-12.3.0-klad7nj
   netlib-scalapack/2.2.0-gcc-12.3.0-ve3uhz5
   netlib-scalapack/2.2.0-gcc-12.3.0-3mwyatr		 (D)
   nghttp2/1.57.0-gcc-11.4.0-my64owh
   nghttp2/1.57.0-gcc-12.3.0-dvtfejq			 (D)
   ninja/1.11.1-gcc-12.3.0-qf3fwcn
   numactl/2.0.14-gcc-12.3.0-iiok6h4
   openblas/0.3.24-gcc-12.3.0-qoggfi2
   openmpi/4.1.6-gcc-12.3.0-cndwedm
   openssh/9.5p1-gcc-12.3.0-6ksmdje
   openssl/3.1.3-gcc-11.4.0-35j7wvr
   openssl/3.1.3-gcc-12.3.0-v7jc5lq			 (D)
   perl/5.38.0-gcc-11.4.0-dg34i2a
   perl/5.38.0-gcc-12.3.0-hh6v2va			 (D)
   pigz/2.7-gcc-11.4.0-catlxmo
   pigz/2.7-gcc-12.3.0-m7r2rmw				 (D)
   pkgconf/1.9.5-gcc-11.4.0-zjgtpdo
   pkgconf/1.9.5-gcc-12.3.0-ccpwoda			 (D)
   pmix/5.0.1-gcc-12.3.0-tmm3jjd
   py-beniget/0.4.1-gcc-12.3.0-fizom4w
   py-cython/0.29.36-gcc-12.3.0-ylumqbe
   py-flit-core/3.9.0-gcc-12.3.0-nzh222k
   py-gast/0.5.3-gcc-12.3.0-h7hu6wn
   py-meson-python/0.13.1-gcc-12.3.0-6zbxyin
   py-numpy/1.26.1-gcc-12.3.0-4rqb2wc
   py-packaging/23.1-gcc-12.3.0-7rj6npn
   py-pip/23.1.2-gcc-12.3.0-lvdwpa6
   py-ply/3.11-gcc-12.3.0-x3c55tr
   py-pybind11/2.11.0-gcc-12.3.0-bo5zrbu
   py-pyproject-metadata/0.7.1-gcc-12.3.0-bw34ifa
   py-pythran/0.12.2-gcc-12.3.0-7fwfpfj
   py-scipy/1.11.3-gcc-12.3.0-b6ldv5o
   py-setuptools/68.0.0-gcc-12.3.0-crc6iaj
   py-wheel/0.41.2-gcc-12.3.0-zwngepq
   python/3.11.6-gcc-12.3.0-oa7j22b
   re2c/2.2-gcc-12.3.0-cw5qvcn
   readline/8.2-gcc-11.4.0-xxgqlmj
   readline/8.2-gcc-12.3.0-cz4lfdu			 (D)
   sqlite/3.43.2-gcc-12.3.0-p3srvwu
   tar/1.34-gcc-11.4.0-vdb3ozo
   tar/1.34-gcc-12.3.0-waes7yx				 (D)
   tcl/8.6.12-gcc-11.4.0-tt2hv6r
   texinfo/7.0.3-gcc-11.4.0-km6pqxp
   unzip/6.0-gcc-11.4.0-uy5dhue
   util-linux-uuid/2.38.1-gcc-12.3.0-oagevhm
   util-macros/1.19.3-gcc-12.3.0-t67cwmg
   xz/5.4.1-gcc-11.4.0-axxqoeq
   xz/5.4.1-gcc-12.3.0-taa3gkk				 (D)
   yaksa/0.3-gcc-12.3.0-hmywdkq
   zlib-ng/2.1.4-gcc-11.4.0-5xcetrv
   zlib-ng/2.1.4-gcc-12.3.0-draqwfy			 (D)
   zstd/1.5.5-gcc-11.4.0-jkznmrm
   zstd/1.5.5-gcc-12.3.0-ngvd73i			 (D)

  Where:
   D:  Default Module

If the avail list is too long consider trying:

"module --default avail" or "ml -d av" to just list the default modules.
"module overview" or "ml ov" to display the number of modules for each name.

Use "module spider" to find all possible modules and extensions.
Use "module keyword key1 key2 ..." to search for all possible modules matching any of the
"keys".

The non-hierarchical module files that have been generated so far follow Spack’s default rules for module generation. Taking a look at the gcc module you’ll see, for example:

$ module show gcc
------------------------------------------------------------------------------------------
   /home/spack/spack/share/spack/modules/linux-ubuntu22.04-x86_64_v3/gcc/12.3.0-gcc-11.4.0-chmemdi:
------------------------------------------------------------------------------------------
whatis("The GNU Compiler Collection includes front ends for C, C++, Objective-C, Fortran, Ada, and Go, as well as libraries for these languages.")
depends_on("gmp/6.2.1-gcc-11.4.0-2fyiqrp")
depends_on("mpc/1.3.1-gcc-11.4.0-jueol5k")
depends_on("mpfr/4.2.0-gcc-11.4.0-qpadvjw")
depends_on("zlib-ng/2.1.4-gcc-11.4.0-5xcetrv")
depends_on("zstd/1.5.5-gcc-11.4.0-jkznmrm")
prepend_path("PATH","/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-11.4.0/gcc-12.3.0-chmemdiqoycjlxz2myvdxqzt5don54uw/bin")
prepend_path("MANPATH","/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-11.4.0/gcc-12.3.0-chmemdiqoycjlxz2myvdxqzt5don54uw/share/man")
prepend_path("CMAKE_PREFIX_PATH","/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-11.4.0/gcc-12.3.0-chmemdiqoycjlxz2myvdxqzt5don54uw/.")
setenv("CC","/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-11.4.0/gcc-12.3.0-chmemdiqoycjlxz2myvdxqzt5don54uw/bin/gcc")
setenv("CXX","/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-11.4.0/gcc-12.3.0-chmemdiqoycjlxz2myvdxqzt5don54uw/bin/g++")
setenv("FC","/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-11.4.0/gcc-12.3.0-chmemdiqoycjlxz2myvdxqzt5don54uw/bin/gfortran")
setenv("F77","/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-11.4.0/gcc-12.3.0-chmemdiqoycjlxz2myvdxqzt5don54uw/bin/gfortran")
append_path("MANPATH","")
help([[Name   : gcc
Version: 12.3.0
Target : x86_64_v3

The GNU Compiler Collection includes front ends for C, C++, Objective-C,
Fortran, Ada, and Go, as well as libraries for these languages.
]])

As expected, a few environment variables representing paths will be modified by the module file according to the default prefix inspection rules.

Filter unwanted modifications to the environment

Now consider the case that your site has decided that CC, CXX, FC and F77 modifications should not be present in module files. What you can do to abide by the rules is to create a configuration file ${SPACK_ROOT}/etc/spack/modules.yaml with the following content:

modules:
  default:
    tcl:
      all:
        filter:
          exclude_env_vars:
          - "CC"
          - "CXX"
          - "FC"
          - "F77"

This can be done either editing the configuration manually, or directly from the command line:

$ spack config add "modules:default:tcl:all:filter:exclude_env_vars:['CC', 'CXX', 'F77', 'FC']"

Next you should regenerate all the module files:

$ spack module tcl refresh -y
==> Regenerating tcl module files

If you take a look now at the module for gcc you’ll see that the unwanted paths have disappeared:

$ module show gcc
------------------------------------------------------------------------------------------
   /home/spack/spack/share/spack/modules/linux-ubuntu22.04-x86_64_v3/gcc/12.3.0-gcc-11.4.0-chmemdi:
------------------------------------------------------------------------------------------
whatis("The GNU Compiler Collection includes front ends for C, C++, Objective-C, Fortran, Ada, and Go, as well as libraries for these languages.")
depends_on("gmp/6.2.1-gcc-11.4.0-2fyiqrp")
depends_on("mpc/1.3.1-gcc-11.4.0-jueol5k")
depends_on("mpfr/4.2.0-gcc-11.4.0-qpadvjw")
depends_on("zlib-ng/2.1.4-gcc-11.4.0-5xcetrv")
depends_on("zstd/1.5.5-gcc-11.4.0-jkznmrm")
prepend_path("PATH","/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-11.4.0/gcc-12.3.0-chmemdiqoycjlxz2myvdxqzt5don54uw/bin")
prepend_path("MANPATH","/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-11.4.0/gcc-12.3.0-chmemdiqoycjlxz2myvdxqzt5don54uw/share/man")
prepend_path("CMAKE_PREFIX_PATH","/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-11.4.0/gcc-12.3.0-chmemdiqoycjlxz2myvdxqzt5don54uw/.")
append_path("MANPATH","")
help([[Name   : gcc
Version: 12.3.0
Target : x86_64_v3

The GNU Compiler Collection includes front ends for C, C++, Objective-C,
Fortran, Ada, and Go, as well as libraries for these languages.
]])

Prevent some module files from being generated

Another common request at many sites is to avoid exposing software that is only needed as an intermediate step when building a newer stack. Let’s try to prevent the generation of module files for anything that is compiled with gcc@11 (the OS provided compiler).

To do this you should add the exclude keyword to ${SPACK_ROOT}/etc/spack/modules.yaml:

modules:
  default:
    tcl:
      exclude:
      -  '%gcc@11'
      all:
        filter:
          exclude_env_vars:
          - "CC"
          - "CXX"
          - "FC"
          - "F77"

and regenerate the module files. This time we’ll pass the option --delete-tree so that Spack will delete the existing module tree and regenerate a new one, instead of overwriting the files in the existing directory.

$ spack module tcl refresh --delete-tree -y
==> Regenerating tcl module files
$ module avail

------------- /home/spack/spack/share/spack/modules/linux-ubuntu22.04-x86_64_v3 --------------
   amdblis/4.1-gcc-12.3.0-ywpdccr
   autoconf/2.69-gcc-12.3.0-kroqjku
   automake/1.16.5-gcc-12.3.0-dajnwux
   berkeley-db/18.1.40-gcc-12.3.0-krlqpve
   bison/3.8.2-gcc-12.3.0-tr45sog
   bzip2/1.0.8-gcc-12.3.0-bbwtdnr
   ca-certificates-mozilla/2023-05-30-gcc-12.3.0-l7zpjxt
   cmake/3.27.7-gcc-12.3.0-dukasmm
   curl/8.4.0-gcc-12.3.0-u2ni6an
   diffutils/3.9-gcc-12.3.0-sbfcnap
   expat/2.5.0-gcc-12.3.0-grrt7ig
   findutils/4.9.0-gcc-12.3.0-pdopgfr
   gdbm/1.23-gcc-12.3.0-w66nich
   gettext/0.22.3-gcc-12.3.0-y7ty4lo
   gmake/4.4.1-gcc-12.3.0-6qiak7n
   hwloc/2.9.1-gcc-12.3.0-rvotk5a
   krb5/1.20.1-gcc-12.3.0-5d6b7ng
   libbsd/0.11.7-gcc-12.3.0-sygavnw
   libedit/3.1-20210216-gcc-12.3.0-fm2rgwy
   libevent/2.1.12-gcc-12.3.0-3rudtaf
   libfabric/1.19.0-gcc-12.3.0-46fsov7
   libffi/3.4.4-gcc-12.3.0-ecpriyn
   libiconv/1.17-gcc-12.3.0-a34xpad
   libmd/1.0.4-gcc-12.3.0-ivvykht
   libpciaccess/0.17-gcc-12.3.0-el7pkf4
   libsigsegv/2.14-gcc-12.3.0-46vv5f3
   libtool/2.4.7-gcc-12.3.0-hvedpuf
   libxcrypt/4.4.35-gcc-12.3.0-guszc5m
   libxml2/2.10.3-gcc-12.3.0-damyeos
   m4/1.4.19-gcc-12.3.0-oebiztm
   meson/1.2.2-gcc-12.3.0-htbkkj2
   mpich/4.1.2-gcc-12.3.0-cxezwh7
   ncurses/6.4-gcc-12.3.0-glwymee
   netlib-lapack/3.11.0-gcc-12.3.0-4uab534
   netlib-scalapack/2.2.0-gcc-12.3.0-f4bc72j
   netlib-scalapack/2.2.0-gcc-12.3.0-klad7nj
   netlib-scalapack/2.2.0-gcc-12.3.0-ve3uhz5
   netlib-scalapack/2.2.0-gcc-12.3.0-3mwyatr		 (D)
   nghttp2/1.57.0-gcc-12.3.0-dvtfejq
   ninja/1.11.1-gcc-12.3.0-qf3fwcn
   numactl/2.0.14-gcc-12.3.0-iiok6h4
   openblas/0.3.24-gcc-12.3.0-qoggfi2
   openmpi/4.1.6-gcc-12.3.0-cndwedm
   openssh/9.5p1-gcc-12.3.0-6ksmdje
   openssl/3.1.3-gcc-12.3.0-v7jc5lq
   perl/5.38.0-gcc-12.3.0-hh6v2va
   pigz/2.7-gcc-12.3.0-m7r2rmw
   pkgconf/1.9.5-gcc-12.3.0-ccpwoda
   pmix/5.0.1-gcc-12.3.0-tmm3jjd
   py-beniget/0.4.1-gcc-12.3.0-fizom4w
   py-cython/0.29.36-gcc-12.3.0-ylumqbe
   py-flit-core/3.9.0-gcc-12.3.0-nzh222k
   py-gast/0.5.3-gcc-12.3.0-h7hu6wn
   py-meson-python/0.13.1-gcc-12.3.0-6zbxyin
   py-numpy/1.26.1-gcc-12.3.0-4rqb2wc
   py-packaging/23.1-gcc-12.3.0-7rj6npn
   py-pip/23.1.2-gcc-12.3.0-lvdwpa6
   py-ply/3.11-gcc-12.3.0-x3c55tr
   py-pybind11/2.11.0-gcc-12.3.0-bo5zrbu
   py-pyproject-metadata/0.7.1-gcc-12.3.0-bw34ifa
   py-pythran/0.12.2-gcc-12.3.0-7fwfpfj
   py-scipy/1.11.3-gcc-12.3.0-b6ldv5o
   py-setuptools/68.0.0-gcc-12.3.0-crc6iaj
   py-wheel/0.41.2-gcc-12.3.0-zwngepq
   python/3.11.6-gcc-12.3.0-oa7j22b
   re2c/2.2-gcc-12.3.0-cw5qvcn
   readline/8.2-gcc-12.3.0-cz4lfdu
   sqlite/3.43.2-gcc-12.3.0-p3srvwu
   tar/1.34-gcc-12.3.0-waes7yx
   util-linux-uuid/2.38.1-gcc-12.3.0-oagevhm
   util-macros/1.19.3-gcc-12.3.0-t67cwmg
   xz/5.4.1-gcc-12.3.0-taa3gkk
   yaksa/0.3-gcc-12.3.0-hmywdkq
   zlib-ng/2.1.4-gcc-12.3.0-draqwfy
   zstd/1.5.5-gcc-12.3.0-ngvd73i

  Where:
   D:  Default Module

If the avail list is too long consider trying:

"module --default avail" or "ml -d av" to just list the default modules.
"module overview" or "ml ov" to display the number of modules for each name.

Use "module spider" to find all possible modules and extensions.
Use "module keyword key1 key2 ..." to search for all possible modules matching any of the
"keys".

if you look closely you’ll see though that we went too far in excluding modules: the module for gcc@12.3.0 disappeared as it was bootstrapped with gcc@11. To specify exceptions to the exclude rules you can use include:

modules:
  default:
    tcl:
      include:
      -  gcc
      exclude:
      -  '%gcc@11'
      all:
        filter:
          exclude_env_vars:
          - "CC"
          - "CXX"
          - "FC"
          - "F77"

include rules always have precedence over exclude rules. If you regenerate the modules again:

$ spack module tcl refresh -y
==> Regenerating tcl module files

you’ll see that now the module for gcc@12.3.0 has reappeared:

$ module avail gcc/

------------- /home/spack/spack/share/spack/modules/linux-ubuntu22.04-x86_64_v3 --------------
   gcc/12.3.0-gcc-11.4.0-chmemdi

If the avail list is too long consider trying:

"module --default avail" or "ml -d av" to just list the default modules.
"module overview" or "ml ov" to display the number of modules for each name.

Use "module spider" to find all possible modules and extensions.
Use "module keyword key1 key2 ..." to search for all possible modules matching any of the
"keys".

An additional feature that you can leverage to unclutter the environment is to skip the generation of module files for implicitly installed packages. In this case you only need to add the following line:

modules:
  default:
    tcl:
      exclude_implicits: true
      include:
      -  gcc
      exclude:
      -  '%gcc@11'
      all:
        filter:
          exclude_env_vars:
          - "CC"
          - "CXX"
          - "FC"
          - "F77"

to modules.yaml and regenerate the module file tree as above.

Change module file naming

The next step in making module files more user-friendly is to improve their naming scheme. To reduce the length of the hash or remove it altogether you can use the hash_length keyword in the configuration file:

modules:
  default:
    tcl:
      hash_length: 0
      include:
      -  gcc
      exclude:
      -  '%gcc@11'
      all:
        filter:
          exclude_env_vars:
          - "CC"
          - "CXX"
          - "FC"
          - "F77"

If you try to regenerate the module files now you will get an error:

$ spack module tcl refresh --delete-tree -y
==> Error: Name clashes detected in module files:

file: /home/spack/spack/share/spack/modules/linux-ubuntu22.04-x86_64_v3/netlib-scalapack/2.2.0-gcc-12.3.0
spec: netlib-scalapack@=2.2.0%gcc@=12.3.0/3mwyatr ~ipo~pic+shared build_system=cmake build_type=Release generator=make patches=072b006,1c9ce5f,244a9aa arch=linux-ubuntu22.04-x86_64_v3
spec: netlib-scalapack@=2.2.0%gcc@=12.3.0/f4bc72j ~ipo~pic+shared build_system=cmake build_type=Release generator=make patches=072b006,1c9ce5f,244a9aa arch=linux-ubuntu22.04-x86_64_v3
spec: netlib-scalapack@=2.2.0%gcc@=12.3.0/ve3uhz5 ~ipo~pic+shared build_system=cmake build_type=Release generator=make patches=072b006,1c9ce5f,244a9aa arch=linux-ubuntu22.04-x86_64_v3
spec: netlib-scalapack@=2.2.0%gcc@=12.3.0/klad7nj ~ipo~pic+shared build_system=cmake build_type=Release generator=make patches=072b006,1c9ce5f,244a9aa arch=linux-ubuntu22.04-x86_64_v3

==> Error: Operation aborted

Note

We try to check for errors up front!

In Spack we check for errors upfront whenever possible, so don’t worry about your module files: as a name clash was detected nothing has been changed on disk.

The problem here is that without the hashes the four different flavors of netlib-scalapack map to the same module file name. We can change how the names are formatted to differentiate them:

modules:
  default:
    tcl:
      hash_length: 0
      include:
      -  gcc
      exclude:
      -  '%gcc@11'
      all:
        conflict:
        - '{name}'
        filter:
          exclude_env_vars:
          - "CC"
          - "CXX"
          - "FC"
          - "F77"
      projections:
        all:               '{name}/{version}-{compiler.name}-{compiler.version}'
        netlib-scalapack:  '{name}/{version}-{compiler.name}-{compiler.version}-{^lapack.name}-{^mpi.name}'
        ^python^lapack:    '{name}/{version}-{compiler.name}-{compiler.version}-{^lapack.name}'

As you can see it is possible to specify rules that apply only to a restricted set of packages using anonymous specs like ^python^lapack. Here we declare a conflict between any two modules with the same name, so they cannot be loaded together. We also format the names of modules according to compiler, compiler version, and MPI provider name using the spec format syntax. This allows us to match specs by their dependencies, and format them based on their DAGs.

$ spack module tcl refresh --delete-tree -y
==> Regenerating tcl module files
$ module avail

------------- /home/spack/spack/share/spack/modules/linux-ubuntu22.04-x86_64_v3 --------------
   amdblis/4.1-gcc-12.3.0
   autoconf/2.69-gcc-12.3.0
   automake/1.16.5-gcc-12.3.0
   berkeley-db/18.1.40-gcc-12.3.0
   bison/3.8.2-gcc-12.3.0
   bzip2/1.0.8-gcc-12.3.0
   ca-certificates-mozilla/2023-05-30-gcc-12.3.0
   cmake/3.27.7-gcc-12.3.0
   curl/8.4.0-gcc-12.3.0
   diffutils/3.9-gcc-12.3.0
   expat/2.5.0-gcc-12.3.0
   findutils/4.9.0-gcc-12.3.0
   gcc/12.3.0-gcc-11.4.0
   gdbm/1.23-gcc-12.3.0
   gettext/0.22.3-gcc-12.3.0
   gmake/4.4.1-gcc-12.3.0
   hwloc/2.9.1-gcc-12.3.0
   krb5/1.20.1-gcc-12.3.0
   libbsd/0.11.7-gcc-12.3.0
   libedit/3.1-20210216-gcc-12.3.0
   libevent/2.1.12-gcc-12.3.0
   libfabric/1.19.0-gcc-12.3.0
   libffi/3.4.4-gcc-12.3.0
   libiconv/1.17-gcc-12.3.0
   libmd/1.0.4-gcc-12.3.0
   libpciaccess/0.17-gcc-12.3.0
   libsigsegv/2.14-gcc-12.3.0
   libtool/2.4.7-gcc-12.3.0
   libxcrypt/4.4.35-gcc-12.3.0
   libxml2/2.10.3-gcc-12.3.0
   m4/1.4.19-gcc-12.3.0
   meson/1.2.2-gcc-12.3.0
   mpich/4.1.2-gcc-12.3.0
   ncurses/6.4-gcc-12.3.0
   netlib-lapack/3.11.0-gcc-12.3.0
   netlib-scalapack/2.2.0-gcc-12.3.0-netlib-lapack-mpich
   netlib-scalapack/2.2.0-gcc-12.3.0-netlib-lapack-openmpi
   netlib-scalapack/2.2.0-gcc-12.3.0-openblas-mpich
   netlib-scalapack/2.2.0-gcc-12.3.0-openblas-openmpi	   (D)
   nghttp2/1.57.0-gcc-12.3.0
   ninja/1.11.1-gcc-12.3.0
   numactl/2.0.14-gcc-12.3.0
   openblas/0.3.24-gcc-12.3.0
   openmpi/4.1.6-gcc-12.3.0
   openssh/9.5p1-gcc-12.3.0
   openssl/3.1.3-gcc-12.3.0
   perl/5.38.0-gcc-12.3.0
   pigz/2.7-gcc-12.3.0
   pkgconf/1.9.5-gcc-12.3.0
   pmix/5.0.1-gcc-12.3.0
   py-beniget/0.4.1-gcc-12.3.0
   py-cython/0.29.36-gcc-12.3.0
   py-flit-core/3.9.0-gcc-12.3.0
   py-gast/0.5.3-gcc-12.3.0
   py-meson-python/0.13.1-gcc-12.3.0
   py-numpy/1.26.1-gcc-12.3.0-openblas
   py-packaging/23.1-gcc-12.3.0
   py-pip/23.1.2-gcc-12.3.0
   py-ply/3.11-gcc-12.3.0
   py-pybind11/2.11.0-gcc-12.3.0
   py-pyproject-metadata/0.7.1-gcc-12.3.0
   py-pythran/0.12.2-gcc-12.3.0-openblas
   py-scipy/1.11.3-gcc-12.3.0-openblas
   py-setuptools/68.0.0-gcc-12.3.0
   py-wheel/0.41.2-gcc-12.3.0
   python/3.11.6-gcc-12.3.0
   re2c/2.2-gcc-12.3.0
   readline/8.2-gcc-12.3.0
   sqlite/3.43.2-gcc-12.3.0
   tar/1.34-gcc-12.3.0
   util-linux-uuid/2.38.1-gcc-12.3.0
   util-macros/1.19.3-gcc-12.3.0
   xz/5.4.1-gcc-12.3.0
   yaksa/0.3-gcc-12.3.0
   zlib-ng/2.1.4-gcc-12.3.0
   zstd/1.5.5-gcc-12.3.0

  Where:
   D:  Default Module

If the avail list is too long consider trying:

"module --default avail" or "ml -d av" to just list the default modules.
"module overview" or "ml ov" to display the number of modules for each name.

Use "module spider" to find all possible modules and extensions.
Use "module keyword key1 key2 ..." to search for all possible modules matching any of the
"keys".

Note

The conflict directive is Tcl-specific and can’t be used in the lmod section of the configuration file.

Add custom environment modifications

At many sites it is customary to set an environment variable in a package’s module file that points to the folder in which the package is installed. You can achieve this with Spack by adding an environment directive to the configuration file:

modules:
  default:
    tcl:
      hash_length: 0
      naming_scheme: '{name}/{version}-{compiler.name}-{compiler.version}'
      include:
      -  gcc
      exclude:
      -  '%gcc@11'
      all:
        conflict:
        - '{name}'
        filter:
          exclude_env_vars:
          - "CC"
          - "CXX"
          - "FC"
          - "F77"
        environment:
          set:
            '{name}_ROOT': '{prefix}'
      projections:
        all:               '{name}/{version}-{compiler.name}-{compiler.version}'
        netlib-scalapack:  '{name}/{version}-{compiler.name}-{compiler.version}-{^lapack.name}-{^mpi.name}'
        ^python^lapack:    '{name}/{version}-{compiler.name}-{compiler.version}-{^lapack.name}'

Under the hood Spack uses the format() API to substitute tokens in either environment variable names or values. There are two caveats though:

  • The set of allowed tokens in variable names is restricted to name, version, compiler, compiler.name, compiler.version, architecture

  • Any token expanded in a variable name is made uppercase, but other than that case sensitivity is preserved

Regenerating the module files results in something like:

$ spack module tcl refresh -y
==> Regenerating tcl module files
$ module show gcc
------------------------------------------------------------------------------------------
   /home/spack/spack/share/spack/modules/linux-ubuntu22.04-x86_64_v3/gcc/12.3.0-gcc-11.4.0:
------------------------------------------------------------------------------------------
whatis("The GNU Compiler Collection includes front ends for C, C++, Objective-C, Fortran, Ada, and Go, as well as libraries for these languages.")
conflict("gcc")
prepend_path("PATH","/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-11.4.0/gcc-12.3.0-chmemdiqoycjlxz2myvdxqzt5don54uw/bin")
prepend_path("MANPATH","/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-11.4.0/gcc-12.3.0-chmemdiqoycjlxz2myvdxqzt5don54uw/share/man")
prepend_path("CMAKE_PREFIX_PATH","/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-11.4.0/gcc-12.3.0-chmemdiqoycjlxz2myvdxqzt5don54uw/.")
setenv("GCC_ROOT",""/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-11.4.0/gcc-12.3.0-chmemdiqoycjlxz2myvdxqzt5don54uw"")
append_path("MANPATH","")
help([[Name   : gcc
Version: 12.3.0
Target : x86_64_v3

The GNU Compiler Collection includes front ends for C, C++, Objective-C,
Fortran, Ada, and Go, as well as libraries for these languages.
]])

As you can see, the gcc module has the environment variable GCC_ROOT set.

Sometimes it’s also useful to apply environment modifications selectively and target only certain packages. You can for instance apply modifications to the openmpi module as follows:

modules:
  default:
    tcl:
      hash_length: 0
      naming_scheme: '{name}/{version}-{compiler.name}-{compiler.version}'
      include:
      - gcc
      exclude:
      - '%gcc@11'
      all:
        conflict:
        - '{name}'
        filter:
          exclude_env_vars:
          - "CC"
          - "CXX"
          - "FC"
          - "F77"
        environment:
          set:
            '{name}_ROOT': '{prefix}'
      openmpi:
        environment:
          set:
            SLURM_MPI_TYPE: pmi2
            OMPI_MCA_btl_openib_warn_default_gid_prefix: '0'
      projections:
        all:               '{name}/{version}-{compiler.name}-{compiler.version}'
        netlib-scalapack:  '{name}/{version}-{compiler.name}-{compiler.version}-{^lapack.name}-{^mpi.name}'
        ^python^lapack:    '{name}/{version}-{compiler.name}-{compiler.version}-{^lapack.name}'

This time we will be more selective and regenerate only the openmpi module file:

$ spack module tcl refresh -y openmpi
==> Regenerating tcl module files
$ module show openmpi
------------------------------------------------------------------------------------------
   /home/spack/spack/share/spack/modules/linux-ubuntu22.04-x86_64_v3/openmpi/4.1.6-gcc-12.3.0:
------------------------------------------------------------------------------------------
whatis("An open source Message Passing Interface implementation.")
depends_on("hwloc/2.9.1-gcc-12.3.0")
depends_on("numactl/2.0.14-gcc-12.3.0")
depends_on("openssh/9.5p1-gcc-12.3.0")
depends_on("pmix/5.0.1-gcc-12.3.0")
depends_on("zlib-ng/2.1.4-gcc-12.3.0")
conflict("openmpi")
prepend_path("PATH","/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-12.3.0/openmpi-4.1.6-cndwedmmdi6vycttha5zw7yrvn7cgeix/bin")
prepend_path("MANPATH","/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-12.3.0/openmpi-4.1.6-cndwedmmdi6vycttha5zw7yrvn7cgeix/share/man")
prepend_path("PKG_CONFIG_PATH","/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-12.3.0/openmpi-4.1.6-cndwedmmdi6vycttha5zw7yrvn7cgeix/lib/pkgconfig")
prepend_path("CMAKE_PREFIX_PATH","/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-12.3.0/openmpi-4.1.6-cndwedmmdi6vycttha5zw7yrvn7cgeix/.")
setenv("MPICC","/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-12.3.0/openmpi-4.1.6-cndwedmmdi6vycttha5zw7yrvn7cgeix/bin/mpicc")
setenv("MPICXX","/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-12.3.0/openmpi-4.1.6-cndwedmmdi6vycttha5zw7yrvn7cgeix/bin/mpic++")
setenv("MPIF77","/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-12.3.0/openmpi-4.1.6-cndwedmmdi6vycttha5zw7yrvn7cgeix/bin/mpif77")
setenv("MPIF90","/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-12.3.0/openmpi-4.1.6-cndwedmmdi6vycttha5zw7yrvn7cgeix/bin/mpif90")
setenv("OPENMPI_ROOT",""/home/spack/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-12.3.0/openmpi-4.1.6-cndwedmmdi6vycttha5zw7yrvn7cgeix"")
setenv("SLURM_MPI_TYPE","pmi2")
setenv("OMPI_MCA_btl_openib_warn_default_git_prefix","0")
append_path("MANPATH","")
help([[Name   : openmpi
Version: 4.1.6
Target : x86_64_v3

An open source Message Passing Interface implementation. The Open MPI
Project is an open source Message Passing Interface implementation that
is developed and maintained by a consortium of academic, research, and
industry partners. Open MPI is therefore able to combine the expertise,
technologies, and resources from all across the High Performance
Computing community in order to build the best MPI library available.
Open MPI offers advantages for system and software vendors, application
developers and computer science researchers.
]])

Autoload dependencies

Spack can also generate module files that contain code to load the dependencies automatically. You can, for instance generate python modules that load their dependencies by adding the autoload directive and assigning it the value direct:

modules:
  default:
    tcl:
      verbose: true
      hash_length: 0
      naming_scheme: '{name}/{version}-{compiler.name}-{compiler.version}'
      include:
      - gcc
      exclude:
      - '%gcc@11'
      all:
        conflict:
        - '{name}'
        filter:
          exclude_env_vars:
          - "CC"
          - "CXX"
          - "FC"
          - "F77"
        environment:
          set:
            '{name}_ROOT': '{prefix}'
      openmpi:
        environment:
          set:
            SLURM_MPI_TYPE: pmi2
            OMPI_MCA_btl_openib_warn_default_gid_prefix: '0'
      projections:
        all:               '{name}/{version}-{compiler.name}-{compiler.version}'
        netlib-scalapack:  '{name}/{version}-{compiler.name}-{compiler.version}-{^lapack.name}-{^mpi.name}'
        ^python^lapack:    '{name}/{version}-{compiler.name}-{compiler.version}-{^lapack.name}'
      ^python:
        autoload:  direct

and regenerating the module files for every package that depends on python:

$ spack module tcl refresh -y ^python
==> Regenerating tcl module files

and will contain code to autoload all the dependencies:

$ module load py-scipy

In case messages are unwanted during the autoload procedure, it will be sufficient to omit the line setting verbose: true in the configuration file above.

Hierarchical Module Files

So far we worked with non-hierarchical module files, i.e. with module files that are all generated in the same root directory and don’t attempt to dynamically modify the MODULEPATH. This results in a flat module structure where all the software is visible at the same time:

$ module avail

------------- /home/spack/spack/share/spack/modules/linux-ubuntu22.04-x86_64_v3 --------------
   amdblis/4.1-gcc-12.3.0
   autoconf/2.69-gcc-12.3.0
   automake/1.16.5-gcc-12.3.0
   berkeley-db/18.1.40-gcc-12.3.0
   bison/3.8.2-gcc-12.3.0
   bzip2/1.0.8-gcc-12.3.0
   ca-certificates-mozilla/2023-05-30-gcc-12.3.0
   cmake/3.27.7-gcc-12.3.0
   curl/8.4.0-gcc-12.3.0
   diffutils/3.9-gcc-12.3.0
   expat/2.5.0-gcc-12.3.0
   findutils/4.9.0-gcc-12.3.0
   gcc/12.3.0-gcc-11.4.0
   gdbm/1.23-gcc-12.3.0
   gettext/0.22.3-gcc-12.3.0
   gmake/4.4.1-gcc-12.3.0
   hwloc/2.9.1-gcc-12.3.0
   krb5/1.20.1-gcc-12.3.0
   libbsd/0.11.7-gcc-12.3.0
   libedit/3.1-20210216-gcc-12.3.0
   libevent/2.1.12-gcc-12.3.0
   libfabric/1.19.0-gcc-12.3.0
   libffi/3.4.4-gcc-12.3.0
   libiconv/1.17-gcc-12.3.0
   libmd/1.0.4-gcc-12.3.0
   libpciaccess/0.17-gcc-12.3.0
   libsigsegv/2.14-gcc-12.3.0
   libtool/2.4.7-gcc-12.3.0
   libxcrypt/4.4.35-gcc-12.3.0
   libxml2/2.10.3-gcc-12.3.0
   m4/1.4.19-gcc-12.3.0
   meson/1.2.2-gcc-12.3.0
   mpich/4.1.2-gcc-12.3.0
   ncurses/6.4-gcc-12.3.0
   netlib-lapack/3.11.0-gcc-12.3.0
   netlib-scalapack/2.2.0-gcc-12.3.0-netlib-lapack-mpich
   netlib-scalapack/2.2.0-gcc-12.3.0-netlib-lapack-openmpi
   netlib-scalapack/2.2.0-gcc-12.3.0-openblas-mpich
   netlib-scalapack/2.2.0-gcc-12.3.0-openblas-openmpi	   (D)
   nghttp2/1.57.0-gcc-12.3.0
   ninja/1.11.1-gcc-12.3.0
   numactl/2.0.14-gcc-12.3.0
   openblas/0.3.24-gcc-12.3.0
   openmpi/4.1.6-gcc-12.3.0
   openssh/9.5p1-gcc-12.3.0
   openssl/3.1.3-gcc-12.3.0
   perl/5.38.0-gcc-12.3.0
   pigz/2.7-gcc-12.3.0
   pkgconf/1.9.5-gcc-12.3.0
   pmix/5.0.1-gcc-12.3.0
   py-beniget/0.4.1-gcc-12.3.0
   py-cython/0.29.36-gcc-12.3.0
   py-flit-core/3.9.0-gcc-12.3.0
   py-gast/0.5.3-gcc-12.3.0
   py-meson-python/0.13.1-gcc-12.3.0
   py-numpy/1.26.1-gcc-12.3.0-openblas
   py-packaging/23.1-gcc-12.3.0
   py-pip/23.1.2-gcc-12.3.0
   py-ply/3.11-gcc-12.3.0
   py-pybind11/2.11.0-gcc-12.3.0
   py-pyproject-metadata/0.7.1-gcc-12.3.0
   py-pythran/0.12.2-gcc-12.3.0-openblas
   py-scipy/1.11.3-gcc-12.3.0-openblas
   py-setuptools/68.0.0-gcc-12.3.0
   py-wheel/0.41.2-gcc-12.3.0
   python/3.11.6-gcc-12.3.0
   re2c/2.2-gcc-12.3.0
   readline/8.2-gcc-12.3.0
   sqlite/3.43.2-gcc-12.3.0
   tar/1.34-gcc-12.3.0
   util-linux-uuid/2.38.1-gcc-12.3.0
   util-macros/1.19.3-gcc-12.3.0
   xz/5.4.1-gcc-12.3.0
   yaksa/0.3-gcc-12.3.0
   zlib-ng/2.1.4-gcc-12.3.0
   zstd/1.5.5-gcc-12.3.0

  Where:
   D:  Default Module

If the avail list is too long consider trying:

"module --default avail" or "ml -d av" to just list the default modules.
"module overview" or "ml ov" to display the number of modules for each name.

Use "module spider" to find all possible modules and extensions.
Use "module keyword key1 key2 ..." to search for all possible modules matching any of the
"keys".

This layout is quite simple to deploy, but you can see from the above snippet that nothing prevents users from loading incompatible sets of modules:

$ module purge
$ module load netlib-lapack openblas
$ module list

Currently Loaded Modules:
  1) netlib-lapack/3.11.0-gcc-12.3.0   2) openblas/0.3.24-gcc-12.3.0

Even if conflicts directives are carefully placed in module files, they:

  • won’t enforce a consistent environment, but will just report an error

  • need constant updates, for instance as soon as a new compiler or MPI library is installed

Hierarchical module files try to overcome these shortcomings by showing at start-up only a restricted view of what is available on the system: more specifically only the software that has been installed with OS provided compilers. Among this software there will be other - usually more recent - compilers that, once loaded, will prepend new directories to MODULEPATH unlocking all the software that was compiled with them. This “unlocking” idea can then be extended arbitrarily to virtual dependencies, as we’ll see in the following section.

Core/Compiler/MPI

The most widely used hierarchy is the so called Core/Compiler/MPI where, on top of the compilers, different MPI libraries also unlock software linked to them. There are just a few steps needed to adapt the modules.yaml file we used previously:

  1. enable the lmod file generator

  2. change the tcl tag to lmod

  3. remove the tcl specific conflict directive

  4. declare which compilers are considered core_compilers

  5. remove the mpi related suffixes in projections (as they will be substituted by hierarchies)

After these modifications your configuration file should look like:

modules:
  default:
    enable::
      - lmod
    lmod:
      core_compilers:
      - 'gcc@11'
      hierarchy:
      - mpi
      hash_length: 0
      include:
      - gcc
      exclude:
      - '%gcc@11'
      all:
        filter:
          exclude_env_vars:
          - "C_INCLUDE_PATH"
          - "CPLUS_INCLUDE_PATH"
          - "LIBRARY_PATH"
        environment:
          set:
            '{name}_ROOT': '{prefix}'
      openmpi:
        environment:
          set:
            SLURM_MPI_TYPE: pmi2
            OMPI_MCA_btl_openib_warn_default_gid_prefix: '0'
      projections:
        all:          '{name}/{version}'
        ^lapack:      '{name}/{version}-{^lapack.name}'

Note

Double colon in configuration files

The double colon after enable is intentional and it serves the purpose of overriding the default list of enabled generators so that only lmod will be active (see Overriding entire sections for more details).

The directive core_compilers accepts a list of compilers. Everything built using these compilers will create a module in the Core part of the hierarchy, which is the entry point for hierarchical module files. It is common practice to put the OS provided compilers in the list and only build common utilities and other compilers with them.

If we now regenerate the module files:

$ spack module lmod refresh --delete-tree -y
==> Regenerating lmod module files

and update MODULEPATH to point to the Core:

$ module purge
$ module unuse $HOME/spack/share/spack/modules/linux-ubuntu18.04-x86_64
$ module use $HOME/spack/share/spack/lmod/linux-ubuntu18.04-x86_64/Core

asking for the available modules will return:

$ module avail

-------------- /home/spack/spack/share/spack/lmod/linux-ubuntu22.04-x86_64/Core --------------
   autoconf-archive/2023.02.20		 gmake/4.4.1		    nghttp2/1.57.0
   autoconf/2.69			 gmp/6.2.1		    openssl/3.1.3
   automake/1.16.5			 libiconv/1.17		    perl/5.38.0
   bc/1.07.1				 libsigsegv/2.14	    pigz/2.7
   berkeley-db/18.1.40			 libtool/2.4.7		    pkgconf/1.9.5
   bzip2/1.0.8				 libxml2/2.10.3		    readline/8.2
   ca-certificates-mozilla/2023-05-30	 lmod/8.7.18		    tar/1.34
   curl/8.4.0				 lua-luafilesystem/1.8.0    tcl/8.6.12
   diffutils/3.9			 lua-luaposix/36.1	    texinfo/7.0.3
   ed/1.4				 lua/5.4.4		    unzip/6.0
   gawk/5.2.2				 m4/1.4.19		    xz/5.4.1
   gcc/12.3.0				 mpc/1.3.1		    zlib-ng/2.1.4
   gdbm/1.23				 mpfr/4.2.0		    zstd/1.5.5
   gettext/0.22.3			 ncurses/6.4

If the avail list is too long consider trying:

"module --default avail" or "ml -d av" to just list the default modules.
"module overview" or "ml ov" to display the number of modules for each name.

Use "module spider" to find all possible modules and extensions.
Use "module keyword key1 key2 ..." to search for all possible modules matching any of the
"keys".

Unsurprisingly, the only visible module is gcc. Loading that we’ll unlock the Compiler part of the hierarchy:

$ module load gcc
$ module avail

----------- /home/spack/spack/share/spack/lmod/linux-ubuntu22.04-x86_64/gcc/12.3.0 -----------
   amdblis/4.1				     numactl/2.0.14
   autoconf/2.69		      (D)    openblas/0.3.24
   automake/1.16.5		      (D)    openmpi/4.1.6
   berkeley-db/18.1.40		      (D)    openssh/9.5p1
   bison/3.8.2				     openssl/3.1.3		 (D)
   bzip2/1.0.8			      (D)    perl/5.38.0		 (D)
   ca-certificates-mozilla/2023-05-30 (D)    pigz/2.7			 (D)
   cmake/3.27.7				     pkgconf/1.9.5		 (D)
   curl/8.4.0			      (D)    pmix/5.0.1
   diffutils/3.9		      (D)    py-beniget/0.4.1
   expat/2.5.0				     py-cython/0.29.36
   findutils/4.9.0			     py-flit-core/3.9.0
   gdbm/1.23			      (D)    py-gast/0.5.3
   gettext/0.22.3		      (D)    py-meson-python/0.13.1
   gmake/4.4.1			      (D)    py-numpy/1.26.1-openblas
   hwloc/2.9.1				     py-packaging/23.1
   krb5/1.20.1				     py-pip/23.1.2
   libbsd/0.11.7			     py-ply/3.11
   libedit/3.1-20210216			     py-pybind11/2.11.0
   libevent/2.1.12			     py-pyproject-metadata/0.7.1
   libfabric/1.19.0			     py-pythran/0.12.2-openblas
   libffi/3.4.4				     py-scipy/1.11.3-openblas
   libiconv/1.17		      (D)    py-setuptools/68.0.0
   libmd/1.0.4				     py-wheel/0.41.2
   libpciaccess/0.17			     python/3.11.6
   libsigsegv/2.14		      (D)    re2c/2.2
   libtool/2.4.7		      (D)    readline/8.2		 (D)
   libxcrypt/4.4.35			     sqlite/3.43.2
   libxml2/2.10.3		      (D)    tar/1.34			 (D)
   m4/1.4.19			      (D)    util-linux-uuid/2.38.1
   meson/1.2.2				     util-macros/1.19.3
   mpich/4.1.2				     xz/5.4.1			 (D)
   ncurses/6.4			      (D)    yaksa/0.3
   netlib-lapack/3.11.0			     zlib-ng/2.1.4		 (L,D)
   nghttp2/1.57.0		      (D)    zstd/1.5.5			 (L,D)
   ninja/1.11.1

-------------- /home/spack/spack/share/spack/lmod/linux-ubuntu22.04-x86_64/Core --------------
   autoconf-archive/2023.02.20		     gmake/4.4.1		    nghttp2/1.57.0
   autoconf/2.69			     gmp/6.2.1		     (L)    openssl/3.1.3
   automake/1.16.5			     libiconv/1.17		    perl/5.38.0
   bc/1.07.1				     libsigsegv/2.14		    pigz/2.7
   berkeley-db/18.1.40			     libtool/2.4.7		    pkgconf/1.9.5
   bzip2/1.0.8				     libxml2/2.10.3		    readline/8.2
   ca-certificates-mozilla/2023-05-30	     lmod/8.7.18		    tar/1.34
   curl/8.4.0				     lua-luafilesystem/1.8.0	    tcl/8.6.12
   diffutils/3.9			     lua-luaposix/36.1		    texinfo/7.0.3
   ed/1.4				     lua/5.4.4			    unzip/6.0
   gawk/5.2.2				     m4/1.4.19			    xz/5.4.1
   gcc/12.3.0			      (L)    mpc/1.3.1		     (L)    zlib-ng/2.1.4
   gdbm/1.23				     mpfr/4.2.0		     (L)    zstd/1.5.5
   gettext/0.22.3			     ncurses/6.4

  Where:
   D:  Default Module
   L:  Module is loaded

If the avail list is too long consider trying:

"module --default avail" or "ml -d av" to just list the default modules.
"module overview" or "ml ov" to display the number of modules for each name.

Use "module spider" to find all possible modules and extensions.
Use "module keyword key1 key2 ..." to search for all possible modules matching any of the
"keys".

The same holds true also for the MPI part, that you can enable by loading either mpich or openmpi. Let’s start by loading mpich:

$ module load mpich
$ module avail

---- /home/spack/spack/share/spack/lmod/linux-ubuntu22.04-x86_64/mpich/4.1.2-cxezwh7/gcc/12.3.0 ----
   netlib-scalapack/2.2.0-netlib-lapack	   netlib-scalapack/2.2.0-openblas (D)

----------- /home/spack/spack/share/spack/lmod/linux-ubuntu22.04-x86_64/gcc/12.3.0 -----------
   amdblis/4.1				       numactl/2.0.14
   autoconf/2.69		      (D)      openblas/0.3.24
   automake/1.16.5		      (D)      openmpi/4.1.6
   berkeley-db/18.1.40		      (D)      openssh/9.5p1
   bison/3.8.2				       openssl/3.1.3		   (D)
   bzip2/1.0.8			      (D)      perl/5.38.0		   (D)
   ca-certificates-mozilla/2023-05-30 (D)      pigz/2.7			   (D)
   cmake/3.27.7				       pkgconf/1.9.5		   (D)
   curl/8.4.0			      (D)      pmix/5.0.1
   diffutils/3.9		      (D)      py-beniget/0.4.1
   expat/2.5.0				       py-cython/0.29.36
   findutils/4.9.0			       py-flit-core/3.9.0
   gdbm/1.23			      (D)      py-gast/0.5.3
   gettext/0.22.3		      (D)      py-meson-python/0.13.1
   gmake/4.4.1			      (D)      py-numpy/1.26.1-openblas
   hwloc/2.9.1			      (L)      py-packaging/23.1
   krb5/1.20.1				       py-pip/23.1.2
   libbsd/0.11.7			       py-ply/3.11
   libedit/3.1-20210216			       py-pybind11/2.11.0
   libevent/2.1.12			       py-pyproject-metadata/0.7.1
   libfabric/1.19.0		      (L)      py-pythran/0.12.2-openblas
   libffi/3.4.4				       py-scipy/1.11.3-openblas
   libiconv/1.17		      (L,D)    py-setuptools/68.0.0
   libmd/1.0.4				       py-wheel/0.41.2
   libpciaccess/0.17		      (L)      python/3.11.6
   libsigsegv/2.14		      (D)      re2c/2.2
   libtool/2.4.7		      (D)      readline/8.2		   (D)
   libxcrypt/4.4.35			       sqlite/3.43.2
   libxml2/2.10.3		      (L,D)    tar/1.34			   (D)
   m4/1.4.19			      (D)      util-linux-uuid/2.38.1
   meson/1.2.2				       util-macros/1.19.3
   mpich/4.1.2			      (L)      xz/5.4.1			   (L,D)
   ncurses/6.4			      (L,D)    yaksa/0.3		   (L)
   netlib-lapack/3.11.0			       zlib-ng/2.1.4		   (L,D)
   nghttp2/1.57.0		      (D)      zstd/1.5.5		   (L,D)
   ninja/1.11.1

-------------- /home/spack/spack/share/spack/lmod/linux-ubuntu22.04-x86_64/Core --------------
   autoconf-archive/2023.02.20		     gmake/4.4.1		    nghttp2/1.57.0
   autoconf/2.69			     gmp/6.2.1		     (L)    openssl/3.1.3
   automake/1.16.5			     libiconv/1.17		    perl/5.38.0
   bc/1.07.1				     libsigsegv/2.14		    pigz/2.7
   berkeley-db/18.1.40			     libtool/2.4.7		    pkgconf/1.9.5
   bzip2/1.0.8				     libxml2/2.10.3		    readline/8.2
   ca-certificates-mozilla/2023-05-30	     lmod/8.7.18		    tar/1.34
   curl/8.4.0				     lua-luafilesystem/1.8.0	    tcl/8.6.12
   diffutils/3.9			     lua-luaposix/36.1		    texinfo/7.0.3
   ed/1.4				     lua/5.4.4			    unzip/6.0
   gawk/5.2.2				     m4/1.4.19			    xz/5.4.1
   gcc/12.3.0			      (L)    mpc/1.3.1		     (L)    zlib-ng/2.1.4
   gdbm/1.23				     mpfr/4.2.0		     (L)    zstd/1.5.5
   gettext/0.22.3			     ncurses/6.4

  Where:
   D:  Default Module
   L:  Module is loaded

If the avail list is too long consider trying:

"module --default avail" or "ml -d av" to just list the default modules.
"module overview" or "ml ov" to display the number of modules for each name.

Use "module spider" to find all possible modules and extensions.
Use "module keyword key1 key2 ..." to search for all possible modules matching any of the
"keys".
$ module load openblas netlib-scalapack/2.2.0-openblas
$ module list

Currently Loaded Modules:
  1) gmp/6.2.1	     7) libpciaccess/0.17  13) libfabric/1.19.0
  2) mpfr/4.2.0	     8) libiconv/1.17	   14) yaksa/0.3
  3) mpc/1.3.1	     9) xz/5.4.1	   15) mpich/4.1.2
  4) zlib-ng/2.1.4  10) libxml2/2.10.3	   16) openblas/0.3.24
  5) zstd/1.5.5	    11) ncurses/6.4	   17) netlib-scalapack/2.2.0-openblas
  6) gcc/12.3.0	    12) hwloc/2.9.1

At this point we can showcase the improved consistency that a hierarchical layout provides over a non-hierarchical one:

$ export LMOD_AUTO_SWAP=yes
$ module load openmpi

Lmod is automatically replacing "mpich/4.1.2" with "openmpi/4.1.6".


Due to MODULEPATH changes, the following have been reloaded:
  1) netlib-scalapack/2.2.0-openblas

Lmod took care of swapping the MPI provider for us, and it also substituted the netlib-scalapack module to conform to the change in the MPI. In this way we can’t accidentally pull-in two different MPI providers at the same time or load a module file for a package linked to openmpi when mpich is also loaded. Consistency for compilers and MPI is ensured by the tool.

Add LAPACK to the hierarchy

The hierarchy just shown is already a great improvement over non-hierarchical layouts, but it still has an asymmetry: LAPACK providers cover the same semantic role as MPI providers, but yet they are not part of the hierarchy.

To be more practical, this means that although we have gained an improved consistency in our environment when it comes to MPI, we still have the same problems as we had before for LAPACK implementations:

$ module list

Currently Loaded Modules:
  1) gmp/6.2.1		10) xz/5.4.1	    19) openssl/3.1.3
  2) mpfr/4.2.0		11) libxml2/2.10.3  20) krb5/1.20.1
  3) mpc/1.3.1		12) ncurses/6.4	    21) libedit/3.1-20210216
  4) zlib-ng/2.1.4	13) hwloc/2.9.1	    22) libxcrypt/4.4.35
  5) zstd/1.5.5		14) numactl/2.0.14  23) openssh/9.5p1
  6) gcc/12.3.0		15) bzip2/1.0.8	    24) libevent/2.1.12
  7) openblas/0.3.24	16) pigz/2.7	    25) pmix/5.0.1
  8) libpciaccess/0.17	17) tar/1.34	    26) openmpi/4.1.6
  9) libiconv/1.17	18) gettext/0.22.3  27) netlib-scalapack/2.2.0-openblas



$ module avail

---- /home/spack/spack/share/spack/lmod/linux-ubuntu22.04-x86_64/openmpi/4.1.6-cndwedm/gcc/12.3.0 ----
   netlib-scalapack/2.2.0-netlib-lapack	   netlib-scalapack/2.2.0-openblas (L,D)

----------- /home/spack/spack/share/spack/lmod/linux-ubuntu22.04-x86_64/gcc/12.3.0 -----------
   amdblis/4.1				       numactl/2.0.14		   (L)
   autoconf/2.69		      (D)      openblas/0.3.24		   (L)
   automake/1.16.5		      (D)      openmpi/4.1.6		   (L)
   berkeley-db/18.1.40		      (D)      openssh/9.5p1		   (L)
   bison/3.8.2				       openssl/3.1.3		   (L,D)
   bzip2/1.0.8			      (L,D)    perl/5.38.0		   (D)
   ca-certificates-mozilla/2023-05-30 (D)      pigz/2.7			   (L,D)
   cmake/3.27.7				       pkgconf/1.9.5		   (D)
   curl/8.4.0			      (D)      pmix/5.0.1		   (L)
   diffutils/3.9		      (D)      py-beniget/0.4.1
   expat/2.5.0				       py-cython/0.29.36
   findutils/4.9.0			       py-flit-core/3.9.0
   gdbm/1.23			      (D)      py-gast/0.5.3
   gettext/0.22.3		      (L,D)    py-meson-python/0.13.1
   gmake/4.4.1			      (D)      py-numpy/1.26.1-openblas
   hwloc/2.9.1			      (L)      py-packaging/23.1
   krb5/1.20.1			      (L)      py-pip/23.1.2
   libbsd/0.11.7			       py-ply/3.11
   libedit/3.1-20210216		      (L)      py-pybind11/2.11.0
   libevent/2.1.12		      (L)      py-pyproject-metadata/0.7.1
   libfabric/1.19.0			       py-pythran/0.12.2-openblas
   libffi/3.4.4				       py-scipy/1.11.3-openblas
   libiconv/1.17		      (L,D)    py-setuptools/68.0.0
   libmd/1.0.4				       py-wheel/0.41.2
   libpciaccess/0.17		      (L)      python/3.11.6
   libsigsegv/2.14		      (D)      re2c/2.2
   libtool/2.4.7		      (D)      readline/8.2		   (D)
   libxcrypt/4.4.35		      (L)      sqlite/3.43.2
   libxml2/2.10.3		      (L,D)    tar/1.34			   (L,D)
   m4/1.4.19			      (D)      util-linux-uuid/2.38.1
   meson/1.2.2				       util-macros/1.19.3
   mpich/4.1.2				       xz/5.4.1			   (L,D)
   ncurses/6.4			      (L,D)    yaksa/0.3
   netlib-lapack/3.11.0			       zlib-ng/2.1.4		   (L,D)
   nghttp2/1.57.0		      (D)      zstd/1.5.5		   (L,D)
   ninja/1.11.1

-------------- /home/spack/spack/share/spack/lmod/linux-ubuntu22.04-x86_64/Core --------------
   autoconf-archive/2023.02.20		     gmake/4.4.1		    nghttp2/1.57.0
   autoconf/2.69			     gmp/6.2.1		     (L)    openssl/3.1.3
   automake/1.16.5			     libiconv/1.17		    perl/5.38.0
   bc/1.07.1				     libsigsegv/2.14		    pigz/2.7
   berkeley-db/18.1.40			     libtool/2.4.7		    pkgconf/1.9.5
   bzip2/1.0.8				     libxml2/2.10.3		    readline/8.2
   ca-certificates-mozilla/2023-05-30	     lmod/8.7.18		    tar/1.34
   curl/8.4.0				     lua-luafilesystem/1.8.0	    tcl/8.6.12
   diffutils/3.9			     lua-luaposix/36.1		    texinfo/7.0.3
   ed/1.4				     lua/5.4.4			    unzip/6.0
   gawk/5.2.2				     m4/1.4.19			    xz/5.4.1
   gcc/12.3.0			      (L)    mpc/1.3.1		     (L)    zlib-ng/2.1.4
   gdbm/1.23				     mpfr/4.2.0		     (L)    zstd/1.5.5
   gettext/0.22.3			     ncurses/6.4

  Where:
   D:  Default Module
   L:  Module is loaded

If the avail list is too long consider trying:

"module --default avail" or "ml -d av" to just list the default modules.
"module overview" or "ml ov" to display the number of modules for each name.

Use "module spider" to find all possible modules and extensions.
Use "module keyword key1 key2 ..." to search for all possible modules matching any of the
"keys".


$ module load netlib-scalapack/2.2.0-netlib-lapack

The following have been reloaded with a version change:
  1) netlib-scalapack/2.2.0-openblas => netlib-scalapack/2.2.0-netlib-lapack

$ module list

Currently Loaded Modules:
  1) gmp/6.2.1		14) numactl/2.0.14	  27) libmd/1.0.4
  2) mpfr/4.2.0		15) bzip2/1.0.8		  28) libbsd/0.11.7
  3) mpc/1.3.1		16) pigz/2.7		  29) expat/2.5.0
  4) zlib-ng/2.1.4	17) tar/1.34		  30) readline/8.2
  5) zstd/1.5.5		18) gettext/0.22.3	  31) gdbm/1.23
  6) gcc/12.3.0		19) openssl/3.1.3	  32) libffi/3.4.4
  7) openblas/0.3.24	20) krb5/1.20.1		  33) sqlite/3.43.2
  8) libpciaccess/0.17	21) libedit/3.1-20210216  34) util-linux-uuid/2.38.1
  9) libiconv/1.17	22) libxcrypt/4.4.35	  35) python/3.11.6
 10) xz/5.4.1		23) openssh/9.5p1	  36) amdblis/4.1
 11) libxml2/2.10.3	24) libevent/2.1.12	  37) netlib-lapack/3.11.0
 12) ncurses/6.4	25) pmix/5.0.1		  38) netlib-scalapack/2.2.0-netlib-lapack
 13) hwloc/2.9.1	26) openmpi/4.1.6

Hierarchies that are deeper than Core/Compiler/MPI are probably still considered “unusual” or “impractical” at many sites, mainly because module files are written manually and keeping track of the combinations among multiple providers quickly becomes quite involved.

For instance, having both MPI and LAPACK in the hierarchy means we must classify software into one of four categories:

  1. Software that doesn’t depend on MPI or LAPACK

  2. Software that depends only on MPI

  3. Software that depends only on LAPACK

  4. Software that depends on both

to decide when to show it to the user. The situation becomes more involved as the number of virtual dependencies in the hierarchy increases.

We can take advantage of the DAG that Spack maintains for the installed software and solve this combinatorial problem in a clean and automated way. In some sense Spack’s ability to manage this combinatorial complexity makes deeper hierarchies feasible.

Coming back to our example, let’s add lapack to the hierarchy and remove the remaining suffix projection for lapack:

modules:
  default:
    enable::
    - lmod
    lmod:
      core_compilers:
      - 'gcc@11'
      hierarchy:
      - mpi
      - lapack
      hash_length: 0
      include:
      - gcc
      exclude:
      - '%gcc@11'
      all:
        filter:
          exclude_env_vars:
          - "C_INCLUDE_PATH"
          - "CPLUS_INCLUDE_PATH"
          - "LIBRARY_PATH"
        environment:
          set:
            '{name}_ROOT': '{prefix}'
      openmpi:
        environment:
          set:
            SLURM_MPI_TYPE: pmi2
            OMPI_MCA_btl_openib_warn_default_gid_prefix: '0'
      projections:
        all:          '{name}/{version}'

After module files have been regenerated as usual:

$ module purge
$ spack module lmod refresh --delete-tree -y
==> Regenerating lmod module files

we can see that now we have additional components in the hierarchy:

$ module load gcc

Lmod is automatically replacing "openblas/0.3.24" with "netlib-lapack/3.11.0".


Inactive Modules:
  1) netlib-scalapack/2.2.0-netlib-lapack     2) openblas

$ module load openblas

Lmod is automatically replacing "netlib-lapack/3.11.0" with "openblas/0.3.24".


Activating Modules:
  1) openblas/0.3.24

$ module avail

---- /home/spack/spack/share/spack/lmod/linux-ubuntu22.04-x86_64/openmpi/4.1.6-cndwedm/openblas/0.3.24-qoggfi2/gcc/12.3.0 ----
   netlib-scalapack/2.2.0

---- /home/spack/spack/share/spack/lmod/linux-ubuntu22.04-x86_64/openblas/0.3.24-qoggfi2/gcc/12.3.0 ----
   py-numpy/1.26.1    py-pythran/0.12.2	   py-scipy/1.11.3

----------- /home/spack/spack/share/spack/lmod/linux-ubuntu22.04-x86_64/gcc/12.3.0 -----------
   amdblis/4.1			      (L)      nghttp2/1.57.0		   (D)
   autoconf/2.69		      (D)      ninja/1.11.1
   automake/1.16.5		      (D)      numactl/2.0.14		   (L)
   berkeley-db/18.1.40		      (D)      openblas/0.3.24		   (L)
   bison/3.8.2				       openmpi/4.1.6		   (L)
   bzip2/1.0.8			      (L,D)    openssh/9.5p1		   (L)
   ca-certificates-mozilla/2023-05-30 (D)      openssl/3.1.3		   (L,D)
   cmake/3.27.7				       perl/5.38.0		   (D)
   curl/8.4.0			      (D)      pigz/2.7			   (L,D)
   diffutils/3.9		      (D)      pkgconf/1.9.5		   (D)
   expat/2.5.0			      (L)      pmix/5.0.1		   (L)
   findutils/4.9.0			       py-beniget/0.4.1
   gdbm/1.23			      (L,D)    py-cython/0.29.36
   gettext/0.22.3		      (L,D)    py-flit-core/3.9.0
   gmake/4.4.1			      (D)      py-gast/0.5.3
   hwloc/2.9.1			      (L)      py-meson-python/0.13.1
   krb5/1.20.1			      (L)      py-packaging/23.1
   libbsd/0.11.7		      (L)      py-pip/23.1.2
   libedit/3.1-20210216		      (L)      py-ply/3.11
   libevent/2.1.12		      (L)      py-pybind11/2.11.0
   libfabric/1.19.0			       py-pyproject-metadata/0.7.1
   libffi/3.4.4			      (L)      py-setuptools/68.0.0
   libiconv/1.17		      (L,D)    py-wheel/0.41.2
   libmd/1.0.4			      (L)      python/3.11.6		   (L)
   libpciaccess/0.17		      (L)      re2c/2.2
   libsigsegv/2.14		      (D)      readline/8.2		   (L,D)
   libtool/2.4.7		      (D)      sqlite/3.43.2		   (L)
   libxcrypt/4.4.35		      (L)      tar/1.34			   (L,D)
   libxml2/2.10.3		      (L,D)    util-linux-uuid/2.38.1	   (L)
   m4/1.4.19			      (D)      util-macros/1.19.3
   meson/1.2.2				       xz/5.4.1			   (L,D)
   mpich/4.1.2				       yaksa/0.3
   ncurses/6.4			      (L,D)    zlib-ng/2.1.4		   (L,D)
   netlib-lapack/3.11.0			       zstd/1.5.5		   (L,D)

-------------- /home/spack/spack/share/spack/lmod/linux-ubuntu22.04-x86_64/Core --------------
   autoconf-archive/2023.02.20		     gmake/4.4.1		    nghttp2/1.57.0
   autoconf/2.69			     gmp/6.2.1		     (L)    openssl/3.1.3
   automake/1.16.5			     libiconv/1.17		    perl/5.38.0
   bc/1.07.1				     libsigsegv/2.14		    pigz/2.7
   berkeley-db/18.1.40			     libtool/2.4.7		    pkgconf/1.9.5
   bzip2/1.0.8				     libxml2/2.10.3		    readline/8.2
   ca-certificates-mozilla/2023-05-30	     lmod/8.7.18		    tar/1.34
   curl/8.4.0				     lua-luafilesystem/1.8.0	    tcl/8.6.12
   diffutils/3.9			     lua-luaposix/36.1		    texinfo/7.0.3
   ed/1.4				     lua/5.4.4			    unzip/6.0
   gawk/5.2.2				     m4/1.4.19			    xz/5.4.1
   gcc/12.3.0			      (L)    mpc/1.3.1		     (L)    zlib-ng/2.1.4
   gdbm/1.23				     mpfr/4.2.0		     (L)    zstd/1.5.5
   gettext/0.22.3			     ncurses/6.4

  Where:
   D:  Default Module
   L:  Module is loaded

If the avail list is too long consider trying:

"module --default avail" or "ml -d av" to just list the default modules.
"module overview" or "ml ov" to display the number of modules for each name.

Use "module spider" to find all possible modules and extensions.
Use "module keyword key1 key2 ..." to search for all possible modules matching any of the
"keys".


$ module load openmpi
$ module avail

---- /home/spack/spack/share/spack/lmod/linux-ubuntu22.04-x86_64/openmpi/4.1.6-cndwedm/openblas/0.3.24-qoggfi2/gcc/12.3.0 ----
   netlib-scalapack/2.2.0

---- /home/spack/spack/share/spack/lmod/linux-ubuntu22.04-x86_64/openblas/0.3.24-qoggfi2/gcc/12.3.0 ----
   py-numpy/1.26.1    py-pythran/0.12.2	   py-scipy/1.11.3

----------- /home/spack/spack/share/spack/lmod/linux-ubuntu22.04-x86_64/gcc/12.3.0 -----------
   amdblis/4.1			      (L)      nghttp2/1.57.0		   (D)
   autoconf/2.69		      (D)      ninja/1.11.1
   automake/1.16.5		      (D)      numactl/2.0.14		   (L)
   berkeley-db/18.1.40		      (D)      openblas/0.3.24		   (L)
   bison/3.8.2				       openmpi/4.1.6		   (L)
   bzip2/1.0.8			      (L,D)    openssh/9.5p1		   (L)
   ca-certificates-mozilla/2023-05-30 (D)      openssl/3.1.3		   (L,D)
   cmake/3.27.7				       perl/5.38.0		   (D)
   curl/8.4.0			      (D)      pigz/2.7			   (L,D)
   diffutils/3.9		      (D)      pkgconf/1.9.5		   (D)
   expat/2.5.0			      (L)      pmix/5.0.1		   (L)
   findutils/4.9.0			       py-beniget/0.4.1
   gdbm/1.23			      (L,D)    py-cython/0.29.36
   gettext/0.22.3		      (L,D)    py-flit-core/3.9.0
   gmake/4.4.1			      (D)      py-gast/0.5.3
   hwloc/2.9.1			      (L)      py-meson-python/0.13.1
   krb5/1.20.1			      (L)      py-packaging/23.1
   libbsd/0.11.7		      (L)      py-pip/23.1.2
   libedit/3.1-20210216		      (L)      py-ply/3.11
   libevent/2.1.12		      (L)      py-pybind11/2.11.0
   libfabric/1.19.0			       py-pyproject-metadata/0.7.1
   libffi/3.4.4			      (L)      py-setuptools/68.0.0
   libiconv/1.17		      (L,D)    py-wheel/0.41.2
   libmd/1.0.4			      (L)      python/3.11.6		   (L)
   libpciaccess/0.17		      (L)      re2c/2.2
   libsigsegv/2.14		      (D)      readline/8.2		   (L,D)
   libtool/2.4.7		      (D)      sqlite/3.43.2		   (L)
   libxcrypt/4.4.35		      (L)      tar/1.34			   (L,D)
   libxml2/2.10.3		      (L,D)    util-linux-uuid/2.38.1	   (L)
   m4/1.4.19			      (D)      util-macros/1.19.3
   meson/1.2.2				       xz/5.4.1			   (L,D)
   mpich/4.1.2				       yaksa/0.3
   ncurses/6.4			      (L,D)    zlib-ng/2.1.4		   (L,D)
   netlib-lapack/3.11.0			       zstd/1.5.5		   (L,D)

-------------- /home/spack/spack/share/spack/lmod/linux-ubuntu22.04-x86_64/Core --------------
   autoconf-archive/2023.02.20		     gmake/4.4.1		    nghttp2/1.57.0
   autoconf/2.69			     gmp/6.2.1		     (L)    openssl/3.1.3
   automake/1.16.5			     libiconv/1.17		    perl/5.38.0
   bc/1.07.1				     libsigsegv/2.14		    pigz/2.7
   berkeley-db/18.1.40			     libtool/2.4.7		    pkgconf/1.9.5
   bzip2/1.0.8				     libxml2/2.10.3		    readline/8.2
   ca-certificates-mozilla/2023-05-30	     lmod/8.7.18		    tar/1.34
   curl/8.4.0				     lua-luafilesystem/1.8.0	    tcl/8.6.12
   diffutils/3.9			     lua-luaposix/36.1		    texinfo/7.0.3
   ed/1.4				     lua/5.4.4			    unzip/6.0
   gawk/5.2.2				     m4/1.4.19			    xz/5.4.1
   gcc/12.3.0			      (L)    mpc/1.3.1		     (L)    zlib-ng/2.1.4
   gdbm/1.23				     mpfr/4.2.0		     (L)    zstd/1.5.5
   gettext/0.22.3			     ncurses/6.4

  Where:
   D:  Default Module
   L:  Module is loaded

If the avail list is too long consider trying:

"module --default avail" or "ml -d av" to just list the default modules.
"module overview" or "ml ov" to display the number of modules for each name.

Use "module spider" to find all possible modules and extensions.
Use "module keyword key1 key2 ..." to search for all possible modules matching any of the
"keys".

Both MPI and LAPACK providers will now benefit from the same safety features:

$ module load py-numpy netlib-scalapack

Activating Modules:
  1) netlib-scalapack/2.2.0

$ module load mpich

Lmod is automatically replacing "openmpi/4.1.6" with "mpich/4.1.2".


Due to MODULEPATH changes, the following have been reloaded:
  1) netlib-scalapack/2.2.0

$ module load netlib-lapack

Lmod is automatically replacing "openblas/0.3.24" with "netlib-lapack/3.11.0".


Inactive Modules:
  1) py-numpy

Due to MODULEPATH changes, the following have been reloaded:
  1) netlib-scalapack/2.2.0

Because we only compiled py-numpy with openblas the module is made inactive when we switch the LAPACK provider. The user environment is now consistent by design!

Working with Templates

As briefly mentioned in the introduction, Spack uses Jinja2 to generate each individual module file. This means that you have all of its flexibility and power when it comes to customizing what gets generated!

Module file templates

The templates that Spack uses to generate module files are stored in the share/spack/templates/module directory within the Spack prefix, and they all share the same common structure. Usually, they start with a header that identifies the type of module being generated. In the case of hierarchical module files it’s:

-- -*- lua -*-
-- Module file created by spack (https://github.com/spack/spack) on {{ timestamp }}
--
-- {{ spec.short_spec }}
--

The statements within double curly brackets {{ ... }} denote expressions that will be evaluated and substituted at module generation time. The rest of the file is then divided into blocks that can be overridden or extended by users, if need be. Control structures , delimited by {% ... %}, are also permitted in the template language:

{% block conflict %}
{% for name in conflicts %}
conflict("{{ name }}")
{% endfor %}
{% endblock %}

{% block environment %}
{% for command_name, cmd in environment_modifications %}
{% if command_name == 'PrependPath' %}
prepend_path("{{ cmd.name }}", "{{ cmd.value }}", "{{ cmd.separator }}")
{% elif command_name in ('AppendPath', 'AppendFlagsEnv') %}
append_path("{{ cmd.name }}", "{{ cmd.value }}", "{{ cmd.separator }}")
{% elif command_name in ('RemovePath', 'RemoveFlagsEnv') %}
remove_path("{{ cmd.name }}", "{{ cmd.value }}", "{{ cmd.separator }}")
{% elif command_name == 'SetEnv' %}

The locations where Spack looks for templates are specified in config.yaml:

  # Locations where templates should be found
  template_dirs:
    - $spack/share/spack/templates

and can be extended by users to employ custom templates, as we’ll see next.

Extend the default templates

Let’s assume one of our software is protected by group membership: allowed users belong to the same linux group, and access is granted at group level. Wouldn’t it be nice if people that are not yet entitled to use it could receive a helpful message at module load time that tells them who to contact in your organization to be inserted in the group?

To automate the generation of module files with such site-specific behavior we’ll start by extending the list of locations where Spack looks for module files. Let’s create the file ${SPACK_ROOT}/etc/spack/config.yaml with the content:

config:
  template_dirs:
    - $HOME/.spack/templates

This tells Spack to also search another location when looking for template files. Next, we need to create our custom template extension in the folder listed above:

{% extends "modules/modulefile.lua" %}
{% block footer %}
-- Access is granted only to specific groups
if not isDir("{{ spec.prefix }}") then
    LmodError (
        "You don't have the necessary rights to run \"{{ spec.name }}\".\n\n",
        "\tPlease write an e-mail to 1234@foo.com if you need further information on how to get access to it.\n"
    )
end
{% endblock %}

Let’s name this file group-restricted.lua. The line:

{% extends "modules/modulefile.lua" %}

tells Jinja2 that we are reusing the standard template for hierarchical module files. The section:

{% block footer %}
-- Access is granted only to specific groups
if not isDir("{{ spec.prefix }}") then
    LmodError (
        "You don't have the necessary rights to run \"{{ spec.name }}\".\n\n",
        "\tPlease write an e-mail to 1234@foo.com if you need further information on how to get access to it.\n"
    )
end
{% endblock %}

overrides the footer block. Finally, we need to add a couple of lines in modules.yaml to tell Spack which specs need to use the new custom template. For the sake of illustration let’s assume it’s netlib-scalapack:

modules:
  enable::
    - lmod
  lmod:
    core_compilers:
      - 'gcc@11'
    hierarchy:
      - mpi
      - lapack
    hash_length: 0
    include:
      - gcc
    exclude:
      - '%gcc@11'
      - readline
    all:
      filter:
        exclude_env_vars:
          - "C_INCLUDE_PATH"
          - "CPLUS_INCLUDE_PATH"
          - "LIBRARY_PATH"
      environment:
        set:
          '{name}_ROOT': '{prefix}'
    openmpi:
      environment:
        set:
          SLURM_MPI_TYPE: pmi2
          OMPI_MCA_btl_openib_warn_default_gid_prefix: '0'
    netlib-scalapack:
      template: 'group-restricted.lua'

If we regenerate the module files one last time:

$ spack module lmod refresh -y netlib-scalapack
==> Regenerating lmod module files

we’ll find the following at the end of each netlib-scalapack module file:

-- Access is granted only to specific groups
if not isDir("/home/spack/spack/opt/spack/linux-ubuntu18.04-x86_64/gcc-12.1.0/netlib-scalapack-2.0.2-2p75lzqjbsnev7d2j2osgpkz7ib33oca") then
    LmodError (
        "You don't have the necessary rights to run \"netlib-scalapack\".\n\n",
        "\tPlease write an e-mail to 1234@foo.com if you need further information on how to get access to it.\n"
    )
end

and every user that doesn’t have access to the software will now be redirected to the right e-mail address where to ask for it!

Restore settings for future sections

For future sections of the tutorial, we will not use the gcc@12.3.0 compiler. Since it is currently the default compiler (our current default is the most recent version of gcc available), we will remove it now.

$ spack compiler rm gcc@12.3.0

This will ensure the rest of the tutorial goes smoothly for you.