1

我想在 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()
4

1 回答 1

2

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

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

sudo 端口安装 mpi4py +openmpi

于 2016-02-15T11:33:57.553 回答