-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathMatrixMultiplication.java
More file actions
209 lines (184 loc) · 8.61 KB
/
MatrixMultiplication.java
File metadata and controls
209 lines (184 loc) · 8.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package sbu.cs;
import javax.swing.plaf.TableHeaderUI;
import java.beans.PropertyEditorSupport;
import java.util.ArrayList;
import java.util.List;
public class MatrixMultiplication {
// You are allowed to change all code in the BlockMultiplier class
public static class BlockMultiplier implements Runnable
{
List<List<Integer>> tempMatrixProduct;
List<List<Integer>> matrix_A;
List<List<Integer>> matrix_B;
public BlockMultiplier(List<List<Integer>> matrix_A, List<List<Integer>> matrix_B) { //constructor
// TODO
this.matrix_A = matrix_A;
this.matrix_B = matrix_B;
}
@Override
public void run() { // the only task of threads is to multiplying those matrices
/*
TODO
Perform the calculation and store the final values in tempMatrixProduct
*/
tempMatrixProduct = multiplyingMatrix(matrix_A , matrix_B);
}
public List<List<Integer>> multiplyingMatrix(List<List<Integer>> matrix_A, List<List<Integer>> matrix_B) { //this method will multiply that two matrices which we pass it
int rows1 = matrix_A.size();
int cols1 = matrix_A.get(0).size();
int cols2 = matrix_B.get(0).size();
List<List<Integer>> result = new ArrayList<>();
for (int i = 0; i < rows1; i++) {
List<Integer> row = new ArrayList<>();
for (int j = 0; j < cols2; j++) {
int sum = 0;
for (int k = 0; k < cols1; k++) {
sum += matrix_A.get(i).get(k) * matrix_B.get(k).get(j);
}
row.add(sum);
}
result.add(row);
}
tempMatrixProduct = result;
return result;
}
}
// ---------------------------------- combining the four matrices(blocks) together ------------------------------------------
public List<List<Integer>> khodeMatrix (List<List<Integer>> block1 , List<List<Integer>> block2 , List<List<Integer>> block3 , List<List<Integer>> block4) {
// block 1 : up & left
// block 2 : up & right
// block 3 : down & left
// block 4 : down & right
// first we combine upper blocks then lower block with each other, then combine them to a final matrix
List<List<Integer>> finalMatrix = new ArrayList<>();
// combining block 1 & block 2 (upper blocks)
for (int i = 0 ; i < block1.size() ; i++) {
List<Integer> row = new ArrayList<>(block1.get(i));
row.addAll(block2.get(i));
finalMatrix.add(row);
}
// combining block 3 & block 4 (lower blocks)
for (int i = 0 ; i < block3.size() ; i++) {
List<Integer> row = new ArrayList<>(block3.get(i));
row.addAll(block4.get(i));
finalMatrix.add(row);
}
return finalMatrix;
}
// ---------------------------------- combining the four matrices(blocks) together ------------------------------------------
/*
Matrix A is of the form p x q
Matrix B is of the form q x r
both p and r are even numbers
*/
public static List<List<Integer>> ParallelizeMatMul(List<List<Integer>> matrix_A, List<List<Integer>> matrix_B)
{
List<List<Integer>> temp1 = new ArrayList<>();
List<List<Integer>> temp2 = new ArrayList<>();
List<List<Integer>> temp3 = new ArrayList<>();
List<List<Integer>> temp4 = new ArrayList<>();
//----------------------------------------------for separating the matrix---------------------------------------------------
for (int i = 0; i < matrix_A.size()/2; i++) { // the upper part of the matrix ---> thread 1
temp1.add(matrix_A.get(i));
}
for (int i = 0; i < matrix_B.size(); i++) { // the left part of the matrix ---> thread 2
List<Integer> row = new ArrayList<>();
for (int j = 0; j < matrix_B.get(0).size()/2 ; j++) {
row.add(matrix_B.get(i).get(j));
}
temp2.add(row);
}
for (int i = matrix_A.size()/2 ; i < matrix_A.size(); i++) { // the down part of the matrix --->thread 3
temp3.add(matrix_A.get(i));
}
for (int i = 0; i < matrix_B.size(); i++) { // the right part of the matrix ---> thread 4
List<Integer> row = new ArrayList<>();
for (int j = matrix_B.get(0).size()/2 ; j < matrix_B.get(0).size() ; j++) {
row.add(matrix_B.get(i).get(j));
}
temp4.add(row);
}
// ----------------------------------------------for separating the matrix---------------------------------------------------
List<BlockMultiplier> listOfBlocks = new ArrayList<>();
BlockMultiplier blockMultiplier1 = new BlockMultiplier(temp1 , temp2); //up & left
listOfBlocks.add(blockMultiplier1);
BlockMultiplier blockMultiplier2 = new BlockMultiplier(temp1 , temp4); //up & right
listOfBlocks.add(blockMultiplier2);
BlockMultiplier blockMultiplier3 = new BlockMultiplier(temp3 , temp2); // down & left
listOfBlocks.add(blockMultiplier3);
BlockMultiplier blockMultiplier4 = new BlockMultiplier(temp3 , temp4); //down & right
listOfBlocks.add(blockMultiplier4);
// -------------------------------------------------------------------------------------------------------------------------
// ----------------------------------------------for starting the threads---------------------------------------------------
List<Thread> listOfThreads = new ArrayList<>();
for (BlockMultiplier blockMultiplier : listOfBlocks) {
Thread thread = new Thread(blockMultiplier);
listOfThreads.add(thread);
}
for (Thread listOfThread : listOfThreads) {
listOfThread.start();
}
for (Thread thread : listOfThreads) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// ----------------------------------------------for starting the threads---------------------------------------------------
// -------------------------------------------------------------------------------------------------------------------------
// ---------------------------------- combining the four matrices(blocks) together ------------------------------------------
MatrixMultiplication matrixMultiplication = new MatrixMultiplication();
// ---------------------------------- combining the four matrices(blocks) together ------------------------------------------
// -------------------------------------------------------------------------------------------------------------------------
List<List<Integer>> matrix = matrixMultiplication.khodeMatrix(blockMultiplier1.tempMatrixProduct ,
blockMultiplier2.tempMatrixProduct , blockMultiplier3.tempMatrixProduct , blockMultiplier4.tempMatrixProduct);
return matrix;
}
public static void main(String[] args) {
// Test your code here
List<List<Integer>> matrix_A = new ArrayList<>();
List<List<Integer>> matrix_B = new ArrayList<>();
List<Integer> row1A = new ArrayList<>();
row1A.add(1);
row1A.add(2);
row1A.add(3);
List<Integer> row2A = new ArrayList<>();
row2A.add(4);
row2A.add(5);
row2A.add(6);
List<Integer> row3A = new ArrayList<>();
row3A.add(7);
row3A.add(8);
row3A.add(9);
List<Integer> row4A = new ArrayList<>();
row4A.add(10);
row4A.add(11);
row4A.add(12);
matrix_A.add(row1A);
matrix_A.add(row2A);
matrix_A.add(row3A);
matrix_A.add(row4A);
List<Integer> row1B = new ArrayList<>();
row1B.add(13);
row1B.add(14);
List<Integer> row2B = new ArrayList<>();
row2B.add(17);
row2B.add(16);
List<Integer> row3B = new ArrayList<>();
row3B.add(17);
row3B.add(18);
matrix_B.add(row1B);
matrix_B.add(row2B);
matrix_B.add(row3B);
BlockMultiplier blockMultiplier = new BlockMultiplier(matrix_A , matrix_B);
List<List<Integer>> result;
result = blockMultiplier.multiplyingMatrix(matrix_A , matrix_B);
for (List<Integer> innerList : result) {
for (Integer element : innerList) {
System.out.print(element + " ");
}
System.out.println();
}
}
}