Using the Intel® MPI Library in a server/client setup


In some instances, it can be advantageous to have an MPI program join a job after it has started. Additional resources can be added to a long job as they become available, or a more traditional server/client program can be created. This can be facilitated with the MPI_Comm_accept and MPI_Comm_connect functions.

Key Functions

  • MPI_Open_port - Creates the port that is used for the communications. This port is given a name that is used to reference it later, both by the server and the client. Only the server program calls MPI_Open_port
  • MPI_Comm_accept - Uses the previously opened port to listen for a connecting MPI program. This is called by the server and will create an intercommunicator once it completes.
  • MPI_Comm_connect - Connects to another MPI program at the named port. This is called by the client and will create an intercommunicator once it completes.


  • The programs must use the same fabric in order to connect, as the port is dependent on the fabric.
  • The programs must be on the same operating system in order to connect. Different versions/distributions of the same operating systems could work, this has not been tested and is not supported.
  • The method of getting the port name from the server to the client can vary. In the sample provided, a text file is written containing the port name.


A very simple example is attached to this article. The server opens a port, writes the name of the port to a file, and waits for the client. The client will read the file and attempt to connect to the port. To verify that the two programs are connected, each sends a pre-defined value to the other. To compile and run the example, download the files and place them in the same folder. Open two terminals and navigate to the folder where the files are located.  In the first terminal, use:

mpiicpc server.cpp -o server
mpirun -n 1 ./server

And in the second terminal:

mpiicpc client.cpp -o client
mpirun -n 1 ./client

In Windows*, change mpirun to mpiexec. With the code as provided, the server should show:

Waiting for a client
A client has connected
The server sent the value: 25
The server received the value: 42

And the client should show:

Attempting to connect
Connected to the server
The client sent the value: 42
The client received the value: 25