Ubuntu安装CUDA以GPU运行Fortran
1. 检查
验证是否具有支持CUDA的GPU:
$ lspci | grep -i nvidia
01:00.0 VGA compatible controller: NVIDIA Corporation GP107GL [Quadro P620] (rev a1)
01:00.1 Audio device: NVIDIA Corporation GP107GL High Definition Audio Controller (rev a1)
验证是否具有受支持的Linux版本:
$ uname -m && cat /etc/*release
x86_64
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=20.04
DISTRIB_CODENAME=focal
DISTRIB_DESCRIPTION="Ubuntu 20.04.2 LTS"
NAME="Ubuntu"
VERSION="20.04.2 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.2 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
验证系统是否已安装gcc:
$ gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
验证系统是否安装了正确的内核头文件和开发包:
$ sudo apt-get install linux-headers-$(uname -r)
Reading package lists... Done
Building dependency tree
Reading state information... Done
linux-headers-5.8.0-59-generic is already the newest version (5.8.0-59.66~20.04.1).
linux-headers-5.8.0-59-generic set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 5 not upgraded.
2. 关闭nouveau
关闭nouveau:在/etc/modprobe.d/blacklist-nouveau.conf
(用sudo vi)加入以下内容:
blacklist nouveau
options nouveau modeset=0
并更新kernel:
$ sudo update-initramfs -u
update-initramfs: Generating /boot/initrd.img-5.8.0-59-generic
# 关闭nouveau后需要重启
sudo reboot
# 检查确保nouveau没有被加载
lsmod | grep nouveau
3. Ubuntu20.04LTS安装NVIDIA驱动
$ sudo apt update
$ sudo apt upgrade
$ ubuntu-drivers devices
WARNING:root:_pkg_get_support nvidia-driver-390: package has invalid Support Legacyheader, cannot determine support level
== /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0 ==
modalias : pci:v000010DEd00001CB6sv00001028sd00001264bc03sc00i00
vendor : NVIDIA Corporation
model : GP107GL [Quadro P620]
driver : nvidia-driver-450-server - distro non-free
driver : nvidia-driver-390 - distro non-free
driver : nvidia-driver-418-server - distro non-free
driver : nvidia-driver-465 - distro non-free
driver : nvidia-driver-460-server - distro non-free
driver : nvidia-driver-460 - distro non-free recommended
driver : xserver-xorg-video-nouveau - distro free builtin
在recommended的一行的数字460:
sudo apt install nvidia-driver-460
4. 安装CUDA
高性能计算 (HPC) SDK | NVIDIA选择对应的CUDA工具包,并安装:
# 下载CUDA工具包
wget https://developer.download.nvidia.com/compute/cuda/11.1.0/local_installers/cuda_11.1.0_455.23.05_linux.run
# 安装CUDA
sudo sh cuda_11.1.0_455.23.05_linux.run
CUDA FORTRAN | NVIDIA Developer
5. 环境变量设置
export PATH=/usr/local/cuda-11.1/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-11.2/lib\
${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
# 检查激活状态
systemctl status nvidia-persistenced
# 激活
sudo systemctl enable nvidia-persistenced
6. CUDA测试
cd /usr/local/cuda/samples/1_Utilities/deviceQuery
make
./deviceQuery
7. PGI安装和环境变量设置
tar -xzcf pgixxx.tar.gz
sudo ./install
export PGI=/opt/pgi
MANPATH=$MANPATH:$PGI/linux86-64/13.3/man
export PATH=$PGI/linux86-64/13.3/bin:$PATH
export LD_LIBRARY_PATH=/usr/lib:$PGI/linux86-64/13.3/lib:$LD_LIBRARY_PATH
8. PGI测试
b.f90
module b_m
integer , device :: b_d
end module b_m
a.f90
module a_m
integer,device :: a_d
contains
attributes(global) subroutine aPlusB()
use b_m
implicit none
a_d=a_d+b_d
end subroutine aPlusB
end module a_m
aPlusB.f90
program twoPlusThree
use a_m
use b_m
implicit none
integer :: a
a_d=2
b_d=3
call aPlusB <<<1,1>>>()
a=a_d
write(*,"('2+3=',i0)") a
end program twoPlusThree
编译:
pgf90 -Mcuda -c b.f90
pgf90 -Mcuda -c a.f90
pgf90 -Mcuda aPlusB.f90 a.o b.o
./a.out
#or
#pgf90 -c b.cuf
#pgf90 -c a.cuf
#pgf90 aPlusB.cuf a.o b.o
#./a.out