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