#include #include #include #include #define N 16 int main(int argc, char** argv) { int rank, size; int* data; int i; /* 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); /* print the data */ for (i = 0; i < (N / size); i++) { printf("Process %d has %d!\n", rank, portion[i]); } /* quit MPI */ MPI_Finalize(); return 0; }