[mpich-discuss] How to gracefully exit an MPI server process when it is waiting on MPI_Comm_accept

Zhou, Hui zhouh at anl.gov
Fri Jan 28 08:37:14 CST 2022


Hi Rupsa,

With MPICH, you can set info hint "timeout" in seconds. Both MPI_Comm_accept and MPI_Comm_connect accept this info key.

--
Hui Zhou
________________________________
From: Rupsa Chakraborty via discuss <discuss at mpich.org>
Sent: Thursday, January 27, 2022 10:54 PM
To: discuss at mpich.org <discuss at mpich.org>
Cc: Rupsa Chakraborty <c.rupsa at gmail.com>
Subject: [mpich-discuss] How to gracefully exit an MPI server process when it is waiting on MPI_Comm_accept


Hello,

I am asking this question in this forum as I had already asked this question in stack overflow and did not receive any answer/comment:
https://stackoverflow.com/questions/70745740/how-to-gracefully-exit-an-mpi-server-process-when-it-is-waiting-on-mpi-comm-acce

My concern is as follows:
I have an MPI server program that calls MPI_Comm_accept, in an infinite loop, on a separate thread. The main thread spawns this thread and in parallel does some other work. At some point of time the main thread decides to exit. It closes the port using MPI_Close_port(portName), however the MPI_Comm_accept() is still waiting for connection requests.

My question is:
- How does the main thread exit gracefully? Is there an equivalent of socket close in MPI? I am using mpich which I can use from main thread?

- Is there anyway I can set timeout to the MPI_Comm_accept call?

Here is a dummy code that looks similar to my server code:

// MPI Server Program

void accept(std::string portName) {

  while (true) {
    MPI_Comm intercomm;
    MPI_Comm_accept(portName.c_str(), MPI_INFO_NULL, 0, MPI_COMM_SELF, &intercomm);
    // handle connection
    MPI_Comm_disconnect(&intercomm);
  }
}

int main() {

  char ch;
  MPI_Init(NULL, NULL);
  char portName[MPI_MAX_PORT_NAME];
  MPI_Open_port(MPI_INFO_NULL, portName);
  publishName("nameServerFile.txt", "ocean", std::string(portName));

  std::thread th(&accept, std::string(portName));

  // do something

  MPI_Close_port(portName);
  std::cout <<"\nClosed port" << std::endl;
  th.join();       // main thread waits infinitely at this join
                   // as child thread is still waiting on MPI_Comm_accept
  MPI_Finalize();
}

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mpich.org/pipermail/discuss/attachments/20220128/5d991f77/attachment.html>


More information about the discuss mailing list