Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * COSC 1020 - Project 8
- * This class opens a file with two squares matrices and uses concurrent programming to multiply them.
- * @author Chris Nadolny John Austin
- * Instructor Jain
- * TA-BOT:MAILTO chris.nadolny@marquette.edu john.austin@marquette.edu
- */
- import java.util.Arrays;
- import java.util.Scanner;
- import java.io.File;
- import java.io.FileNotFoundException;
- public class RouteMatrixThread extends Thread {
- private static int[][][] ma;
- private static int[][][] mb;
- private static int[][][] result;
- private static int numb;
- private int lower;
- private int upper;
- public static int[][][] getResult() {
- return result;
- }
- public static void print() {
- System.out.println("Result Matrix:");
- for (int[][] row : result) {
- for (int[] list : row)
- if (list == null)
- System.out.print("[] ");
- else {
- System.out.print("[");
- for (int i=0; i<list.length; i++) {
- System.out.print(list[i]);
- if (i < list.length-1)
- System.out.print(",");
- }
- System.out.print("] ");
- }
- System.out.println();
- }
- }
- /* The method returns null, if any of the following conditions apply:
- - either of the input arrays is null;
- - the last integer of the first argument array is not equal
- to the first integer of the second argument array;
- - the concatenated path would form a loop (i.e. it has duplicates).
- Use your Project 4 implementation.
- */
- private static int[] concatenate (int[] a, int[] b) {
- if (a==null || b==null)
- return null;
- int [] a2 = Arrays.copyOf(a, a.length);
- int [] b2 = Arrays.copyOf(b, b.length);
- Arrays.sort(a2);
- Arrays.sort(b2);
- int countA = 0;
- int countB = 0;
- int [] c = new int[a.length+b.length-1];
- for (int i = 0; i < a2.length-1; i++)
- if (a2[i] == a2[i+1])
- countA++;
- for (int i = 0; i < b2.length-1; i++)
- if (b2[i] == b2[i+1])
- countB++;
- if (a[a.length-1]==b[0]) {
- if (countA == 0 && countB == 0) {
- System.arraycopy(a, 0, c, 0, a.length-1);
- System.arraycopy(b, 0, c, a.length-1, b.length);
- }
- }
- else return null;
- int [] c2 = Arrays.copyOf(c, c.length);
- Arrays.sort(c2);
- int countC = 0;
- for (int i=0; i<c2.length-1; i++)
- if (c2[i] == c2[i+1])
- countC++;
- if (countC != 0) return null;
- return c;
- }
- public RouteMatrixThread(int lower, int upper) {
- this.lower = lower;
- this.upper = upper;
- }
- private static int[] shortest(int[][] a) {
- int[] lengths = new int[a.length];
- for (int x = 0; x < a.length; x++) {
- if (a[x]!= null)
- lengths[x] = a[x].length;
- }
- int shortest = lengths[0];
- int shortestVariable = 0;
- if (lengths.length!= 0) {
- for (int z = 0; z < lengths.length; z++)
- {
- if(z!=lengths.length-1 && lengths[z] > lengths[z+1])
- {
- shortest = lengths[z];
- shortestVariable = z;
- }
- }
- return a[shortestVariable];
- }
- else
- return null;
- }
- public static void main(String[] args) {
- if (args.length < 2) {
- System.out.print("You need to provide a file name and the number of threads!");
- System.out.println(" Exiting...");
- System.exit(0);
- }
- ma = null;
- mb = null;
- int size = 0;
- // Read the two matrices from the input file
- // =========================================
- File file = new File(args[0]);
- Scanner in = null;
- try {
- in = new Scanner(file);
- size = in.nextInt();
- ma = new int[size][size][];
- mb = new int[size][size][];
- result = new int[size][size][];
- int row = 0;
- int col = 0;
- int round = 1;
- int[][][] m = ma;
- System.out.println("Reading in Matrix A...");
- while( in.hasNextLine() && round <= 2) {
- String line = in.nextLine();
- if (line.length() <= 1)
- continue;
- String[] path = line.split(" ");
- for (String s : path) {
- if (s.length() == 2) { // s is "[]"
- m[row][col] = null; // unnecessary, only making it explicit
- }
- if (s.length() > 2) {
- s = s.substring(1, s.length()-1); // drop opening and closing square brackets []
- String[] node = s.split(",");
- m[row][col] = new int[node.length];
- for (int k=0; k<node.length; k++) {
- m[row][col][k] = Integer.parseInt(node[k]);
- }
- }
- col++;
- if (col == size)
- col = 0;
- }
- row++;
- if (row == size) {
- if (round == 1) { // time to start reading in the second matrix
- row = 0;
- m = mb;
- System.out.print("\n\nReading in Matrix B...");
- }
- round++;
- }
- }
- }
- catch( FileNotFoundException e ) {
- System.out.println("File " + file.getName() + " not found!");
- }
- finally { if( in != null ) in.close(); }
- System.out.println();
- int n = Integer.parseInt(args[1]); // the number of threads the user specified
- numb = size;
- // Create Threads to Compute the Result Matrix
- RouteMatrixThread[] threads = new RouteMatrixThread[n];
- int count_lower = 0;
- for (int i=0; i<threads.length; i++ ) {
- threads[i] = new RouteMatrixThread(count_lower, size);
- threads[i].start();
- count_lower++;
- }try {
- for (int i=0; i<threads.length;i++)
- threads[i].join();
- }
- catch(InterruptedException e) {
- System.out.print("Thread didn't finish");
- }
- // ===========================================
- print();
- }
- @Override
- public void run() {
- int[][] temp = new int[numb+1][0];
- for (int i=lower; i<upper; i++) {
- for (int j = 0; j < ma[i].length; j++) {
- for (int k=0; k < mb.length; k++) {
- temp[k] = concatenate(ma[i][k], mb[k][j]);
- }
- result[i][j] = shortest(temp);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement