Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package project;
- import java.util.Locale;
- import java.util.Random;
- import java.util.Scanner;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- public class test {
- // possible args
- // -n 13 - size 13, elements are randomly generated
- // -i m3x-data.in - matrix and size are stored in a text file m3x-data.in
- // -o m3x-data.out - result of matrix is stored in a text file m3x-data.out
- // -t 3 - specify how many threads will be running
- // -q - don't print whenever a thread starts/stops, only print how long it
- // took for the whole process
- public static int predictProcesses(int x) {
- return (x == 2 ? 1 : x * predictProcesses(x - 1));
- }
- public static void main(String[] args) {
- Matrix matrix = null;
- int size = 0;
- int inputMethod = 0;
- // need this to know which input method to exclude, if both are in the
- // args
- // 0 means it hasn't it been input yet
- // 1 for random
- // 2 for text file
- String outputFile = ""; // won't save if it's still ""
- int threadMax = 1;
- double result = 0;
- boolean quiet = false;
- Random r = new Random();
- for (int i = 0; i < args.length; i += 2) {
- // input random elements into the matrix
- switch (args[i]) {
- case "-n": {
- if (inputMethod == 0) {
- size = Integer.parseInt(args[i + 1]);
- matrix = new Matrix(size);
- for (int p = 0; p < size; p++) {
- for (int d = 0; d < size; d++) {
- int Low = 10;
- int High = 100;
- int Result = r.nextInt(High - Low) + Low;
- matrix.SetElement(Result, p, d);
- }
- }
- inputMethod = 1;
- }
- break;
- }
- // input elements into the matrix from a text file
- case "-i": {
- if (inputMethod == 0) {
- File file = new File(args[i + 1]);
- try {
- Scanner input = new Scanner(file).useLocale(Locale.US);
- size = input.nextInt();
- matrix = new Matrix(size);
- for (int p = 0; p < size; p++) {
- for (int d = 0; d < size; d++) {
- matrix.SetElement(input.nextDouble(), p, d);
- }
- }
- input.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- inputMethod = 2;
- }
- break;
- }
- // output file
- case "-o": {
- outputFile = args[i + 1];
- break;
- }
- // set thread count
- case "-t": {
- threadMax = Integer.parseInt(args[i + 1]);
- Matrix.threadMax = threadMax;
- break;
- }
- // quiet or not
- case "-q": {
- quiet = true;
- break;
- }
- }
- }
- // start the process
- if (matrix != null) {
- matrix.parent = matrix;
- // get start time
- long starttime = System.nanoTime();
- // calculate exactly how many processes the algorithm will do, for
- // safety reasons
- Matrix.numberOfProcesses = predictProcesses(size);
- ThreadedMatrix.results = new double[Matrix.numberOfProcesses];
- // create threads
- for (int i = 0; i < threadMax; i++) {
- ThreadedMatrix p = new ThreadedMatrix(quiet, i);
- Thread t = new Thread(p);
- p.thisThread = t;
- Matrix.threadedMatrices.add(p);
- }
- // add matrix to the first thread
- Matrix.threadedMatrices.get(0).Push(matrix);
- // get start of calculation time
- long starttimeCalcOnly = System.nanoTime();
- // start threads
- for (ThreadedMatrix thread : Matrix.threadedMatrices) {
- thread.thisThread.start();
- }
- // main thread loop
- while (true) {
- // check if threads have finished their ques
- boolean finish = true;
- for (ThreadedMatrix thread : Matrix.threadedMatrices) {
- if (thread.que.size() > 0) {
- synchronized (thread.thisThread) {
- thread.thisThread.notify();
- }
- finish = false;
- break;
- }
- }
- // also check aditional factors
- // tell the threads that they need to finish
- if (finish && Matrix.matricesToBeProcessed
- .size() == 0 /* && Matrix.numberOfProcesses == 0 */) {
- for (ThreadedMatrix thread : Matrix.threadedMatrices) {
- synchronized (thread.thisThread) {
- thread.finished = true;
- thread.thisThread.notify();
- }
- }
- break;
- }
- }
- // wait for the threads to finish
- try {
- for (ThreadedMatrix thread : Matrix.threadedMatrices) {
- thread.thisThread.join();
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- // calculate result
- for (int i = 0; i < ThreadedMatrix.results.length; i++) {
- result += ThreadedMatrix.results[i];
- }
- long calcTimeTotal = System.nanoTime() - starttime;
- long calcTime = System.nanoTime() - starttimeCalcOnly;
- // print matrix
- for (int p = 0; p < size; p++) {
- for (int d = 0; d < size; d++) {
- System.out.print(matrix.Get(d, p) + " ");
- }
- System.out.print("\n");
- }
- // report times
- System.out.print("total time: " + calcTimeTotal + " ns\n");
- System.out.print("time to calc only: " + calcTime + " ns");
- System.out.print("\nresult: " + result);
- // save output file
- if (outputFile != "") {
- try {
- PrintWriter out = new PrintWriter(outputFile);
- out.println(result);
- out.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement