#include #include #include #include #define N 16 int main(int argc, char** argv) { int rank, size, i; int* data; /* initialize MPI */ MPI_Init(&argc, &argv); /* get the rank (process id) and size (number of processes) */ MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); /* have process 0 read in the data */ if (rank == 0) { printf("Enter %d values: ", N); fflush(stdout); data = malloc(N * sizeof(int)); for (i = 0 ; i < N; i++) { scanf("%d", data + i); } } /* our portion of the data */ int portion[N / size]; /* now scatter the characters to all processes*/ MPI_Scatter(data, N / size, MPI_INT, portion, N / size, MPI_INT, 0, MPI_COMM_WORLD); /* have each process square its data */ for (i = 0; i < (N / size); i++) { portion[i] *= portion[i]; } /* gather the data back up in process 0 */ MPI_Gather(portion, N / size, MPI_INT, data, N / size, MPI_INT, 0, MPI_COMM_WORLD); /* have process 0 print the results */ if (rank == 0) { printf("Results: "); for (i = 0; i < N; i++) { printf("%d ", data[i]); } printf("\n"); } /* quit MPI */ MPI_Finalize(); return 0; }