/* Cpt S 483, Introduction to Parallel Computing * School of Electrical Engineering and Computer Science * * Example code // Computing the sum of n numbers in parallel // Assumptions: p is a power of 2 // n >> p and n divisible by p // value of n passed as argument to the program // code will auto-generate n/p elements within each proc using a rand function. // * */ #include #include #include #include #include #include #include int rank,p,n; void genInput(int *,int); int main(int argc,char *argv[]) { MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD,&rank); MPI_Comm_size(MPI_COMM_WORLD,&p); // printf("my rank=%d\n",rank); // printf("Rank=%d: number of processes =%d\n",rank,p); // if(argc!=2) { printf("Usage: sum \n"); MPI_Finalize(); return(1); } n = atoi(argv[1]); if(rank==0) { printf("Info: input size n = %d\n",n); } assert(n%p==0); int size = n/p; //1. generate local array of size of n/p int *a=malloc(sizeof(int)*size); assert(a!=NULL); genInput(a,size); //2. compute local sum int i; int local_sum = 0; for(i=0;i: rec'd message from %d\n",rank,iter,stat.MPI_SOURCE); // fflush(stdout); temp = temp<<1; iter++; MPI_Barrier(MPI_COMM_WORLD); } printf("Rank=%d: Global sum = %d\n",rank,global_sum); MPI_Finalize(); } void genInput(int *a,int size) { srand48(time(NULL)+rank); int i=0; for(i=0;i\n",rank,s); #endif }