Professional Documents
Culture Documents
Distributed Computing
Lab2 : Point-to-Point Communication
Am al Said Khalifa
Lab 2
Point-to-Point Communications
There are two kinds of communication for sending and receiving messages via MPI
point-to-point facilities, blocking and non-blocking. The blocking point-to-point operations will
wait until a communication has completed on its local processor before continuing. For example,
a blocking Send operation will not return until the message has entered into MPI's internal
buffers to be transmitted, while a blocking Receive operation will wait until a message has been
received and completely decoded before returning. MPI’s non-blocking point-to-point operations,
on the other hand, will initiate a communication without waiting for that communication to be
completed. Instead, a Request object, which can be used to query, complete, or cancel the
communication, will be returned. For our initial examples, we will use blocking communication.
1
Lab 2
Ping Pong:
For our first example of point-to-point communication, we will write a program in which Task 0
pings task 1 and awaits return pong. This program is executed on two processes only..
#include "mpi.h"
#include <iostream>
int MyId, P;
int source, dest;
char msg = 'x';
MPI::Init(argc,argv);
P = MPI::COMM_WORLD.Get_size();
MyId = MPI::COMM_WORLD.Get_rank();
if ( MyId == 0)
{
source = 1;
dest = 1;
MPI::Finalize();
2
Lab 2
#include "mpi.h"
#include <iostream>
MPI::Init(argc,argv);
P = MPI::COMM_WORLD.Get_size();
MyId = MPI::COMM_WORLD.Get_rank();
if ( MyId == 0)
{
cout << "enter a number >> ";
cin>> x;
MPI::COMM_WORLD.Send(&x, 1, MPI::INT, MyId+1, 0);
MPI::COMM_WORLD.Recv(&x, 1, MPI::INT, P-1, 0);
cout<<"Message Recieved, value = "<< x;
cout<<"\n All done!!";
}
else
{
MPI::COMM_WORLD.Recv(&x, 1, MPI::INT, MyId-1, 0);
cout<<"This is node no. "<<MyId<< ". The message was
recieved"<<endl;
MPI::COMM_WORLD.Send(&x, 1, MPI::INT, (MyId+1)%P, 0);
MPI::Finalize();
return 0;
3
Lab 2
Parallel Computations
The following program must be run on at least 4 machines. The instructions are to have
the master process generate some integers, send them to process 3 which will use some of
those values to generate some real numbers that will then be sent back to the master. The
instructions are as follows:
#include "mpi.h"
#include <iostream>
4
Lab 2
// Process 1 knows that it will generate 200 integers, and may receive no
// more than 200 reals.
if ( rank == 1 )
{
count = 200;
for ( i = 0; i < count; i++ )
i_buffer[i] = i * i;
dest = 3;
tag = 1;
MPI::COMM_WORLD.Send ( i_buffer, count, MPI::INT, dest, tag );
cout << "P:" << rank << " sent " << count
<< " integers to process " << dest << ".\n";
source = 3;
tag = 2;
MPI::COMM_WORLD.Recv ( r_buffer, 200, MPI::FLOAT, source, tag,
status );
cout << "\nP:" << rank << " received real values from process 3.\n";
count = status.Get_count ( MPI::FLOAT );
cout << "P:" << rank << " Number of real values received is "
<< count << ".\n";
cout << "P:" << rank << " First 3 values = "
<< r_buffer[0] << " "
<< r_buffer[1] << " "
<< r_buffer[2] << "\n";
}
//
// Process 3 receives the integer data from process 1, selects some of the
// data, does a real computation on it, and sends that part back to process 1.
else if ( rank == 3 )
{
source = 1;
tag = 1;
count2 = 0;
5
Lab 2
dest = 1;
tag = 2;
MPI::COMM_WORLD.Send ( r_buffer, count2, MPI::FLOAT, dest, tag );
cout << "P:" << rank << " sent " << count2 << " reals to process "
<< dest << ".\n";
}
else
{
cout << "\n";
cout << "P:" << rank << " - MPI has no work for me!\n";
}
MPI::Finalize ( );
return 0;
}
Assignment: