Post

Lmod管理环境变量

Lmod管理环境变量

集群上装的module起初用不习惯,后面越用越顺手;现在用上自己的机子没有module居然有点不适应了。那就开整!

Linux版本:Rocky 8.10

安装

1
2
3
4
5
6
# 安装Lua依赖包
sudo dnf config-manager --enable powertools
sudo dnf install lua-filesystem lua-posix

# 安装Lmod
sudo dnf install Lmod

验证安装

1
2
3
4
5
# 查找Lmod初始化目录
find /usr/share/lmod -name "init" -type d 2>/dev/null || echo "未找到init目录"

# 检查Lmod版本
lmod --version

配置环境

编辑 ~/.bashrc 文件,添加以下内容:

1
2
3
4
5
6
# Lmod环境模块系统初始化
source /usr/share/lmod/lmod/init/bash

# 添加个人模块文件目录
mkdir -p ~/modulefiles
module use ~/modulefiles

重新加载配置

1
source ~/.bashrc

使用方法

基本命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 查看可用模块
module avail
module spider

# 加载模块
module load 模块名/版本

# 查看已加载的模块
module list

# 卸载模块
module unload 模块名

# 卸载所有模块
module purge

# 查看模块帮助
module help 模块名

# 查看模块详细信息
module show 模块名

创建模块

创建文件~/modulefiles/gaussian16/g16.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
local g16root = "/apps/gaussian16"

whatis("Gaussian 16 量子化学计算软件")

-- 设置环境变量
setenv("g16root", g16root)
setenv("GAUSS_SCRDIR", "/data/tmp")
setenv("GAUSS_EXEDIR", pathJoin(g16root, "g16"))
setenv("OMP_THREAD_LIMIT", "256")

-- source原始g16.profile脚本
execute{cmd="source " .. pathJoin(g16root, "g16/bsd/g16.profile"), modeA={"load"}}

-- 与其他版本的Gaussian冲突
conflict("g09")

使用:

1
2
3
4
5
6
7
8
9
10
11
12
# 加载模块
module load gaussian16/g16

# 验证环境
echo $g16root
echo $GAUSS_SCRDIR

# 运行Gaussian
g16 < input.com > output.log

# 卸载模块
module unload gaussian16/g16

模块文件

基本结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
-- 模块注释
local var = "value"

whatis("模块简短描述")

-- 环境变量设置
setenv("VAR", "value")
prepend_path("PATH", "/path/to/bin")

-- 执行外部脚本
execute{cmd="source /path/to/script", modeA={"load"}}

-- 冲突管理
conflict("other_module")

常用Lmod函数

  • setenv(var, value) - 设置环境变量
  • prepend_path(var, path) - 路径前置添加
  • pathJoin(path1, path2) - 安全路径连接
  • execute{cmd="command", modeA={"load"}} - 执行shell命令
  • conflict("module") - 声明冲突模块
  • whatis("description") - 模块描述

调试命令

1
2
# 检查模块文件语法
lmod lua check ~/modulefiles/gaussian16/g16.lua
This post is licensed under CC BY 4.0 by the author.
Total hits!