12.9.6. Client/Server Examples

PreviousUpNext
Up: Establishing Communication Next: Other Functionality Previous: Reserved Key Values


Example Printing Port Name Example---Completely Portable.

The following example shows the simplest way to use the client/server interface. It does not use service names at all.

On the server side:

char myport[MPI_MAX_PORT_NAME]; 
MPI_Comm intercomm; 
/* ... */ 
MPI_Open_port(MPI_INFO_NULL, myport); 
printf("port name is: %s\n", myport); 
 
MPI_Comm_accept(myport, MPI_INFO_NULL, 0, MPI_COMM_SELF, &intercomm); 
/* do something with intercomm */ 
The server prints out the port name to the terminal and the user must type it in when starting up the client (assuming the MPI implementation supports stdin such that this works). On the client side:
MPI_Comm intercomm; 
char name[MPI_MAX_PORT_NAME]; 
printf("enter port name: "); 
gets(name); 
MPI_Comm_connect(name, MPI_INFO_NULL, 0, MPI_COMM_SELF, &intercomm); 


Example Ocean/Atmosphere---Relies on Name Publishing

In this example, the ``ocean'' application is the ``server'' side of a coupled ocean-atmosphere climate model. It assumes that the MPI implementation publishes names.


char port_name[MPI_MAX_PORT_NAME]; 
MPI_Comm intercomm; 
/* ... */ 
MPI_Open_port(MPI_INFO_NULL, port_name); 
MPI_Publish_name("ocean", MPI_INFO_NULL, port_name); 
 
MPI_Comm_accept(port_name, MPI_INFO_NULL, 0, MPI_COMM_SELF, 
                &intercomm); 
/* do something with intercomm */ 
MPI_Unpublish_name("ocean", MPI_INFO_NULL, port_name); 
On the client side:
MPI_Lookup_name("ocean", MPI_INFO_NULL, port_name); 
MPI_Comm_connect(port_name, MPI_INFO_NULL, 0, MPI_COMM_SELF, 
                 &intercomm); 


Example Simple Client-Server Example

This is a simple example; the server accepts only a single connection at a time and serves that connection until the client requests to be disconnected. The server is a single process.

Here is the server. It accepts a single connection and then processes data until it receives a message with tag 1. A message with tag 0 tells the server to exit.

#include "mpi.h" 
int main(int argc, char *argv[]) 
{ 
    MPI_Comm client; 
    MPI_Status status; 
    char port_name[MPI_MAX_PORT_NAME]; 
    double buf[MAX_DATA]; 
    int    size, again; 
 
    MPI_Init(&argc, &argv); 
    MPI_Comm_size(MPI_COMM_WORLD, &size); 
    if (size != 1) error(FATAL, "Server too big"); 
    MPI_Open_port(MPI_INFO_NULL, port_name); 
    printf("server available at %s\n", port_name); 
    while (1) { 
        MPI_Comm_accept(port_name, MPI_INFO_NULL, 0, MPI_COMM_WORLD, 
                        &client); 
        again = 1; 
        while (again) { 
            MPI_Recv(buf, MAX_DATA, MPI_DOUBLE, 
                     MPI_ANY_SOURCE, MPI_ANY_TAG, client, &status); 
            switch (status.MPI_TAG) { 
                case 0: MPI_Comm_free(&client); 
                        MPI_Close_port(port_name); 
                        MPI_Finalize(); 
                        return 0; 
                case 1: MPI_Comm_disconnect(&client); 
                        again = 0; 
                        break; 
                case 2: /* do something */ 
                ... 
                default: 
                        /* Unexpected message type */ 
                        MPI_Abort(MPI_COMM_WORLD, 1); 
            } 
        } 
    } 
} 

Here is the client.


#include "mpi.h" 
int main(int argc, char *argv[]) 
{ 
   MPI_Comm server; 
   int done = 0; 
   double buf[MAX_DATA]; 
   char port_name[MPI_MAX_PORT_NAME]; 
 
   MPI_Init(&argc, &argv); 
   strcpy(port_name, argv[1]);/* assume server's name is cmd-line arg */ 
 
   MPI_Comm_connect(port_name, MPI_INFO_NULL, 0, MPI_COMM_WORLD, 
                    &server); 
 
   while (!done) { 
       tag = 2; /* Action to perform */ 
       MPI_Send(buf, n, MPI_DOUBLE, 0, tag, server); 
       /* etc */ 
   } 
   MPI_Send(buf, 0, MPI_DOUBLE, 0, 1, server); 
   MPI_Comm_disconnect(&server); 
   MPI_Finalize(); 
   return 0; 
} 


PreviousUpNext
Up: Establishing Communication Next: Other Functionality Previous: Reserved Key Values


Return to MPI-5.0 Standard Index
Return to MPI Forum Home Page

(Unofficial) MPI-5.0 of June 9, 2025
HTML Generated on March 2, 2025