-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_02_HW1_concurrentMultiply.patch
More file actions
182 lines (177 loc) · 6.85 KB
/
Copy path2_02_HW1_concurrentMultiply.patch
File metadata and controls
182 lines (177 loc) · 6.85 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
Index: src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java (date 1508266595000)
+++ src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java (date 1508268723000)
@@ -1,8 +1,9 @@
package ru.javaops.masterjava.matrix;
-import java.util.Random;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorService;
+import java.util.*;
+import java.util.concurrent.*;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
/**
* gkislin
@@ -10,11 +11,153 @@
*/
public class MatrixUtil {
- // TODO implement parallel multiplication matrixA*matrixB
public static int[][] concurrentMultiply(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException, ExecutionException {
final int matrixSize = matrixA.length;
final int[][] matrixC = new int[matrixSize][matrixSize];
+ class ColumnMultipleResult {
+ private final int col;
+ private final int[] columnC;
+
+ private ColumnMultipleResult(int col, int[] columnC) {
+ this.col = col;
+ this.columnC = columnC;
+ }
+ }
+
+ final CompletionService<ColumnMultipleResult> completionService = new ExecutorCompletionService<>(executor);
+
+ for (int j = 0; j < matrixSize; j++) {
+ final int col = j;
+ final int[] columnB = new int[matrixSize];
+ for (int k = 0; k < matrixSize; k++) {
+ columnB[k] = matrixB[k][col];
+ }
+ completionService.submit(() -> {
+ final int[] columnC = new int[matrixSize];
+
+ for (int row = 0; row < matrixSize; row++) {
+ final int[] rowA = matrixA[row];
+ int sum = 0;
+ for (int k = 0; k < matrixSize; k++) {
+ sum += rowA[k] * columnB[k];
+ }
+ columnC[row] = sum;
+ }
+ return new ColumnMultipleResult(col, columnC);
+ });
+ }
+
+ for (int i = 0; i < matrixSize; i++) {
+ ColumnMultipleResult res = completionService.take().get();
+ for (int k = 0; k < matrixSize; k++) {
+ matrixC[k][res.col] = res.columnC[k];
+ }
+ }
+ return matrixC;
+ }
+
+ public static int[][] concurrentMultiplyCayman(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException, ExecutionException {
+ final int matrixSize = matrixA.length;
+ final int[][] matrixResult = new int[matrixSize][matrixSize];
+ final int threadCount = Runtime.getRuntime().availableProcessors();
+ final int maxIndex = matrixSize * matrixSize;
+ final int cellsInThread = maxIndex / threadCount;
+ final int[][] matrixBFinal = new int[matrixSize][matrixSize];
+
+ for (int i = 0; i < matrixSize; i++) {
+ for (int j = 0; j < matrixSize; j++) {
+ matrixBFinal[i][j] = matrixB[j][i];
+ }
+ }
+
+ Set<Callable<Boolean>> threads = new HashSet<>();
+ int fromIndex = 0;
+ for (int i = 1; i <= threadCount; i++) {
+ final int toIndex = i == threadCount ? maxIndex : fromIndex + cellsInThread;
+ final int firstIndexFinal = fromIndex;
+ threads.add(() -> {
+ for (int j = firstIndexFinal; j < toIndex; j++) {
+ final int row = j / matrixSize;
+ final int col = j % matrixSize;
+
+ int sum = 0;
+ for (int k = 0; k < matrixSize; k++) {
+ sum += matrixA[row][k] * matrixBFinal[col][k];
+ }
+ matrixResult[row][col] = sum;
+ }
+ return true;
+ });
+ fromIndex = toIndex;
+ }
+ executor.invokeAll(threads);
+ return matrixResult;
+ }
+
+ public static int[][] concurrentMultiplyDarthVader(int[][] matrixA, int[][] matrixB, ExecutorService executor)
+ throws InterruptedException, ExecutionException {
+
+ final int matrixSize = matrixA.length;
+ final int[][] matrixC = new int[matrixSize][matrixSize];
+
+ List<Callable<Void>> tasks = IntStream.range(0, matrixSize)
+ .parallel()
+ .mapToObj(i -> new Callable<Void>() {
+ private final int[] tempColumn = new int[matrixSize];
+
+ @Override
+ public Void call() throws Exception {
+ for (int c = 0; c < matrixSize; c++) {
+ tempColumn[c] = matrixB[c][i];
+ }
+ for (int j = 0; j < matrixSize; j++) {
+ int row[] = matrixA[j];
+ int sum = 0;
+ for (int k = 0; k < matrixSize; k++) {
+ sum += tempColumn[k] * row[k];
+ }
+ matrixC[j][i] = sum;
+ }
+ return null;
+ }
+ })
+ .collect(Collectors.toList());
+
+ executor.invokeAll(tasks);
+ return matrixC;
+ }
+
+ public static int[][] concurrentMultiply2(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException, ExecutionException {
+ final int matrixSize = matrixA.length;
+ final int[][] matrixC = new int[matrixSize][];
+
+ final int[][] matrixBT = new int[matrixSize][matrixSize];
+ for (int i = 0; i < matrixSize; i++) {
+ for (int j = 0; j < matrixSize; j++) {
+ matrixBT[i][j] = matrixB[j][i];
+ }
+ }
+
+ List<Callable<Void>> tasks = new ArrayList<>(matrixSize);
+ for (int j = 0; j < matrixSize; j++) {
+ final int row = j;
+ tasks.add(() -> {
+ final int[] rowC = new int[matrixSize];
+ for (int col = 0; col < matrixSize; col++) {
+ final int[] rowA = matrixA[row];
+ final int[] columnB = matrixBT[col];
+ int sum = 0;
+ for (int k = 0; k < matrixSize; k++) {
+ sum += rowA[k] * columnB[k];
+ }
+ rowC[col] = sum;
+ }
+ matrixC[row] = rowC;
+ return null;
+ });
+ }
+ executor.invokeAll(tasks);
return matrixC;
}
@@ -64,4 +207,4 @@
}
return true;
}
-}
+}
\ No newline at end of file