# Environment Modules ::::::{tab-set} :sync-group: lang :class: lang-tabs :::::{tab-item} En :sync: en The [Environment Modules](http://modules.sourceforge.net/) system allows users to use various non-system software packages and versions thereof. The system was originally developed by Cray Inc., and is now used on most large Unix systems where new programs not included in the base distribution are installed. The following commands are important for the user: - module avail - lists the available system-installed software modules - module list - lists the modules that the user has selected or that were enforced by the system profile - module load - sets the environment variables for the search paths of the given module - module help - describes the module with basic information - module display - lists the environment variables that will be set by module load ## Shell settings Using `module load` in the .profile or .bash\_profile file, the user sets up the libraries, compilers and programs they want to use. All permanent settings should be written into the login shell and not into .bashrc, which is interpreted every time a shell is opened. Because of *suid/sgid ld.so* programs such as ssh-agent and konsole, $LD\_LIBRARY\_PATH is not passed on from the login shell, so we restore the environment by saving it in the login shell and then restoring it in .bashrc. In ${HOME}/.bash\_profile, add a line before the module load commands ``` bash export MODULESBEGINENV=$HOME/.modulesbeginenv module load intel/11.1 openmpi module load ansys ``` Then restore the modules in ${HOME}/.bashrc with the command ``` bash [ -n "${MODULESBEGINENV}" ] && module update ``` ## Using modules in SLURM scripts In principle there is no need to load modules for programs in SLURM scripts, since the entire environment is also passed to the sbatch command, which runs in the background. However, if we have programs compiled with different compilers, or want to make it easier to run older programs without recompiling them with the environment we are currently using, the correct modules can be written directly into the SLURM script. This is done by removing the currently loaded modules with *purge* and loading the appropriate ones with *load*. Example of the HPL test: ``` bash #!/bin/bash #SBATCH --partition=rome #SBATCH --ntasks=624 #SBATCH --ntasks-per-core=1 #SBATCH --job-name="HPL 2.0" #SBATCH --mail-type=END module () { eval `/usr/local/Modules/default/bin/modulecmd bash $*` } module purge module load intel/11.1 intel-mkl/10.2 openmpi/1.4.3 mpirun xhpl ``` ## User modules In addition to the system-provided modules, users can create their own modules for their libraries or for libraries they use that are not supported by the system. The following example shows the use of the modules environment, which is generally suitable for systems with various non-standard libraries or programs. ### Example of creating the user module "maxnumber" Let us create a user library containing the function maxnumber, which returns the larger integer. Each library consists of functions packaged in static or dynamic form and an associated "header" file for correct conversion and checking of function arguments during compilation. The file maxnumber.c for our example is as follows: ` ` ``` c #include "maxnumber.h" int maxnumber(int a, int b) { return a > b ? a : b; } ``` with the associated file maxnumber.h ``` c #ifndef MAXNUMBER #define MAXNUMBER int maxnumber(int a, int b); #endif ``` By convention, all libraries have two subdirectories: - include/ - contains declarations of functions and constants in .h (header) files for use in the compilation phase (*compilation*) - lib/ - contains compiled and prepared libraries for use when linking (*linking*) the program In addition, there may be other subdirectories for source code, documentation, tests, examples and more To prepare the files according to the above rules, run the following commands: 1. Move the file maxnumber.h to the include directory - mkdir include - mv maxnumber.h include/ 2. Prepare the library with the following commands - cc -c -I./include maxnumber.c - ar ru libmaxnumber.a maxnumber.o - ranlib libmaxnumber.a - mkdir lib/ - mv libmaxnumber.a lib/ Also prepare the file modulefile\_maxnumber with the module description in the following form: Add the path to the library to the MODULEPATH environment variable, and it is then visible with module avail and available for use in compilation, for example ``` text #%Module1.0##################################################################### ## Maxnumber 1.0 Module File proc ModulesHelp { } { puts stderr "" puts stderr "" puts stderr "" puts stderr "Release date: October 19th 2010" puts stderr "" puts stderr "Help and usage of the package" puts stderr "(19-10-2010, Leon Kos)" puts stderr "" } module-whatis "Maxnumber 1.0 (Release date: October 19th 2010)" set pkgroot /home/leon/modules/maxnumber setenv MAXNUMBERPATH $pkgroot setenv MAXNUMBER_INC ${pkgroot}/include prepend-path LIBRARY_PATH ${pkgroot}/lib ``` Which is compiled with: ``` bash cc -I$MAXNUMBER_INC moj_program.c -o moj_program ``` ::::: :::::{tab-item} Slo :sync: sl **Okolje module** [Okolje module](http://modules.sourceforge.net/) omogoča uporabnikom uporabo različnih nesistemskih programskih paketov in verzij le teh. Prvotno je sistem razvil Cray inc., sedaj pa se uporablja na večini večjih unix sistemov na katere se nameščajo novi programi, katere osnovna distribucija ne vsebuje. Za uporabnika so pomembni naslednji ukazi: - module avail - izpiše razpoložljive sistemsko nameščene programske module - module list - izpiše module, ki jih je uporabnik izbral sam ali pa so bili vsiljeni s sistemskim profilom - module load - vpiše sistemske spremenljivke za iskalne poti podanega modula - module help - opiše modul z osnovnimi podatki - module display - izpiše sistemske spremenljivke, ki se bodo vpisale z module load ## Nastavitve v ukazni lupini Uporabnik si z `module load` v datoteki .profile ali .bash\_profile nastavi knjižnice, prevajalnike in programe, ki jih želi uporabljati. Vse stalne nastavitve naj bi se pisale v prijavno lupino in ne v .bashrc, ki se interpretira pri vsakem odpiranju lupine. Zaradi *suid/sgid ld.so* programov, kot sta ssh-agent in konsole, se $LD\_LIBRARY\_PATH iz prijavne školjke ne prenese, zato uporabimo obnovitev okolja tako, da se okolje v prijavni školjki shrani in nato v .bashrc obnovi. V ${HOME}/.bash\_profile dodamo vrstico pred ukazi module load ``` bash export MODULESBEGINENV=$HOME/.modulesbeginenv module load intel/11.1 openmpi module load ansys ``` Nato v ${HOME}/.bashrc module obnovimo z ukazom ``` bash [ -n "${MODULESBEGINENV}" ] && module update ``` ## Uporaba modulov v skriptah SLURM Načeloma ni potrebe, da bi s skriptah SLURM nalagali module za programe, saj se celotno okolje prenese tudi v sbatch ukaz, ki se izvede v ozadju. Če pa imamo programe, ki smo jih prevajali z različnimi prevajalniki in ali želimo olajšati poganjanje starejših programov, ne da bi jih ponovno prevedli z okoljem, ki ga trenutno uporabljamo, lahko pravilne module napišemo kar v skripto SLURM. To storimo tako, da pobrišemo z *purge* trenutno naložene module in naložimo ustrezne z *load*. Primer HPL testa: ``` bash #!/bin/bash #SBATCH --partition=rome #SBATCH --ntasks=624 #SBATCH --ntasks-per-core=1 #SBATCH --job-name="HPL 2.0" #SBATCH --mail-type=END module () { eval `/usr/local/Modules/default/bin/modulecmd bash $*` } module purge module load intel/11.1 intel-mkl/10.2 openmpi/1.4.3 mpirun xhpl ``` ## Uporabniški moduli Poleg sistemsko pripravljenih modulov, si lahko uporabniki sami naredijo module za svoje knjižnice ali za knjižnjice, ki jih uporabljajo, pa niso sistemsko podprte.  Na naslednjem primeru je pokazana uporaba okolja modules, ki je splošno primerno za sisteme z različnimi nestandarnimi knjižnicamin ali programi. ### Primer izdelave uporabniškega modula "maxnumber"  Izdelajmo uporabniško knjižnico, ki vsebuje funkcijo maxnumber in vrača večje celo število. Vsaka knjižnica se sestoji iz funkcij zapakiranih v statični ali dinamični obliki in pripadajoča "header" datoteka za pravilno pretvorbo in kontrolo argumentov funkcij pri prevajanju. Datoteka maxnumber.c je za naš primer naslednja: ` ` ``` c #include "maxnumber.h" int maxnumber(int a, int b) { return a > b ? a : b; } ```  s pripadajočo datoteko maxnumber.h ``` c #ifndef MAXNUMBER #define MAXNUMBER int maxnumber(int a, int b); #endif ``` Standardno imajo vse knjižnice dva podimenika: - include/ - vsebuje deklaracije funkcij in konstant v .h  (header) datotekah za uporabo v fazi prevajanja (*compilation*) - lib/ - vsebuje pevedeni in pripravljene knjižnice za uporabo pri povezovanju (*linking*) progama Poleg navedenih so lahko še drugi podimeniki za izvorno kodo, dokumentacijo, teste, primere in drugo Za pripravo datotek po zgornjih pravilih izvedemo naslednje ukaze: 1. Premaknemo datoteko maxnumber.h v imenik include - mkdir include - mv maxnumber.h include/ 2. Pripravimo knjižnico z naslednjimi ukazi - cc -c -I./include maxnumber.c - ar ru libmaxnumber.a maxnumber.o - ranlib libmaxnumber.a - mkdir lib/ - mv libmaxnumber.a lib/ Pripravimo še datoteko modulefile\_maxnumber z opisom modula v naslednji obliki: Dodamo port do knjižnice v sistemsko spremenljivko MODULEPATH in že je vidna z module avail in na voljo za uporabo pri prevajanju kot je npr ``` text #%Module1.0##################################################################### ## Maxnumber 1.0 Module File proc ModulesHelp { } { puts stderr "" puts stderr "" puts stderr "" puts stderr "Release date: October 19th 2010" puts stderr "" puts stderr "Help and usage of the package" puts stderr "(19-10-2010, Leon Kos)" puts stderr "" } module-whatis "Maxnumber 1.0 (Release date: October 19th 2010)" set pkgroot /home/leon/modules/maxnumber setenv MAXNUMBERPATH $pkgroot setenv MAXNUMBER_INC ${pkgroot}/include prepend-path LIBRARY_PATH ${pkgroot}/lib ``` Kar prevedemo z: ``` bash cc -I$MAXNUMBER_INC moj_program.c -o moj_program ``` ::::: ::::::