无法使用 gfortran 打开模块文件

Can't open module file using gfortran

我正在使用 gfortran 运行 .F90 代码,但出现两个错误,

program fhello_world_mpi.F90
   1
Error: Invalid form of PROGRAM statement at (1)
fhello_world_mpi.F90:2:6:

   use mpi
       1
 Fatal Error: Can't open module file ‘mpi.mod’ for reading at (1): 
 No such file or directory
 compilation terminated.

我查看了mpi安装库(系统中存在mpich、openmpi库)

程序如下:

program fhello_world_mpi.F90
  use mpi
  implicit none
   integer ( kind = 4 ) error
   integer ( kind = 4 ) id
   integer p
   character(len=MPI_MAX_PROCESSOR_NAME) :: name
   integer clen
   integer, allocatable :: mype(:)
   real ( kind = 8 ) wtime

   call MPI_Init ( error )
   call MPI_Comm_size ( MPI_COMM_WORLD, p, error )
   call MPI_Comm_rank ( MPI_COMM_WORLD, id, error )
  if ( id == 0 ) then
     wtime = MPI_Wtime ( )

     write ( *, '(a)' ) ' '
     write ( *, '(a)' ) 'HELLO_MPI - Master process:'
     write ( *, '(a)' ) '  FORTRAN90/MPI version'
     write ( *, '(a)' ) ' '
     write ( *, '(a)' ) '  An MPI test program.'
     write ( *, '(a)' ) ' '
     write ( *, '(a,i8)' ) '  The number of processes is ', p
     write ( *, '(a)' ) ' '
  end if
  call MPI_GET_PROCESSOR_NAME(NAME, CLEN, ERROR)
  write ( *, '(a)' ) ' '
  write ( *, '(a,i8,a,a)' ) '  Process ', id, ' says "Hello, world!" ',name(1:clen)

  call MPI_Finalize ( error )
end program

更新1

删除句点解决了第一个问题。 我使用了这些命令:

mpif90 fhello_world_mpi.F90

mpirun -np 2 ./fhello_world_mpi

它给出了以下错误:

mpirun was unable to launch the specified application as it could not 
access or execute an executable:

Executable: ./fhello_world_mpi
Node: user

  while attempting to start process rank 0.

 2 total processes failed to start``

更新2 有效。 运行 命令:

mpif90 -o fhello_world_mpi fhello_world_mpi.F90

mpirun -np 2 ./fhello_world_mpi

输出

HELLO_MPI - Master process:
  FORTRAN90/MPI version

  An MPI test program.

  The number of processes is        2



   Process        1 says "Hello, world!" user
   Process        0 says "Hello, world!" user
  1. 更改程序的第一行以删除句点。

    program fhello_world_mpi
    

    Fortran 实体(例如程序、变量、常量、类型、)的名称中不允许使用句点 (.)。

  2. 尝试 mpif90 文件名.F90。 MPI 功能通常作为 "library" 实现,要编译代码,您需要提供额外信息:编译时 .mod 文件的位置和 lib*.so 文件的位置链接时的文件。这是通过编译器包装器 mpif90 实现的(通常,名称可能会有所不同):

    mpif90 -o fhello_world_mpi fhello_world_mpi.F90
    

    同样,要执行您需要包装器的代码:

    mpirun -np 2 ./fhello_world_mpi
    

    我假设文件名是 fhello_world_mpi.F90,根据需要更正。

一般来说,您不应尝试手动使用这些标志,但如果您希望看到它们,您可以使用 mpif90 -show。无论如何都需要 mpirun,因为它会初始化并行环境。