Gud*_*ddu 0 fortran fortran77 openmpi
我是MPI和Fortran 77菜鸟.我有一个fortran 77代码FKRPRO.f
,我想使用OpenMPI并行化.该代码需要大量参数,这些参数在运行时从单独的文件中输入.编译和运行就是这样的
gfortran -o FKRPRO FKRPRO.f
./FKRPRO < Modelfile.txt
Run Code Online (Sandbox Code Playgroud)
代码中的等价行(不是我的代码)是
PARAMETER(LIN=5)
INTEGER ERROR
LOGICAL PRNTA
PRNTA=.FALSE.
READ(LIN,'(L3)') PRNTA
READ(LIN,21) M1,M2
21 FORMAT(11I5)
Run Code Online (Sandbox Code Playgroud)
等等.有人可以向我解释一下是什么READ(LIN,'(L3)') PRNTA
意思.输入文件Modelfile.txt中的输入是这样的
.F.
0 64
and so on..
Run Code Online (Sandbox Code Playgroud)
我在代码中放了必要的MPI语句.
INCLUDE 'MPIF.H'
...
CALL MPI_INIT(ERROR)
CALL MPI_COMM_SIZE(MPI_COMM_WORLD,NPROCS,ERROR)
CALL MPI_COMM_RANK(MPI_COMM_WORLD,PRANK,ERROR)
...
CALL MPI_TYPE_FREE(NEWMATRIX,ERROR)
CALL MPI_FINALIZE(ERROR)
Run Code Online (Sandbox Code Playgroud)
所有进程都无法读取输入文件.我编译并运行这样的代码
mpif77 -o bc3 FKRPROG5.f
mpirun -np 4 bc3 < Modelfile.txt
Run Code Online (Sandbox Code Playgroud)
这不起作用.我收到以下错误.只有第一个进程或等级0才能读取该文件.
At line 50 of file FKRPROG5.f (unit = 5, file = 'stdin')
Fortran runtime error: End of file
At line 50 of file FKRPROG5.f (unit = 5, file = 'stdin')
Fortran runtime error: End of file
At line 50 of file FKRPROG5.f (unit = 5, file = 'stdin')
Fortran runtime error: End of file
mpirun has exited due to process rank 3 with PID 866 on
node Avinash-rMBP.local exiting improperly. There are two reasons this could occur:
1. this process did not call "init" before exiting, but others in
the job did. This can cause a job to hang indefinitely while it waits
for all processes to call "init". By rule, if one process calls "init",
then ALL processes must call "init" prior to termination.
2. this process called "init", but exited without calling "finalize".
By rule, all processes that call "init" MUST call "finalize" prior to
exiting or it will be considered an "abnormal termination"
This may have caused other processes in the application to be
terminated by signals sent by mpirun (as reported here).
Run Code Online (Sandbox Code Playgroud)
第50行是.READ(LIN,'(L3)') PRNTA
有人指出我出错的地方:(那么,我怎么能从这个输入文件中读取所有进程<Modelfile.txt ??谢谢.
该声明
READ(LIN,'(L3)') PRNTA
Run Code Online (Sandbox Code Playgroud)
使程序从附加到具有id的通道的单元读取LIN
表示逻辑值的3字符序列,并将读取的值分配给变量PRNTA
.从您向我们展示的片段中,程序将读取.F.
并设置PRNTA
为.false.
.
LIN
设置为常数值5,这通常意味着stdin
.这种5
用来表示stdin
不是法律上的标准,它更像是事实上的标准.
将参数文件读入MPI程序的直接方法是确保只有一个进程读取文件,然后将值发送给需要它们的其他进程.
您似乎编写了一个程序,其中所有进程都尝试读取相同的输入文件,但是在运行时,您过去使用的重定向Modelfile.txt
仅适用于一个进程(可能是排名为0的进程).其他进程根本没有得到输入文件并且正在抱怨,然后使程序崩溃.您显示的错误消息是典型的Fortran程序,它在尝试读取时根本找不到输入文件.
编写代码更好:
call mpi_init ...
...
if (myrank==0) then
open(...) inputfile
read(...) parameters
close(...)
end if
...
call mpi_bcast(parameters from 0 to all)
...
Run Code Online (Sandbox Code Playgroud)
通常,不要指望MPI进程的运行时环境是顺序程序的运行时环境的相同副本.我认为您已经看到证据表明您的运行时仅将输入定向到程序运行时创建的第一个进程.既然mpirun
没有标准化(虽然mpiexec
是),我认为你不能依赖于所有MPI实现的运行时行为.为了便于携带和兼容,您最好在程序中显式处理I/O,而不是使用重定向等o/s功能.
您可以,而不是让进程0读取参数并将它们分发给其他进程,编写代码使每个进程读取相同的文件.如果您以这种方式编写代码,请注意确保进程不会争夺对I/O通道的访问权限; 让多个进程尝试(几乎)同时读取单个输入通道是减慢速度的可靠方法.