###### tags: `HPC` `module` `lmod`
# Lmod使用介紹
Lmod與Environment Module的命令完全相容,若原本就熟悉Environment Module的操作,可依既有的使用方式操作,以下將以Lmod的操作範例來說明。Lmod與Enviroment Module的操作命令皆為module(Lmod另提供ml命令,可參閱[Lmod便捷模式](https://man.twcc.ai/YXV2tsRiSUSsNQgO1DsI2g?both#3-Lmod%E5%BF%AB%E6%8D%B7%E6%A8%A1%E5%BC%8F)),依不同指令進行shell操作環境切換
## 1. __常用指令與功能__ <span><font color="salmon">(Done)</font>
* add/load 讀取模組設定(modulefile)到shell環境設定中
* rm/unload/del 移除讀取的模組設定
* swap/sw/switch m1 m2 卸載m1設定並讀取m2設定
* show/display 顯示modulefile的內容
* list 列出已讀取到shell的module file設定
* purge 清除所有已讀取的module file設定
* avail/av 列出可用模組
* spider 搜尋所有可能的模組
* whatis/help 查詢模組說明
* save/s 儲存模組組合
* restore/r 讀取儲存的模組組合
* savelist 列出已儲存的模組組合
* use [-a] Prepend or Append path to MODULEPATH
## 2. __操作範例__
* __avail/av 列出可用模組__ <span><font color="salmon">(Done)</font>
```
[user@tchead ~]$ module avail
---------------------------- /pkg/modulefiles/Core -----------------------------
compiler/gcc/10.2 compiler/intel/2020_u4
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".
```
* __whatis/help 查詢模組設定說明__ <span><font color="salmon">(Done)</font>
```
[user@tchead ~]$ module help compiler/gcc/10.2
----------------- Module Specific Help for "compiler/gcc/10.2" -----------------
This module loads the gcc-10.2.0 compilers (10.2.0). The
following additional environment variables are defined:
CC (path to gcc compiler wrapper )
CXX (path to g++ compiler wrapper )
F77 (path to gfortran compiler wrapper )
F90 (path to gfortran compiler wrapper )
FC (path to gfortran compiler wrapper )
See the man pages for gcc, g++, gfortran (f77, f90). For
more detailed information on available compiler options and
command-line syntax.
[user@tchead ~]$ module whatis compiler/gcc/10.2
compiler/gcc/10.2 : Description: GCC 10.2.0 compilers
compiler/gcc/10.2 : URL: www.gnu.org
```
* __show/display 顯示模組設定內容__ <span><font color="salmon">(Done)</font>
```
[user@tchead ~]$ module show compiler/gcc/10.2
----------------------------------------------------------------------
/pkg/modulefiles/Core/compiler/gcc/10.2.lua:
----------------------------------------------------------------------
help([[This module loads the gcc-10.2.0 compilers (10.2.0). The
following additional environment variables are defined:
CC (path to gcc compiler wrapper )
CXX (path to g++ compiler wrapper )
F77 (path to gfortran compiler wrapper )
F90 (path to gfortran compiler wrapper )
FC (path to gfortran compiler wrapper )
See the man pages for gcc, g++, gfortran (f77, f90). For
more detailed information on available compiler options and
command-line syntax.
]])
whatis("Description: GCC 10.2.0 compilers")
whatis("URL: www.gnu.org")
prepend_path("PATH","/pkg/gcc/10.2/bin")
prepend_path("PATH","/pkg/gcc/10.2/include")
prepend_path("LD_LIBRARY_PATH","/pkg/gnu/isl/lib")
prepend_path("LD_LIBRARY_PATH","/pkg/gcc/10.2/lib64")
prepend_path("MANPATH","/pkg/gcc/10.2/man")
pushenv("CC","/pkg/gcc/10.2/bin/gcc")
pushenv("CXX","/pkg/gcc/10.2/bin/g++")
pushenv("F77","/pkg/gcc/10.2/bin/gfortran")
pushenv("FORT","/pkg/gcc/10.2/bin/gfortran")
pushenv("cc","/pkg/gcc/10.2/bin/gcc")
pushenv("cxx","/pkg/gcc/10.2/bin/g++")
pushenv("f77","/pkg/gcc/10.2/bin/gfortran")
pushenv("fort","/pkg/gcc/10.2/bin/gfortran")
pushenv("FC","/pkg/gcc/10.2/bin/gfortran")
pushenv("fc","/pkg/gcc/10.2/bin/gfortran")
prepend_path("MODULEPATH","/pkg/modulefiles/comp/gcc/10.2")
family("compiler")
```
* __add/load 讀取模組設定__ <span><font color="salmon">(Done)</font>
```
[user@tchead ~]$ which gcc
/usr/bin/gcc
[user@tchead ~]$ module load compiler/gcc/10.2
[user@tchead ~]$ which gcc
/pkg/gcc/10.2/bin/gcc
```
* __list 列出已讀取的模組設定__ <span><font color="salmon">(Done)</font>
```
[user@tchead ~]$ module list
Currently Loaded Modules:
1) compiler/gcc/10.2
```
* __swap/sw/switch m1 m2 卸載m1設定並讀取m2設定__ <span><font color="salmon">(Done)</font>
```
[user@tchead ~]$ module list
Currently Loaded Modules:
1) compiler/gcc/10.2
[user@tchead ~]$ module av
-------------------- /pkg/modulefiles/comp/gcc/10.2 --------------------
openmpi/4.0.5
------------------------ /pkg/modulefiles/Core -------------------------
compiler/gcc/10.2 (L) compiler/intel/2020_u4
Where:
L: Module is loaded
[user@tchead ~]$ module sw compiler/gcc/10.2 compiler/intel/2020_u4
[user@tchead ~]$ module list
Currently Loaded Modules:
1) compiler/intel/2020_u4
```
* __spider 搜尋所有可能的模組__ <span><font color="salmon">(Done)</font>
```
[user@tchead ~]$ module spider
----------------------------------------------------------------------------
The following is a list of the modules and extensions currently available:
----------------------------------------------------------------------------
compiler/gcc: compiler/gcc/10.2
GCC 10.2.0 compilers
compiler/intel: compiler/intel/2020_u4
Intel Compiler Collection - PSXE.
openmpi: openmpi/4.0.5
OpenMPI-4.0.5 with GNU 10.2 compilers
----------------------------------------------------------------------------
```
* __purge 清除所有讀取的模組設定__ <span><font color="salmon">(Done)</font>
```
[user@tchead ~]$ module list
Currently Loaded Modules:
1) compiler/gcc/10.2
[user@tchead ~]$ module av
------------------------ /pkg/modulefiles/comp/gcc/10.2 ------------------------
openmpi/4.0.5
---------------------------- /pkg/modulefiles/Core -----------------------------
compiler/gcc/10.2 (L) compiler/intel/2020_u4
Where:
L: Module is loaded
[user@tchead ~]$ module load openmpi/4.0.5
[user@tchead ~]$ module list
Currently Loaded Modules:
1) compiler/gcc/10.2 2) openmpi/4.0.5
[user@tchead ~]$ module purge
[user@tchead ~]$ module list
No modules loaded
```
* __rm/unload/del 移除讀取的模組設定__ <span><font color="salmon">(Done)</font>
```
[user@tchead ~]$ module list
Currently Loaded Modules:
1) compiler/gcc/10.2 2) openmpi/4.0.5
[user@tchead ~]$ module del openmpi/4.0.5
[user@tchead ~]$ module list
Currently Loaded Modules:
1) compiler/gcc/10.2
```
* __save/s 儲存模組組合__ <span><font color="salmon">(Done)</font>
```
[user@tchead ~]$ module list
Currently Loaded Modules:
1) compiler/gcc/10.2 2) openmpi/4.0.5
[user@tchead ~]$ module save gcc10-OpenMPI
Saved current collection of modules to: "gcc10-OpenMPI"
```
* __savelist 列出已儲存的模組組合__ <span><font color="salmon">(Done)</font>
```
[user@tchead ~]$ module savelist
Named collection list :
1) gcc10-OpenMPI
```
* __restore/r 讀取儲存的模組組合__ <span><font color="salmon">(Done)</font>
```
[user@tchead ~]$ module list
No modules loaded
[user@tchead ~]$ module r gcc10-OpenMPI
Restoring modules from user's gcc10-OpenMPI
[user@tchead ~]$ module list
Currently Loaded Modules:
1) compiler/gcc/10.2 2) openmpi/4.0.5
```
## 3. __Lmod便捷模式__
若覺得module太長、字數太多或常常輸入錯誤如:modeul...等,lmod提供"ml"命令,可以更簡短輸入使用。
`
$ ml
`
單純的ml是module list
`$ ml foo`
等同 module load foo
`$ ml -foo`
等同module unload foo
`$ ml foo -bar`
等同module unload bar 然後module load foo
```
$ ml spider
$ ml av
$ ml show foo
```
module的指令都可以在ml下使用,詳細用法可參閱[原始文件](https://lmod.readthedocs.io/en/latest/010_user.html#ml-a-convenient-tool)。
## 4. __modulefile基本語法__
待補