// ================================================================== // 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 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 MPI_Comm_size(MPI_COMM_WORLD, &max_cpus); // get program identifier MPI_Comm_rank(MPI_COMM_WORLD, &cpu_rank); // get cpu name MPI_Get_processor_name(cpu_name, &length); 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); }