// hello_world.cc // // Hello World Program #include #include #include #include "mpi.h" using namespace std; int main(void) { MPI::Init(); // initialisiere MPI int rank=MPI::COMM_WORLD.Get_rank(); // ascertain own rank int size=MPI::COMM_WORLD.Get_size(); // and number of processes const int max_proc_name_length=MPI::MAX_PROCESSOR_NAME+1; int proc_name_length; char *proc_name; try { proc_name=new char[max_proc_name_length]; } catch (bad_alloc &e) { // not enough memory MPI::COMM_WORLD.Abort(EXIT_FAILURE); // terminate all processes } MPI::Get_processor_name(proc_name, proc_name_length); // which hostname? proc_name[proc_name_length]='\0'; if (rank==0) { // rank 0 is indicated ofstream out("hello_world.out"); // create output file if (!out) // error => terminate processs MPI::COMM_WORLD.Abort(EXIT_FAILURE); // Receive status reports of other ranks and write to file for (int i=0; i0) MPI::COMM_WORLD.Recv(proc_name, max_proc_name_length, MPI::CHAR, i, 0); out << "Hello World! My rank is " << i << " of " << size << ". " << "I am running on " << proc_name << "." << endl; } } else // Send string MPI::COMM_WORLD.Send(proc_name, proc_name_length+1, MPI::CHAR, 0, 0); MPI::Finalize(); // terminate MPI return EXIT_SUCCESS; }