-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultithreadedMatrixMultiplication.java
More file actions
115 lines (96 loc) · 3.79 KB
/
Copy pathMultithreadedMatrixMultiplication.java
File metadata and controls
115 lines (96 loc) · 3.79 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
package DAA_Practicals;
import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
class MultithreadedMatrixMultiplication {
public static int[][] multiplySequential(int[][] A, int[][] B) {
int m = A.length;
int n = A[0].length;
int p = B[0].length;
int[][] C = new int[m][p];
for (int i = 0; i < m; i++) {
for (int j = 0; j < p; j++) {
C[i][j] = 0;
for (int k = 0; k < n; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
return C;
}
public static int[][] multiplyWithOneThreadPerRow(int[][] A, int[][] B, int numThreads) {
int m = A.length;
int p = B[0].length;
int[][] C = new int[m][p];
ExecutorService executor = Executors.newFixedThreadPool(numThreads);
for (int i = 0; i < m; i++) {
final int row = i;
executor.execute(() -> {
for (int j = 0; j < p; j++) {
C[row][j] = 0;
for (int k = 0; k < B.length; k++) {
C[row][j] += A[row][k] * B[k][j];
}
}
});
}
executor.shutdown();
try {
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
return C;
}
public static int[][] multiplyWithOneThreadPerCell(int[][] A, int[][] B, int numThreads) {
int m = A.length;
int p = B[0].length;
int[][] C = new int[m][p];
ExecutorService executor = Executors.newFixedThreadPool(numThreads);
for (int i = 0; i < m; i++) {
for (int j = 0; j < p; j++) {
final int row = i;
final int col = j;
executor.execute(() -> {
C[row][col] = 0;
for (int k = 0; k < B.length; k++) {
C[row][col] += A[row][k] * B[k][col];
}
});
}
}
executor.shutdown();
try {
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
return C;
}
public static void main(String[] args) {
int[][] A = {{1, 2, 3}, {4, 5, 6}};
int[][] B = {{7, 8}, {9, 10}, {11, 12}};
long startTime, endTime;
// Sequential multiplication
startTime = System.nanoTime();
int[][] CSequential = multiplySequential(A, B);
endTime = System.nanoTime();
System.out.println("Sequential multiplication took " + (endTime - startTime) + " ns");
// Multithreaded multiplication with one thread per row
int numThreadsRow = 2;
startTime = System.nanoTime();
int[][] CRow = multiplyWithOneThreadPerRow(A, B, numThreadsRow);
endTime = System.nanoTime();
System.out.println("Multithreaded (one thread per row) multiplication took " + (endTime - startTime) + " ns");
// Multithreaded multiplication with one thread per cell
int numThreadsCell = 4;
startTime = System.nanoTime();
int[][] CCell = multiplyWithOneThreadPerCell(A, B, numThreadsCell);
endTime = System.nanoTime();
System.out.println("Multithreaded (one thread per cell) multiplication took " + (endTime - startTime) + " ns");
// Verify that the results are the same
System.out.println("Results are equal: " + Arrays.deepEquals(CSequential, CRow) + " (Row)");
System.out.println("Results are equal: " + Arrays.deepEquals(CSequential, CCell) + " (Cell)");
}
}