clean up scheduling codes
[IRC.git] / Robust / src / Main / Main.java
1 package Main;
2
3 import java.io.FileOutputStream;
4 import java.io.PrintStream;
5 import java.io.Reader;
6 import java.io.BufferedReader;
7 import java.io.FileReader;
8 import java.io.FileInputStream;
9 import java.util.Hashtable;
10 import java.util.Iterator;
11 import java.util.Set;
12 import java.util.Vector;
13
14 import IR.Tree.ParseNode;
15 import IR.Tree.BuildIR;
16 import IR.Tree.SemanticCheck;
17 import IR.Flat.BuildCodeMultiCore;
18 import IR.Flat.BuildFlat;
19 import IR.Flat.BuildCode;
20 import IR.ClassDescriptor;
21 import IR.State;
22 import IR.TaskDescriptor;
23 import IR.TypeUtil;
24 import Analysis.Scheduling.Schedule;
25 import Analysis.Scheduling.ScheduleAnalysis;
26 import Analysis.Scheduling.ScheduleSimulator;
27 import Analysis.TaskStateAnalysis.TaskAnalysis;
28 import Analysis.TaskStateAnalysis.TaskTagAnalysis;
29 import Analysis.TaskStateAnalysis.TaskGraph;
30 import Analysis.CallGraph.CallGraph;
31 import Analysis.TaskStateAnalysis.FEdge;
32 import Analysis.TaskStateAnalysis.FlagState;
33 import Analysis.TaskStateAnalysis.TagAnalysis;
34 import Analysis.TaskStateAnalysis.GarbageAnalysis;
35 import Analysis.TaskStateAnalysis.ExecutionGraph;
36 import Analysis.TaskStateAnalysis.SafetyAnalysis;
37 import Analysis.Locality.LocalityAnalysis;
38 import Analysis.Locality.GenerateConversions;
39 import Analysis.Prefetch.PrefetchAnalysis;
40 import Analysis.FlatIRGraph.FlatIRGraph;
41 import Analysis.OwnershipAnalysis.OwnershipAnalysis;
42 import Interface.*;
43 import Util.GraphNode;
44 import Util.GraphNode.DFS;
45 import Util.GraphNode.SCC;
46
47 public class Main {
48
49   /** Main method for the compiler.  */
50
51   public static void main(String args[]) throws Exception {
52     String ClassLibraryPrefix="./ClassLibrary/";
53     State state=new State();
54
55     for(int i=0; i<args.length; i++) {
56       String option=args[i];
57       if (option.equals("-precise"))
58         IR.Flat.BuildCode.GENERATEPRECISEGC=true;
59       else if (option.equals("-prefetch"))
60         state.PREFETCH=true;
61       else if (option.equals("-dir"))
62         IR.Flat.BuildCode.PREFIX=args[++i]+"/";
63       else if (option.equals("-fastcheck"))
64         state.FASTCHECK=true;
65       else if (option.equals("-selfloop"))
66         state.selfloops.add(args[++i]);
67       else if (option.equals("-excprefetch"))
68         state.excprefetch.add(args[++i]);
69       else if (option.equals("-classlibrary"))
70         ClassLibraryPrefix=args[++i]+"/";
71       else if(option.equals("-numcore")) {
72         ++i;
73         state.CORENUM = Integer.parseInt(args[i]);
74       } else if (option.equals("-mainclass"))
75         state.main=args[++i];
76       else if (option.equals("-trueprob")) {
77         state.TRUEPROB=Double.parseDouble(args[++i]);
78       } else if (option.equals("-printflat"))
79         State.PRINTFLAT=true;
80       else if (option.equals("-printscheduling"))
81         State.PRINTSCHEDULING=true;
82       else if (option.equals("-printschedulesim"))
83         State.PRINTSCHEDULESIM=true;
84       else if (option.equals("-struct"))
85         state.structfile=args[++i];
86       else if (option.equals("-conscheck"))
87         state.CONSCHECK=true;
88       else if (option.equals("-task"))
89         state.TASK=true;
90       else if (option.equals("-taskstate"))
91         state.TASKSTATE=true;
92       else if (option.equals("-tagstate"))
93         state.TAGSTATE=true;
94       else if (option.equals("-flatirtasks")) {
95         state.FLATIRGRAPH=true;
96         state.FLATIRGRAPHTASKS=true;
97       } else if (option.equals("-flatirusermethods")) {
98         state.FLATIRGRAPH=true;
99         state.FLATIRGRAPHUSERMETHODS=true;
100       } else if (option.equals("-flatirlibmethods")) {
101         state.FLATIRGRAPH=true;
102         state.FLATIRGRAPHLIBMETHODS=true;
103       } else if (option.equals("-multicore"))
104         state.MULTICORE=true;
105       else if (option.equals("-ownership"))
106         state.OWNERSHIP=true;
107       else if (option.equals("-ownallocdepth")) {
108         state.OWNERSHIPALLOCDEPTH=Integer.parseInt(args[++i]);
109       } else if (option.equals("-ownwritedots")) {
110         state.OWNERSHIPWRITEDOTS=true;
111         if (args[++i].equals("all")) {
112           state.OWNERSHIPWRITEALL=true;
113         }
114       } else if (option.equals("-ownaliasfile"))
115         state.OWNERSHIPALIASFILE=args[++i];
116       else if (option.equals("-optional"))
117         state.OPTIONAL=true;
118       else if (option.equals("-raw"))
119         state.RAW=true;
120       else if (option.equals("-scheduling"))
121         state.SCHEDULING=true;
122       else if (option.equals("-useprofile"))
123         state.USEPROFILE=true;
124       else if (option.equals("-thread"))
125         state.THREAD=true;
126       else if (option.equals("-dsm"))
127         state.DSM=true;
128       else if (option.equals("-webinterface"))
129         state.WEBINTERFACE=true;
130       else if (option.equals("-instructionfailures"))
131         state.INSTRUCTIONFAILURE=true;
132       else if (option.equals("-help")) {
133         System.out.println("-classlibrary classlibrarydirectory -- directory where classlibrary is located");
134         System.out.println("-selfloop task -- this task doesn't self loop its parameters forever");
135         System.out.println("-dir outputdirectory -- output code in outputdirectory");
136         System.out.println("-struct structfile -- output structure declarations for repair tool");
137         System.out.println("-mainclass -- main function to call");
138         System.out.println("-dsm -- distributed shared memory support");
139         System.out.println("-precise -- use precise garbage collection");
140         System.out.println("-conscheck -- turn on consistency checking");
141         System.out.println("-task -- compiler for tasks");
142         System.out.println("-fastcheck -- fastcheckpointing for Bristlecone");
143         System.out.println("-thread -- threads");
144         System.out.println("-trueprob <d> -- probability of true branch");
145         System.out.println("-printflat -- print out flat representation");
146         System.out.println("-instructionfailures -- insert code for instruction level failures");
147         System.out.println("-taskstate -- do task state analysis");
148         System.out.println("-flatirtasks -- create dot files for flat IR graphs of tasks");
149         System.out.println("-flatirusermethods -- create dot files for flat IR graphs of user methods");
150         System.out.println("-flatirlibmethods -- create dot files for flat IR graphs of library class methods");
151         System.out.println("  note: -flatirusermethods or -flatirlibmethods currently generate all class method flat IR graphs");
152         System.out.println("-ownership -- do ownership analysis");
153         System.out.println("-ownallocdepth <d> -- set allocation depth for ownership analysis");
154         System.out.println("-ownwritedots <all/final> -- write ownership graphs; can be all results or just final results");
155         System.out.println("-ownaliasfile <filename> -- write a text file showing all detected aliases in program tasks");
156         System.out.println("-optional -- enable optional arguments");
157         System.out.println("-scheduling do task scheduling");
158         System.out.println("-multicore generate multi-core version binary");
159         System.out.println("-numcore set the number of cores (should be used together with -multicore), defaultly set as 1");
160         System.out.println("-raw generate raw version binary (should be used together with -multicore)");
161         System.out.println("-interrupt generate raw version binary with interruption (should be used togethere with -raw)");
162         System.out.println("-rawconfig config raw simulator as 4xn (should be used together with -raw)");
163         System.out.println("-rawpath print out execute path information for raw version (should be used together with -raw)");
164         System.out.println("-useprofile use profiling data for scheduling (should be used together with -raw)");
165         System.out.println("-threadsimulate generate multi-thread simulate version binary");
166         System.out.println("-rawuseio use standard io to output profiling data (should be used together with -raw and -profile), it only works with single core version");
167         System.out.println("-printscheduling -- print out scheduling graphs");
168         System.out.println("-printschedulesim -- print out scheduling simulation result graphs");
169         System.out.println("-webinterface -- enable web interface");
170         System.out.println("-help -- print out help");
171         System.exit(0);
172       } else {
173         readSourceFile(state, args[i]);
174       }
175     }
176
177
178     readSourceFile(state, ClassLibraryPrefix+"System.java");
179     readSourceFile(state, ClassLibraryPrefix+"String.java");
180     readSourceFile(state, ClassLibraryPrefix+"HashSet.java");
181     readSourceFile(state, ClassLibraryPrefix+"HashMap.java");
182     readSourceFile(state, ClassLibraryPrefix+"HashMapIterator.java");
183     readSourceFile(state, ClassLibraryPrefix+"HashEntry.java");
184     readSourceFile(state, ClassLibraryPrefix+"Integer.java");
185     readSourceFile(state, ClassLibraryPrefix+"StringBuffer.java");
186     //if(!state.RAW) {
187     readSourceFile(state, ClassLibraryPrefix+"FileInputStream.java");
188     readSourceFile(state, ClassLibraryPrefix+"InputStream.java");
189     readSourceFile(state, ClassLibraryPrefix+"OutputStream.java");
190     readSourceFile(state, ClassLibraryPrefix+"FileOutputStream.java");
191     readSourceFile(state, ClassLibraryPrefix+"File.java");
192     readSourceFile(state, ClassLibraryPrefix+"InetAddress.java");
193     readSourceFile(state, ClassLibraryPrefix+"SocketInputStream.java");
194     readSourceFile(state, ClassLibraryPrefix+"SocketOutputStream.java");
195     //}
196     readSourceFile(state, ClassLibraryPrefix+"Math.java");
197     readSourceFile(state, ClassLibraryPrefix+"gnu/Random.java");
198     readSourceFile(state, ClassLibraryPrefix+"Vector.java");
199     readSourceFile(state, ClassLibraryPrefix+"Enumeration.java");
200     readSourceFile(state, ClassLibraryPrefix+"Dictionary.java");
201     readSourceFile(state, ClassLibraryPrefix+"Writer.java");
202     readSourceFile(state, ClassLibraryPrefix+"BufferedWriter.java");
203     readSourceFile(state, ClassLibraryPrefix+"OutputStreamWriter.java");
204     readSourceFile(state, ClassLibraryPrefix+"FileWriter.java");
205     readSourceFile(state, ClassLibraryPrefix+"Date.java");
206
207     if (state.TASK) {
208       if (state.FASTCHECK)
209         readSourceFile(state, ClassLibraryPrefix+"ObjectFC.java");
210       else
211         readSourceFile(state, ClassLibraryPrefix+"Object.java");
212       readSourceFile(state, ClassLibraryPrefix+"TagDescriptor.java");
213     } else if (state.DSM) {
214       readSourceFile(state, ClassLibraryPrefix+"ThreadDSM.java");
215       readSourceFile(state, ClassLibraryPrefix+"ObjectJavaDSM.java");
216       readSourceFile(state, ClassLibraryPrefix+"Barrier.java");
217     } else {
218       if (state.THREAD) {
219         readSourceFile(state, ClassLibraryPrefix+"Thread.java");
220         readSourceFile(state, ClassLibraryPrefix+"ObjectJava.java");
221       } else
222         readSourceFile(state, ClassLibraryPrefix+"ObjectJavaNT.java");
223     }
224
225     if (state.TASK) {
226       readSourceFile(state, ClassLibraryPrefix+"StartupObject.java");
227       readSourceFile(state, ClassLibraryPrefix+"Socket.java");
228       readSourceFile(state, ClassLibraryPrefix+"ServerSocket.java");
229     } else {
230       readSourceFile(state, ClassLibraryPrefix+"SocketJava.java");
231       readSourceFile(state, ClassLibraryPrefix+"ServerSocketJava.java");
232     }
233
234     BuildIR bir=new BuildIR(state);
235     bir.buildtree();
236
237     TypeUtil tu=new TypeUtil(state);
238
239     SemanticCheck sc=new SemanticCheck(state,tu);
240     sc.semanticCheck();
241     tu.createFullTable();
242
243     BuildFlat bf=new BuildFlat(state,tu);
244     bf.buildFlat();
245     SafetyAnalysis sa=null;
246     PrefetchAnalysis pa=null;
247
248     if (state.TAGSTATE) {
249       CallGraph callgraph=new CallGraph(state);
250       TagAnalysis taganalysis=new TagAnalysis(state, callgraph);
251       TaskTagAnalysis tta=new TaskTagAnalysis(state, taganalysis);
252     }
253
254     if (state.TASKSTATE) {
255       CallGraph callgraph=new CallGraph(state);
256       TagAnalysis taganalysis=new TagAnalysis(state, callgraph);
257       TaskAnalysis ta=new TaskAnalysis(state, taganalysis);
258       ta.taskAnalysis();
259       TaskGraph tg=new TaskGraph(state, ta);
260       tg.createDOTfiles();
261
262       if (state.OPTIONAL) {
263         ExecutionGraph et=new ExecutionGraph(state, ta);
264         et.createExecutionGraph();
265         sa = new SafetyAnalysis(et.getExecutionGraph(), state, ta);
266         sa.doAnalysis();
267         state.storeAnalysisResult(sa.getResult());
268         state.storeOptionalTaskDescriptors(sa.getOptionalTaskDescriptors());
269       }
270
271       if (state.WEBINTERFACE) {
272         GarbageAnalysis ga=new GarbageAnalysis(state, ta);
273         WebInterface wi=new WebInterface(state, ta, tg, ga, taganalysis);
274         JhttpServer serve=new JhttpServer(8000,wi);
275         serve.run();
276       }
277
278       if (state.SCHEDULING) {
279         // Use ownership analysis to get alias information
280         CallGraph callGraph = new CallGraph(state);
281         OwnershipAnalysis oa = new OwnershipAnalysis(state,
282                                                      tu,
283                                                      callGraph,
284                                                      state.OWNERSHIPALLOCDEPTH,
285                                                      state.OWNERSHIPWRITEDOTS,
286                                                      state.OWNERSHIPWRITEALL,
287                                                      state.OWNERSHIPALIASFILE);
288
289         // generate multiple schedulings
290         ScheduleAnalysis scheduleAnalysis = new ScheduleAnalysis(state, 
291                                                                  ta);
292         // necessary preparation such as read profile info etc.
293         scheduleAnalysis.preparation();  
294         scheduleAnalysis.preSchedule();
295         scheduleAnalysis.scheduleAnalysis();
296         //scheduleAnalysis.setCoreNum(scheduleAnalysis.getSEdges4Test().size());
297         scheduleAnalysis.setCoreNum(state.CORENUM);
298         scheduleAnalysis.schedule();
299
300         //simulate these schedulings
301         ScheduleSimulator scheduleSimulator = new ScheduleSimulator(scheduleAnalysis.getCoreNum(), 
302                                                                     state, 
303                                                                     ta);
304         Vector<Vector<Schedule>> schedulings = scheduleAnalysis.getSchedulings();
305         Vector<Integer> selectedScheduling = scheduleSimulator.simulate(schedulings);
306
307         if(state.MULTICORE) {
308           Vector<Schedule> scheduling = scheduleAnalysis.getSchedulings().elementAt(selectedScheduling.firstElement());
309           BuildCodeMultiCore bcm=new BuildCodeMultiCore(state, 
310                                                         bf.getMap(), 
311                                                         tu, 
312                                                         sa, 
313                                                         scheduling, 
314                                                         scheduleAnalysis.getCoreNum(), 
315                                                         pa);
316           bcm.setOwnershipAnalysis(oa);
317           bcm.buildCode();
318           scheduling = null;
319         }
320         selectedScheduling = null;
321       }
322
323     }
324
325     if(!state.MULTICORE) {
326       if (state.DSM) {
327         CallGraph callgraph=new CallGraph(state);
328         if (state.PREFETCH) {
329           //speed up prefetch generation using locality analysis results
330           LocalityAnalysis la=new LocalityAnalysis(state, callgraph, tu);
331           pa=new PrefetchAnalysis(state, callgraph, tu, la);
332         }
333
334         LocalityAnalysis la=new LocalityAnalysis(state, callgraph, tu);
335         GenerateConversions gc=new GenerateConversions(la, state);
336         BuildCode bc=new BuildCode(state, bf.getMap(), tu, la, pa);
337         bc.buildCode();
338       } else {
339         BuildCode bc=new BuildCode(state, bf.getMap(), tu, sa, pa);
340         bc.buildCode();
341       }
342     }
343
344     if (state.FLATIRGRAPH) {
345       FlatIRGraph firg = new FlatIRGraph(state,
346                                          state.FLATIRGRAPHTASKS,
347                                          state.FLATIRGRAPHUSERMETHODS,
348                                          state.FLATIRGRAPHLIBMETHODS);
349     }
350
351     if (state.OWNERSHIP) {
352       CallGraph callGraph = new CallGraph(state);
353       OwnershipAnalysis oa = new OwnershipAnalysis(state,
354                                                    tu,
355                                                    callGraph,
356                                                    state.OWNERSHIPALLOCDEPTH,
357                                                    state.OWNERSHIPWRITEDOTS,
358                                                    state.OWNERSHIPWRITEALL,
359                                                    state.OWNERSHIPALIASFILE);
360     }
361
362     System.exit(0);
363   }
364
365   /** Reads in a source file and adds the parse tree to the state object. */
366
367   private static void readSourceFile(State state, String sourcefile) throws Exception {
368     Reader fr = new BufferedReader(new FileReader(sourcefile));
369     Lex.Lexer l = new Lex.Lexer(fr);
370     java_cup.runtime.lr_parser g;
371     g = new Parse.Parser(l);
372     ParseNode p=null;
373     try {
374       p=(ParseNode) g./*debug_*/ parse().value;
375     } catch (Exception e) {
376       System.err.println("Error parsing file:"+sourcefile);
377       e.printStackTrace();
378       System.exit(-1);
379     }
380     state.addParseNode(p);
381     if (l.numErrors()!=0) {
382       System.out.println("Error parsing "+sourcefile);
383       System.exit(l.numErrors());
384     }
385   }
386 }