#include #include #define NUMBER_OF_PROCS 4 #define NUMBER_OF_ELEMENTS 1000 #define NUMBER_OF_ELEMENTS_PER_PROCESS 250 #define ROOT 0 int main (int argc, char** argv) { int myRank; int partialSum = 0; int finalSum = 0; int sendBuffer[NUMBER_OF_ELEMENTS]; int recvBuffer[NUMBER_OF_ELEMENTS_PER_PROCESS]; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &myRank); printf("My rank: %d\n", myRank); if (myRank == ROOT) { // Master int i; for (i = 0; i < NUMBER_OF_ELEMENTS; i++) sendBuffer[i] = i+1; } MPI_Scatter(sendBuffer, NUMBER_OF_ELEMENTS_PER_PROCESS, MPI_INT, recvBuffer, NUMBER_OF_ELEMENTS_PER_PROCESS, MPI_INT, ROOT, MPI_COMM_WORLD); int j; for (j = 0; j < NUMBER_OF_ELEMENTS_PER_PROCESS; j++) partialSum += recvBuffer[j]; MPI_Reduce(&partialSum, &finalSum, 1, MPI_INT, MPI_SUM, ROOT, MPI_COMM_WORLD); if (myRank == ROOT) printf("Final sum: %d\n", finalSum); MPI_Finalize(); return 1; }