Advertisement
Guest User

Untitled

a guest
May 31st, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.17 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.                         synchronized (thread.thisThread) {
  126.                             thread.thisThread.notify();
  127.                         }
  128.                         finish = false;
  129.                         break;
  130.                     }
  131.                 }
  132.                 // also check aditional factors
  133.                 // tell the threads that they need to finish
  134.                 if (finish && Matrix.matricesToBeProcessed
  135.                         .size() == 0 /* && Matrix.numberOfProcesses == 0 */) {
  136.                     for (ThreadedMatrix thread : Matrix.threadedMatrices) {
  137.                         synchronized (thread.thisThread) {
  138.                             thread.finished = true;
  139.                             thread.thisThread.notify();
  140.                         }
  141.                     }
  142.                     break;
  143.                 }
  144.             }
  145.             // wait for the threads to finish
  146.             try {
  147.                 for (ThreadedMatrix thread : Matrix.threadedMatrices) {
  148.                     thread.thisThread.join();
  149.                 }
  150.             } catch (InterruptedException e) {
  151.                 e.printStackTrace();
  152.             }
  153.             // calculate result
  154.             for (int i = 0; i < ThreadedMatrix.results.length; i++) {
  155.                 result += ThreadedMatrix.results[i];
  156.             }
  157.             long calcTimeTotal = System.nanoTime() - starttime;
  158.             long calcTime = System.nanoTime() - starttimeCalcOnly;
  159.             // print matrix
  160.             for (int p = 0; p < size; p++) {
  161.                 for (int d = 0; d < size; d++) {
  162.                     System.out.print(matrix.Get(d, p) + " ");
  163.                 }
  164.                 System.out.print("\n");
  165.             }
  166.             // report times
  167.             System.out.print("total time: " + calcTimeTotal + " ns\n");
  168.             System.out.print("time to calc only: " + calcTime + " ns");
  169.             System.out.print("\nresult: " + result);
  170.             // save output file
  171.             if (outputFile != "") {
  172.                 try {
  173.                     PrintWriter out = new PrintWriter(outputFile);
  174.                     out.println(result);
  175.                     out.close();
  176.                 } catch (FileNotFoundException e) {
  177.                     e.printStackTrace();
  178.                 }
  179.             }
  180.         }
  181.  
  182.     }
  183.  
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement