/* worker */ #include "mpi.h" #include int main(int argc, char *argv[]) { int size, rank, world_size; char port[MPI_MAX_PORT_NAME]; MPI_Comm parent, univ, univ2, comm, connect_intracomm, univ_intercomm, newintercomm; MPI_Init(&argc, &argv); MPI_Comm_get_parent(&parent); MPI_Comm_size(MPI_COMM_WORLD, &world_size); if (parent == MPI_COMM_NULL) { printf("enter the port name:\n"); fflush(stdout); gets(port); MPI_Comm_connect(port, MPI_INFO_NULL, 0, MPI_COMM_WORLD, &comm); printf("connected to the server\n"); fflush(stdout); MPI_Intercomm_merge(comm, 1, &univ); MPI_Comm_spawn("worker.exe", MPI_ARGV_NULL, 1, MPI_INFO_NULL, 0, univ, &newintercomm, MPI_ERRCODES_IGNORE); MPI_Intercomm_merge(newintercomm, 0, &univ2); MPI_Comm_rank(univ2, &rank); MPI_Comm_size(univ2, &size); printf("[%d/%d] Connecting worker process \n", rank, size); fflush(stdout); } else { MPI_Intercomm_merge(parent, 1, &univ); MPI_Comm_rank(univ, &rank); MPI_Comm_size(univ, &size); printf("[%d/%d] Spawned worker process \n", rank, size); fflush(stdout); } MPI_Comm_size(univ, &size); MPI_Comm_rank(univ, &rank); // printf("Worker's rank %d; intracomm's size %d\n", rank, size); fflush(stdout); if(parent == MPI_COMM_NULL) MPI_Comm_disconnect(&comm); MPI_Finalize(); return 0; }