Xbel
Xbel

Reputation: 787

Closing connection when calling mpi multiple times

I'm using mpi4py inside a function. This function can be called multiple times, so I do comm = MPI.COMM_WORLD at the beginning of the function. Inside I use Scatter and Gather, and I don't know if I should somehow "close the connection" after each call. Let's use the following example:

def test():
    comm = MPI.COMM_WORLD

    if comm.rank == 0:  # Start with the node 0 having the whole data
        print("Master process.")

    else:  # Other nodes has no data ("for now")
        print("Slave process.")


    # comm.Free()
    # comm.Disconnect()
    # MPI.Finalize()

def call_test_twice():
    test()
    test()

if __name__ == "__main__":
    call_test_twice()

Then, the question is, in this code, should any of the commented commands at the end of test() be used?

Upvotes: 0

Views: 395

Answers (1)

Hristo Iliev
Hristo Iliev

Reputation: 74455

MPI.COMM_WORLD is the predefined world communicator. It is created by the MPI environment when the program starts and persists until the very end of the program execution. You should never meddle with the world communicator or bad things will happen. Meddling includes freeing or disconnecting it.

Calling MPI.Finalize() finalises the MPI environment and the process can no longer participate in any further communication, therefore you won't be able to call test() again (provided that the rank property of the world communicator is not caching). mpi4py automatically finalises MPI when the script exits.

The answer is simple - you don't have to do anything as long as you are using MPI.COMM_WORLD.

Upvotes: 1

Related Questions