int MPI_Scan(void *sbuf, void *rbuf, int count,
MPI_Datatype dtype, MPI_Op op, MPI_Comm comm)
{
MPI_Status status;
int i, myrank, nprocs;
void *tmpbuf;
MPI_Comm_rank(comm, &myrank);
MPI_Comm_size(comm, &nprocs);
/* copy the send buffer into the receive buffer */
MPI_Sendrecv(sbuf, count, dtype, myrank, IMPI_SCAN_TAG,
rbuf, count, dtype, myrank, IMPI_SCAN_TAG, comm, &status);
if (myrank > 0) {
/* receive previous buffer into a temporary and reduce */
create a temporary buffer tmpbuf large enough for count dtypes;
MPI_Recv(tmpbuf, count, dtype, myrank-1, IMPI_SCAN_TAG, comm, &status);
call reduction function op on tmpbuf (invec) and rbuf (inoutvec)
free tmpbuf;
if (myrank < (nprocs - 1)) {
/* send result to next process */
MPI_Send(rbuf, count, dtype, myrank + 1, IMPI_SCAN_TAG, comm);
}
}
return(MPI_SUCCESS);
}