-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParallelSunlightApp.java
68 lines (48 loc) · 1.69 KB
/
ParallelSunlightApp.java
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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.concurrent.ForkJoinPool;
public class ParallelSunlightApp {
static double[][] terrain;
static final ForkJoinPool fjPool = new ForkJoinPool();
public static void main(String[] args) throws Exception {
String inFile = args[0], outFile = args[1];
int x, y, numTrees;
Tree[] trees;
BufferedReader buffIn = new BufferedReader(new FileReader(args[0]));
BufferedWriter buffOut = new BufferedWriter (new FileWriter (outFile));
String[] line;
line = buffIn.readLine().split(" ");
x = Integer.parseInt(line[0]);
y = Integer.parseInt(line[1]);
terrain = new double[x][y];
line = buffIn.readLine().split(" ");
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
terrain[i][j] = Double.parseDouble(line[i*y + j]);
}
}
numTrees = Integer.parseInt(buffIn.readLine());
trees = new Tree[numTrees];
for (int i = 0; i < numTrees; i++) {
line = buffIn.readLine().split(" ");
Tree t = new Tree (Integer.parseInt(line[0]), Integer.parseInt(line[1]), Integer.parseInt(line[2]));
trees[i] = t;
}
buffIn.close();
buffIn = null; line = null; inFile = null; outFile = null;
System.gc();
long before = System.currentTimeMillis();
double grandTotal = fjPool.invoke(new TreeCalculation (0, trees.length, trees, terrain));
double average = (float) (grandTotal/trees.length);
long after = System.currentTimeMillis();
System.out.println(after - before);
buffOut.write(average + "\n");
buffOut.write(trees.length + "\n");
for (Tree t : trees) {
buffOut.write(t.getTotalSunlight() + "\n");
}
buffOut.close();
}
}