import java.util.*;
import java.io.*;
import java.net.*;
import java.lang.*;

public class matrixmast 
{
   public static void main(String args[]) throws IOException
   {
      int job_id = 1;
      int task_id = 100;

      int main_pno;
      String main_pt;
      int gp_no;
      String gp_name;
      int pt_no;
      String  pt_name;
      String str = "";

      int i, j, status;
      int nl;
      int matA[][] = new int[10][10];
      int matB[][] = new int[10][10];
      int matC[][] = new int[10][10];

      JMPF def_obj = new JMPF(job_id, task_id);
      System.out.println("The hostname is " + def_obj.hostname);
      System.out.println("The host id is " + def_obj.host_id);

      gp_name = "gp1";
      gp_no = def_obj.CreatePortGroup(gp_name);

      System.out.println("Enter the no. of rows in matrix A : ");
      int rowA = System.in.read() - 48;
      nl = System.in.read();

      System.out.println("The rowA is " + rowA);
      System.out.println("Enter the no. of cols in matrix A : ");
      int colA = System.in.read() - 48;
      nl = System.in.read();

      System.out.println("The rows & cols are " + rowA + "&" + colA);

      System.out.println("Enter the values of matrix A : " );
      for (i = 0; i < rowA; i++)
      {
         for (j = 0; j < colA; j++)
         {
            matA[i][j] = System.in.read() - 48;
            nl = System.in.read();
         }
      }

      System.out.println("Enter the no. of rows in matrix B : ");
      int rowB = System.in.read() - 48;
      nl = System.in.read();
      System.out.println("Enter the no. of cols in matrix B : ");
      int colB = System.in.read() - 48;
      nl = System.in.read();

      System.out.println("Enter the values of matrix B : " );
      for (i = 0; i < rowB; i++)
      {
         for (j = 0; j < colB; j++)
         {
            matB[i][j] = System.in.read() - 48;
            nl = System.in.read();
         }
      }
 
      System.out.println("The values of matrix A : " );
      for (i = 0; i < rowA; i++)
      {
         for (j = 0; j < colA; j++)
            System.out.print(matA[i][j] + "   ");
         System.out.println("\n");
      }

      System.out.println("The values of matrix B : " );
      for (i = 0; i < rowB; i++)
      {
         for (j = 0; j < colB; j++)
            System.out.print(matB[i][j] + "   ");
         System.out.println("\n");
      }

      main_pt = "main";
      main_pno = def_obj.CreatePort(main_pt);


      def_obj.SyncsendGroup(gp_name, rowA);
      def_obj.SyncsendGroup(gp_name, colA); 
      def_obj.SyncsendGroup(gp_name, rowB); 
      def_obj.SyncsendGroup(gp_name, colB); 


      str = "";
      for (i = 0; i < rowB; i++)
      {
         for (j = 0; j < colB; j++)
         {
            def_obj.SyncsendGroup(gp_name, matB[i][j]); 
//            System.out.println("The data send is : " + matB[i][j]);
         }
      }

      for (i = 0; i < rowA; i++)
      {
         pt_no = def_obj.LocatePort(String.valueOf(i));
         for (j = 0; j < colA; j++)
         {
            status = def_obj.Send(pt_no, matA[i][j]);
//            System.out.println("The data send is : " + matA[i][j]);
//            System.out.println("The status of sending matA is : " + status);
         }
      } 

      Integer S = new Integer(2);
      for (j = 0; j < colB; j++)
      {
         str = "";
         synchronized(S)
         {
            for (i = 0; i < rowA; i++)
            {
               str = def_obj.ReceiveString(main_pno);
               StringTokenizer st = new StringTokenizer(str);
               int row = Integer.parseInt(st.nextToken());
               int col = Integer.parseInt(st.nextToken());
               matC[row][col] = Integer.parseInt(st.nextToken());
            }
         }
      }

      System.out.println("The resultant matrix C : " );
      for (i = 0; i < rowA; i++)
      {
         for (j = 0; j < colB; j++)
            System.out.print(matC[i][j] + "   ");
         System.out.println("\n");
      }

      def_obj.ClosePorts();
   }
}

