You are here:

Advanced Modules Tutorial

Modules has a number of advanced features that can make working with modules even easier. Users can  build custom module files so that they can use a custom modules environment.

CHPC uses the Lmod version of modules produced by TACC. For more information, please visit the Lmod Homepage.

Saving module environments

The commands module save and module restore can be used to save a loaded module environment for later use. If you are loading the same modules repeatedly, saving a module environment can same time, especially if there are multiple modules needed in the environment.

Suppose you want to use the Amber14 package to perform some molecular dynamics experiments with a small molecule, a process that may require a compiler, MPI library, amber, the visualization program VMD, and gaussian09, and possibly access to the CSD database to obtain a structure, and the structure file converter openbabel.

>module list
Currently Loaded Modules:
  1) csd/2015           3) mpich2/3.0.4.i   5) openbabel/2.3.2   7) gaussian09/EM64T.D01
  2) intel/2015.1.133   4) amber/14         6) vmd/1.9.1

In total, this comprises 7 modules; if the user needs this environment on a regular basis, instead of typing these in each time, the user can save this environment with the use of module save:

>module save md-setup
Saved current collection of modules to: md-setup

If no name is specified, the name "default" will be used. To load this list again use module restore:

>module purge
>module list
No modules loaded
>module restoremd-setup
Restoring modules to user's md-setup
>module list
Currently Loaded Modules:
  1) csd/2015           3) mpich2/3.0.4.i   5) openbabel/2.3.2   7) gaussian09/EM64T.D01
  2) intel/2015.1.133   4) amber/14         6) vmd/1.9.1

To see a list of your module savepacks, use the command module savelist:

>module savelist
Named collection list:
  1) md-setup  2) default

The files for these savepacks are stored in $HOME/.lmod.d. If you delete this directory, you will lose all savepacks!

Creating a custom module environment

A module environment is started by creating a directory, putting modulefiles into that directory in a specifically organized way, and then adding that directory to the $MODULEPATH variable using module use. Under Lmod, a modulefile is a simple lua script containing Lmod specific commands to set up environment variables. Other versions of modules use TCL/TK scripts; Lmod is compatible with these scripts, but the CHPC module installaton only supports modulefiles written in lua.  Writing modulefiles will be discussed in a later section.

The hierarchy of a modulefiles directory is straightforward. The top level directory should contain a folder called Core. This directory is the landing place for modules which have no particular dependencies. Within this directory should be additional directories, one for each package that the user anticipates using:

mymodules> ls
Core Compiler MPI
mymodules> cd Core
mymodules/Core> ls
package1 package2 package3

Each folder inside Core should contain one or more modulefiles, named according to the version of the package it references:

mymodules/Core> cd package1
mymodules/Core/package1> ls
1.0.lua 1.2.lua 2.0.lua

There are two important details about the contents of this directory: 1) Lmod treats ALL files in this directory as if they are modulefiles, and 2) All files that do not have the extension .lua will be treated as TCL/TK scripts. This means that if you put a file in the directory modulefiles/Core that is NOT a modulefile, it will be treated like a modulefile. Attempting to load this will result in errors, and possibly unpredictable behaviour. Therefore, do not put anything besides modulefiles in a modulefile directory.

To use a modulefile directory, the command module use is utilized. This command modifies the environment variable $MODULEPATH by prepending the given path:

>echo $MODULEPATH
/some/path/to/modulefiles
> module use $HOME/mymodules/Core
> echo $MODULEPATH
/home/u0123456/mymodules/Core:/some/path/to/modulefiles

The user then should be able to use any modules contained in users modulefiles directory.

There is a way to remove this directory from the path, but first, some important notes:

1) Be extremely careful when unloading paths from modules. Unloading the wrong path can result in a broken CHPC environment.
2) Adding the wrong path to modules will break your spider cache and cause major errors when using module commands.
3) Do NOT modify $MODULEPATH directly; if you do this you will break modules and cause yourself headaches.

The command to unload a modulepath is module unuse:

> module unuse $HOME/mymodules/Core
>echo $MODULEPATH
/some/path/to/modulefiles

For a more detailed explanation of module hierarchies, please visit the following page: Module hierarchies

Writing module files

A detailed list of commands that can be used in a modulefile can be found here: Module commands tutorial. In addition to these commands, generally, any valid lua code may be used in a modulefile; complex modulefiles that use extensive logic are discouraged for security reasons and the sake of simplicity. Users are discouraged from using anything beyond the basic Lmod commands given here:

The most basic and most used command in a modulefile is prepend_path. The first argument is the variable that will be prepended; generally this is PATH or LD_LIBRARY_PATH. The second argument is the path to be prepended. Prepend_path will create the variable if it does not already exist (as may be the case for $MANPATH or $LD_LIBRARY_PATH).

The second basic command that users will find the most value in is setenv. This is essentially identical to prepend_path, except that instead of prepending to the path, the environment variable is replaced with the given value or created from scratch.

Users may find value in the help and whatis commands, which are useful for providing information to users on the contents and nature of a package, as well as any particular instructions which may apply to a particular module.

If users need help creating more complex modulefiles, please contact CHPC at issues@chpc.utah.edu. If your software comes with an environment setup script, CHPC has a scripting utility available that can automate the creation process; please inquire via email if you need access.

A template for users to pattern their own modulefiles after can be found at the following path:

/uufs/chpc.utah.edu/sys/modulefiles/templates/template.lua

Here is a simple example of a modulefile written in lua for openbabel, which covers the basic commands a user might need.

-- -*- lua -*-
help([[
Openbabel is a software for converting between different chemical structure formats. This module loads Openbabel.
Once loaded, the command to execute openbabel is "obabel"
]])

prepend_path("PATH","/uufs/chpc.utah.edu/sys/installdir/openbabel/2.3.2/bin")
prepend_path("LD_LIBRARY_PATH","/uufs/chpc.utah.edu/sys/installdir/openbabel/2.3.2/lib")
setenv("BABEL_LIBDIR","/uufs/chpc.utah.edu/sys/installdir/openbabel/2.3.2/lib")
setenv("BABEL_DATADIR","/uufs/chpc.utah.edu/sys/installdir/openbabel/2.3.2/data/data")
set_alias("babel", "obabel")

whatis("Name         : OpenBabel")
whatis("Version      : 2.3.2")
whatis("Category     : Application")
whatis("Description  : Chemical Structure Format Converter")
whatis("URL          : http://openbabel.org/")
whatis("Installed on : 05 Feb 2015")
Last Updated: 5/16/17