/********************************************************** File: hello_world.cc Description: Every process writes hello and its rank. Author: Dana Vrajitoru Class: B424 B524 Last update: September 14, 2004 **********************************************************/ #include #include #include #include #include using namespace std; /**********************************************************/ /* The function that writes the message on the screen. */ void Write_message(int rank, char *message) { cout << message << " from process number " << rank << endl; } /**********************************************************/ /* Main */ int main(int argc, char **argv) { double time; int mpirank, mpisize; int n=0; /* Initialize MPI */ MPI_Init (&argc, &argv); /* Get the rank of this process */ MPI_Comm_rank (MPI_COMM_WORLD, &mpirank); /* Get nb of processes */ MPI_Comm_size (MPI_COMM_WORLD, &mpisize); /* Each process writes hello and then waits for all of the others */ time = MPI_Wtime(); Write_message(mpirank, "Hello"); MPI_Barrier(MPI_COMM_WORLD); Write_message(mpirank, "after the barrier"); if (mpirank == 0) n = 5; MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD); cout << "n is equal to " << n << " for process " << mpirank << endl; time = MPI_Wtime() - time; if (mpirank==0) cout << "This program has run on " << mpisize << " processes in " << time << " seconds." << endl; /* Finalize MPI */ MPI_Finalize (); return 0; } /* End of the program */ /**********************************************************/