-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
108 lines (76 loc) · 2.96 KB
/
Main.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
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
103
104
105
106
107
108
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicIntegerArray;
import java.io.FileWriter;
public class Main {
private static int NUMBER_OF_PROCESSES = 5;
private static String FILE_NAME = "results.csv";
public static void main(String[] args) throws InterruptedException, IOException {
// initialize shared variables
if (args.length > 0){
Process.NUMBER_OF_ITERATIONS = Integer.parseInt(args[0]);
}
if (args.length > 1){
NUMBER_OF_PROCESSES = Integer.parseInt(args[1]);
}
Process.levels = (int) Math.ceil(Math.log(NUMBER_OF_PROCESSES) / Math.log(2));
Process.flag = new AtomicIntegerArray[Process.levels];
Process.turn = new AtomicIntegerArray[Process.levels];
for (int i=0; i < Process.flag.length; i++){
int[] myArray = new int[(int) Math.pow(2, Process.levels)];
for (int j=0; j < myArray.length; j++){
myArray[j] = 0;
}
Process.flag[i] = new AtomicIntegerArray(myArray);
}
int cols = (NUMBER_OF_PROCESSES + 1) / 2;
for (int i=0; i < Process.levels; i++){
int[] myArray = new int[cols];
for (int j=0; j < cols; j++){
myArray[j] = -1;
}
Process.turn[i] = new AtomicIntegerArray(myArray);
}
// boot the algorithm
Thread threads[] = new Thread[NUMBER_OF_PROCESSES];
long startTimeMillis = System.currentTimeMillis();
for (int i=0; i < NUMBER_OF_PROCESSES; i++){
Process p = new Process(i, NUMBER_OF_PROCESSES);
Thread t = new Thread(p);
threads[i] = t;
t.start();
}
for (int i=0; i < NUMBER_OF_PROCESSES; i++){
threads[i].join();
}
long diff = System.currentTimeMillis() - startTimeMillis;
double throughput = (NUMBER_OF_PROCESSES * Process.NUMBER_OF_ITERATIONS) / (diff * 1.0);
BufferedWriter bw = null;
FileWriter fw = null;
try {
String data = NUMBER_OF_PROCESSES + "," + Process.NUMBER_OF_ITERATIONS + "," + diff + "," + throughput + "\n";
File file = new File(FILE_NAME);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
// true = append file
fw = new FileWriter(file.getAbsoluteFile(), true);
bw = new BufferedWriter(fw);
bw.write(data);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
System.out.println("count = " + Process.counter);
}
}