参考
Lammps实现GPU加速,博客界面很好看。
显卡驱动安装(本文默认为英伟达)
- 检查自己的驱动是否安装成功
nvidia-smi
如果安装成功会有如下输出:
如果未安装会有如下结果:
nvidia-smi command not found
- 安装驱动
ubuntu-drivers devices # 查询所有ubuntu推荐的驱动
sudo ubuntu-drivers autoinstall
手动安装可以部分参考我的另一篇博客:报错解决:Before you can run VMware, several modules must be compiled and …(更換內核)
禁用nouveau driver
nouveau开源驱动,性能很差,基本都禁用它,之前安装Ubuntu系统时,引导文件里就进行了禁用:
sudo vi /etc/modprobe.d/blacklist.conf
最后添加:
blacklist nouveau
options nouveau modeset=0
保存退出后,终端输入:
sudo update-initramfs -u
sudo reboot
reboot之后输入:
lsmod | grep nouveau
无输出代表禁用成功
CUDA Toolkit 安装
上NVIDIA官网进行下载:CUDA Toolkit 10.2 Download,安装流程很详细,但是网速很感人,建议大家翻墙…
安装完cuda之后添加环境变量:
vi .bashrc
export PATH=$PATH:/usr/local/cuda-10.2/bin
之后输入:nvcc -V
,就会以下信息:
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2019 NVIDIA Corporation
Built on Wed_Oct_23_19:24:38_PDT_2019
Cuda compilation tools, release 10.2, V10.2.89
Generic Linux Makefile for CUDA文件配置
进入gpu目录,修改Makefile.linux
cd lammps-3Mar20/lib/gpu
vim Makefile.linux
修改以下信息,符合自己的机子和计算要求即可:
CUDA_HOME = /usr/local/cuda-10.2 #注意版本
# Turing hardware
CUDA_ARCH = -arch=sm_75
CUDA_PRECISION = -D_SINGLE_DOUBLE #我选的计算过程中的精度
hardware参数可以见维基的介绍CUDA,我的gtx 1660ti对应的Micro-architecture和Compute capability (version)分别为Turing,7.5,所以应该选择sm_75。
之后进行make:
make -f Makefile.linux
如果之后更改Makefile.linux 中的内容之后需要重新编译需要先 make clean 一下:
make -f Makefile.linux clean
Lammps GPU package
cd lammps-3Mar20/src
make package-status #查阅自己的package安装信息
make yes-gpu #添加gpu包
make mpi #编译mpi
如果编译过mpi文件,则可以先clean一波:make clean-all
,如果还是有问题,可以考虑万能的sudo
测试
官网的例子,还是shear:
cd lammps/lammps-3Mar20/examples/shear
12线程无GPU:
mpirun -np 12 lmp_mpi < in.shear
12线程1GPU:
mpirun -np 12 lmp_mpi -sf gpu -pk gpu 1 -in in.friction
由于是小体系的example,GPU加速效果不是很明显,甚至慢很多,但是跑自己的程序,原子数目一多,GPU加速特别明显!!!!!!!!!!!!大概是五倍以上的速度!!!!
一万多原子,4000步摩擦,12线程无GPU花了3:28s,12线程1GPU只花了37s,真的太香了。
每10秒输出一次显卡状态:
watch -n 10 nvidia-smi