Blame view

src/mior/controler/MiorLauncher.java 10.3 KB
89f70c1ec   glaville   import current mc...
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
  package mior.controler;
  
  
  import java.io.IOException;
  import java.util.Arrays;
  
  import javax.swing.SwingUtilities;
  import javax.swing.UIManager;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  import mcmas.core.MCMChrono;
  import mior.model.IMiorModel;
  import mior.model.ModelFactory;
  import mior.view.MiorResultWriter;
  import mior.view.MiorView;
  import joptsimple.OptionParser;
  import joptsimple.OptionSet;
  
  public class MiorLauncher {
  	
  	private final ModelFactory factory;
  	
  	private int modelVersion;
  	private boolean topologyOnly;
  	private boolean guiEnabled;
  	private String outputFile;
  	private String prefix;
  	
  	private int nbRepetitions;
  	private int nbSimulations;
  	//private int nbKernels;
  	
  	private final static Logger logger = LoggerFactory.getLogger(MiorLauncher.class);
  	
  	public MiorLauncher() {
  		this.modelVersion  = 4;
  		this.factory       = new ModelFactory();
  		this.topologyOnly  = false;
  		this.guiEnabled    = false;
  		this.outputFile    = "output.dat";
  		this.prefix        = "";
  		
  		this.nbRepetitions     = 0;
  		//this.nbKernels     = 0;
  		this.nbSimulations     = 0;
  	}
  	
  	public void setTopologyOnly(boolean topologyOnly) {
  		this.topologyOnly = topologyOnly;
  	}
  	
  	public boolean isTopologyOnly() {
  		return topologyOnly;
  	}
  	
  	public String getPrefix() {
  		return prefix;
  	}
  	
  	public void setPrefix(String prefix) {
  		this.prefix = prefix;
  	}
  	
  	public int getModelVersion() {
  		return modelVersion;
  	}
  	
  	public void setModelVersion(int modelVersion) {
  		this.modelVersion = modelVersion;
  	}
  	
  	public void setBatchSize(int batchSize) {
  		this.nbRepetitions = batchSize;
  	}
  	
  	public int getBatchSize() {
  		return nbRepetitions;
  	}
  	
  	/*
  	public void setNbKernels(int nbKernels) {
  		this.nbKernels = nbKernels;
  	}
  	
  	public int getNbKernels() {
  		return nbKernels;
  	}*/
  	
  	public void setTotalSize(int totalSize) {
  		this.nbSimulations = totalSize;
  	}
  	
  	public int getTotalSize() {
  		return nbSimulations;
  	}
  	
  	public String getOutputFile() {
  		return outputFile;
  	}
  	
  	public void setOutputFile(String outputFile) {
  		this.outputFile = outputFile;
  	}
  	
  	public boolean isGuiEnabled() {
  		return guiEnabled;
  	}
  	
  	public void setGuiEnabled(boolean guiEnabled) {
  		this.guiEnabled = guiEnabled;
  	}
  	
  	public ModelFactory getFactory() {
  		return factory;
  	}
  	
  	public void start(int nbMM, int nbOM, int scale) {
  		logger.info("Simulation size: " + nbMM + "x" + nbOM + " (scale: " + scale + ")");
  		IMiorModel model = factory.createModel(nbMM, nbOM, scale, modelVersion);
  		model.addUpdateListener(new MiorResultWriter(outputFile));
  		
  		if (guiEnabled) {
  			startGUI(model);
  		}
  		
  		if ((nbRepetitions < 1 || nbSimulations < 1) && !guiEnabled) {
  			throw new RuntimeException("No GUI and no simulation to execute");
  		}
  		
  		System.err.println("# Running " + nbRepetitions + " time(s) the simulation");
  		
  		//System.out.println(guiEnabled);
  		//System.out.println(nbKernels);
  		long [] results = new long[nbRepetitions];
  		final int groupSize = getFactory().getGroupSize();
  		
  		final int nbKernels = (nbSimulations % groupSize == 0 ? nbSimulations / groupSize : nbSimulations / groupSize + 1);
  		final int actualTotalSize = nbKernels * groupSize;
  		final double correctionFactor = (1.0d * nbSimulations) /  actualTotalSize;
  		
  		System.err.println("# " + correctionFactor);
  		
  		for (int i = 0; i < nbRepetitions; i++) {
  			MCMChrono simChrono = new MCMChrono("Mior simulation");
  			
  			System.err.println("# Running " + nbKernels + " times(s) a kernel of size " + groupSize + " to complete the simulation");
  			simChrono.start();
  			
  			for (int j = 0; j < nbKernels; j++) {
  				
  				if (topologyOnly) {
  					model.reset();
  					model.doTopology();
  				} else {
  					model.reset();
  					model.doAutoLive();
  				}
  			}
  			
  			results[i] = (long) (simChrono.stop().getValue() * correctionFactor);
  		}
  		
  		//System.out.println(Arrays.toString(results));
  		if (nbRepetitions != 0) {
  			printStats(results);
  		}
  		
  		if (! guiEnabled) {
  			model.release();
  		}
  	}
  	
  	private void startGUI(IMiorModel model) {
  		try {
  			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  		} catch (final Exception e) {
  			e.printStackTrace();
  		}
  		
  		final MiorView view = new MiorView(model, "MIOR Model version: " + modelVersion);
  		
  		SwingUtilities.invokeLater(new Runnable() {
  
  			@Override
  			public void run() {
  				view.getFrame().setVisible(true);
  			}
  
  		});
  	}
  	
  	private void printStats(long [] rawResults) {
  		// Sort the results
  		long [] results = rawResults.clone(); //Arrays.copyOf(rawResults, rawResults.length);
  		Arrays.sort(results);
  		
  		// Remove the min and max if we have more than 5 values
  		if (results.length >= 5) {
  			results = Arrays.copyOfRange(results, 1, results.length - 1);
  		}
  		
  		// Min and max time
  		final long min = results[0];
  		final long max = results[results.length - 1];
  		
  		// Algebric mean
  		double totalTime = 0;
  		
  		for (long time : results) {
  			totalTime += time;
  		}
  		
  		final double mean = totalTime / results.length;
  		
  		// ET computation
  		
  		long totalET = 0;
  		
  		for (long time : results) {
  			totalET += (time - mean) * (time - mean);
  		}
  		
  		final double ET = Math.sqrt(totalET / results.length);
  		
  		// Display
  		
  		System.out.println("# Min Max Avg ET");
  		System.out.println((prefix != null ? prefix + " " : "") + min + " " + max + " " + mean + " " + ET);
  	}
  	
  	public static void main(String[] args) {
  		final MiorLauncher simulation = new MiorLauncher();
  		
  		OptionParser parser = new OptionParser();
  		
  		parser.acceptsAll(Arrays.asList("o", "output"), "Output File")
  				.withRequiredArg().ofType(String.class)
  				.defaultsTo("output.dat");
  		
  		/*
  		 * batchSize  : Number of time to repeat (and average results) the whole simulation
  		 * totalSize  : Total number of MIOR simulations to execute
  		 * kernelSize : Total number of MIOR to execute in the same kernel (par. implementations only)
  		 */
  		
  		parser.acceptsAll(Arrays.asList("b", "batchSize", "r", "repeat"),
  				"Number of time to repeat (for stats) the whole simulation")
  				.withOptionalArg().ofType(Integer.class).defaultsTo(20);
  		
  		parser.acceptsAll(Arrays.asList("ts", "totalSize", "n", "nbSimulations"),
  				"Total number of MIOR simulations to execute")
  				.withRequiredArg().ofType(Integer.class).defaultsTo(0);
  		
  		parser.acceptsAll(Arrays.asList("ks", "kernelSize", "groupSize"),
  				"Number of simulations to execute each kernel (PAR. IMPLEMENTATIONS ONLY)")
  				.withRequiredArg().ofType(Integer.class);
  		
  		parser.acceptsAll(Arrays.asList("r", "random", "randomizePopulations"),
  				"Enable randomization of Mior models").withOptionalArg()
  				.ofType(Boolean.class).defaultsTo(true);
  		
  		parser.acceptsAll(Arrays.asList("c", "copy", "alwaysCopy"),
  				"Always copy data between GPU and CPU").withOptionalArg()
  				.ofType(Boolean.class).defaultsTo(true);
  		
  		parser.acceptsAll(Arrays.asList("j", "java"),
  				"Use the Java implementation");
  		
  		parser.acceptsAll(Arrays.asList("s", "scale"),
  				"Scale the model by the given factor").withRequiredArg()
  				.ofType(Integer.class).defaultsTo(1);
  		
  		parser.accepts("mm", "Number of MM to use").withRequiredArg()
  				.ofType(Integer.class).defaultsTo(38);
  		
  		parser.accepts("om", "Number of OM to use").withRequiredArg()
  				.ofType(Integer.class).defaultsTo(310);
  		
  		parser.acceptsAll(Arrays.asList("v", "version"),
  				"Use this OpenCL implementation version").withRequiredArg()
  				.ofType(Integer.class).defaultsTo(5);
  		
  		parser.acceptsAll(Arrays.asList("p", "prefix"),
  				"Prefix to use for output").withRequiredArg()
  				.ofType(String.class);
  		
  		parser.acceptsAll(Arrays.asList("t", "topoOnly"),
  				"Execute only the topology step").withOptionalArg()
  				.ofType(Boolean.class).defaultsTo(false);
  		
  		parser.acceptsAll(Arrays.asList("g", "gui"), "Enable GUI")
  				.withOptionalArg().ofType(Boolean.class).defaultsTo(true);
  		
  		parser.acceptsAll(Arrays.asList("h", "help"), "Print this help");
  		
  		OptionSet options = parser.parse(args);
  		
  		/**
  		 * HELP
  		 */
  		
  		if (options.has("help")) {
  			try {
  				parser.printHelpOn(System.out);
  				return;
  			} catch (IOException e) {
  				System.err.println("Failed to print help to standard output");
  				e.printStackTrace();
  			}
  		}
  		
  		/**
  		 * PARALLELIZATION SETTINGS
  		 */
  		
  		if (options.has("batchSize")) {
  			simulation.getFactory().setBatchModeEnabled(true);
  			simulation.setBatchSize((Integer) options.valueOf("batchSize"));
  		}
  
  		
  		if (options.has("totalSize")) {
  			int totalSize = (Integer) options.valueOf("totalSize");
  			simulation.setTotalSize(totalSize);
  			
  			if (options.has("kernelSize")) {
  				int kernelSize = (Integer) options.valueOf("kernelSize");
  				
  				if (kernelSize < 1) {
  					throw new RuntimeException("kernelSize must be a divider of the total number of simulations (" + totalSize  + ")");
  				}
  				
  				//simulation.setNbKernels(totalSize % kernelSize == 0 ? totalSize / kernelSize : (totalSize / kernelSize) + 1);
  				simulation.getFactory().setGroupSize(kernelSize);
  			} else {
  				//simulation.setNbKernels(totalSize);
  				simulation.getFactory().setGroupSize(1);
  			}
  		}
  		
  		if (options.has("blockSize")) {
  			simulation.getFactory().setBlockSize((Integer) options.valueOf("blockSize"));
  		}
  		
  		if (options.has("random")) {
  			final boolean random = (Boolean) options.valueOf("random");
  			simulation.getFactory().setRandomEnabled(random);
  		}
  		
  		if (options.has("alwaysCopy")) {
  			final boolean copy = (Boolean) options.valueOf("alwaysCopy");
  			simulation.getFactory().setBatchModeEnabled(!copy);
  		}
  		
  		/**
  		 * MODEL VERSION SETTINGS
  		 */
  		
  		if (options.has("java")) {
  			if (! options.has("version")) {
  				simulation.setModelVersion(0);
  			} else {
  				System.err.println("Error: Java and OpenCL implementation can't be used at the same time.");
  				System.exit(1);
  			}
  		} else {
  			simulation.setModelVersion((Integer) options.valueOf("version"));
  		}
  		
  		/**
  		 * OTHER PARAMETERS
  		 */
  		
  		if (options.has("gui")) {
  			simulation.setGuiEnabled((Boolean) options.valueOf("gui"));
  		} else if (simulation.getFactory().getGroupSize() == 0) {
  			simulation.setGuiEnabled(true);
  		}
  		
  		simulation.setTopologyOnly((Boolean) options.valueOf("topoOnly"));
  		simulation.setOutputFile((String) options.valueOf("output"));
  		simulation.setPrefix((String) options.valueOf("prefix"));
  		
  		final int nbMM = (Integer) options.valueOf("mm");
  		final int nbOM = (Integer) options.valueOf("om");
  		final int scale = (Integer) options.valueOf("scale");
  		
  		simulation.start(nbMM, nbOM, scale);
  	}
  
  }