// ================================================================== // Author: Jean-Michel Richer // Email: jean-michel.richer@univ-angers.fr // Date: Aug 2016 // Purpose: Demonstrate basic functionalities of MPI with C++ // with synchronization for display // ================================================================== #include #include #include // for sleep using namespace std; #include // ================================================================== // C++ version // ================================================================== int main(int argc, char ** argv) { // maximum number of CPUs int max_cpus; // cpu identifier (called rank) int cpu_rank; // C-string to store the name of the host char cpu_name[MPI_MAX_PROCESSOR_NAME]; // length of the C-string int length; // initialization also using command line parameters MPI_Init(&argc, &argv); // get number of programs running max_cpus = MPI::COMM_WORLD.Get_size(); // get program identifier cpu_rank = MPI::COMM_WORLD.Get_rank(); // get cpu name memset(cpu_name, 0, MPI::MAX_PROCESSOR_NAME); MPI::Get_processor_name(cpu_name, length); cout << "Hello, from " << cpu_rank << endl; sleep( cpu_rank + 1 ); MPI::COMM_WORLD.Barrier(); cout << "Bye, from " << cpu_rank << endl; // free resources, don't forget ! MPI::Finalize(); exit(EXIT_SUCCESS); }