/********************************************************** File: barrier_bcast.cc Description: Implementing the barrier and broadcast functions based on a simple send and receive. Author: Dana Vrajitoru Class: B424 B524 Last update: October 2020 **********************************************************/ #include #include #include #include #include #include using namespace std; // Function prototypes. void Barrier(int no_proc, int my_id); void Broadcast(int &data, int source, int no_proc, int my_id); void Write_message(int rank, char *message); void Test_send_recv(int nr_proc, int my_id); // Main function, needs the arguments. int main(int argc, char **argv) { double time; int my_id, nr_proc; int n=0; const int source_proc=2; char cpu_name[80]; /* Initialize MPI */ MPI_Init (&argc, &argv); /* Get the rank of this process */ MPI_Comm_rank (MPI_COMM_WORLD, &my_id); /* Get nb of processes */ MPI_Comm_size (MPI_COMM_WORLD, &nr_proc); time = MPI_Wtime(); // Testing the send and receive just to make sure they work. You can // comment this out if you want. Test_send_recv(nr_proc, my_id); // An example of finding out which machine we are running on. gethostname(cpu_name,80); Write_message(my_id, cpu_name); // Testing the barrier. The before and after messages should not be // mixed on the screen. Write_message(my_id, "before barrier"); Barrier(nr_proc, my_id); Write_message(my_id, "after barrier"); // This will be the source process having a different value for the // variable n to test the broadcast. if (my_id == source_proc) n = 5; // Testing the broadcast. All values of n should be 5 if the number // of processes is greater than the id of the source_proc, 0 // otherwise. Broadcast(n, source_proc, nr_proc, my_id); cout << "n is " << n << endl; time = MPI_Wtime() - time; if (my_id==0) cout << "This program has run on " << nr_proc << " processes in " << time << " seconds." << endl; /* Finalize MPI */ MPI_Finalize (); return 0; } // The function that writes the message on the screen. rank is the // identity of the current process obtained from MPI_Comm_rank. void Write_message(int rank, char *message) { cout << message << " from process " << rank << endl; } // A test for the send and receive functions so that you have an // example for doing the homework. void Test_send_recv(int nr_proc, int my_id) { const int some_tag=0, src=0, dest=1; if (nr_proc > 1) if (my_id == src && nr_proc > 1) { float a = 1.25; cout << "0 sending a to process 1" << endl; MPI_Send(&a, 1, MPI_FLOAT, dest, some_tag, MPI_COMM_WORLD); cout << "0 send done" << endl; } else if (my_id == dest) { float b=0; MPI_Status status; cout << "1 receiving b from 0" << endl; MPI_Recv(&b, 1, MPI_FLOAT, src, some_tag, MPI_COMM_WORLD, &status); cout << "b received: " << b << endl; } } // Reimplementation of the barrier function. The nr_proc is the number // of processes in the program, obtained by calling MPI_Comm_size. // my_id is the identity of the current process, obtained with // MPI_Comm_rank. This function should block all the processes inside // it until all of them have made a call to it. Then it must release // all of them. void Barrier(int nr_proc, int my_id) { // Code to be supplied by the student. } // Reimplementation of the broadcast function with the data being // transmitted being a simple integer. The first parameter is the // variable to be broadcast to all the processes. The source is the // identity of the process who must send the value of the variable to // all the others. The last two parameters are the same as for the // previous function. Note: if the identity of the source process is // greater than nr_proc -1, or less than 0, then the broadcast should // not be doing anything. void Broadcast(int &data, int source, int nr_proc, int my_id) { // Code to be supplied by the student. }