LAMMPS线程与独立分区实操
实际比较普通和OpenMP路径的短NVE轨迹,运行两个独立MPI分区并解释进程与线程。
阅读本篇目录
一项任务中使用多个线程,与把MPI进程分成几个独立计算世界,是两种不同的运行方式。本篇通过108原子的LJ短轨迹,实际检查1/2/4线程路径,并运行两个不同初温的独立分区。
下载六组输入、真实轨迹、分区日志、NDX、VMD脚本与绘图工具,解压进入run.sh目录。
1 提交完整流程#
sbatch submit.slurm
核时P100模板申请1GPU4CPU,顺序运行六组短流程;本篇使用P100构建中的普通CPU与OPENMP路径,不启用GPU后缀,不作GPU加速或线程性能结论。构建LAMMPS30Sep2026、GNU C++13.3.0/C++17、Open MPI5.0.10;分析ASE3.29.0。
套餐用户用yskit生成所属分区脚本,保留4个CPU核,在已分配资源中执行bash run.sh;不要在登录节点模拟。脚本在分配的CPU亲和范围内选择最多4核。inputs为新输入,reference为本文实际输出,work为新运行;已有work请重新解压。
2 普通与OpenMP输入#
三个目录使用同一FCC初始结构、相同速度种子和0.001时间步,运行100步NVE;LJ截断为2.5,pair_modify shift yes将势能在截断处平移到零,力仍是截断LJ力。短轨迹只用于操作与数值一致性。
两线程完整输入如下;四线程目录将package omp 2改为4,普通目录不设置package与suffix:
package omp 2
suffix omp
units lj
atom_style atomic
boundary p p p
lattice fcc 0.8442
region box block 0 3 0 3 0 3
create_box 1 box
create_atoms 1 box
mass 1 1.0
pair_style lj/cut 2.5
pair_coeff * * 1.0 1.0 2.5
pair_modify shift yes
neighbor 0.3 bin
neigh_modify delay 0 every 1 check yes
velocity all create 1.0 42791 mom yes rot no dist gaussian
fix integrate all nve
timestep 0.001
thermo 20
thermo_style custom step atoms temp pe ke etotal press
thermo_modify norm no format float %.16g
timer full sync
run 100
write_dump all custom atoms.dump id type x y z vx vy vz fx fy fz modify sort id format float %.16g
package omp在建盒前设置线程包参数,suffix omp让支持OpenMP的style选择对应后缀;仅设置OMP_NUM_THREADS不会自动把所有style切换为OpenMP实现。
在已申请的资源中手动运行时:
cp -r inputs/lj-omp2 my-omp2
cd my-omp2
module load lammps/30Sep2026-p100-gpu
export OMP_NUM_THREADS=2
lmp -in in.lammps
这里是1个MPI进程、2个OpenMP线程;4线程也不表示4个MPI进程。timer full sync增加分项计时与同步信息,适合排查执行方式;小体系的启动和计时开销很大,不用这100步排名速度。
3 实际数值对照#
| 对照路径 | 最大原子位置/速度/力分量差(各自LJ约化单位) | 最大总能差(LJ约化单位) |
|---|---|---|
| lj-omp2相对普通路径 | 5.418E-14 | 3.331E-11 |
| lj-omp4相对普通路径 | 5.507E-14 | 4.036E-11 |
差值逐项比较同一ID原子的最终位置、速度和受力,并比较每个热力学采样点的总能;没有把不同单位的分量相加。各分量使用相应的LJ约化单位。

NVE不固定温度,初始FCC结构的动能会转为势能,温度因此变化。这段100步轨迹不代表已平衡液体,也不用于扩散或热输运统计。长时间守恒还需检查时间步、截断和平滑方式。
4 两个独立MPI分区完整输入#
variable label world low high
variable target world 0.8 1.2
partition yes 1 print "SELECTED_LOW_PARTITION"
partition yes 2 print "SELECTED_HIGH_PARTITION"
units lj
atom_style atomic
boundary p p p
lattice fcc 0.8442
region box block 0 3 0 3 0 3
create_box 1 box
create_atoms 1 box
mass 1 1.0
pair_style lj/cut 2.5
pair_coeff * * 1.0 1.0 2.5
pair_modify shift yes
neighbor 0.3 bin
neigh_modify delay 0 every 1 check yes
velocity all create ${target} 42791 mom yes rot no dist gaussian
fix integrate all nve
timestep 0.001
thermo 20
thermo_style custom step atoms temp pe ke etotal press
thermo_modify norm no format float %.16g
run 100
write_dump all custom atoms-${label}.dump id type x y z vx vy vz modify sort id format float %.16g
variable ... world为每个分区提供一个值;partition yes 1或2只在对应分区执行后面的命令。两个分区初温分别为0.8和1.2,各自运行NVE并写入atoms-low.dump与atoms-high.dump。
在已分配至少两个CPU核的资源中手动运行:
cp -r inputs/partition-temperatures my-partitions
cd my-partitions
module load lammps/30Sep2026-p100-gpu
export OMP_NUM_THREADS=1
mpirun --bind-to none -np 2 lmp -partition 2x1 -in in.lammps
ls log.lammps.* atoms-*.dump
-np 2总共启动2个MPI进程,-partition 2x1将它们分成2个世界,每个世界1个进程。这两个体系独立演化,没有温度交换或副本交换;若要交换,需要另设相应算法。
grep -A 1 'Step.*Atoms.*Temp' work/cases/partition-temperatures/log.lammps.0
grep -A 1 'Step.*Atoms.*Temp' work/cases/partition-temperatures/log.lammps.1
提取与重画#
module load ase/3.29.0
python analyze.py work
python plot_results.py work
python check_results.py work
cat work/analysis/short-nve.csv
ls work/figures
CSV包含线程路径与独立分区的初始温度、最终温度、总能和短轨迹能量变化;JSON保存完整热力学采样、逐原子对照和索引成员。查看本文数据时将work改成reference。