Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.47 KB | None | 0 0
  1. package project;
  2.  
  3. //for the scanner getting decimals correctly
  4. import java.util.Locale;
  5. //for the random matrix generator
  6. import java.util.Random;
  7. //for the text file reading
  8. import java.util.Scanner;
  9. //for the text file input/output
  10. import java.io.File;
  11. import java.io.FileNotFoundException;
  12. import java.io.PrintWriter;
  13.  
  14. public class test {
  15.  
  16.     // possible args
  17.     // -n 13 - size 13, elements are randomly generated
  18.     // -i m3x-data.in - matrix and size are stored in a text file m3x-data.in
  19.     // -o m3x-data.out - result of matrix is stored in a text file m3x-data.out
  20.     // -t 3 - specify how many threads will be running
  21.     // -q - don't print whenever a thread starts/stops, only print how long it
  22.     // took for the whole process
  23.  
  24.     // see exactly how many size 2 matrices the program will have to calculate
  25.     // using this as the index for the array of results
  26.     public static int predictProcesses(int x) {
  27.         return (x == 2 ? 1 : x * predictProcesses(x - 1));
  28.     }
  29.  
  30.     // should the program report when it's started/stopped a thread
  31.     public static boolean quiet = false;
  32.  
  33.     // how many threads the program will be running
  34.     public static int threadMax = 1;
  35.  
  36.     public static void main(String[] args) {
  37.         Matrix matrix = null;
  38.         int size = 0;
  39.         int inputMethod = 0;
  40.         // need this to know which input method to exclude, if both are in the
  41.         // args
  42.         // 0 means it hasn't it been input yet
  43.         // 1 for random
  44.         // 2 for text file
  45.         String outputFile = ""; // won't save if it's still ""
  46.         double result = 0;
  47.         Random r = new Random();
  48.         for (int i = 0; i < args.length; i += 2) {
  49.             // input random elements into the matrix
  50.             switch (args[i]) {
  51.             case "-n": {
  52.                 if (inputMethod == 0) {
  53.                     size = Integer.parseInt(args[i + 1]);
  54.                     matrix = new Matrix(size);
  55.                     for (int p = 0; p < size; p++) {
  56.                         for (int d = 0; d < size; d++) {
  57.                             int Low = 10;
  58.                             int High = 100;
  59.                             int Result = r.nextInt(High - Low) + Low;
  60.                             matrix.SetElement(Result, p, d);
  61.                         }
  62.                     }
  63.                     inputMethod = 1;
  64.                 }
  65.                 break;
  66.             }
  67.             // input elements into the matrix from a text file
  68.             case "-i": {
  69.                 if (inputMethod == 0) {
  70.                     File file = new File(args[i + 1]);
  71.                     try {
  72.                         Scanner input = new Scanner(file).useLocale(Locale.US);
  73.                         size = input.nextInt();
  74.                         matrix = new Matrix(size);
  75.                         for (int p = 0; p < size; p++) {
  76.                             for (int d = 0; d < size; d++) {
  77.                                 matrix.SetElement(input.nextDouble(), p, d);
  78.                             }
  79.                         }
  80.                         input.close();
  81.                     } catch (FileNotFoundException e) {
  82.                         e.printStackTrace();
  83.                     }
  84.                     inputMethod = 2;
  85.                 }
  86.                 break;
  87.             }
  88.             // output file
  89.             case "-o": {
  90.                 outputFile = args[i + 1];
  91.                 break;
  92.             }
  93.             // set thread count
  94.             case "-t": {
  95.                 threadMax = Integer.parseInt(args[i + 1]);
  96.                 Matrix.threadMax = threadMax;
  97.                 break;
  98.             }
  99.             // quiet or not
  100.             case "-q": {
  101.                 quiet = true;
  102.                 break;
  103.             }
  104.             }
  105.         }
  106.         // start the process
  107.         if (matrix != null) {
  108.             // get start time
  109.             long starttime = System.currentTimeMillis();
  110.             // calculate exactly how many processes the algorithm will do
  111.             Matrix.numberOfProcesses = predictProcesses(size);
  112.             // create the array of results
  113.             ThreadedMatrix.results = new double[Matrix.numberOfProcesses];
  114.             // create threads
  115.             for (int i = 0; i < threadMax; i++) {
  116.                 ThreadedMatrix p = new ThreadedMatrix(i);
  117.                 Thread t = new Thread(p);
  118.                 p.thisThread = t;
  119.                 Matrix.threadedMatrices.add(p);
  120.             }
  121.             // add matrix to the first thread
  122.             Matrix.threadedMatrices.get(0).Push(matrix);
  123.             // start threads
  124.             for (ThreadedMatrix thread : Matrix.threadedMatrices) {
  125.                 thread.thisThread.start();
  126.             }
  127.             // main thread loop
  128.             while (true) {
  129.                 // check if threads have finished their ques
  130.                 boolean finish = true;
  131.                 for (ThreadedMatrix thread : Matrix.threadedMatrices) {
  132.                     if (thread.que.size() > 0) {
  133.                         // unpause a thread if it has new matrices
  134.                         if (thread.paused) {
  135.                             synchronized (thread.thisThread) {
  136.                                 thread.paused = false;
  137.                                 thread.thisThread.notify();
  138.                             }
  139.                         }
  140.                         // not finished, continue
  141.                         finish = false;
  142.                         break;
  143.                     }
  144.                 }
  145.                 // also check aditional factors
  146.                 // tell the threads that they need to finish
  147.                 if (finish && Matrix.matricesToBeProcessed.size() == 0) {
  148.                     for (ThreadedMatrix thread : Matrix.threadedMatrices) {
  149.                         synchronized (thread.thisThread) {
  150.                             thread.finished = true;
  151.                             thread.thisThread.notify();
  152.                         }
  153.                     }
  154.                     break;
  155.                 }
  156.             }
  157.             // wait for the threads to finish
  158.             try {
  159.                 for (ThreadedMatrix thread : Matrix.threadedMatrices) {
  160.                     thread.thisThread.join();
  161.                 }
  162.             } catch (InterruptedException e) {
  163.                 e.printStackTrace();
  164.             }
  165.             // calculate result
  166.             for (int i = 0; i < ThreadedMatrix.results.length; i++) {
  167.                 result += ThreadedMatrix.results[i];
  168.             }
  169.             // print matrix
  170.             for (int p = 0; p < size; p++) {
  171.                 for (int d = 0; d < size; d++) {
  172.                     System.out.print(matrix.Get(d, p) + " ");
  173.                 }
  174.                 System.out.print("\n");
  175.             }
  176.             // report times
  177.             long calcTimeTotal = System.currentTimeMillis() - starttime;
  178.             System.out.print("total time: " + calcTimeTotal + " ms\n");
  179.             System.out.print("\nresult: " + result);
  180.             // save output file
  181.             if (outputFile != "") {
  182.                 try {
  183.                     PrintWriter out = new PrintWriter(outputFile);
  184.                     out.println(result);
  185.                     out.close();
  186.                 } catch (FileNotFoundException e) {
  187.                     e.printStackTrace();
  188.                 }
  189.             }
  190.         }
  191.  
  192.     }
  193.  
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement