Skip to content

Instantly share code, notes, and snippets.

@DaemonDave
Last active December 16, 2016 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaemonDave/21fea476847d94326ec6c9664c15fb87 to your computer and use it in GitHub Desktop.
Save DaemonDave/21fea476847d94326ec6c9664c15fb87 to your computer and use it in GitHub Desktop.
OpenMPI publish name example as explained here : http://tinyurl.com/gnba6zj
/*!
*
* OpenMPI nameserver example
*
* client that looks up name from nameserver then connects to that server
*
* */
#include "mpi.h"
#include "stdio.h"
#include "string.h"
#define MAX_DATA 50
/*!
\brief How to execute mpi name server
mpirun -np 1 ompi-server --no-daemonize -r + &
*
* Success looks like this :
*
"server available at 3653042176.0;tcp://192.168.10.191:52434+3653042177.0;tcp://192.168.10.191:48880:300"
*/
int main( int argc, char **argv )
{
MPI_Comm server;
double buf[MAX_DATA];
char port_name[MPI_MAX_PORT_NAME];
int tag = 0;
int done = 0;
int n;
double * p;
buf[0] = 25.5;
buf[1] = 26.5;
buf[2] = 27.5;
n = 2;
MPI_Init( &argc, &argv );
//strcpy(port_name, argv[1] );/* assume server's name is cmd-line arg */
fprintf(stderr, "looking up server ... \n");
// second command line parameter is the name of server to lookup
MPI_Lookup_name( "ocean", MPI_INFO_NULL, port_name);
// connect to name and place data into server
MPI_Comm_connect( port_name, MPI_INFO_NULL, 0, MPI_COMM_WORLD, &server );
// aim p at first double entry
p = buf;
while (!done)
{
tag = 2; /* Action to perform */
MPI_Send( (void *) p, 1, MPI_DOUBLE, 0, tag, server );
/* etc */
n--;
p++;
if ( n < 0) done = 1;
}
MPI_Send( buf, 0, MPI_DOUBLE, 0, 1, server );
MPI_Comm_disconnect( &server );
MPI_Finalize();
return 0;
}
/* client side lookup request
MPI_Lookup_name("ocean", MPI_INFO_NULL, port_name);
MPI_Comm_connect( port_name, MPI_INFO_NULL, 0, MPI_COMM_SELF,
&intercomm);
*/
/*!
*
* OpenMPI nameserver example
*
* server that publishes name to nameserver then awaits client connections
*
* */
#include "mpi.h"
#include "stdio.h"
#include "string.h"
#define MAX_DATA 50
/*!
\brief How to execute mpi name server
mpirun -np 1 ompi-server --no-daemonize -r + &
*
* Success looks like this :
*
"server available at 3653042176.0;tcp://192.168.10.191:52434+3653042177.0;tcp://192.168.10.191:48880:300"
*/
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_Info info;
double * p;
MPI_Init( &argc, &argv );
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (size != 1) error( "Server too big");
MPI_Open_port(MPI_INFO_NULL, port_name);
printf("server available at %s\n",port_name);
MPI_Info_create(&info);
MPI_Info_set(info, "ompi_global_scope", "true");
// publish to name server with global scope info
MPI_Publish_name("ocean", info, port_name);
while (1)
{
// accept clients
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_Unpublish_name("ocean", MPI_INFO_NULL, port_name);
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 */
p = (double *) buf;
fprintf(stderr, " we got a client's data: %f\n", *p);
break;
default:
/* Unexpected message type */
// radical original version aborted everything...
;//MPI_Abort( MPI_COMM_WORLD, 1 );
}
}
}
}
/*
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);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment