/* ===================================================== MPI example: find max and min in array ===================================================== */ #include #include #include #include struct timeval start_time, stop_time; int elapsed; #define MAX 100000000 int num_procs; double x[MAX]; int main(int argc, char *argv[]) { char *s; int i, n; int start, stop; int myid; double my_min, others_min; double my_max, others_max; MPI_Init(&argc,&argv); // Initialize MPI_Comm_size(MPI_COMM_WORLD, &num_procs); // Get # processors MPI_Comm_rank(MPI_COMM_WORLD, &myid); // Get my rank (id) /* ------------------------- Generate random number ------------------------- */ for (i = 0; i < MAX; i++) x[i] = random()/(double)1147483648; if ( myid == 0 ) cout << "Using " << num_procs << " processes..." << endl << endl; gettimeofday(&start_time, NULL); /* ******************************************************************* */ /* -------------------------------------- Find the min. among my numbers The min is stored in min[s] -------------------------------------- */ n = MAX/num_procs; start = myid * n; if ( myid != (num_procs-1) ) { stop = start + n; } else { stop = MAX; } my_min = x[start]; for (i = start+1; i < stop; i = i + n ) { if ( x[i] < my_min ) my_min = x[i]; } /* ******************************************************************* */ if ( myid == 0 ) { cout << "Done... Now collecting output from others" << endl; /* ------------------------------------- Get the min from others and compare ------------------------------------- */ for (i = 1; i < num_procs; i++) { MPI_Recv(&others_min, 1, MPI_DOUBLE, i, 0, MPI_COMM_WORLD, NULL); cout << "Min received from proc " << i << " = " << others_min << endl; if ( others_min < my_min ) my_min = others_min; } } else { MPI_Send(&my_min, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD); } /* ----------------------------------------------- ----------------------------------------------- Now find the max ----------------------------------------------- ----------------------------------------------- */ my_max = x[start]; for (i = start+1; i < stop; i = i + n ) { if ( x[i] > my_max ) my_max = x[i]; } /* ******************************************************************* */ if ( myid == 0 ) { cout << "Done... Now collecting output from others" << endl; /* ------------------------------------- Get the max from others and compare ------------------------------------- */ for (i = 1; i < num_procs; i++) { MPI_Recv(&others_max, 1, MPI_DOUBLE, i, 0, MPI_COMM_WORLD, NULL); cout << "Max received from proc " << i << " = " << others_max << endl; if ( others_max > my_max ) my_max = others_max; } gettimeofday(&stop_time, NULL); elapsed = (stop_time.tv_sec*1000000 + stop_time.tv_usec) - (start_time.tv_sec*1000000 + start_time.tv_usec); cout << "min = " << my_min << endl << endl; cout << "max = " << my_max << endl << endl; cout << "Elapsed time = " << elapsed << " microseconds" << endl; } else { MPI_Send(&my_max, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD); } /* --------------------------- Now find the max --------------------------- */ MPI_Finalize(); }