-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixGUIController.java
More file actions
102 lines (84 loc) · 3.47 KB
/
Copy pathMatrixGUIController.java
File metadata and controls
102 lines (84 loc) · 3.47 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
package ui;
import model.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.IntConsumer;
public class MatrixGUIController {
private final MatrixGUI view;
public MatrixGUIController(MatrixGUI view) {
this.view = view;
this.view.addRunListener(this::runBenchmark);
this.view.addClearListener(_ -> view.clearTable());
this.view.setVisible(true);
}
private void runBenchmark(ActionEvent e) {
int size;
int threshold;
// Get Inputs
try {
size = view.getSelectedSize();
threshold = view.getThreshold();
if (threshold < 1) throw new NumberFormatException();
} catch (NumberFormatException ex) {
view.showError("Invalid input. Threshold must be a positive integer.");
return;
}
view.setControlsEnabled(false);
// Start Background Worker (Background عشان في ال Frezz الي كان بيحصل :))
SwingWorker<Void, Long> worker = new SwingWorker<>() {
private int rowIndex = -1;
private long seqTime = 0L;
private long parTime = 0L;
@Override
protected Void doInBackground() throws Exception {
Matrix A = MatrixUtils.randomMatrix(size, size);
Matrix B = MatrixUtils.randomMatrix(size, size);
// Sequential Run
MatrixMultiplier seq = new SequentialMatrixMultiplier();
seqTime = MatrixUtils.measure(() -> seq.multiply(A, B));
// Update View: Add row immediately
SwingUtilities.invokeAndWait(() -> {
rowIndex = view.addInitialRow(size + "x" + size, threshold, seqTime);
});
// Parallel Run
ForkJoinMatrixMultiplier par = new ForkJoinMatrixMultiplier(threshold);
final long start = System.nanoTime();
IntConsumer progressCallback = completedRows -> {
long elapsedMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
publish(elapsedMs);
};
par.multiply(A, B, progressCallback);
parTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
return null;
}
@Override
protected void process(List<Long> chunks) {
// Live update of the timer in the table
if (rowIndex >= 0) {
view.updateParallelTime(rowIndex, chunks.getLast());
}
}
@Override
protected void done() {
try {
get(); // Catch any exceptions that happened in background
if (rowIndex >= 0) {
String speedupStr = "N/A";
if (parTime > 0) {
double speedup = (double) seqTime / parTime;
speedupStr = String.format("%.2fx", speedup);
}
view.updateParallelResults(rowIndex, parTime, speedupStr);
}
} catch (Exception ex) {
view.showError("Error: " + ex.getMessage());
} finally {
view.setControlsEnabled(true);
}
}
};
worker.execute();
}
}