#include #include #include using namespace std; int main(int argc, char * argv[]) { MPI_Init(&argc, &argv); // Get the rank and size in the original communicator int rank, size; MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); vector gatherLoc(size); MPI_Gather(&rank, 1, MPI_INT, gatherLoc.data(), 1, MPI_INT, 0, MPI_COMM_WORLD); if(rank == 0) { for(int i = 0; i < gatherLoc.size(); ++i) { cout << i << " holds " << gatherLoc[i] << endl; } } MPI_Allgather(&rank, 1, MPI_INT, gatherLoc.data(), 1, MPI_INT, MPI_COMM_WORLD); if(rank == 0) { for(int i =0; i < gatherLoc.size(); ++i) { cout << rank << " has " << gatherLoc[i] << " at " << i << endl; } } MPI_Finalize(); return 0; }