语言, Fortran专题

在Linux系统下Fortran和MKL环境的安装和设置

在Windows系统下Fortran和MKL环境的安装和设置,参考博文:Visual Studio和Fortran的下载以及设置MKL环境。Linux系统的安装可参考:Ubuntu系统的安装

一、下载和安装

在Linux系统下,同样需要安装Intel oneAPI(Base Toolkit和HPC Toolkit的Linux版本):https://www.intel.com/content/www/us/en/developer/tools/oneapi/toolkits.html。Intel oneAPI的组件选择可参考:Visual Studio和Fortran的下载以及设置MKL环境

这里不需要安装Visual Studio,也不支持。本篇不选择图形界面下的其他IDE,仅在终端界面下安装和使用。

在Linux系统下,安装包需要通过sh命令安装,以下为官网的说明:

Use $ sudo sh ./<installer>.sh to launch the GUI Installer as the root.Optionally, use $ sh ./<installer>.sh to launch the GUI Installer as the current user.

如果是系统管理员,推荐使用sudo sh来安装。

安装后可以查看是否存在ifort的命令:

ifort -V

大概率是提示没有发现,即没有在系统的环境中。ifort的命令大概在这里:/opt/intel/oneapi/compiler/2022.0.2/linux/bin/intel64。

可通过以下命令查看安装的版本:

/opt/intel/oneapi/compiler/2022.0.2/linux/bin/intel64/ifort -V

通过以下命令完成ifort的安装(方法来自于官方的说明:https://www.intel.com/content/www/us/en/develop/documentation/fortran-compiler-oneapi-dev-guide-and-reference/top/compiler-setup/using-the-command-line/specifying-the-location-of-compiler-components.html):

source /opt/intel/oneapi/setvars.sh intel64

查看是否完成ifort的安装:

ifort -V

但该方法在系统重启后就失效了,需要每次启动时运行一次。

以下提供两种方法(说明:这两种方法好像会出问题,有一些库可能会链接不上,可以尝试。但仍然推荐使用上面的source方法,只是每次开机的时候记得运行一次,否则会找不到ifort环境)。

(1)方法一:修改.bashrc文件(仅对当前用户有效)

打开

vim ~/.bashrc 

在最底部添加:

export PATH="/opt/intel/oneapi/compiler/2022.0.2/linux/bin/intel64:$PATH"

关闭终端,重新打开。

(2)方法二:进入修改/etc/environment(对所有用户有效)

打开

sudo vim /etc/environment

在最后的引号前面添加:

:/opt/intel/oneapi/compiler/2022.0.2/linux/bin/intel64/

系统重启。

参考资料:Linux PATH 作用以及查看和修改方法

二、测试

以下运行时可能还会提示安装gcc:

sudo apt install gcc

查看gcc版本:

gcc --version

书写最简单的fortran文件(文件名为a.f90):

program main
implicit none 
write(*,*) 'hello world' 
end program

运行该文件:

ifort a.f90

得到a.out文件,通过命令 ./a.out,得到

hello world

三、使用MKL

如果要连接MKL库,可通过makefile文件来完成。一个makfile的例子(文件为makefile,无后缀),仅作参考:

INC := -I//opt/intel/oneapi/mkl/2022.0.2/include/intel64/lp64
LIB := -L/opt/intel/oneapi/mkl/2022.0.2/lib/intel64
src:=a
exec:=a
all: $(src).f90
	ifort $(src).f90 $(INC) $(LIB) -lmkl_intel_lp64 -Wl,--start-group -lmkl_intel_thread -lmkl_lapack95_lp64 -lmkl_core -lmkl_blas95_lp64  -Wl,--end-group -liomp5 -lpthread -O2 -o $(exec).exe

新建使用MKL库的.f90文件:a.f90。例子可参考:Fortran常用语句

在终端运行以下命令:

make

得到a.exe文件。通过命令 ./a.exe 运行该文件,得到计算结果。

makefile文件中详细参数以及MPI功能的设置等,这里暂时略去说明。感兴趣的可阅读官方的资料:

2,180 次浏览

【说明:本站主要是个人的一些笔记和代码分享,内容可能会不定期修改。为了使全网显示的始终是最新版本,这里的文章未经同意请勿转载。引用请注明出处:https://www.guanjihuan.com

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Captcha Code