【Fortran】CUDA+PGI Fortran安装教程

本文提供了详细的CUDA及PGI Fortran安装步骤,包括验证系统环境、安装CUDA工具包、配置环境变量及测试等内容,并针对可能遇到的问题提供了解决方案。
部署运行你感兴趣的模型镜像

目录

0 前言

1 CUDA安装教程

1.1 安装前的操作

1.1.1 验证是否具有支持CUDA的GPU

1.1.2 验证是否具有受支持的Linux版本

1.1.3 验证系统是否已安装gcc

1.1.4 验证系统是否安装了正确的内核头文件和开发包

1.2 安装CUDA

1.2.1 关闭nouveau

1.2.2 安装NVIDIA驱动

1.2.3 安装CUDA

1.3 安装后设置

1.4 CUDA测试

2 PGI Fortran安装

2.0 下载地址

2.1 安装PGI

2.2 环境变量设置

 2.3 PGI测试

2.4 可能遇到的问题

2.4.1 gcc版本过高

参考


0 前言

教程以Ubuntu系统,显卡为GTX960为例。仅供参考。

1 CUDA安装教程

1.1 安装前的操作

1.1.1 验证是否具有支持CUDA的GPU

lspci | grep -i nvidia
# result :
01:00.0 VGA compatible controller: NVIDIA Corporation GM206 [GeForce GTX 960] (rev a1)
01:00.1 Audio device: NVIDIA Corporation Device 0fba (rev a1)

1.1.2 验证是否具有受支持的Linux版本

uname -m && cat /etc/*release
# result :
x86_64
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.6 LTS"
NAME="Ubuntu"
VERSION="16.04.6 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.6 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial

1.1.3 验证系统是否已安装gcc

gcc --version
# result :
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609
Copyright (C) 2015 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.

1.1.4 验证系统是否安装了正确的内核头文件和开发包

sudo apt-get install linux-headers-$(uname -r)
# result :
Reading package lists... Done
Building dependency tree       
Reading state information... Done
linux-headers-4.4.0-142-generic is already the newest version (4.4.0-142.168).
linux-headers-4.4.0-142-generic set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 191 not upgraded.

1.2 安装CUDA

1.2.1 关闭nouveau

# 关闭nouveau后需要重启
sudo reboot 
# 检查确保nouveau没有被加载
lsmod | grep nouveau

1.2.2 安装NVIDIA驱动

sudo sh NVIDIA-Linux-x86_64-450.80.02.run
# 查看驱动号
nvidia-smi

1.2.3 安装CUDA

CUDA工具包11.1下载地址:https://developer.nvidia.com/zh-cn/cuda-downloads

根据本机配置选择对应的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

1.3 安装后设置

环境变量配置:

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

1.4 CUDA测试

用cuda自带的测试例子测试cuda:

cd /usr/local/cuda/samples/1_Utilities/deviceQuery
make
./deviceQuery

结果(显卡的一些信息):

2 PGI Fortran安装

2.0 下载地址

https://www.pgroup.com/support/release_archive.php

2.1 安装PGI

tar -xzcf pgixxx.tar.gz
sudo ./install

根据提示进行下一步操作,完成安装。

2.2 环境变量设置

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

 2.3 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

结果:

2.4 可能遇到的问题

2.4.1 gcc版本过高

解决方法如下:(手动安装gcc4.4版本)

gcc下载地址:Index of /ubuntu/pool/universe/g/gcc-4.4

sudo dpkg -i gcc-4.4_4.4.7-8ubuntu1_amd64.deb
sudo dpkg -i g++-4.4_4.4.7-8ubuntu1_amd64.deb  libstdc++6-4.4-dev_4.4.7-8ubuntu1_amd64.deb
ls /usr/bin/gcc* -ll

#增加gcc4.4版本的选择
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.4 50
#切换到4.4版本
update-alternatives --config gcc
#确认当前版本
gcc --version

参考

1、NVIDIA CUDA Installation Guide for Linux Installation Guide Linux :: CUDA Toolkit Documentation

2、NVDIA官网-CUDA Linux安装指南 Installation Guide Linux :: CUDA Toolkit Documentation

3、PGI安装教程 Ubuntu16-PGI编译器(OpenACC)社区版安装教程

4、PGI-Fortran的使用提示 https://www.pgroup.com/resources/openacc_tips_fortran.htm

5、PGI官网安装指南和发行说明 https://www.pgroup.com/resources/docs/20.4/openpower/pgi-release-notes/index.htm

您可能感兴趣的与本文相关的镜像

PyTorch 2.5

PyTorch 2.5

PyTorch
Cuda

PyTorch 是一个开源的 Python 机器学习库,基于 Torch 库,底层由 C++ 实现,应用于人工智能领域,如计算机视觉和自然语言处理

### 安装Visual Studio并配置Fortran和GPU开发支持 在Windows系统上安装支持Fortran和GPU开发的Visual Studio,主要涉及以下几个关键步骤: 1. **安装Visual Studio** 下载并安装Visual Studio(建议使用2019或更新版本),在安装过程中选择“使用C++的桌面开发”工作负载。这将确保安装必要的C/C++编译器和开发工具。此外,还需要确保安装了“通用Windows平台(UWP)开发”组件,以便支持GPU开发所需的Windows SDK[^3]。 2. **配置CUDA Toolkit** 为了支持GPU开发,需要安装与显卡兼容的CUDA Toolkit版本。访问NVIDIA的[官方CUDA Toolkit下载页面](https://developer.nvidia.com/cuda-downloads),选择与系统匹配的版本进行安装安装完成后,需要将CUDA的bin目录添加到系统环境变量PATH中,以确保Visual Studio能够识别CUDA编译器和工具[^2]。 3. **安装PGI(Portland Group)编译器支持Fortran** Visual Studio本身并不直接支持Fortran语言,因此需要借助PGI编译器来实现Fortran开发。从PGI官网下载并安装支持CUDA Fortran的编译器版本。安装过程中,确保选择与CUDA Toolkit兼容的版本,并在安装后配置PGI编译器路径,以便在Visual Studio中使用[^5]。 4. **在Visual Studio中配置CUDAPGI编译器** 在Visual Studio中创建新项目后,需要手动配置CUDAPGI编译器支持。对于CUDA项目,可以通过“项目属性”->“配置属性”->“CUDA C/C++”->“设备”中设置目标GPU架构。对于Fortran项目,需要将PGI编译器路径添加到Visual Studio的工具选项中,并在项目属性中指定PGI作为默认编译器。 5. **测试环境配置** 创建一个简单的CUDA程序和Fortran程序,分别进行编译和运行测试。确保CUDA程序能够调用GPU资源,而Fortran程序能够正确编译并执行,以验证配置是否成功。例如,以下是一个简单的CUDA程序示例: ```cpp #include <stdio.h> __global__ void kernel() { printf("Hello from GPU!\n"); } int main() { kernel<<<1, 1>>>(); cudaDeviceReset(); return 0; } ``` 对于Fortran程序,可以使用PGI编译器编译一个简单的“Hello World”程序: ```fortran program hello print *, "Hello from Fortran!" end program hello ``` 确保这两个程序都能在Visual Studio中成功编译和运行[^3]。 6. **环境变量和路径设置** 为了确保Visual Studio能够正确识别CUDAPGI编译器,需要检查环境变量是否已正确配置。尤其是CUDA的`bin`和`libnvvp`目录以及PGI的`bin`目录,应包含在系统的PATH变量中。可以通过命令行工具(如`x64 Native Tools Command Prompt for VS 2019`)验证环境变量是否生效[^4]。 ---
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值