#include #include #include #include #include #include //Example compilation //mpicc allreduce_starter.c -lm -o allreduce_starter //Example execution //mpirun -np 1 -hostfile myhostfile.txt ./allreduce_starter int main(int argc, char **argv) { int my_rank, nprocs; MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD,&my_rank); MPI_Comm_size(MPI_COMM_WORLD,&nprocs); if (nprocs!=4) { if (my_rank==0) { printf("Please enter 4 process ranks.\n\n"); } MPI_Finalize(); return 0; } //Each rank generates a different number of elements //Assume the ranks do not know how many elements each other produce //The values are hard coded here, but in practice they will not be double * data; int nElems=0; if (my_rank==0) { nElems=3; data=(double *)malloc(sizeof(double)*nElems); } else if (my_rank==1) { nElems=5; data=(double *)malloc(sizeof(double)*nElems); } else if (my_rank==2) { nElems=2; data=(double *)malloc(sizeof(double)*nElems); } else if (my_rank==3) { nElems=7; data=(double *)malloc(sizeof(double)*nElems); } //Initialize data to be equal to the iteration for (int i=0; i