#include #include #include #include #undef min /* Within the given communicator, assuming that rank 0 is the root of the tree and, at each level, there are `nrBranches` children (assuming there are enough nodes), * this function computes, for the current node: * - who is the parent (-1 if we are the root) * - how many nodes the current process has in its sub-tree (including itself) * */ void parentAndTotalSubordinates(MPI_Comm comm, int nrBranches, int& parent, int& nrProcs) { int me; int nrTotalProcs; MPI_Comm_rank(comm, &me); MPI_Comm_size(comm, &nrTotalProcs); if(me == 0) { parent = -1; nrProcs = nrTotalProcs; return; } parent = 0; while(true) { int nrChildren = std::min(nrBranches, nrTotalProcs); for(int i=0 ; i= parent+begin && me data(sizeToRecv); MPI_Recv(data.data(), sizeToRecv, MPI_INT, parent, mpiTagInputData, comm, &status); int sum = sumVectorN(comm, nrProcs, nrBranches, data.data(), sizeToRecv); MPI_Ssend(&sum, 1, MPI_INT, parent, mpiTagResult, comm); } #define NR_BRANCHES 3 int main() { MPI_Init(0, 0); int me; MPI_Comm_rank(MPI_COMM_WORLD, &me); if(me == 0) { int nrElements = 1000; std::vector input(nrElements); for(int i=0 ; i