Open MPI logo

Open MPI User's Mailing List Archives

  |   Home   |   Support   |   FAQ   |   all Open MPI User's mailing list

Subject: [OMPI users] client-server example
From: John Chludzinski (john.chludzinski_at_[hidden])
Date: 2013-04-13 01:58:40


Found the following client-server example (code) on
http://www.mpi-forum.org and I'm trying to get it to work. Not sure
what argv[1] should be for the client? The output from the server
side is:

       server available at
4094230528.0;tcp://192.168.1.4:55803+4094230529.0;tcp://192.168.1.4:51618:300

// SERVER
#include <stdio.h>
#include <error.h>
#include <errno.h>
#include "mpi.h"

#define MAX_DATA 100
#define FATAL 1

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, errno, "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 */
              fprintf( stderr, "Do something ...\n" );
            default:
              /* Unexpected message type */
              MPI_Abort( MPI_COMM_WORLD, 1 );
            }
        }
    }
}

//CLIENT
#include <string.h>
#include "mpi.h"

#define MAX_DATA 100

int main( int argc, char **argv )
{
  MPI_Comm server;
  double buf[MAX_DATA];
  char port_name[MPI_MAX_PORT_NAME];
  int done = 0, tag, n, CNT=0;

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

  n = MAX_DATA;

  while (!done)
    {
      tag = 2; /* Action to perform */
      if ( CNT == 5 ) { tag = 0; done = 1; }
      MPI_Send( buf, n, MPI_DOUBLE, 0, tag, server );
      CNT++;
      /* etc */
    }

  MPI_Send( buf, 0, MPI_DOUBLE, 0, 1, server );
  MPI_Comm_disconnect( &server );
  MPI_Finalize();

  return 0;
}