Guest User

Untitled

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