int MPI_Reduce_scatter(void *sbuf, void *rbuf, int *rcounts,
MPI_Datatype dtype, MPI_Op op, MPI_Comm comm)
{
int *disps = 0;
int i, myrank, nprocs;
void *tmpbuf;
MPI_Comm_rank(comm, &myrank);
MPI_Comm_size(comm, &nprocs);
if (myrank == 0) {
create a temporary buffer tmpbuf large enough for count dtypes;
disps = malloc(nprocs * sizeof(int));
disps[0] = 0;
for (i = 0; i < (nprocs - 1); i++) {
disps[i + 1] = disps[i] + rcounts[i];
}
}
MPI_Reduce(sbuf, tmpbuf, count, dtype, op, 0, comm);
MPI_Scatterv(tmpbuf, rcounts, disps, dtype,
rbuf, rcounts[myrank], dtype, 0, comm);
if (disps) free(disps);
return(MPI_SUCCESS);
}