<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Hi Rupsa,</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
With MPICH, you can set info hint "timeout" in seconds. Both MPI_Comm_accept and MPI_Comm_connect accept this info key.</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
-- <br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Hui Zhou<br>
</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Rupsa Chakraborty via discuss <discuss@mpich.org><br>
<b>Sent:</b> Thursday, January 27, 2022 10:54 PM<br>
<b>To:</b> discuss@mpich.org <discuss@mpich.org><br>
<b>Cc:</b> Rupsa Chakraborty <c.rupsa@gmail.com><br>
<b>Subject:</b> [mpich-discuss] How to gracefully exit an MPI server process when it is waiting on MPI_Comm_accept</font>
<div> </div>
</div>
<div>
<div dir="ltr"><br>
Hello, <br>
<br>
<span style="color:rgb(35,38,41); font-size:15px">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:</span>
<div><a href="https://stackoverflow.com/questions/70745740/how-to-gracefully-exit-an-mpi-server-process-when-it-is-waiting-on-mpi-comm-acce">https://stackoverflow.com/questions/70745740/how-to-gracefully-exit-an-mpi-server-process-when-it-is-waiting-on-mpi-comm-acce</a><span style="color:rgb(35,38,41); font-size:15px"><br>
</span></div>
<div><br>
</div>
<div>
<div class="x_gmail-votecell x_gmail-post-layout--left" style="margin:0px; padding:0px 16px 0px 0px; border:0px; font-variant-numeric:inherit; font-variant-east-asian:inherit; font-stretch:inherit; line-height:inherit; font-size:13px; vertical-align:top; box-sizing:inherit; width:auto; color:rgb(35,38,41)">
<div class="x_gmail-js-voting-container x_gmail-d-flex x_gmail-jc-center x_gmail-fd-column x_gmail-ai-stretch x_gmail-gs4 x_gmail-fc-black-200" style="padding:0px; border:0px; font-style:inherit; font-variant:inherit; font-weight:inherit; font-stretch:inherit; line-height:inherit; font-family:inherit; vertical-align:baseline; box-sizing:inherit; display:flex">
<div class="x_gmail-js-vote-count x_gmail-flex--item x_gmail-d-flex x_gmail-fd-column x_gmail-ai-center x_gmail-fc-black-500 x_gmail-fs-title" style="margin:2px; padding:0px; border:0px; font-style:inherit; font-variant:inherit; font-weight:inherit; font-stretch:inherit; line-height:inherit; font-family:inherit; vertical-align:baseline; box-sizing:inherit; font-size:1.61538rem; display:flex">
<span style="font-family:inherit; font-style:inherit; font-variant-ligatures:inherit; font-variant-caps:inherit; font-weight:inherit; font-size:15px">My concern is as follows:<br>
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.</span></div>
</div>
</div>
<div class="x_gmail-postcell x_gmail-post-layout--right" style="margin:0px; padding:0px 16px 0px 0px; border:0px; font-variant-numeric:inherit; font-variant-east-asian:inherit; font-stretch:inherit; line-height:inherit; font-size:13px; vertical-align:top; box-sizing:inherit; width:auto; min-width:0px; color:rgb(35,38,41)">
<div class="x_gmail-s-prose x_gmail-js-post-body" style="margin:0px; padding:0px; border:0px; font-style:inherit; font-variant:inherit; font-weight:inherit; font-stretch:inherit; font-size:15px; vertical-align:baseline; box-sizing:inherit; width:659px">
<p style="margin-top:0px; margin-right:0px; margin-left:0px; padding:0px; border:0px; font-style:inherit; font-variant:inherit; font-weight:inherit; font-stretch:inherit; line-height:inherit; font-family:inherit; vertical-align:baseline; box-sizing:inherit; clear:both">
<br>
My question is:<br>
- 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?</p>
<p style="margin-top:0px; margin-right:0px; margin-left:0px; padding:0px; border:0px; font-style:inherit; font-variant:inherit; font-weight:inherit; font-stretch:inherit; line-height:inherit; font-family:inherit; vertical-align:baseline; box-sizing:inherit; clear:both">
- Is there anyway I can set timeout to the MPI_Comm_accept call?</p>
<p style="margin-top:0px; margin-right:0px; margin-left:0px; padding:0px; border:0px; font-style:inherit; font-variant:inherit; font-weight:inherit; font-stretch:inherit; line-height:inherit; font-family:inherit; vertical-align:baseline; box-sizing:inherit; clear:both">
Here is a dummy code that looks similar to my server code:</p>
<pre style="margin-top:0px; margin-bottom:0px; padding:12px; border:0px; font-style:inherit; font-variant:inherit; font-weight:inherit; font-stretch:inherit; line-height:1.30769; font-size:13px; vertical-align:baseline; box-sizing:inherit; width:auto; max-height:600px; overflow:auto; border-radius:5px"><code style="margin:0px; padding:0px; border:0px; font-style:inherit; font-variant:inherit; font-weight:inherit; font-stretch:inherit; line-height:inherit; vertical-align:baseline; box-sizing:inherit; background-color:transparent; white-space:inherit; border-radius:0px">// 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();   
}</code></pre>
</div>
</div>
</div>
<div><br>
</div>
</div>
</div>
</body>
</html>