Simple Chat program C++ in MPI -


my program includes 1 rank process of server , 1 rank process of client. client transfer number (array of type double) server.

here code

//server rank if (rank == 0){     double buf[max_data];     mpi_open_port(mpi_info_null, port_name);     printf("server available @ %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, 0, 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: /* */                 printf("case 2\n");                 break;             default:                 /* unexpected message type */                 mpi_abort(mpi_comm_world, 1);             }         }     } } // client rank else {     mpi_comm server;     double buf[max_data];     mpi_comm_connect(port_name, mpi_info_null, 0, mpi_comm_world, &server);     int = 0;     while (true) {         int tag = 2; /* action perform */         mpi_send(buf, max_data, mpi_double, 0, tag, server);     }     mpi_send(buf, 0, mpi_double, 0, 1, server);     mpi_comm_disconnect(&server); }  mpi_finalize(); 

but code doesn't work, , stucked when type command "mpiexec -n 2 mpi_helloworld.exe" (there 2 processes created run program)

i modify code source : "http://www.mcs.anl.gov/research/projects/mpi/mpi-standard/mpi-report-2.0/node106.htm"

program's result: gets stucks: server doesnot receive message enter image description here

few things:

1) server needs tell client port accepting communications on. so, before starting accept needs send message client , tell client port_name.

so, call needed:

mpi_send(port_name, mpi_max_port_name, mpi_char, 1, 0, mpi_comm_world); 

and client needs receive message. so, client first needs do:

mpi_recv(port_name, mpi_max_port_name, mpi_char, 0, 0, mpi_comm_world, &status); 

2) client/server requires point-to-point communication (you have multiple clients between 2 processes @ same time), cannot use mpi_comm_world(which includes processes) in mpi_comm_accept() , mpi_comm_connect(). correct communicator use mpi_comm_self.

so calls need changed to:

mpi_comm_accept(port_name, mpi_info_null, 0, mpi_comm_self, &client); 

and

mpi_comm_connect(port_name, mpi_info_null, 0, mpi_comm_self, &server); 

the following code has been tested on system , runs:

#include <stdio.h> #include <mpi.h> #define max_data    100  int main (int argc, char *argv[]){    int rank, size;   char port_name[mpi_max_port_name];    mpi_init (&argc, &argv);  /* starts mpi */   mpi_comm_rank (mpi_comm_world, &rank);    /* current process id */   mpi_comm_size (mpi_comm_world, &size);    /* number of processes */    //server rank   if (rank == 0){     double buf[max_data];     mpi_open_port(mpi_info_null, port_name);     printf("server available @ %s\n", port_name);      // server tells client port_name     mpi_send(port_name, mpi_max_port_name, mpi_char, 1, 0, mpi_comm_world);     mpi_comm client;     mpi_status status;     while (1) {         mpi_comm_accept(port_name, mpi_info_null, 0, mpi_comm_self, &client);         int again = 1;         while (again) {             mpi_recv(buf, max_data, mpi_double, 1, mpi_any_tag, client, &status);             printf("server received sth!\n");                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: /* */                 printf("case 2\n");                 break;             default:                 /* unexpected message type */                 mpi_abort(mpi_comm_world, 1);             }         }     }   } else{     mpi_status status;     // client receives port information server     mpi_recv(port_name, mpi_max_port_name, mpi_char, 0, 0, mpi_comm_world, &status);      mpi_comm server;     double buf[max_data];      mpi_comm_connect(port_name, mpi_info_null, 0, mpi_comm_self, &server);     int = 0;     while (1) {         int tag = 2; /* action perform */         mpi_send(buf, max_data, mpi_double, 0, tag, server);         printf("client send somthing!\n");         sleep(1);     }     mpi_send(buf, 0, mpi_double, 0, 1, server);     mpi_comm_disconnect(&server);   }    mpi_finalize(); } 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -