-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBenchmarkLife.java
More file actions
223 lines (162 loc) · 5.65 KB
/
Copy pathBenchmarkLife.java
File metadata and controls
223 lines (162 loc) · 5.65 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import java.time.Duration;
import java.time.Instant;
import java.util.LinkedList;
import java.util.Random;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicIntegerArray;
/**
* ClassNV.java
* Purpose: generic Class that you can modify and adapt easily for any application
* that need data visualization.
* @author: Jeffrey Pallarés Núñez.
* @version: 1.0 23/07/19
*/
public class BenchmarkLife implements Runnable
{
private static int[][] matrix;
private static int[][] actualGen, nextGen;
private static int width, height;
private static int cfrontier = 0;
private static int cells_number;
public static int generations;
private static Random randomGenerator;
private int task_number;
private static int total_tasks;
private static CyclicBarrier barrier = null;
private int in;
private int fn;
public static Boolean abort = false;
private static int gens;
private static int size_pool;
private static ThreadPoolExecutor myPool;
public void run() {
for (int i = 0; i < generations-1 ; i++) {
nextGen(i);
try
{
int l = barrier.await();
if(barrier.getParties() == 0)
barrier.reset();
if(this.task_number==1) {
changeRefs();
}
l = barrier.await();
if(barrier.getParties() == 0)
barrier.reset();
}catch(Exception e){}
}
}
public BenchmarkLife() {}
public BenchmarkLife(int i) {
task_number = i;
int paso = cells_number /total_tasks;
fn = paso * task_number;
in = fn - paso;
if( total_tasks == task_number)
fn =cells_number;
}
public static void next_gen_concurrent(int nt, int gens) {
size_pool =nt;
generations = gens;
barrier = new CyclicBarrier (size_pool);
total_tasks = size_pool;
myPool = new ThreadPoolExecutor(
size_pool, size_pool, 60000L,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
BenchmarkLife[] tareas = new BenchmarkLife[nt];
for(int t = 0; t < nt; t++)
{
tareas[t] = new BenchmarkLife(t+1);
myPool.execute(tareas[t]);
}
myPool.shutdown();
try{
myPool.awaitTermination(10, TimeUnit.HOURS);
} catch(Exception e){
System.out.println(e.toString());
}
}
private static void randomInitializer() {
int nCells = (height*height)/2;
for(int i=0; i < nCells; i++) {
actualGen[randomGenerator.nextInt(height)][randomGenerator.nextInt(height)]=1;
}
}
public void initializer (int cells_number, int generations) {
randomGenerator = new Random();
width = cells_number;
height = cells_number;
actualGen = new int[width][width]; nextGen = new int[width][width];
matrix = new int[height][width];
BenchmarkLife.cells_number = cells_number;
BenchmarkLife.generations = generations;
BenchmarkLife.randomInitializer();
}
public static void changeRefs() {
int[][] aux = actualGen;
actualGen = nextGen;
nextGen = aux;
}
public static LinkedList<Long>caComputation(int taskNumber, int nGen) {
LinkedList<Long> times = new LinkedList<>();
generations = nGen;
for (int i = 1; i <= taskNumber; i++) {
Instant startTime = Instant.now();
next_gen_concurrent(i,nGen);
Instant end = Instant.now();
times.add(Duration.between(startTime,end).getSeconds());
}
return times;
}
private int computeVonNeumannNeighborhood(int i, int j) {
int cellsAlive = 0 ;
if(cfrontier==0) {
for(int x = i-1; x<=i+1; x++) {
for(int y = j-1; y<=j+1; y++) {
if((x >= 0 && x < width) && (y >= 0 && y < width) && (( x != i) || (y != j)) && (actualGen[x][y] == 1))
cellsAlive ++;
}
}
}
return cellsAlive;
}
private int transitionFunction(int cellsAlive, int i, int j) {
int transitionFunctionValue;
if(cellsAlive <2 || cellsAlive >3)
transitionFunctionValue = 0;
else if( cellsAlive == 2)
transitionFunctionValue = actualGen[i][j];
else
transitionFunctionValue = 1;
return transitionFunctionValue;
}
public int getCellValue(int i, int j){
int cellsAlive = computeVonNeumannNeighborhood(i,j);
return transitionFunction(cellsAlive, i, j);
}
public void nextGen(int actual_gen) {
for(int i = 0; i< width; i++)
for (int j = in; j < fn; j++) {
nextGen[i][j] = getCellValue(i,j);
}
}
public static void main(String[] args) {
BenchmarkLife life = new BenchmarkLife();
life.initializer(1000, 1000);
LinkedList<Long> times = caComputation(8,1000);
System.out.print("Medidas de tiempo");
for (Long time: times )
System.out.println(time);
long firstValue =0;
System.out.print("Medidas de Speed up");
for (int i = 0; i < times.size() ; i++) {
if (i==0)
firstValue= times.get(i);
System.out.println((firstValue+0.0)/(double)times.get(i));
}
}
}