#include #include #include #include #include /* A collective sum-reduction operation */ /*@ \mpi_collective(comm, P2P): @ requires datatype == MPI_INT; @ requires 0 @ recvbuf[i] == \sum(0, \mpi_comm_size-1, @ \lambda int k; \on(k, 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); sum = (int *)malloc(sizeof(int) * count); memcpy(sum, sendbuf, size); for (int i = 0; i