Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132944 views
License: OTHER
1
import java.io.BufferedReader;
2
import java.io.FileReader;
3
import java.io.IOException;
4
import java.util.LinkedList;
5
import java.util.List;
6
import java.util.ArrayList;
7
import java.util.concurrent.Callable;
8
import java.util.concurrent.ExecutionException;
9
import java.util.concurrent.ExecutorService;
10
import java.util.concurrent.Executors;
11
import java.util.concurrent.Future;
12
13
public class Shell {
14
static List<ArrayList<ArrayList<Integer>>> read(String filename) {
15
ArrayList<ArrayList<Integer>> A = new ArrayList<ArrayList<Integer>>();
16
ArrayList<ArrayList<Integer>> B = new ArrayList<ArrayList<Integer>>();
17
18
String thisLine;
19
20
try {
21
BufferedReader br = new BufferedReader(new FileReader(filename));
22
// Begin reading A
23
while ((thisLine = br.readLine()) != null) {
24
if (thisLine.trim().equals("")) {
25
break;
26
} else {
27
ArrayList<Integer> line = new ArrayList<Integer>();
28
String[] lineArray = thisLine.split("\t");
29
for (String number : lineArray) {
30
line.add(Integer.parseInt(number));
31
}
32
A.add(line);
33
}
34
}
35
36
// Begin reading B
37
while ((thisLine = br.readLine()) != null) {
38
ArrayList<Integer> line = new ArrayList<Integer>();
39
String[] lineArray = thisLine.split("\t");
40
for (String number : lineArray) {
41
line.add(Integer.parseInt(number));
42
}
43
B.add(line);
44
}
45
br.close();
46
} catch (IOException e) {
47
System.err.println("Error: " + e);
48
}
49
50
List<ArrayList<ArrayList<Integer>>> res = new LinkedList<ArrayList<ArrayList<Integer>>>();
51
res.add(A);
52
res.add(B);
53
return res;
54
}
55
56
static void printMatrix(int[][] matrix) {
57
for (int[] line : matrix) {
58
int i = 0;
59
StringBuilder sb = new StringBuilder(matrix.length);
60
for (int number : line) {
61
if (i != 0) {
62
sb.append("\t");
63
} else {
64
i++;
65
}
66
sb.append(number);
67
}
68
System.out.println(sb.toString());
69
}
70
}
71
72
public static int[][] parallelMult(ArrayList<ArrayList<Integer>> A,
73
ArrayList<ArrayList<Integer>> B, int threadNumber) {
74
int[][] C = new int[A.size()][B.get(0).size()];
75
ExecutorService executor = Executors.newFixedThreadPool(threadNumber);
76
List<Future<int[][]>> list = new ArrayList<Future<int[][]>>();
77
78
int part = A.size() / threadNumber;
79
if (part < 1) {
80
part = 1;
81
}
82
for (int i = 0; i < A.size(); i += part) {
83
System.err.println(i);
84
Callable<int[][]> worker = new LineMultiplier(A, B, i, i+part);
85
Future<int[][]> submit = executor.submit(worker);
86
list.add(submit);
87
}
88
89
// now retrieve the result
90
int start = 0;
91
int CF[][];
92
for (Future<int[][]> future : list) {
93
try {
94
CF = future.get();
95
for (int i=start; i < start+part; i += 1) {
96
C[i] = CF[i];
97
}
98
} catch (InterruptedException e) {
99
e.printStackTrace();
100
} catch (ExecutionException e) {
101
e.printStackTrace();
102
}
103
start+=part;
104
}
105
executor.shutdown();
106
107
return C;
108
}
109
110
public static void main(String[] args) {
111
String filename;
112
int cores = Runtime.getRuntime().availableProcessors();
113
System.err.println("Number of cores:\t" + cores);
114
115
int threads;
116
if (args.length < 3) {
117
filename = "3.in";
118
threads = cores;
119
} else {
120
filename = args[1];
121
threads = Integer.parseInt(args[2]);
122
}
123
124
List<ArrayList<ArrayList<Integer>>> matrices = read(filename);
125
ArrayList<ArrayList<Integer>> A = matrices.get(0);
126
ArrayList<ArrayList<Integer>> B = matrices.get(1);
127
int[][] C = parallelMult(A, B, threads);
128
printMatrix(C);
129
}
130
}
131
132