|
| 1 | +# Configuring site-specific Lmod hooks |
| 2 | +You may want to customize what happens when certain modules are loaded, for example, you may want to set additional environment variables. This is possible with [LMOD hooks](https://lmod.readthedocs.io/en/latest/170_hooks.html). A typical example would be when you want to tune the OpenMPI module for your system by setting additional environment variables when an OpenMPI module is loaded. |
| 3 | + |
| 4 | + |
| 5 | +## Location of the hooks |
| 6 | +The EESSI software stack provides its own set of hooks in `$LMOD_PACKAGE_PATH/SitePackage.lua`. This `SitePackage.lua` also searches for site-specific hooks in two additional locations: |
| 7 | + |
| 8 | +- `$EESSI_CVMFS_REPO/host_injections/$EESSI_VERSION/.lmod/SitePackage.lua` |
| 9 | +- `$EESSI_CVMFS_REPO/host_injections/$EESSI_VERSION/software/$EESSI_OS_TYPE/$EESSI_SOFTWARE_SUBDIR/.lmod/SitePackage.lua` |
| 10 | + |
| 11 | +The first allows for hooks that need to be executed for that system, irrespective of the CPU architecture. The second allows for hooks specific to a certain architecture. |
| 12 | + |
| 13 | +## Architecture-independent hooks |
| 14 | +Hooks are written in Lua and can use any of the standard Lmod functionality as described in the [Lmod documentation](https://lmod.readthedocs.io/en/latest/170_hooks.html). While there are many types of hooks, you most likely want to specify a load or unload hook. Note that the EESSI hooks provide a nice example of what you can do with hooks. Here, as an example, we will define a `load` hook that environment variable `MY_ENV_VAR` to `1` whenever an `OpenMPI` module is loaded. |
| 15 | + |
| 16 | +First, you typically want to load the necessary Lua packages: |
| 17 | +```lua |
| 18 | +-- $EESSI_CVMFS_REPO/host_injections/$EESSI_VERSION/.lmod/SitePackage.lua |
| 19 | + |
| 20 | +-- The Strict package checks for the use of undeclared variables: |
| 21 | +require("strict") |
| 22 | + |
| 23 | +-- Load the Lmod Hook package |
| 24 | +local hook=require("Hook") |
| 25 | +``` |
| 26 | + |
| 27 | +Next, we define a function that we want to use as a hook. Unfortunately, registering multiple hooks of the same type (e.g. multiple `load` hooks) is only supported in Lmod 8.7.35+. EESSI version 2023.06 uses Lmod 8.7.30. Thus, we define our function without the local keyword, so that we can still add to it later in an architecture-specific hook (if we wanted to): |
| 28 | + |
| 29 | +```lua |
| 30 | +-- Define a function for the hook |
| 31 | +-- Note that we define this without 'local' keyword |
| 32 | +-- That way we can still add to this function in an architecture-specific hook |
| 33 | +function set_my_env_var_openmpi(t) |
| 34 | + local simpleName = string.match(t.modFullName, "(.-)/") |
| 35 | + if simpleName == 'OpenMPI' then |
| 36 | + setenv('MY_ENV_VAR', '1') |
| 37 | + end |
| 38 | +end |
| 39 | +``` |
| 40 | + |
| 41 | +for the same reason that multiple hooks cannot be registered, we need to combine this function for our site-specific (architecture-independent) with the function that specifies the EESSI `load` hook. Note that all EESSI hooks will be called `eessi_<hook_type>_hook` by convention. |
| 42 | + |
| 43 | +```lua |
| 44 | +-- Registering multiple hook functions, e.g. multiple load hooks is only supported in Lmod 8.7.35+ |
| 45 | +-- EESSI version 2023.06 uses lmod 8.7.30. Thus, we first have to combine all functions into a single one, |
| 46 | +-- before registering it as a hook |
| 47 | +local function combined_load_hook(t) |
| 48 | + -- Call the EESSI load hook (if it exists) |
| 49 | + -- Note that if you wanted to overwrite the EESSI hooks (not recommended!), you would omit this |
| 50 | + if eessi_load_hook ~= nil then |
| 51 | + eessi_load_hook(t) |
| 52 | + end |
| 53 | + -- Call the site-specific load hook |
| 54 | + set_my_env_var_openmpi(t) |
| 55 | +end |
| 56 | +``` |
| 57 | + |
| 58 | +Then, we can finally register this function as an Lmod hook: |
| 59 | + |
| 60 | +```lua |
| 61 | +hook.register("load", combined_load_hook) |
| 62 | +``` |
| 63 | + |
| 64 | +Thus, our complete `$EESSI_CVMFS_REPO/host_injections/$EESSI_VERSION/.lmod/SitePackage.lua` now looks like this (omitting the comments): |
| 65 | + |
| 66 | +```lua |
| 67 | +require("strict") |
| 68 | +local hook=require("Hook") |
| 69 | + |
| 70 | +function set_my_env_var_openmpi(t) |
| 71 | + local simpleName = string.match(t.modFullName, "(.-)/") |
| 72 | + if simpleName == 'OpenMPI' then |
| 73 | + setenv('MY_ENV_VAR', '1') |
| 74 | + end |
| 75 | +end |
| 76 | + |
| 77 | +local function combined_load_hook(t) |
| 78 | + if eessi_load_hook ~= nil then |
| 79 | + eessi_load_hook(t) |
| 80 | + end |
| 81 | + set_my_env_var_openmpi(t) |
| 82 | +end |
| 83 | + |
| 84 | +hook.register("load", combined_load_hook) |
| 85 | +``` |
| 86 | + |
| 87 | +Note that for future EESSI versions, if they use Lmod 8.7.35+, this would be simplified to: |
| 88 | + |
| 89 | +```lua |
| 90 | +require("strict") |
| 91 | +local hook=require("Hook") |
| 92 | + |
| 93 | +local function set_my_env_var_openmpi(t) |
| 94 | + local simpleName = string.match(t.modFullName, "(.-)/") |
| 95 | + if simpleName == 'OpenMPI' then |
| 96 | + setenv('MY_ENV_VAR', '1') |
| 97 | + end |
| 98 | +end |
| 99 | + |
| 100 | +hook.register("load", set_my_env_var_openmpi, "append") |
| 101 | +``` |
| 102 | + |
| 103 | +## Architecture-dependent hooks |
| 104 | +Now, assume that in addition we want to set an environment variable `MY_SECOND_ENV_VAR` to `5`, but only for nodes that have the `zen3` architecture. First, again, you typically want to load the necessary Lua packages: |
| 105 | + |
| 106 | +```lua |
| 107 | +-- $EESSI_CVMFS_REPO/host_injections/$EESSI_VERSION/software/linux/x86_64/amd/zen3/.lmod/SitePackage.lua |
| 108 | + |
| 109 | +-- The Strict package checks for the use of undeclared variables: |
| 110 | +require("strict") |
| 111 | + |
| 112 | +-- Load the Lmod Hook package |
| 113 | +local hook=require("Hook") |
| 114 | +``` |
| 115 | + |
| 116 | +Next, we define the function for the hook itself |
| 117 | + |
| 118 | +```lua |
| 119 | +-- Define a function for the hook |
| 120 | +-- This time, we can define it as a local function, as there are no hooks more specific than this |
| 121 | +local function set_my_second_env_var_openmpi(t) |
| 122 | + local simpleName = string.match(t.modFullName, "(.-)/") |
| 123 | + if simpleName == 'OpenMPI' then |
| 124 | + setenv('MY_SECOND_ENV_VAR', '5') |
| 125 | + end |
| 126 | +end |
| 127 | +``` |
| 128 | + |
| 129 | +Then, we combine the functions into one |
| 130 | + |
| 131 | +```lua |
| 132 | +local function combined_load_hook(t) |
| 133 | + -- Call the EESSI load hook first |
| 134 | + if eessi_load_hook ~= nil then |
| 135 | + eessi_load_hook(t) |
| 136 | + end |
| 137 | + -- Then call the architecture-independent load hook |
| 138 | + if set_my_env_var_openmpi(t) ~= nil then |
| 139 | + set_my_env_var_openmpi(t) |
| 140 | + end |
| 141 | + -- And finally the architecture-dependent load hook we just defined |
| 142 | + set_my_second_env_var_openmpi(t) |
| 143 | +end |
| 144 | +``` |
| 145 | + |
| 146 | +before finally registering it as an Lmod hook |
| 147 | + |
| 148 | +```lua |
| 149 | +hook.register("load", combined_load_hook) |
| 150 | +``` |
| 151 | + |
| 152 | +Thus, our full `$EESSI_CVMFS_REPO/host_injections/$EESSI_VERSION/software/linux/x86_64/amd/zen3/.lmod/SitePackage.lua` now looks like this (omitting the comments): |
| 153 | + |
| 154 | +```lua |
| 155 | +require("strict") |
| 156 | +local hook=require("Hook") |
| 157 | + |
| 158 | +local function set_my_second_env_var_openmpi(t) |
| 159 | + local simpleName = string.match(t.modFullName, "(.-)/") |
| 160 | + if simpleName == 'OpenMPI' then |
| 161 | + setenv('MY_SECOND_ENV_VAR', '5') |
| 162 | + end |
| 163 | +end |
| 164 | + |
| 165 | +local function combined_load_hook(t) |
| 166 | + if eessi_load_hook ~= nil then |
| 167 | + eessi_load_hook(t) |
| 168 | + end |
| 169 | + if set_my_env_var_openmpi(t) ~= nil then |
| 170 | + set_my_env_var_openmpi(t) |
| 171 | + end |
| 172 | + set_my_second_env_var_openmpi(t) |
| 173 | +end |
| 174 | + |
| 175 | +hook.register("load", combined_load_hook) |
| 176 | +``` |
| 177 | + |
| 178 | +Again, note that for future EESSI versions, if they use Lmod 8.7.35+, this would simplify to |
| 179 | + |
| 180 | +```lua |
| 181 | +require("strict") |
| 182 | +local hook=require("Hook") |
| 183 | + |
| 184 | +local function set_my_second_env_var_openmpi(t) |
| 185 | + local simpleName = string.match(t.modFullName, "(.-)/") |
| 186 | + if simpleName == 'OpenMPI' then |
| 187 | + setenv('MY_SECOND_ENV_VAR', '5') |
| 188 | + end |
| 189 | +end |
| 190 | + |
| 191 | +hook.register("load", set_my_second_var_openmpi, "append") |
| 192 | +``` |
0 commit comments