#include #include #include using namespace std; // The only argument must be the number of processes in communicator we expect to build. int main(int argc, char** argv) { int np = atoi(argv[1]); int ac = 0; MPI_Init(&ac, &argv); char portName[MPI_MAX_PORT_NAME]; MPI_Open_port(MPI_INFO_NULL, portName); MPI_Publish_name("my_service", MPI_INFO_NULL, portName); cout << portName << "\n"; MPI_Comm intercomm, intracomm = MPI_COMM_SELF; // Build an intracom dynamically until n processes are reached. for (int i = 1; i < np; i++) { MPI_Comm_accept(portName, MPI_INFO_NULL, 0, intracomm, &intercomm); cout << "Accepted.\n"; MPI_Intercomm_merge(intercomm, false, &intracomm); cout << "Merged to an intracom.\n"; MPI_Comm_free(&intercomm); } // Intracomm contains the one we can now use with n-grid. MPI_Comm_free(&intracomm); MPI_Unpublish_name("my_service", MPI_INFO_NULL, portName); MPI_Close_port(portName); MPI_Finalize(); }