Skip to content

Message Passing Interface

Message Passing Interface

PROBLEM STATEMENT

Write a program to implement a message passing interface (MPI). The program contains two or more clients and one server which host the message passing interface. Message primitives will receive to any process from any other process, which should be displayed on the output window of the destination process. Any client can select any primitive of the following: MPI_bsend, MPI_sendrecv and MPI_recv.

DESCRIPTION

MPI is a de facto standard for communication among the processes modeling a parallel program on a distributed memory system

The need to be hardware independent eventually led to the definition of a standard for message passing, simply called the Message-Passing Interface or MPI.

MPI is designed for parallel application. MPI assumes communication tales place within a known group of processes. Each group is assigned as identifier.

Some of the most intuitive message-passing primitives of MPI.

Primitive
Meaning
MPI_bsend
Append outgoing message to local send buffer.
MPI_send
Send a message and wait until copied to local or remote buffer.
MPI_ssend
Send a message and wait until receipt starts.
MPI_sendrecv
Send a message and wait for reply.
MPI_isend
Pass reference to outgoing message, and continue.
MPI_issend
Pass reference to outgoing message, and wait until receipt starts.
MPI_recv
Receive a message; block if there is none.
MPI_irecv
Check if there is an incoming message, but do not block.

SOURCE PROGRAM

MPIServer.java

import java.io.*;
import java.net.*;

public class MPIServer
{

    public static void main(String args[])throws Exception
        {
            InetAddress lclhost=InetAddress.getLocalHost();
            while(true)
                {
                    Server s=new Server(lclhost);
                    s.recPort(8002);
                    s.sendPort(9001);
                    s.recData();  
                }
        }
}

class Server
{
    InetAddress lclhost;
    int recport,sendport;
    Server(InetAddress lclhost)
        {
            this.lclhost=lclhost;
        }
   
    void recPort(int recport)
        {
            this.recport=recport;
        }
    void sendPort(int sendport)
        {
            this.sendport=sendport;
        }

    void recData()throws Exception
        {
            String str;
            byte[] buff=new byte[256];
            DatagramSocket ds;
            DatagramPacket dp;  
            BufferedReader br;
            ds=new DatagramSocket(recport);
            dp=new DatagramPacket(buff,buff.length);
            ds.receive(dp);
            ds.close();
            str=new String(dp.getData(),0,dp.getLength());  
            System.out.println(“Message = “+str);
            if(str.startsWith(“MPI_Send”)==false)
            sendData();
   
          
        }

    void sendData()throws Exception
        {
            DatagramPacket dp;
            DatagramSocket ds;
            BufferedReader br;
          
            System.out.println(“Enter the data”);
            br=new BufferedReader(new InputStreamReader(System.in));
            String str=br.readLine();
            ds=new DatagramSocket(sendport);
            dp=new DatagramPacket(str.getBytes(),str.length(),lclhost,sendport-1000);
            ds.send(dp);
            ds.close();  
        }
      
}  

MPIClientOne.java

import java.io.*;
import java.net.*;

public class MPIClientOne
{
   
    public static void main(String args[])throws Exception
        {
            boolean cond=true;
            InetAddress ip;
            BufferedReader br;
            ip=InetAddress.getLocalHost();
            Client c=new Client(ip);
            c.sendPort(9002);
            c.recPort(8001);
      
            while(cond)
            {
    System.out.println(“Select the operation to be performed”);
    System.out.println(“1. To send message without any response”);        System.out.println(“2. To send message with response”);
    System.out.println(“3. To recieve message”);
    System.out.println(“4. To end the program”);

                br=new BufferedReader(new InputStreamReader(System.in));
                String str=br.readLine();
                if(str.equals(“1”))
                {
                    c.MPI_Send();
                }
          
                if(str.equals(“2”))
                {
                    c.MPI_SendRec();
                }
                if(str.equals(“3”))
                {
                    c.MPI_Rec();
                }
                if(str.equals(“4”))
                {
                    cond = false;
                }

            }                      
    }
}

class Client
{
    InetAddress ip;
    int sendport,recport;

    Client(InetAddress ip)
    {
        this.ip=ip;
    }
   
    void sendPort(int sendport)
    {
        this.sendport = sendport;
    }
   
    void recPort(int recport)
    {
        this.recport = recport;
    }
   
    void MPI_Send()throws Exception
    {
        DatagramSocket dc;
        DatagramPacket dp;
        BufferedReader br;
        String str;
        br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println(“Enter the data:”);
        str = “MPI_Send ” + br.readLine();
        dc = new DatagramSocket(sendport);
        dp = new         DatagramPacket(str.getBytes(),str.length(),ip,sendport-1000);
        dc.send(dp);
        dc.close();
    }
   
    void MPI_SendRec()throws Exception
    {
        DatagramSocket sendds,recds;
        DatagramPacket senddp,recdp;
        BufferedReader br;
        byte[] buf=new byte[256];
        String str,msgstr;      
        System.out.println(“Enter the data”);
        br = new BufferedReader(new InputStreamReader(System.in));
        str = “MPI_SdRec ” + br.readLine();      
        sendds = new DatagramSocket(sendport);
        senddp = new             DatagramPacket(str.getBytes(),str.length(),ip,sendport-1000);
        sendds.send(senddp);
        sendds.close();
      
        recds = new DatagramSocket(recport);
        recdp = new DatagramPacket(buf,buf.length);
        recds.receive(recdp);
        recds.close();
   
        msgstr = new String(recdp.getData(),0,recdp.getLength());
        System.out.println(“The Received data is ” + msgstr);
    }

        void MPI_Rec()throws Exception
        {
        byte[] buf = new byte[256];
        DatagramSocket sendds,recds;
        DatagramPacket senddp,recdp;
      
        String str,msgstr;      
str = “MPI_Rec”;      
        sendds = new DatagramSocket(sendport);
        senddp = new                     DatagramPacket(str.getBytes(),str.length(),ip,sendport-1000);
        sendds.send(senddp);
        sendds.close();

        recds = new DatagramSocket(recport);
        recdp = new DatagramPacket(buf,buf.length);
        recds.receive(recdp);
        recds.close();
   
        msgstr = new String(recdp.getData(),0,recdp.getLength());
        System.out.println(“The Received data is” + msgstr);
   
    }
   
}

MPIClientTwo.java

import java.io.*;
import java.net.*;

public class MPIClientTwo
{
   
    public static void main(String args[])throws Exception
        {
            boolean cond=true;
            InetAddress ip;
            BufferedReader br;
            ip=InetAddress.getLocalHost();
            Client c=new Client(ip);
            c.sendPort(9002);
            c.recPort(8001);
      
            while(cond)
            {
    System.out.println(“Select the operation to be performed”);
    System.out.println(“1. To send message without any response”);        System.out.println(“2. To send message with response”);
    System.out.println(“3. To recieve message”);
    System.out.println(“4. To end the program”);

    br=new BufferedReader(new InputStreamReader(System.in));
                String str=br.readLine();
          
                if(str.equals(“1”))
                {
                    c.MPI_Send();
                }
          
                if(str.equals(“2”))
                {
                    c.MPI_SendRec();
                }
                if(str.equals(“3”))
                {
                    c.MPI_Rec();
                }
                if(str.equals(“4”))
                {
                    cond = false;
                }

            }                      
    }
}

class Client
{
    InetAddress ip;
    int sendport,recport;

    Client(InetAddress ip)
    {
        this.ip=ip;
    }
   
    void sendPort(int sendport)
    {
        this.sendport = sendport;
    }
   
    void recPort(int recport)
    {
        this.recport = recport;
    }
   
    void MPI_Send()throws Exception
    {
        DatagramSocket dc;
        DatagramPacket dp;
        BufferedReader br;
        String str;
        br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println(“enter the data”);
        str = “MPI_Send ” + br.readLine();
        dc = new DatagramSocket(sendport);
        dp = new         DatagramPacket(str.getBytes(),str.length(),ip,sendport-1000);
        dc.send(dp);
        dc.close();
    }
   
    void MPI_SendRec()throws Exception
    {
        DatagramSocket sendds,recds;
        DatagramPacket senddp,recdp;
        BufferedReader br;
        byte[] buf=new byte[256];
        String str,msgstr;      
        System.out.println(“Enter the Data”);
        br = new BufferedReader(new InputStreamReader(System.in));
        str = “MPI_SdRec ” + br.readLine();      
      
        sendds = new DatagramSocket(sendport);
        senddp = new             DatagramPacket(str.getBytes(),str.length(),ip,sendport-1000);
        sendds.send(senddp);
        sendds.close();
      
        recds = new DatagramSocket(recport);
        recdp = new DatagramPacket(buf,buf.length);
        recds.receive(recdp);
        recds.close();
   
        msgstr = new String(recdp.getData(),0,recdp.getLength());
        System.out.println(“The Received data is” + msgstr);
    }

        void MPI_Rec()throws Exception
    {
        byte[] buf = new byte[256];
        DatagramSocket sendds,recds;
        DatagramPacket senddp,recdp;
        String str,msgstr;      
str = “MPI_Rec”;      
        sendds = new DatagramSocket(sendport);
        senddp = new                     DatagramPacket(str.getBytes(),str.length(),ip,sendport-1000);
        sendds.send(senddp);
        sendds.close();

        recds = new DatagramSocket(recport);
        recdp = new DatagramPacket(buf,buf.length);
        recds.receive(recdp);
        recds.close();
   
        msgstr = new String(recdp.getData(),0,recdp.getLength());
        System.out.println(“The Received data is ” + msgstr);
   
    }
}

OUTPUT

MPIServer O/P

E:pracsDCpracsMPI_prac3>java MPIServer
Message = MPI_Send hii
Message = MPI_SdRec hello
Enter the data
how r u?
Message = MPI_Rec
Enter the data
welcome
Message = MPI_Send how r u?
Message = MPI_SdRec so wats up
Enter the data
nothing

E:pracsDCpracsMPI_prac3>java MPIServer
Message = MPI_Send hii
Message = MPI_SdRec how r u?
Enter the data
Fine

MPIClientOne O/P

E:pracsDCpracsMPI_prac3>java MPIClientOne
Select the operation to be performed
1. To send message without any response
2. To send message with response
3. To recieve message
4. To end the program
1
enter the data
hii
Select the operation to be performed
1. To send message without any response
2. To send message with response
3. To recieve message
4. To end the program

MPIClientTwo O/P

E:pracsDCpracsMPI_prac3>java MPIClientTwo
Select the operation to be performed
1. To send message without any response
2. To send message with response
3. To recieve message
4. To end the program
2
Enter the Data
how r u?
The Received data isfine
Select the operation to be performed
1. To send message without any response
2. To send message with response
3. To recieve message
4. To end the program

2 thoughts on “Message Passing Interface”

  1. MPI is for parallel sending and receiving .can the clients send and receive the data at the same time?MPI have collective operations for group communications.This program throws an error if two clients want to send or receive messages at the same time.

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!