#include int main (argc, argv) int argc; char **argv; { int rcv_tag=MPI_ANY_TAG, from=MPI_ANY_SOURCE; int snd_tag=1, rank, size, parent=0, data=1, cnt=1; int i, to, sum; MPI_Status status; /* Initialize MPI */ MPI_Init (&argc, &argv); /* How many processes are in this application */ MPI_Comm_size (MPI_COMM_WORLD, &size); /* Find my rank within this MPI application */ MPI_Comm_rank (MPI_COMM_WORLD, &rank); /* Parent: Sum each child's rank squared value */ if (rank == parent) { for (i = 1, sum = 1; i < size; i++) { MPI_Recv (&data, cnt, MPI_INT, from, rcv_tag, MPI_COMM_WORLD, &status); sum += data; } printf ("Sum of squares (1-to-%d) is %d\n", size, sum); } /* Child: Send the parent my rank squared */ else { data = (rank + 1) * (rank + 1); MPI_Send (&data, cnt, MPI_INT, parent, snd_tag, MPI_COMM_WORLD); } MPI_Finalize (); exit(0); }