阻止执行,直到通过MPI_Comm_spawn调用的子项完成,无法访问子项

时间:2014-10-24 20:57:19

标签: 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, 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

child.f90:

 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

我的问题类似于this one in C++,但如果有人可以澄清如果我无法接触孩子的话,我会很感激。我曾尝试使用mpi_barrier,但没有用。

1 个答案:

答案 0 :(得分:1)

对我而言,Hristo对相关答案的建议非常有效,请密切关注。注意沟通者(INTERCOMMparent)。最后仍然存在一些问题,我以前从未使用过子进程,但它应该指向正确的方向。

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