置顶说明:最新版本(2025 年及之后)的 Intel oneAPI HPC Toolkit 中好像没有 ifort 命令,只有 ifx 命令,可以试着直接用 ifx 代替 ifort。消息参考:
- https://www.intel.com/content/www/us/en/developer/articles/release-notes/fortran-compiler/2025.html
- https://www.intel.com/content/www/us/en/developer/articles/release-notes/fortran-compiler/2024.html
Intel oneAPI HPC Toolkit 2024.2.0 版本是支持 ifort 的最后一个版本,如果需要支持 ifort,可以在别处寻找 2024 年及之前的 Intel oneAPI 版本。由于在 2024 年的后续版本好像已经删掉 ifort 命令了,所以 2024 年的版本不一定有 ifort。如果需要有 ifort 命令,这里推荐 2023 年的 Intel oneAPI 版本,下载链接可以参考(可以尝试是否有效):
wget https://registrationcenter-download.intel.com/akdlm/IRC_NAS/992857b9-624c-45de-9701-f6445d845359/l_BaseKit_p_2023.2.0.49397_offline.sh
wget https://registrationcenter-download.intel.com/akdlm/IRC_NAS/0722521a-34b5-4c41-af3f-d5d14e88248d/l_HPCKit_p_2023.2.0.49440_offline.sh
一、下载和安装
在 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 的命令大概在这里(这里以 2022 年版本为例):/opt/intel/oneapi/compiler/2022.0.2/linux/bin/intel64。如果是用户安装,ifort 路径大概在这里(这里以 2023 年版本为例):~/intel/oneapi/compiler/2023.2.0/linux/bin/intel64/
通过以下命令完成 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
但该方法在系统重启后就失效了,需要每次启动时运行一次。这可以通过在 .bashrc 文件中添加 source 来解决:
echo "source /opt/intel/oneapi/setvars.sh intel64" >> ~/.bashrc
这里另外提供两种环境变量的设置方法(说明:这两种方法好像会出问题,有一些库可能会链接不上,但可以尝试。这里仍然推荐使用上面的 source 方法,只是每次开机的时候记得运行一次,否则会找不到 ifort 环境):点击展开
二、简单测试
以下运行时可能还会提示安装 gcc:
sudo apt install gcc
查看 gcc 版本:
gcc --version
书写最简单的 fortran 文件(文件名为 a.f90):
program main
implicit none
write(*,*) 'hello world'
end program
运行该文件:
ifort a.f90
或者
ifort -o a.exe a.f90
得到 a.out 或 a.exe 文件,通过命令 ./a.out 或 ./a.exe,得到
hello world
三、使用MKL
如果要连接 MKL 库,可通过 makefile 文件来完成, 用于编译 fortran 文件。
以下提供的例子,例子只对该软件路径有效。文件为 makefile,无后缀。
makfile 文件的例子(2023 年版本,用户目录):
INC := -I/home/jhguan/intel/oneapi/mkl/2023.2.0/include/intel64/lp64
LIB := -L/home/jhguan/intel/oneapi/mkl/2023.2.0/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
makfile 文件的例子(2022 年版本,opt 目录):
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
makfile 文件的例子(2019 年版本,opt 目录):
INC := -I/opt/intel2019/mkl/include/intel64/lp64
LIB := -L/opt/intel2019/mkl/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
makefile 文件的例子(2015 年版本,/public/software 目录):
INC := -I/public/software/compiler/intel/composer_xe_2015.2.164/mkl/include/intel64/lp64
LIB := -L/public/software/compiler/intel/composer_xe_2015.2.164/mkl/lib/intel64
mpi:= -I/public/software/mpi/mpich/3.1.4/intel/include /public/software/mpi/mpich/3.1.4/intel/lib -lmpich -limf -lsvml -lintlc
src:=Console1
exec:=a
all: $(src).f90
ifort $(src).f90 $(INC) $(LIB) -openmp -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 功能的设置等,这里暂时略去说明。感兴趣的可阅读官方的资料:
- https://www.intel.com/content/www/us/en/develop/documentation/fortran-compiler-oneapi-dev-guide-and-reference/top.html
- https://www.intel.com/content/www/us/en/developer/tools/oneapi/onemkl-link-line-advisor.html
【说明:本站主要是个人的一些笔记和代码分享,内容可能会不定期修改。为了使全网显示的始终是最新版本,这里的文章未经同意请勿转载。引用请注明出处:https://www.guanjihuan.com】