#include #include #include #include #include /* A collective sum-reduction operation */ /*@ \mpi_collective(comm, P2P): @ requires datatype == MPI_INT; @ requires 0 @ (int)recvbuf[i] == \sum(0, \mpi_comm_size, @ \lambda int k; \on(k, ((int)sendbuf[i]))); @ waitsfor root; @ @*/ int reduce_sum(int * sendbuf, int * recvbuf, MPI_Datatype datatype, int count, int root, MPI_Comm comm) { int rank; 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