Fortran 阻止执行,直到通过MPI_Comm_spawn调用的子项完成,否则无法访问子项
我想从main.f90代码生成child.exe,但我没有修改child.f90的权限。简化的代码如下所示: main.f90:Fortran 阻止执行,直到通过MPI_Comm_spawn调用的子项完成,否则无法访问子项,fortran,mpi,spawn,Fortran,Mpi,Spawn,我想从main.f90代码生成child.exe,但我没有修改child.f90的权限。简化的代码如下所示: main.f90: program main implicit none include 'mpif.h' integer, parameter:: root = 0 integer, dimension(:), allocatable:: errCodes, status integer:: numprocs, request, ierr, taskid,
program main
implicit none
include 'mpif.h'
integer, parameter:: root = 0
integer, dimension(:), allocatable:: errCodes, status
integer:: numprocs, request, ierr, taskid, INTERCOMM
character*(*), PARAMETER:: cmd = "./child.exe"
!
call mpi_init(ierr)
call mpi_comm_rank(mpi_comm_world, taskid, ierr)
call mpi_comm_size(mpi_comm_world, numprocs, ierr )
call mpi_barrier(mpi_comm_world, ierr)
!
allocate( errCodes(numprocs), status(numprocs))
print*, "from parent, CPU: ", taskid
call MPI_COMM_SPAWN(cmd, MPI_ARGV_NULL, 2, MPI_INFO_NULL, root, MPI_COMM_WORLD,INTERCOMM, errCodes, ierr)
!
call mpi_barrier(mpi_comm_world, ierr)
!
print*, "After children, from parent CPU", taskid
call mpi_finalize(ierr)
end program main
program child
implicit none
include 'mpif.h'
integer:: numprocs, temp, ierr, arg_count, taskid, maxproces, errCodes(2)
!
call mpi_init(ierr)
call mpi_comm_rank(mpi_comm_world, taskid, ierr)
call mpi_comm_size(mpi_comm_world, numprocs, ierr )
!
call sleep(1)
!
print*, "from child, CPU: ", taskid
call mpi_finalize(ierr)
end program child
child.f90:
program main
implicit none
include 'mpif.h'
integer, parameter:: root = 0
integer, dimension(:), allocatable:: errCodes, status
integer:: numprocs, request, ierr, taskid, INTERCOMM
character*(*), PARAMETER:: cmd = "./child.exe"
!
call mpi_init(ierr)
call mpi_comm_rank(mpi_comm_world, taskid, ierr)
call mpi_comm_size(mpi_comm_world, numprocs, ierr )
call mpi_barrier(mpi_comm_world, ierr)
!
allocate( errCodes(numprocs), status(numprocs))
print*, "from parent, CPU: ", taskid
call MPI_COMM_SPAWN(cmd, MPI_ARGV_NULL, 2, MPI_INFO_NULL, root, MPI_COMM_WORLD,INTERCOMM, errCodes, ierr)
!
call mpi_barrier(mpi_comm_world, ierr)
!
print*, "After children, from parent CPU", taskid
call mpi_finalize(ierr)
end program main
program child
implicit none
include 'mpif.h'
integer:: numprocs, temp, ierr, arg_count, taskid, maxproces, errCodes(2)
!
call mpi_init(ierr)
call mpi_comm_rank(mpi_comm_world, taskid, ierr)
call mpi_comm_size(mpi_comm_world, numprocs, ierr )
!
call sleep(1)
!
print*, "from child, CPU: ", taskid
call mpi_finalize(ierr)
end program child
我的问题与类似,但如果有人能澄清,如果我无法接触到孩子,该如何处理,我将不胜感激。我曾尝试使用mpi_屏障,但没有任何帮助。对我来说,Hristo从链接答案中得到的建议效果很好,只需密切遵循即可。注意通讯器(
intercom
,parent
)。最后仍然存在一些问题,我以前从未使用过子进程,但它应该为您指出正确的方向
program main
use mpi
implicit none
integer, parameter:: root = 0
integer, dimension(:), allocatable:: errCodes, status
integer:: numprocs, request, ierr, taskid, INTERCOMM
character*(*), PARAMETER:: cmd = "./child.exe"
!
call mpi_init(ierr)
call mpi_comm_rank(mpi_comm_world, taskid, ierr)
call mpi_comm_size(mpi_comm_world, numprocs, ierr )
call mpi_barrier(mpi_comm_world, ierr)
!
allocate( errCodes(numprocs), status(numprocs))
print*, "from parent, CPU: ", taskid
call MPI_COMM_SPAWN(cmd, MPI_ARGV_NULL, 1, MPI_INFO_NULL, root, MPI_COMM_WORLD, INTERCOMM, errCodes, ierr)
!
call mpi_barrier(INTERCOMM, ierr)
!
call mpi_barrier(INTERCOMM, ierr)
print*, "After children, from parent CPU", taskid
call mpi_finalize(ierr)
end program main
program child
use mpi
implicit none
integer:: numprocs, temp, ierr, arg_count, taskid, maxproces, errCodes(2), parent
!
call mpi_init(ierr)
call mpi_Comm_get_parent(parent, ierr)
call mpi_comm_rank(parent, taskid, ierr)
call mpi_comm_size(parent, numprocs, ierr )
!
call sleep(5)
call mpi_barrier(parent, ierr)
!
print*, "from child, CPU: ", taskid
call sleep(1)
call mpi_barrier(parent, ierr)
! call sleep(2)
call mpi_finalize(ierr)
end program child
什么意思:没有帮助“?顺便说一句,include
mpif.h
更多的是为了与FORTRAN 77兼容,我建议在FORTRAN 90及更新版本中使用mpi
。我添加了一个mpi_屏障,以便我可以在子级打印消息之后从主级查看打印消息。实际上,主程序并没有等待孩子完成它的执行。看到答案了吗,你的通讯器错了。我猜它有问题。当我使用“mpirun-np 2 a.out”运行它时,它会在调用子项之前继续打印main.f90中的第一条消息,而不会在生成后打印child.f90中的消息或main.f90中的消息。为什么要使用-np 2
运行它?它实际上对我来说甚至适用于-np 2
,但我不能说它在任何地方都适用。当然,它会打印第一行两次。为了能够设置我想要使用的CPU数量。即使我使用.out来运行main.f90生成的可执行文件,屏幕上也会显示大量“来自父级,CPU:0”。这是一个测试代码,使用多个处理器调用它可能没有意义。