Fortran程序并行和GDB调试


Fortran程序并行和GDB调试

本机器系统为Ubuntu18.04LTS,编译器安装了intel的oneAPI

Fortran+MPI实现并行

MPI是一个适用于C/C++/Fortran77/Fortran90的并行库,初学者只需学会4个基本函数即可上手使用,需要进程通信时还需要学两个。

c     filename: hello_world.f
c     parallel program
      program main
      use mpi           !for Fortran90 use ifort complie
      !INCLUDE "mpif.h" !for Fortran77 
      implicit none
      integer*4::ierr,my_id,num_procs
      call MPI_INIT ( ierr )
      call MPI_COMM_RANK (MPI_COMM_WORLD, my_id, ierr)     !my_id     ==> NO. this process
      call MPI_COMM_SIZE (MPI_COMM_WORLD, num_procs, ierr) !num_procs ==> number of process
      write(*,'('Hello World',1x,i2,a,i2)') my_id,'/',num_procs
      call MPI_FINALIZE ( ierr )
      end program 

并行程序的编译

fortran编译器有对应的mpi编译命令
对于intel的oneAPI(即ifort编译器)

#compile
mpiifort hello_world.f -o z.out 
#run
mpirun -np 3 ./z.out

对于gcc

#compile
mpif90 hello_world.f -o z.out 
#run
mpirun -np 3 ./z.out

GDB调试

首先安装xterm终端模拟器

sudo apt install xterm -y

进行GDB调试

#compile
mpif90 -g hello_world.f -o z.out 
#run
mpirun -np 3 xterm -e gdb ./z.out

接下来会显示三个xterm模拟终端,分别表示这三个并行的进程,在每一个gdb终端里输入‘r’即可开启子进程