# 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 BONES. // // Discussion: // // BONES is a simple demonstration of the use of MPI by a C++ program. // // This program should be run on at least two processes. // Any processes beyond the first two will not be given any work. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 22 January 2003 // // Author: // // John Burkardt // // Reference: // // William Gropp, Ewing Lusk, Anthony Skjellum, // Using MPI: Portable Parallel Programming with the // Message-Passing Interface, // Second Edition, // MIT Press, 1999, // ISBN: 0262571323. // { int count; float data[100]; int dest; int i; int num_procs; int rank; MPI::Status status; int tag; float value[200]; // // Initialize MPI. // MPI::Init ( argc, argv ); // // Determine this process's rank. // rank = MPI::COMM_WORLD.Get_rank ( ); // // Have Process 0 say hello. // if ( rank == 0 ) { num_procs = MPI::COMM_WORLD.Get_size ( ); cout << "\n"; cout << "BONES:\n"; cout << " C++ version\n"; cout << " An MPI example program.\n"; cout << " The number of processes available is " << num_procs << "\n"; } // // Process 0 expects up to 200 real values, from any source. // if ( rank == 0 ) { tag = 55; MPI::COMM_WORLD.Recv ( value, 200, MPI::FLOAT, MPI::ANY_SOURCE, tag, status ); cout << "P:" << rank << " Got data from process " << status.Get_source() << "\n"; count = status.Get_count ( MPI::FLOAT ); cout << "P:" << rank << " Got " << count << " elements.\n"; cout << "P:" << rank << " value[5] = " << value[5] << "\n"; } // // Process 1 sends 100 real values to process 0. // else if ( rank == 1 ) { cout << "\n"; cout << "P:" << rank << " - setting up data to send to process 0.\n"; for ( i = 0; i < 100; i++ ) { data[i] = i; } dest = 0; tag = 55; MPI::COMM_WORLD.Send ( data, 100, MPI::FLOAT, dest, tag ); } // // Any other process is idle. // else { cout << "\n"; cout << "P:" << rank << " - MPI has no work for me!\n"; } MPI::Finalize ( ); if ( rank == 0 ) { cout << "\n"; cout << "BONES:\n"; cout << " Normal end of execution.\n"; } return 0; }