5.4.6. Client/Server Examples


Up: Establishing Communication Next: Simplest Example --- Completely Portable. Previous: Reserved Key Values



Up: Establishing Communication Next: Simplest Example --- Completely Portable. Previous: Reserved Key Values


5.4.6.1. Simplest Example --- Completely Portable.


Up: Client/Server Examples Next: Ocean/Atmosphere - Relies on Name Publishing Previous: Client/Server Examples

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); 



Up: Client/Server Examples Next: Ocean/Atmosphere - Relies on Name Publishing Previous: Client/Server Examples


5.4.6.2. Ocean/Atmosphere - Relies on Name Publishing


Up: Client/Server Examples Next: Simple Client-Server Example. Previous: Simplest Example --- Completely Portable.

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.


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); 



Up: Client/Server Examples Next: Simple Client-Server Example. Previous: Simplest Example --- Completely Portable.


5.4.6.3. Simple Client-Server Example.


Up: Client/Server Examples Next: Other Functionality Previous: Ocean/Atmosphere - Relies on Name Publishing

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; 
    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; }



Up: Client/Server Examples Next: Other Functionality Previous: Ocean/Atmosphere - Relies on Name Publishing


Return to MPI-2 Standard Index
Return to MPI 1.1 Standard Index
Return to MPI Forum Home Page

MPI-2.0 of July 18, 1997
HTML Generated on August 11, 1997