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