# include # include # include using namespace std; # include "mpi.h" int main ( int argc, char *argv[] ); //****************************************************************************80 int main ( int argc, char *argv[] ) //****************************************************************************80 // // Purpose: // // MAIN is the main program for SUM. // // Discussion: // // SUM is an example of using the MPI message passing interface library. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 01 May 2003 // // Author: // // John Burkardt // // Reference: // // Forrest Hoffman, // Message Passing with MPI and PVM, // LINUX Magazine, // Volume 4, Number 4, April 2002, pages 38-41, 63. // // William Gropp, Ewing Lusk, Anthony Skjellum, // Using MPI: Portable Parallel Programming with the // Message-Passing Interface, // Second Edition, // MIT Press, 1999, // ISBN: 0262571323. // { # define N 100 double array[N]; int i; int master = 0; int my_id; int num_procs; double PI = 3.141592653589793238462643; double seed; MPI::Status status; double sum; double sum_all; int tag; // // Initialize MPI. // MPI::Init ( argc, argv ); // // Get the number of processes. // num_procs = MPI::COMM_WORLD.Get_size ( ); // // Determine the rank of this process. // my_id = MPI::COMM_WORLD.Get_rank ( ); // // Say hello. // if ( my_id == master ) { cout << "\n"; cout << "SUM - Master process:\n"; cout << " C++ version\n"; cout << "\n"; cout << " An MPI example program.\n"; cout << " The master process computes some coefficients,\n"; cout << " sends them to each worker process, which sums them.\n"; cout << "\n"; cout << " Compiled on " << __DATE__ << " at " << __TIME__ << ".\n"; cout << "\n"; cout << " The number of processes available is " << num_procs << "\n"; } // // The master process initializes the array. // if ( my_id == master ) { seed = 1.2345; for ( i = 0; i < N; i++ ) { array[i] = ( double ) i * seed * PI; } } // // The master process broadcasts the computed initial values // to all the other processes. // MPI::COMM_WORLD.Bcast ( array, N, MPI::DOUBLE, master ); // // Each process adds up its entries. // sum = 0.0; for ( i = 0; i < N; i++ ) { sum = sum + array[i] * ( double ) my_id; } cout << "\n"; cout << "SUM - Process " << my_id << ":\n"; cout << " My contribution to the sum is " << sum << "\n"; // // Each worker process sends its sum back to the master process. // if ( my_id != master ) { MPI::COMM_WORLD.Send ( &sum, 1, MPI::DOUBLE, master, 1 ); } else { sum_all = sum; for ( i = 1; i < num_procs; i++ ) { tag = 1; MPI::COMM_WORLD.Recv ( &sum, 1, MPI::DOUBLE, MPI::ANY_SOURCE, tag, status ); sum_all = sum_all + sum; } } // // Shut down MPI. // MPI::Finalize ( ); if ( my_id == master ) { cout << "\n"; cout << "SUM - Master process:\n"; cout << " The total sum is " << sum_all << "\n"; } if ( my_id == master ) { cout << "\n"; cout << "SUM - Master process:\n"; cout << " Normal end of execution.\n"; } return 0; # undef N }