Is it possible to send data from a Fortran program to Python using MPI?

后端 未结 4 717
不知归路
不知归路 2021-01-11 11:42

I am working on a tool to model wave energy converters, where I need to couple two software packages to each other. One program is written in Fortran, the other one in C++.

4条回答
  •  一生所求
    2021-01-11 12:08

    If you are to start both the Fortran program and the Python one in the same MPI job, you have to use something like:

    mpiexec -n 1 fortran_program : -n 1 python main.py
    

    The Fortran program will become MPI rank 0 and the Python program will be MPI rank 1. You can also start more than one of each executables, for example:

    mpiexec -n 2 fortran_program : -n 4 python main.py
    

    Ranks 0 and 1 will be from the Fortran program, ranks 2 to 5 - from the Python one.

    Also note that comm.recv() and the other communication methods in mpi4py that start with small letters (comm.send(), comm.irecv(), etc.) use Pickle under the hood and actually operate with serialised Python objects. This is not compatible with the character array sent by the Fortran code. You have to use the communication methods that start with capital letter (comm.Send(), comm.Recv(), etc.) that operate on NumPy arrays and receive explicit type information. Unfortunately, my Python fu is weak and I cannot provide a complete working example right now, but the MPI part should be something like this (unverified code):

    # Create an MPI status object
    status = MPI.Status()
    # Wait for a message without receiving it
    comm.Probe(source=0, tag=22, status=status)
    # Check the length of the message
    nchars = status.Get_count(MPI.CHARACTER)
    # Allocate a big enough data array of characters
    data = np.empty(nchars, dtype='S')
    # Receive the message
    comm.Recv([data, MPI.CHARACTER], source=0, tag=22)
    # Construct somehow the string out of the individual chars in "data"
    

    In the Fortran code you have to specify a destination rank of 1 (in the case you are running one Fortran executable and one Python one).

提交回复
热议问题