// ================================================================== // Author: Jean-Michel Richer // Email: jean-michel.richer@univ-angers.fr // Date: Aug 2016 // Purpose: Demonstrate basic functionnalities of MPI with C++ // ================================================================== #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 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); // sleep sleep(cpu_rank); cerr << "running on " << cpu_name << " with id=" << cpu_rank << "/"; cerr << max_cpus << endl; // free resources, don't forget ! MPI::Finalize(); exit(EXIT_SUCCESS); }