Question

In C++ and MPI: Write a distributed program in MPI that simulates one of the leader...

In C++ and MPI:

Write a distributed program in MPI that simulates one of the leader election algorithms learnt in class for a ring topology using a fixed number of processors.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include "mpi.h"

#include <iostream>

#include <stdlib.h>

using namespace std;

int main(int argc, char* argv[])

{

int size, rank, tag, rc, min, lcounter;

double t1, t2;

int inmsg1; //incoming msg from right

int inmsg2; //incoming msg from left

int msg; //outgoing msg

int stage=1; //stage of election

MPI_Status Stat;

MPI_Request send_req, recv_req;

rc=MPI_Init(&argc,&argv);

bool terminated=false;

bool relayer=false;

if (rc!=0) {cout << "Error starting MPI." << endl; MPI_Abort(MPI_COMM_WORLD, rc);}

MPI_Comm_size(MPI_COMM_WORLD, &size); //size of world

MPI_Comm_rank(MPI_COMM_WORLD, &rank); //my ID

t1 = MPI_Wtime();

msg=rank;

while (!terminated){

//receive ID with counter from left and right

if (rank==0){

MPI_Isend(&msg, 1, MPI_INT, 1, 1, MPI_COMM_WORLD, &send_req);

MPI_Recv(&inmsg1, 1, MPI_INT, size-1, 1, MPI_COMM_WORLD, &Stat);

MPI_Isend(&msg, 1, MPI_INT, size-1, 1, MPI_COMM_WORLD, &send_req);

MPI_Recv(&inmsg2, 1, MPI_INT, 1, 1, MPI_COMM_WORLD, &Stat);

}

else if (rank==size-1){

MPI_Isend(&msg, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &send_req);

MPI_Recv(&inmsg1, 1, MPI_INT, rank-1, 1, MPI_COMM_WORLD, &Stat);

MPI_Isend(&msg, 1, MPI_INT, rank-1, 1, MPI_COMM_WORLD, &send_req);

MPI_Recv(&inmsg2, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &Stat);

}

else {

MPI_Isend(&msg, 1, MPI_INT, rank+1, 1, MPI_COMM_WORLD, &send_req);

MPI_Recv(&inmsg1, 1, MPI_INT, rank-1, 1, MPI_COMM_WORLD, &Stat);

MPI_Isend(&msg, 1, MPI_INT, rank-1, 1, MPI_COMM_WORLD, &send_req);

MPI_Recv(&inmsg2, 1, MPI_INT, rank+1, 1, MPI_COMM_WORLD, &Stat);

}

//forward the message if relayer

if (relayer) {

if (rank==0) {

MPI_Send(&inmsg1, 1, MPI_INT, 1, 1, MPI_COMM_WORLD);

MPI_Send(&inmsg2, 1, MPI_INT, size-1, 1, MPI_COMM_WORLD);

}

else if (rank==size-1) {

MPI_Send(&inmsg1, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);

MPI_Send(&inmsg2, 1, MPI_INT, rank-1, 1, MPI_COMM_WORLD);

}

else {

MPI_Send(&inmsg1, 1, MPI_INT, rank+1, 1, MPI_COMM_WORLD);

MPI_Send(&inmsg2, 1, MPI_INT, rank-1, 1, MPI_COMM_WORLD);

}

}

if (!relayer){

if (inmsg1<rank || inmsg2<rank)

{

relayer=true; //become relayer or survive

// cout << "Node " << rank << " became a relayer" << endl;

}

else if (inmsg1==rank && inmsg2==rank) {

terminated=true;

cout << "Node " << rank << " elected as a leader!" << endl;

t2 = MPI_Wtime();

cout << endl << "Elapsed time: " << (t2-t1) << " seconds" << endl;

MPI_Finalize();

break;

}

}

}

t2 = MPI_Wtime();

MPI_Finalize();

if (rank==min) cout << endl << "Elapsed time: " << (t2-t1) << " seconds" << endl;

}

Add a comment
Know the answer?
Add Answer to:
In C++ and MPI: Write a distributed program in MPI that simulates one of the leader...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using C++ Instructions Person Student Employee Write a program that simulates the three classes defined as...

    Using C++ Instructions Person Student Employee Write a program that simulates the three classes defined as shown: a. Use name as the only data member for the Person class. b. Use name and gpa as data members for the Student class. c. Use name and salary as data members for the Employee class. Demonstrate the relationship in a main function.

  • Write a C++ program that simulates tossing a coin. Allow the user to enter the number...

    Write a C++ program that simulates tossing a coin. Allow the user to enter the number of tosses. Print the number of tosses that yielded heads and the number of tosses that yielded tails. use function

  • C only (not C++) Write a C program that simulates a game. There are two players...

    C only (not C++) Write a C program that simulates a game. There are two players (called Player1 and Player2). At the start of the game, each player chooses a number from 1 to 10 (cannot be the same). Then, the program will generate a random number from 1 to 10. The game ends when either Player1 or Player2 hits the number generated by the program. Display the winning message for the winner. Example output: Player 1: 6 Player 2:...

  • Can you write this program MPI Simplest easiest way to read it as possible For parallel...

    Can you write this program MPI Simplest easiest way to read it as possible For parallel programming This exercise is from Question 6-5 of the textbook at page 194 Implement the butterfly barrier described in Section 6.1.4, assume the number of processes is a power of 2, e.g.2,4, 8, 16, etc. Investigate the time taken by your butterfly barrier by using the code such as: t1 = MP1wtime(); butterfly barrierO;//call to your butterfly barrier t2 MPIwtime(); printf Elapsed time %d...

  • write a C/C++ program which simulates the Producer/Consumer program in Figure 5.16 with a buffer size...

    write a C/C++ program which simulates the Producer/Consumer program in Figure 5.16 with a buffer size of one thousand. Allow the Producer to generate one million items. Use ten consumers. The program needs to perform a normal exit process after all items are consumed. Both the Producer (singular) and Consumers are to be runs as separate processes generated via fork(). The program must us Linux semaphores. The program must clean up the semaphores used and zombies created before termination. Report...

  • write a C or Fortran program that sends a token in a ring for an arbitrary number of nodes: Create a directory in your h...

    write a C or Fortran program that sends a token in a ring for an arbitrary number of nodes: Create a directory in your home directory titled exactly PA1. Make sure you have ALL code located in this directory. 4. Write a makefile that will compile your code. 5. Use MPI to send a token in a ring for any number of nodes 6. Verify that your code is working correctly. 7 Write a C program that sends a token...

  • Write a C++ Program that simulates a basic calculator using functions which performs the operations of...

    Write a C++ Program that simulates a basic calculator using functions which performs the operations of Addition, Subtraction, multiplication, and Division. ( make sure you cover the case to avoid division by a zero) Display a menu for list of operations that can be calculated and get the input from user about his choice of calculation. Based on user choice of operation, take the input of number/numbers from user. Assume all input values are of type double. Calculations must be...

  • Write a C++ Program that simulates a basic calculator using functions which performs the operations of...

    Write a C++ Program that simulates a basic calculator using functions which performs the operations of Addition, Subtraction, multiplication, and Division. ( make sure you cover the case to avoid division by a zero) Display a menu for the list of operations that can be calculated and get the input from the user about his choice of calculation. Based on user choice of operation, take the input of number/numbers from user. Assume all input values are of type double. Calculations...

  • C# prograaming language 1. write a program that generates 10,000 random integers in the range of ...

    c# prograaming language 1. write a program that generates 10,000 random integers in the range of 0-9 and them in binary search tree. 2. use any sort algorithm (insertion, bubble, quick...) to display a list of each of the integers and how many times they appear. 3. at last, add ruction to the BinarySearchTree class that count the number of edges in a tree. Write a program that generates 10,000 random integers in the range of 0-9 and store them...

  • Write a C++ program that simulates coin tossing. For each toss of the coin the program...

    Write a C++ program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. The program should toss a coin 100 times. Count the number of times each side of the coin appears and print the results at the end of the 100 tosses.   The program should have the following functions as a minimum: void toss() - called from main() and will randomly toss the coin and set a variable equal to the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT