TypechoJoeTheme

MetMan's Blog

网站页面

MPI是一个库

MetMan博 主
2023-11-13
/
0 评论
/
136 阅读
/
418 个字
/
百度已收录
11/13
本文最后更新于 2023年11月13日,已超过 311天没有更新。如果文章内容或图片资源失效,请留言反馈,我会及时处理,谢谢!

MPI是消息通信标准,其实现是一个第三方库(library),但我们用比如Intel MPI的mpiifort命令编译链接源代码,这给人一种错觉就是MPI是一门编程语言,mpiifort是一个编译器。

下面我们通过对mpiifort深入分析来说明MPI实际上是一个第三库的事实

mpiifort是shell脚本

Intel MPI的编译链接命令mpiifort实际上是一个封装的shell脚本,用于封装查找mpi头文件/模块文件和链接相应库文件细节。

$ file `which mpiifort`
/opt/compiler/intel/2018_update1/compilers_and_libraries_2018.1.163/linux/mpi/intel64/bin/mpiifort: POSIX shell script, ASCII text executable

打开这个shell脚本阅读脚本代码,脚本前面主要是环境变量、命令行参数处理部分,其最后核心部分是编译、链接处理,具体如下:

if [ "$linking" = yes ] ; then
    cmd_line="$FC $FCFLAGS $allargs $FCMODDIRS $FCINCDIRS -L${libdir}${MPILIBDIR} -L$libdir $rpath_opt $mpilibs $I_MPI_OTHERLIBS $LDFLAGS $MPI_OTHERLIBS"
    if [ "$Show" = echo ] ; then
        echo $cmd_line
    else
        eval `echo $cmd_line`
    fi
    rc=$?
    if [ $rc -eq 0 -a "x$strip_debug_info" = "xyes" ] ; then
    $Show objcopy --only-keep-debug ${executable} ${executable}.dbg
    $Show objcopy --strip-debug ${executable}
    $Show objcopy --add-gnu-debuglink=${executable}.dbg ${executable}
    fi
else
    cmd_line="$FC $FCFLAGS $allargs $FCMODDIRS $FCINCDIRS"
    if [ "$Show" = echo ] ; then
        echo $cmd_line
    else
        eval `echo $cmd_line`
    fi
    rc=$?
fi

其中$linking确定当前是链接阶段还是其它编译阶段。

编译命令

如果是在编译阶段,主要命令是

$FC $FCFLAGS $allargs $FCMODDIRS $FCINCDIRS

变量值如下:

  • FC=ifort 默认使用ifort基础编译器
  • FCFLAGS=
  • allargs= 用户指定编译选项
  • FCMODDIRS=-I/impi/intel64/include
  • FCINDIRS=-I/impi/intel64/include

我们可以使用-show选项来查看实际调用的命令参数

$ mpiifort -show -c sum.f90
ifort -c 'sum.f90' -I/opt/compiler/intel/2018_update1/compilers_and_libraries_2018.1.163/linux/mpi/intel64/include -I/opt/compiler/intel/2018_update1/compilers_and_libraries_2018.1.163/linux/mpi/intel64/include

链接命令

如果在链接阶段,主要命令如下

$FC $FCFLAGS $allargs $FCMODDIRS $FCINCDIRS -L${libdir}${MPILIBDIR} -L$libdir $rpath_opt $mpilibs $I_MPI_OTHERLIBS $LDFLAGS $MPI_OTHERLIBS

使用-show选项来查看实际调用的命令参数

$ mpiifort -show -o main.exe main.o sum.o
ifort -o 'main.exe' 'main.o' 'sum.o' -I/opt/compiler/intel/2018_update1/compilers_and_libraries_2018.1.163/linux/mpi/intel64/include -I/opt/compiler/intel/2018_update1/compilers_and_libraries_2018.1.163/linux/mpi/intel64/include -L/opt/compiler/intel/2018_update1/compilers_and_libraries_2018.1.163/linux/mpi/intel64/lib/release_mt -L/opt/compiler/intel/2018_update1/compilers_and_libraries_2018.1.163/linux/mpi/intel64/lib -Xlinker --enable-new-dtags -Xlinker -rpath -Xlinker /opt/compiler/intel/2018_update1/compilers_and_libraries_2018.1.163/linux/mpi/intel64/lib/release_mt -Xlinker -rpath -Xlinker /opt/compiler/intel/2018_update1/compilers_and_libraries_2018.1.163/linux/mpi/intel64/lib -Xlinker -rpath -Xlinker /opt/intel/mpi-rt/2017.0.0/intel64/lib/release_mt -Xlinker -rpath -Xlinker /opt/intel/mpi-rt/2017.0.0/intel64/lib -lmpifort -lmpi -lmpigi -ldl -lrt -lpthread

包含了链接时需要搜索的Intel MPI库路径及链接库。

与mpif90命令区别

命令mpif90也是一个shell脚本,不同的是其默认的FC=gfortran,可通过-fc=<compiler>或者-f90=<compiler>重置基础编译器。

$ mpif90 -f90=ifort ...

各家MPI实现命令

Intel MPI

OpenMPI

OpenMPI团队建议使用mpifort代替mpif77mpif90,因其更加通用,支持Fortran多种语法标准。

MPICH / MVAPICH

LanguageWrapper compiler name
Cmpicc
C++mpicxx, mpic++
Fortranmpifort,mpif77,mpif90

其中mpic++是mpicxx软链接,mpif77和mpif90是mpiifort软链接。

建议

尽量使用厂商提供的封装好的MPI编译器,而不是使用“基础编译器+MPI库链接”方式编译程序。

参考资料

  1. https://www.intel.com/content/www/us/en/docs/mpi-library/developer-reference-linux/2021-8/compiler-commands.html
  2. https://www.open-mpi.org/faq/?category=mpi-apps
intelmpiparallel
朗读
赞(0)
赞赏
感谢您的支持,我会继续努力哒!
版权属于:

MetMan's Blog

本文链接:

https://blog.metman.top/index.php/archives/74/(转载时请注明本文出处及文章链接)

评论 (0)

互动读者

标签云

最新回复

暂无回复

登录
X
用户名
密码