0% found this document useful (0 votes)
16 views

Lab 11

The document provides instructions for implementing a leader selection algorithm using message passing in MPI. It describes the algorithm, how to set up the MPI environment in Visual Studio, includes example code to test running with multiple processes, and how to execute the code from the command line.

Uploaded by

Nust Razi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lab 11

The document provides instructions for implementing a leader selection algorithm using message passing in MPI. It describes the algorithm, how to set up the MPI environment in Visual Studio, includes example code to test running with multiple processes, and how to execute the code from the command line.

Uploaded by

Nust Razi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Lab-11

Implement the following leader selection algorithm using Message


Passing Interface.

Algorithm

Let Pi be the processor starting the election


(it detected e.g. that the current leader has crashed).
Pi sends a message (i,elect) to its right neighbor (direction of the ring !)
in the ring.
Pj receives a (i,elect) :
if i = j then
Pi is a new coordinator, send (i, Iamtheboss) to its neighbor.
else
Pj sends (max(i,j),elect) to its right neighbor

1. To setup MPI environment in MS Visual Studio 2012, install HPC Pack


2012 MS-MPI redistribution from the following link
https://www.microsoft.com/en-us/download/details.aspx?id=57467
2. Create an empty Visual C++ Win 32 console Application
3. Uncheck the Pre-compiled headers and Security Development Lifecycle
checkboxes
4. Click on empty project check box
5. Right click on your project to add C++ source file
6. Go to project properties to add the additional include and lib files
folder
a. Under C++ tag, select the General tag – Additional Include
Directories and add the path to C:\Program Files
28x8629\Microsoft SDKs\MPI\Include\x86
[check the path in your system]

b. Click on Linker tab under properties – General – Additional


Libraries and browse to lib folder i.e. C:\Program Files
28x8629\Microsoft SDKs\MPI\Lib\x86
[Check the path on your system]
c. Remember to include the x86 architecture
d. Again click on Linker – Input – Additional Dependencies and add
“msmpi.lib”
e. Press apply and write the below mentioned code. It should run
with default number of processes.

#include "stdafx.h"
#include <iostream>
#include "mpi.h"
int main(int argc, char* argv[])
{
//cout << "Hello World\n";
int nTasks, rank;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&nTasks);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
printf ("Number of threads = %d, My rank = %d\n", nTasks, rank);
int a;
std::cin>>a;
MPI_Finalize();
return 0;
}

After compiling the code, you can also run from the command line using
following command.

a. Open command prompt, CD to the directory where .exe file is place


b. Type mpiexec –np 5 text.exe
c. It will display following output

You might also like