LAMMPS最小化的GPU与Kokkos操作
GPU力计算、命名Kokkos最小化器、完整输入及受约束力与物理力的区别。
阅读本篇目录
GPU包负责力计算,并不意味着最小化算法一定使用GPU专用后缀。本文用同一个固定端LJ双原子模型,展示5种CPU最小化器配合GPU力,以及4种命名的Kokkos最小化器;全部输入和结果均来自实际计算。
1. 下载并复算#
下载完整输入、实际结果和绘图工具。在自己的工作目录解压并提交:
mkdir minimize-gpu-demo
cd minimize-gpu-demo
curl -fL https://yuansuan.top/assets/downloads/full-function/lammps-minimizers-wave87-c33bf9fded23.zip -o examples.zip
unzip examples.zip
sbatch submit.slurm
提交脚本申请1个P100和1个CPU核,顺序执行CPU、GPU和Kokkos对照,再生成CSV和图片。每次重新计算请使用新的解压目录,避免覆盖已有结果。直接调用 lmp 的命令在已分配的计算资源中执行。
使用 lammps/30Sep2026-p100-gpu 与 lammps/30Sep2026-p100-kokkos,GCC13.3、OpenMPI5.0.10、CUDA12.8;后处理使用 ase/3.29.0。这里比较数值结果,双原子小模型不适合评价GPU加速比。
模型、解析极小值和5种CPU算法见位置优化算法实操。14份结果是5个CPU条件、5个GPU力条件和4个Kokkos条件,模型都保持盒子不变,只放松粒子2的位置。
2. GPU力计算:算法名称保持不变#
以 inputs/cg-gpu/in.lammps 为例:
package gpu 1
newton off
units lj
atom_style atomic
atom_modify map array sort 0 0.0
boundary f f f
region box block -5 5 -5 5 -5 5 units box
create_box 1 box
create_atoms 1 single 0 0 0 units box
create_atoms 1 single 1.6 .1 .05 units box
mass 1 1
pair_style lj/cut/gpu 2.5
pair_coeff * * 1.0 1.0
pair_modify shift yes
group anchor id 1
group mobile id 2
fix fixed anchor setforce 0 0 0
neighbor .3 bin
neigh_modify every 1 delay 0 check yes
thermo 10
thermo_style custom step pe fnorm fmax
thermo_modify norm no format float %.16g
timestep .01
min_style cg
min_modify dmax .1
minimize 0.0 1.0e-4 4000 20000
write_dump all custom constrained.dump id type x y z fx fy fz modify sort id format float %.16g
print "CONSTRAINED $(pe:%.16g) $(fnorm:%.16g) $(fmax:%.16g)" file constrained-energy.dat screen no
unfix fixed
run 0
write_dump all custom physical.dump id type x y z fx fy fz modify sort id format float %.16g
print "PHYSICAL $(pe:%.16g)" file physical-energy.dat screen no
这里是 min_style cg 配合 pair_style lj/cut/gpu,不是 min_style cg/gpu。package gpu 1 使用1张卡,newton off 与本例GPU包配置相配。其他四个GPU输入只更换相应最小化算法及必要标记。
在已分配的P100计算资源内单独运行:
module purge
module load lammps/30Sep2026-p100-gpu
cd inputs/cg-gpu
lmp -in in.lammps
cd ../..
3. Kokkos:启动选项与输入配套#
inputs/cg-kk/in.lammps 的完整输入如下:
package kokkos neigh half
newton on
units lj
atom_style atomic/kk
atom_modify map array sort 0 0.0
boundary f f f
region box block -5 5 -5 5 -5 5 units box
create_box 1 box
create_atoms 1 single 0 0 0 units box
create_atoms 1 single 1.6 .1 .05 units box
mass 1 1
pair_style lj/cut/kk 2.5
pair_coeff * * 1.0 1.0
pair_modify shift yes
group anchor id 1
group mobile id 2
fix fixed anchor setforce/kk 0 0 0
neighbor .3 bin
neigh_modify every 1 delay 0 check yes
thermo 10
thermo_style custom step pe fnorm fmax
thermo_modify norm no format float %.16g
timestep .01
run_style verlet/kk
min_style cg/kk
min_modify dmax .1
minimize 0.0 1.0e-4 4000 20000
write_dump all custom constrained.dump id type x y z fx fy fz modify sort id format float %.16g
print "CONSTRAINED $(pe:%.16g) $(fnorm:%.16g) $(fmax:%.16g)" file constrained-energy.dat screen no
unfix fixed
run 0
write_dump all custom physical.dump id type x y z fx fy fz modify sort id format float %.16g
print "PHYSICAL $(pe:%.16g)" file physical-energy.dat screen no
module purge
module load lammps/30Sep2026-p100-kokkos
cd inputs/cg-kk
lmp -k on g 1 -in in.lammps
cd ../..
-k on g 1 开启Kokkos并使用1张GPU;atomic/kk、lj/cut/kk、setforce/kk、verlet/kk 与半邻居表/Newton on共同构成本例实际使用的输入。其余三个命名最小化器为 sd/kk、fire/kk、quickmin/kk。本批没有命名的 hftn/kk 条件,不能从图中空位推断所有CPU最小化器与Kokkos势的组合都不可用。
4. 对照实际数值#

14个条件最终可动粒子的力模均小于1e-4;最大粒子间距误差约1.71×10⁻⁶,最大能量极小值偏差约6.01×10⁻⁸。GPU包使用混合精度,CPU/Kokkos本例走双精度路径;固定容差下得到相近结构即可,不要求逐位相同。
图中每个力值对应优化期间的受约束体系:粒子1的力被置零。随后输入执行 unfix fixed 和 run 0,在相同坐标、不再移动粒子的条件下重新输出完整物理力;粒子1的反作用力恢复。physical.dump 的全体系力范数与之前受约束的 fnorm 含义不同,不能混用作停止判据。
5. 导出结构与核对#
module purge
module load ase/3.29.0
python analyze.py work
python export_minima.py work
python plot_results.py work
python check_results.py work
work/cases/ 保存本次原始输出,work/analysis/ 保存表格,work/figures/ 保存高清图。包内 reference/ 是本文实算结果;未重算时,可以把上述命令中的 work 改为 reference 来重新分析和绘图。
python - <<'PYCODE'
import csv
with open('work/analysis/minimizers.csv') as f:
for r in csv.DictReader(f):
if r['method'] == 'cg':
print(r['backend'], r['energy'], r['radius'], r['max_force_formula_error'])
PYCODE
坐标与完整物理力导出到 work/analysis/*-minimum.xyz,单位仍为LJ约化单位。check_results.py 从标量势能做坐标中心差分,独立核对两粒子的6个力分量;它与最小化停止条件是两种检查。
对于自己的材料,先做小体系CPU/GPU能量和力对照,确认势函数、后缀、边界与精度匹配,再扩大规模。更严格的力容差要结合GPU精度实测调整,不能只增加迭代预算。
参考:Kokkos使用说明。