Advertisement
mokahato

Project 8

Apr 17th, 2019
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.40 KB | None | 0 0
  1.  /**
  2.  * COSC 1020 - Project 8
  3.  * This class opens a file with two squares matrices and uses concurrent programming to multiply them.
  4.  * @author Chris Nadolny John Austin
  5.  * Instructor Jain
  6.  * TA-BOT:MAILTO  chris.nadolny@marquette.edu john.austin@marquette.edu
  7.  */
  8. import java.util.Arrays;
  9. import java.util.Scanner;
  10. import java.io.File;
  11. import java.io.FileNotFoundException;
  12.  
  13. public class RouteMatrixThread extends Thread {
  14.  
  15.     private static int[][][] ma;
  16.     private static int[][][] mb;
  17.     private static int[][][] result;
  18.     private static int numb;
  19.  
  20.     private int lower;
  21.     private int upper;
  22.    
  23.     public static int[][][] getResult() {
  24.         return result;
  25.     }
  26.    
  27.     public static void print() {
  28.     System.out.println("Result Matrix:");
  29.     for (int[][] row : result) {
  30.         for (int[] list : row)
  31.         if (list == null)
  32.             System.out.print("[] ");
  33.         else {
  34.             System.out.print("[");
  35.             for (int i=0; i<list.length; i++) {
  36.             System.out.print(list[i]);
  37.             if (i < list.length-1)
  38.                 System.out.print(",");
  39.             }
  40.             System.out.print("] ");
  41.         }
  42.         System.out.println();
  43.     }
  44.     }
  45.  
  46.     /* The method returns null, if any of the following conditions apply:
  47.        - either of the input arrays is null;
  48.        - the last integer of the first argument array is not equal
  49.          to the first integer of the second argument array;
  50.        - the concatenated path would form a loop (i.e. it has duplicates).
  51.  
  52.        Use your Project 4 implementation.
  53.      */
  54.     private static int[] concatenate (int[] a, int[] b) {
  55.         if (a==null || b==null)
  56.             return null;
  57.         int [] a2 = Arrays.copyOf(a, a.length);
  58.         int [] b2 = Arrays.copyOf(b, b.length);
  59.         Arrays.sort(a2);
  60.         Arrays.sort(b2);
  61.         int countA = 0;
  62.         int countB = 0;
  63.         int [] c = new int[a.length+b.length-1];
  64.         for (int i = 0; i < a2.length-1; i++)
  65.             if (a2[i] == a2[i+1])
  66.                 countA++;
  67.         for (int i = 0; i < b2.length-1; i++)
  68.             if (b2[i] == b2[i+1])
  69.                 countB++;
  70.         if (a[a.length-1]==b[0]) {
  71.             if (countA == 0 && countB == 0) {
  72.                 System.arraycopy(a, 0, c, 0, a.length-1);
  73.                 System.arraycopy(b, 0, c, a.length-1, b.length);
  74.             }
  75.         }
  76.         else return null;
  77.         int [] c2 = Arrays.copyOf(c, c.length);
  78.         Arrays.sort(c2);
  79.         int countC = 0;
  80.         for (int i=0; i<c2.length-1; i++)
  81.             if (c2[i] == c2[i+1])
  82.                 countC++;
  83.         if (countC != 0) return null;
  84.        
  85.         return c;
  86.     }    
  87.     public RouteMatrixThread(int lower, int upper) {
  88.         this.lower = lower;
  89.         this.upper = upper;
  90.     }
  91.    
  92.     private static int[] shortest(int[][] a) {
  93.         int[] lengths = new int[a.length];
  94.         for (int x = 0; x < a.length; x++) {
  95.             if (a[x]!= null)
  96.                 lengths[x] = a[x].length;
  97.         }
  98.         int shortest = lengths[0];
  99.         int shortestVariable = 0;
  100.         if (lengths.length!= 0) {
  101.             for (int z = 0; z < lengths.length; z++)
  102.             {
  103.                 if(z!=lengths.length-1 && lengths[z] > lengths[z+1])
  104.                 {
  105.                     shortest = lengths[z];
  106.                     shortestVariable = z;
  107.                 }
  108.             }
  109.             return a[shortestVariable];
  110.         }
  111.         else
  112.             return null;
  113.     }
  114.    
  115.     public static void main(String[] args) {
  116.     if (args.length < 2) {
  117.         System.out.print("You need to provide a file name and the number of threads!");
  118.         System.out.println(" Exiting...");
  119.         System.exit(0);
  120.     }
  121.    
  122.     ma = null;
  123.     mb = null;
  124.     int size = 0;
  125.  
  126.     // Read the two matrices from the input file
  127.     // =========================================
  128.     File file = new File(args[0]);
  129.     Scanner in = null;
  130.     try {
  131.         in = new Scanner(file);
  132.  
  133.         size = in.nextInt();
  134.         ma = new int[size][size][];
  135.         mb = new int[size][size][];
  136.         result = new int[size][size][];
  137.         int row = 0;
  138.         int col = 0;
  139.         int round = 1;
  140.         int[][][] m = ma;
  141.         System.out.println("Reading in Matrix A...");
  142.         while( in.hasNextLine() && round <= 2) {
  143.         String line = in.nextLine();
  144.         if (line.length() <= 1)
  145.             continue;
  146.         String[] path = line.split(" ");
  147.         for (String s : path) {
  148.             if (s.length() == 2) { // s is "[]"
  149.             m[row][col] = null; // unnecessary, only making it explicit
  150.             }
  151.             if (s.length() > 2) {
  152.             s = s.substring(1, s.length()-1); // drop opening and closing square brackets []
  153.             String[] node = s.split(",");
  154.             m[row][col] = new int[node.length];
  155.             for (int k=0; k<node.length; k++) {
  156.                 m[row][col][k] = Integer.parseInt(node[k]);
  157.             }
  158.             }
  159.             col++;
  160.             if (col == size)
  161.             col = 0;
  162.         }
  163.         row++;
  164.         if (row == size) {
  165.             if (round == 1) { // time to start reading in the second matrix
  166.             row = 0;
  167.             m = mb;
  168.             System.out.print("\n\nReading in Matrix B...");
  169.             }
  170.             round++;
  171.         }
  172.         }
  173.     }
  174.     catch( FileNotFoundException e ) {
  175.         System.out.println("File " + file.getName() + " not found!");
  176.     }
  177.     finally { if( in != null ) in.close(); }
  178.  
  179.     System.out.println();
  180.  
  181.     int n = Integer.parseInt(args[1]); // the number of threads the user specified
  182.     numb = size;
  183.    
  184.     // Create Threads to Compute the Result Matrix
  185.     RouteMatrixThread[] threads = new RouteMatrixThread[n];
  186.     int count_lower = 0;
  187.     for (int i=0; i<threads.length; i++ ) {
  188.         threads[i] = new RouteMatrixThread(count_lower, size);
  189.         threads[i].start();
  190.         count_lower++;
  191.     }try {
  192.         for (int i=0; i<threads.length;i++)
  193.             threads[i].join();
  194.     }
  195.     catch(InterruptedException e) {
  196.         System.out.print("Thread didn't finish");
  197.     }
  198.     // ===========================================
  199.     print();
  200.    
  201.    
  202.    
  203.  
  204.     }
  205.  
  206.     @Override
  207.     public void run() {
  208.         int[][] temp = new int[numb+1][0];
  209.        
  210.         for (int i=lower; i<upper; i++) {
  211.             for (int j = 0; j < ma[i].length; j++) {
  212.                 for (int k=0; k < mb.length; k++) {
  213.                     temp[k] = concatenate(ma[i][k], mb[k][j]);
  214.                 }
  215.                 result[i][j] = shortest(temp);
  216.                
  217.             }
  218.         }
  219.  
  220.     }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement