#include #include #include #include /* A collective sum-reduction operation */ int reduce_sum(void* sendbuf, void* recvbuf, MPI_Datatype datatype, int count, int root, MPI_Comm comm) { int rank; int REDUCE_TAG = 999; MPI_Comm_rank(comm, &rank); if (rank != root) MPI_Send(sendbuf, count, datatype, root, REDUCE_TAG, comm); else { int nprocs; int size; int * sum; MPI_Comm_size(comm, &nprocs); size = count * sizeof(int); memcpy(recvbuf, sendbuf, size); sum = (int *)malloc(sizeof(int) * count); for (int i = 0; i