MPI,fortran master和Python worker之间的相互通信?

时间:2016-02-14 20:53:28

标签: python fortran mpi spawn mpi4py

我想在Fortran程序(作为主程序)和python程序(作为从程序)之间与MPI通信。 我写了下面的测试,但是我无法从奴隶那里得到父通信。

 # Master(fortran)
   > my_id :                0
   > num_procs :            1
 # Slave(python)
   MPI initialiation :  True
   Has Parent        :  False

对此有任何帮助将不胜感激!

Fortran(主人)

program main
  include 'mpif.h'
  integer ierr, num_procs, my_id, INTERCOMM

  call MPI_INIT (ierr)

  !find out MY process ID, and how many processes were started.
  call MPI_COMM_RANK (MPI_COMM_WORLD, my_id, ierr)
  call MPI_COMM_SIZE (MPI_COMM_WORLD, num_procs, ierr)

  write(*,*) "# Master(fortran) "
  write(*,*) "  > my_id :     ", my_id
  write(*,*) "  > num_procs : ",num_procs

  if (ierr /= 0) then
    print*,"Erreur d'initialisation de MPI"
    stop
  endif

  if (my_id==0) then
    call MPI_COMM_SPAWN("python", "python_slave.py", 1, MPI_INFO_NULL, my_id, MPI_COMM_WORLD, &
    & INTERCOMM, MPI_ERRCODES_IGNORE,ierr)

    !--
    !Send input to the slave
    !Receive results from the slave
    !--

  endif

  call MPI_FINALIZE ( ierr )

  print *, ">> End of program"
end program main

Python(奴隶方)

from mpi4py import MPI

print " # Slave(python) "
print "   MPI initialiation : ", MPI.Is_initialized()
print "   Has Parent        : ", not(MPI.Comm.Get_parent() == MPI.COMM_NULL)

#--
#Receive some input from the master
#Do some work
#Send some results to the master
#--

#status = MPI.Status()  
#print "   > Status source : ", status.Get_source()    # ANY_SOURCE = -2
#print "   > Status tag    : ", status.Get_tag()       # ANY_TAG = -1
#print "   > Status count  : ", status.Get_count()
#
#pyworld = MPI.COMM_WORLD
#
#print "   Slave    "
#print "   > rank : ", pyworld.Get_rank()
#print "   > size : ", pyworld.Get_size()

1 个答案:

答案 0 :(得分:2)

问题是mpi4py(使用macports)默认使用'mpich'编译而不是'openmpi'

必须确保每一方都使用相同的mpi分布。 在我的情况下,由于我的gcc发行版是使用openmpi编译的,我需要使用以下命令安装mpi4py:

  

sudo port install mpi4py + openmpi