WikiStart: MatrixMultiplication.txt

File MatrixMultiplication.txt, 5.4 KB (added by 152071, 5 years ago)
Line 
1import java.io.*;
2import java.util.ArrayList;
3import java.util.List;
4import java.util.concurrent.Semaphore;
5
6/**
7 * @author Riste Stojanov
8 */
9
10class FileScanner extends Thread {
11
12 static final List<File> matrixFiles = new ArrayList<>();
13 private final File directoryToScan;
14
15 FileScanner(File directoryToScan) {
16 this.directoryToScan = directoryToScan;
17 }
18
19 @Override
20 public void run() {
21 try {
22 List<FileScanner> scanners = new ArrayList<>();
23 File[] files = directoryToScan.listFiles();
24 for (File file : files) {
25
26 if (file.isFile() &&
27 file.getName().endsWith(".mat")) {
28 synchronized (matrixFiles) {
29 matrixFiles.add(file);
30 }
31 }
32 if (file.isDirectory()) {
33 FileScanner fs = new FileScanner(file);
34 scanners.add(fs);
35 fs.start();
36 }
37
38 }
39 for (FileScanner scanner : scanners) {
40 scanner.join();
41 }
42 System.out.println("Done scanning: " + directoryToScan.getAbsolutePath());
43 } catch (Exception e) {
44 e.printStackTrace();
45 }
46 }
47}
48
49class Reader extends Thread {
50 private final String matrixFile;
51 int[][] matrix;
52
53 Reader(String matrixFile) {
54 this.matrixFile = matrixFile;
55 }
56
57 /**
58 * This method should execute in background
59 */
60 @Override
61 public void run() {
62 // todo: complete this method according to the text description
63 // todo: The variable in should provide the readInt() method
64 try (DataInputStream s = new DataInputStream(new FileInputStream(new File(matrixFile)))) {
65
66 int n = s.readInt();
67 this.matrix = new int[n][n];
68 for (int i = 0; i < n; i++) {
69 for (int j = 0; j < n; j++) {
70 matrix[i][j] = s.readInt();
71 System.out.print(matrix[i][j] + " ");
72 }
73 System.out.println();
74 }
75 } catch (Exception e) {
76 e.printStackTrace();
77 }
78 }
79}
80
81class Writer extends Thread {
82
83 private final String outputPath;
84 private final int[][] matrix;
85
86 Writer(String outputPath, int[][] matrix) {
87 this.outputPath = outputPath;
88 this.matrix = matrix;
89 }
90
91
92 @Override
93 public void run() {
94 int n = matrix.length;
95 try (DataOutputStream dos = new DataOutputStream(new FileOutputStream(outputPath))) {
96 dos.writeInt(n);
97 for (int i = 0; i < n; i++) {
98 for (int j = 0; j < n; j++) {
99 // todo: write the element
100 System.out.print(matrix[i][j] + " ");
101 dos.writeInt(matrix[i][j]);
102 }
103 System.out.println();
104 }
105
106 } catch (Exception e) {
107 e.printStackTrace();
108 }
109 }
110}
111
112class Transformer extends Thread {
113 final int row;
114 final int column;
115 private final int[][] matrix;
116 int result;
117
118 Transformer(int[][] matrix, int row, int column) {
119 this.matrix = matrix;
120 this.row = row;
121 this.column = column;
122 }
123
124 @Override
125 public void run() {
126 try {
127 // todo: allow maximum 10 parallel executions
128 MatrixMultiplication.semaphore.acquire();
129 int n = matrix.length;
130 for (int k = 0; k < n; k++) {
131 result += matrix[row][k] * matrix[k][column];
132 }
133 MatrixMultiplication.semaphore.release();
134 System.out.print(".");
135 MatrixMultiplication.barrier.release();
136 } catch (Exception e) {
137 e.printStackTrace();
138 }
139 }
140}
141
142public class MatrixMultiplication {
143
144 static Semaphore barrier = new Semaphore(0);
145 static Semaphore semaphore = new Semaphore(10);
146
147 public static void main1(String[] args) {
148 int n = 30;
149 int[][] matrix = new int[n][n];
150 for (int i = 0; i < n; i++) {
151 for (int j = 0; j < n; j++) {
152 matrix[i][j] = 1;
153 }
154 }
155 Writer w = new Writer("data/matrix.mat", matrix);
156 w.run();
157 System.out.println("\n==============================================================================================");
158 Reader r = new Reader("data/matrix.mat");
159 r.run();
160 }
161
162
163 public static void main(String[] args) throws FileNotFoundException, InterruptedException {
164 List<Transformer> transformers = new ArrayList<>();
165
166 Reader reader = new Reader("data/matrix.mat");
167 // todo: execute file reading in background
168 reader.start();
169
170 // todo: wait for the matrix to be read
171 reader.join();
172
173 // todo: transform the matrix
174 int n = reader.matrix.length;
175
176 for (int i = 0; i < n; i++) {
177 for (int j = 0; j < n; j++) {
178 Transformer t = new Transformer(reader.matrix, i, j);
179 transformers.add(t);
180 // todo: start the background execution
181 t.start();
182 }
183 }
184
185
186 // todo: wait for all transformers to finish
187 barrier.acquire(n * n);
188
189 System.out.println("==============================================================================================");
190 int[][] result = new int[n][n];
191 for (Transformer t : transformers) {
192 result[t.row][t.column] = t.result;
193 }
194
195 Writer writer = new Writer("data/out.bin", result);
196 // todo: execute file writing in background
197 writer.start();
198
199 FileScanner scanner = new FileScanner(new File("data"));
200 // todo: execute file scanning in background
201 scanner.start();
202 // todo: wait for the scanner to finish and show the results
203 scanner.join();
204 for (File matrixFile : scanner.matrixFiles) {
205// System.out.println(matrixFile.getAbsolutePath());
206 }
207
208 }
209
210
211}