#include #include #include #include #undef min enum MpiTags { mpiTagNrProcs = 1, mpiTagSizeToProcess = 2, mpiTagInputData = 3, mpiTagResult = 4 }; /** Processes as the root of the recursive decomposition tree. * * Parameters: * - comm - communicator used * - nrProcs - number of processes used for processing. The current process shall use processes with ranks from the current one to current rank + nrProcs -1. * - nrBranches - number of brances at each level of the tree * - pInput - pointer to the input data * - inputSize - the size of the input data * Returns: the result (the sum of values in the input array) * */ int sumVectorN(MPI_Comm comm, int nrProcs, int nrBranches, int const* pInput, int inputSize) { if(nrProcs == 1) { int s = 0; for(int i=0 ; i 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 5 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