From dd3fac412b0df0fa831c5702537fd8b853508e1e Mon Sep 17 00:00:00 2001 From: AmirPaknahad Date: Tue, 11 Jun 2024 17:30:09 +0330 Subject: [PATCH] commit --- .../java/sbu/cs/MatrixMultiplication.java | 121 +++++++++++++++++- src/main/java/sbu/cs/TaskScheduler.java | 26 +++- 2 files changed, 142 insertions(+), 5 deletions(-) diff --git a/src/main/java/sbu/cs/MatrixMultiplication.java b/src/main/java/sbu/cs/MatrixMultiplication.java index 2f00c59..1ff9d77 100644 --- a/src/main/java/sbu/cs/MatrixMultiplication.java +++ b/src/main/java/sbu/cs/MatrixMultiplication.java @@ -1,5 +1,6 @@ package sbu.cs; +import java.util.ArrayList; import java.util.List; public class MatrixMultiplication { @@ -8,8 +9,19 @@ public class MatrixMultiplication { public static class BlockMultiplier implements Runnable { List> tempMatrixProduct; - public BlockMultiplier() { + List> matrix_A; + List> matrix_B; + int row; + int column; + int num; + public BlockMultiplier(List> matrix_A, List> matrix_B) { // TODO + this.matrix_A = matrix_A; + this.matrix_B = matrix_B; + tempMatrixProduct = new ArrayList<>(); + row = matrix_A.size(); + column = matrix_B.getFirst().size(); + num = matrix_B.size(); } @Override @@ -18,6 +30,21 @@ public void run() { TODO Perform the calculation and store the final values in tempMatrixProduct */ + for (int i = 0; i < row; i++) { + List row = new ArrayList<>(); + for (int j = 0; j < column; j++) { + int sum = 0; + for (int k = 0; k < num; k++) { + sum += matrix_A.get(i).get(k)*matrix_B.get(k).get(j); + } + row.add(sum); + } + tempMatrixProduct.add(row); + } + getTempMatrixProduct(); + } + public List> getTempMatrixProduct() { + return tempMatrixProduct; } } @@ -34,10 +61,98 @@ public static List> ParallelizeMatMul(List> matrix_A Each thread should calculate one block of the final matrix product. Each block should be a quarter of the final matrix. Combine the 4 resulting blocks to create the final matrix product and return it. */ - return null; + + /* + i want to pass two matrix that makes a quarter of the final matrix + to the BlockMultiplier class to calculate that + */ + + List threads = new ArrayList<>(); + List blocks = new ArrayList<>(); + List>> finalBlocks = new ArrayList<>(); + + List> matrix_UA = new ArrayList<>(); //separated the uper part of matrixA + for (int i = 0; i < matrix_A.size()/2; i++) { + matrix_UA.add(matrix_A.get(i)); + } + List> matrix_LA = new ArrayList<>(); //separated the lower part of matrixA + for (int i = matrix_A.size()/2 ; i < matrix_A.size(); i++) { + matrix_LA.add(matrix_A.get(i)); + } + List> matrix_LB = new ArrayList<>(); //separated the left part of matrixB + for (int i = 0; i < matrix_B.size(); i++) { + List row = new ArrayList<>(); + for (int j = 0; j < matrix_B.getFirst().size()/2 ; j++) { + row.add(matrix_B.get(i).get(j)); + } + matrix_LB.add(row); + } + List> matrix_RB = new ArrayList<>(); //separated the right part of matrixB + for (int i = 0; i < matrix_B.size(); i++) { + List row = new ArrayList<>(); + for (int j = matrix_B.getFirst().size()/2 ; j < matrix_B.getFirst().size() ; j++) { + row.add(matrix_B.get(i).get(j)); + } + matrix_RB.add(row); + } + + //make thread of the particular objects + BlockMultiplier block0 = new BlockMultiplier(matrix_UA,matrix_LB); + Thread thread0 = new Thread(block0); + threads.add(thread0); + blocks.add(block0); + BlockMultiplier block1 = new BlockMultiplier(matrix_UA,matrix_RB); + Thread thread1 = new Thread(block1); + threads.add(thread1); + blocks.add(block1); + BlockMultiplier block2 = new BlockMultiplier(matrix_LA,matrix_LB); + Thread thread2 = new Thread(block2); + threads.add(thread2); + blocks.add(block2); + BlockMultiplier block3 = new BlockMultiplier(matrix_LA,matrix_RB); + Thread thread3 = new Thread(block3); + threads.add(thread3); + blocks.add(block3); + + //start all threads + for (Thread thread : threads) { + thread.start(); + } + + for (int i = 0; i < 4; i++) { + try { + threads.get(i).join(); + finalBlocks.add(blocks.get(i).getTempMatrixProduct()); + } catch (InterruptedException e) { + System.out.println(e.getMessage()); + } + } + + List> finalMatrix = new ArrayList<>(); + //make the final matrix with final blocks + for (int i = 0; i < matrix_A.size() / 2; i++) { + List row = new ArrayList<>(); + for (int k = 0; k < 2; k++) { + for (int j = 0; j < matrix_B.getFirst().size() / 2; j++) { + row.add(finalBlocks.get(k).get(i).get(j)); + } + } + finalMatrix.add(row); + } + for (int i = 0; i < matrix_A.size() / 2; i++) { + List row = new ArrayList<>(); + for (int k = 2; k < 4; k++) { + for (int j = 0; j < matrix_B.getFirst().size() / 2; j++) { + row.add(finalBlocks.get(k).get(i).get(j)); + } + } + finalMatrix.add(row); + } + + return finalMatrix; } public static void main(String[] args) { // Test your code here } -} +} \ No newline at end of file diff --git a/src/main/java/sbu/cs/TaskScheduler.java b/src/main/java/sbu/cs/TaskScheduler.java index 8725c2a..78a495f 100644 --- a/src/main/java/sbu/cs/TaskScheduler.java +++ b/src/main/java/sbu/cs/TaskScheduler.java @@ -1,6 +1,7 @@ package sbu.cs; import java.util.ArrayList; +import java.util.*; import java.util.List; public class TaskScheduler @@ -20,13 +21,20 @@ public Task(String taskName, int processingTime) { /* ------------------------- You don't need to modify this part of the code ------------------------- */ - + public int getProcessingTime(){ + return processingTime; + } @Override public void run() { /* TODO Simulate utilizing CPU by sleeping the thread for the specified processingTime */ + try { + Thread.sleep(processingTime); + } catch (InterruptedException e) { + System.out.println(e.getMessage()); + } } } @@ -34,6 +42,9 @@ public static ArrayList doTasks(ArrayList tasks) { ArrayList finishedTasks = new ArrayList<>(); + //sorting tasks base on processing time + Collections.sort(tasks,Comparator.comparing(Task::getProcessingTime).reversed()); + /* TODO Create a thread for each given task, And then start them based on which task has the highest priority @@ -42,10 +53,21 @@ public static ArrayList doTasks(ArrayList tasks) Don't forget to add each task's name to the finishedTasks after it's completely finished. */ + //make a thread for each task and add the name of it to finishedTasks when it completed + for (Task task : tasks) { + try { + Thread thread = new Thread(task); + thread.start(); + thread.join(); //join() method is for waiting for a task to completed and then go to another one + finishedTasks.add(task.taskName); + } catch (InterruptedException e) { + System.out.println(e.getMessage()); + } + } return finishedTasks; } public static void main(String[] args) { // Test your code here } -} +} \ No newline at end of file