Advertisement
Guest User

Untitled

a guest
May 30th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.65 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.  
  20.     public static void main(String[] args) {
  21.         // TODO Auto-generated method stub
  22.         Matrix matrix = null;
  23.         int size = 0;
  24.         int inputMethod = 0;
  25.         // need this to know which input method to exclude, if both are in the
  26.         // args
  27.         // 0 means it hasn't it been input yet
  28.         // 1 for random
  29.         // 2 for text file
  30.         String outputFile = ""; // won't save if it's still ""
  31.         int threadMax = 1;
  32.         double result = 0;
  33.         boolean quiet = false;
  34.         Random r = new Random();
  35.         for (int i = 0; i < args.length; i += 2) {
  36.             switch (args[i]) {
  37.             case "-n": {
  38.                 if (inputMethod == 0) {
  39.                     size = Integer.parseInt(args[i + 1]);
  40.                     matrix = new Matrix(size);
  41.                     for (int p = 0; p < size; p++) {
  42.                         for (int d = 0; d < size; d++) {
  43.                             int Low = 10;
  44.                             int High = 100;
  45.                             int Result = r.nextInt(High - Low) + Low;
  46.                             matrix.SetElement(Result, p, d);
  47.                         }
  48.                     }
  49.                     inputMethod = 1;
  50.                 }
  51.                 break;
  52.             }
  53.             case "-i": {
  54.                 if (inputMethod == 0) {
  55.                     File file = new File(args[i + 1]);
  56.                     try {
  57.                         Scanner input = new Scanner(file).useLocale(Locale.US);
  58.                         size = input.nextInt();
  59.                         matrix = new Matrix(size);
  60.                         for (int p = 0; p < size; p++) {
  61.                             for (int d = 0; d < size; d++) {
  62.                                 matrix.SetElement(input.nextDouble(), p, d);
  63.                             }
  64.                         }
  65.                         input.close();
  66.                     } catch (FileNotFoundException e) {
  67.                         // TODO Auto-generated catch block
  68.                         e.printStackTrace();
  69.                     }
  70.                     inputMethod = 2;
  71.                 }
  72.                 break;
  73.             }
  74.             case "-o": {
  75.                 outputFile = args[i + 1];
  76.                 break;
  77.             }
  78.             case "-t": {
  79.                 threadMax = Integer.parseInt(args[i + 1]);
  80.                 Matrix.threadMax = threadMax;
  81.                 break;
  82.             }
  83.             case "-q": {
  84.                 quiet = true;
  85.                 break;
  86.             }
  87.             }
  88.         }
  89.         if (matrix != null) {
  90.             long starttime = System.nanoTime();
  91.             for (int i = 0; i < threadMax; i++) {
  92.                 ThreadedMatrix p = new ThreadedMatrix(quiet, i);
  93.                 Thread t = new Thread(p);
  94.                 p.thisThread = t;
  95.                 Matrix.threadedMatrices.add(p);
  96.             }
  97.             Matrix.threadedMatrices.get(0).Push(matrix);
  98.             long starttimeCalcOnly = System.nanoTime();
  99.             for (ThreadedMatrix thread : Matrix.threadedMatrices) {
  100.                 thread.thisThread.start();
  101.             }
  102.             try {
  103.                 for (ThreadedMatrix thread : Matrix.threadedMatrices) {
  104.                     thread.thisThread.join();
  105.                 }
  106.             } catch (InterruptedException e) {
  107.             }
  108.             long calcTimeTotal = System.nanoTime() - starttime;
  109.             long calcTime = System.nanoTime() - starttimeCalcOnly;
  110.             result = ThreadedMatrix.result;
  111.             for (int p = 0; p < size; p++) {
  112.                 for (int d = 0; d < size; d++) {
  113.                     System.out.print(matrix.Get(d, p) + " ");
  114.                 }
  115.                 System.out.print("\n");
  116.             }
  117.             System.out.print("total time: " + calcTimeTotal + " ns\n");
  118.             System.out.print("time to calc only: " + calcTime + " ns");
  119.             System.out.print("\nresult: " + result);
  120.             if (outputFile != "") {
  121.                 try {
  122.                     PrintWriter out = new PrintWriter(outputFile);
  123.                     out.println(result);
  124.                     out.close();
  125.                 } catch (FileNotFoundException e) {
  126.                     // TODO Auto-generated catch block
  127.                     e.printStackTrace();
  128.                 }
  129.             }
  130.         }
  131.  
  132.     }
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement