1 package Analysis.Scheduling;
3 import java.io.FileOutputStream;
4 import java.io.PrintStream;
5 import java.util.Hashtable;
6 import java.util.Iterator;
7 import java.util.LinkedList;
8 import java.util.Queue;
10 import java.util.Vector;
11 import java.util.Map.Entry;
13 import Analysis.TaskStateAnalysis.FEdge;
14 import Analysis.TaskStateAnalysis.FlagState;
15 import Analysis.TaskStateAnalysis.TaskAnalysis;
16 import IR.ClassDescriptor;
18 import IR.TaskDescriptor;
21 public class ScheduleSimulator {
23 private Vector<Schedule> scheduling;
24 private Vector<CoreSimulator> cores;
25 private Vector<TaskSimulator> tasks;
26 private long processTime;
27 private int invoketime;
29 private Vector<FlagState> fstates;
30 private Vector<FEdge> fedges;
33 TaskAnalysis taskanalysis;
35 public ScheduleSimulator(int corenum,
37 TaskAnalysis taskanalysis) {
38 this.coreNum = corenum;
39 this.scheduling = null;
45 this.taskanalysis = taskanalysis;
46 this.fstates = new Vector<FlagState>();
47 this.fedges = new Vector<FEdge>();
50 public ScheduleSimulator(int corenum,
51 Vector<Schedule> scheduling,
53 TaskAnalysis taskanalysis) {
55 this.coreNum = corenum;
56 this.scheduling = scheduling;
57 this.cores = new Vector<CoreSimulator>(this.coreNum);
58 for(int i = 0; i < this.coreNum; i++) {
59 this.cores.add(new CoreSimulator(FIFORSchedule.getFIFORSchedule(), i));
61 this.tasks = new Vector<TaskSimulator>();
65 this.taskanalysis = taskanalysis;
66 this.fstates = new Vector<FlagState>();
67 this.fedges = new Vector<FEdge>();
72 // gather all the flag states and fedges together
73 Iterator it_classes = this.state.getClassSymbolTable().getDescriptorsIterator();
74 while(it_classes.hasNext()) {
75 ClassDescriptor cd = (ClassDescriptor) it_classes.next();
76 Iterator<FlagState> it_fStates = this.taskanalysis.getFlagStates(cd).iterator();
78 while(it_fStates.hasNext()) {
79 FlagState fs = it_fStates.next();
80 if(!this.fstates.contains(fs)) {
81 this.fstates.addElement(fs);
83 Iterator<FEdge> it_fe = (Iterator<FEdge>)fs.edges();
84 while(it_fe.hasNext()) {
85 FEdge next = it_fe.next();
86 if(!this.fedges.contains(next)) {
87 this.fedges.addElement(next);
94 public long simulate(Vector<Vector<Schedule>> schedulings,
95 Vector<Integer> selectedScheduling,
96 Vector<SimExecutionNode> selectedSimExeGraphs) {
97 long processTime = Long.MAX_VALUE;
98 /*if(schedulings.size() > 1500) {
100 int upperbound = schedulings.size();
102 java.util.Random r = new java.util.Random(seed);
103 for(int ii = 0; ii < 1500; ii++) {
104 index = (int)((Math.abs((double)r.nextInt()
105 /(double)Integer.MAX_VALUE)) * upperbound);
106 System.out.println("Scheduling index:" + index);
107 Vector<Schedule> scheduling = schedulings.elementAt(index);
108 this.setScheduling(scheduling);
109 Vector<SimExecutionEdge> simexegraph = new Vector<SimExecutionEdge>();
110 Vector<CheckPoint> checkpoints = new Vector<CheckPoint>();
111 int tmpTime = this.process(checkpoints, simexegraph);
112 if(tmpTime < processTime) {
113 selectedScheduling.clear();
114 selectedScheduling.add(index);
115 selectedSimExeGraphs.clear();
116 selectedSimExeGraphs.add(simexegraph);
117 processTime = tmpTime;
118 } else if(tmpTime == processTime) {
119 selectedScheduling.add(index);
120 selectedSimExeGraphs.add(simexegraph);
128 Iterator it_scheduling = schedulings.iterator();
130 while(it_scheduling.hasNext()) {
131 Vector<Schedule> scheduling =
132 (Vector<Schedule>)it_scheduling.next();
133 if(!state.BAMBOOCOMPILETIME) {
134 System.out.println("Scheduling index:" + scheduling.elementAt(0).getGid());
136 this.setScheduling(scheduling);
137 Vector<SimExecutionNode> simexegraph = new Vector<SimExecutionNode>();
138 Vector<CheckPoint> checkpoints = new Vector<CheckPoint>();
139 long tmpTime = process(checkpoints, simexegraph);
140 if(tmpTime < processTime) {
141 selectedScheduling.clear();
142 selectedScheduling.add(index);
143 selectedSimExeGraphs.clear();
144 selectedSimExeGraphs.add(simexegraph.elementAt(0));
145 processTime = tmpTime;
146 } else if(tmpTime == processTime) {
147 if(!selectedScheduling.contains(index)) {
148 selectedScheduling.add(index);
149 selectedSimExeGraphs.add(simexegraph.elementAt(0));
158 it_scheduling = null;
161 if(!state.BAMBOOCOMPILETIME) {
162 System.out.print("Selected schedulings with least exectution time " + processTime + ": \n\t");
163 for(int i = 0; i < selectedScheduling.size(); i++) {
164 int gid = schedulings.elementAt(selectedScheduling.elementAt(i)).elementAt(0).getGid();
165 System.out.print(gid + ", ");
167 System.out.println();
173 public int getCoreNum() {
177 public void setCoreNum(int corenum) {
178 this.coreNum = corenum;
179 if(this.cores != null) {
182 this.cores = new Vector<CoreSimulator>(this.coreNum);
183 for(int i = 0; i < this.coreNum; i++) {
184 this.cores.add(new CoreSimulator(FIFORSchedule.getFIFORSchedule(), i));
186 if(this.scheduling != null) {
191 public int getUtility(int index) {
192 return (int)(this.cores.elementAt(index).getActiveTime() * 100) / (int)this.processTime;
195 public Vector<Schedule> getScheduling() {
199 public void setScheduling(Vector<Schedule> scheduling) {
200 this.scheduling = scheduling;
201 if(this.tasks == null) {
202 this.tasks = new Vector<TaskSimulator>();
206 if(this.cores != null) {
207 for(int i = 0; i < this.coreNum; i++) {
208 CoreSimulator core = this.cores.elementAt(i);
210 core.setRSchedule(FIFORSchedule.getFIFORSchedule());
213 this.cores = new Vector<CoreSimulator>(this.coreNum);
214 for(int i = 0; i < this.coreNum; i++) {
215 this.cores.add(new CoreSimulator(FIFORSchedule.getFIFORSchedule(), i));
222 public void applyScheduling() {
223 assert(this.state != null);
225 for(int i = 0; i < this.scheduling.size(); i++) {
226 Schedule temp = this.scheduling.elementAt(i);
227 CoreSimulator cs = this.cores.elementAt(temp.getCoreNum());
228 cs.deployTasks(temp.getTasks());
229 cs.setTargetCSimulator(temp.getTargetCoreTable());
230 cs.setAllyCSimulator(temp.getAllyCoreTable());
231 cs.setTargetFState(temp.getTargetFStateTable());
233 // inject a Startup Object to each core
234 for(int i = 0; i < this.coreNum; i++) {
235 ClassDescriptor startupobject=(ClassDescriptor)state.getClassSymbolTable().get(TypeUtil.StartupClass);
236 FlagState fsstartup = (FlagState)taskanalysis.getRootNodes(startupobject).elementAt(0);
237 ObjectSimulator newObj = new ObjectSimulator(startupobject, fsstartup);
238 this.cores.elementAt(i).addObject(newObj);
242 public Vector<TaskSimulator> getTasks() {
246 private void init4Simulation() {
248 /*System.err.println("======Init for Sim # "
249 + this.scheduling.elementAt(0).getGid() + "======");*/
250 for(int i = 0; i < this.fstates.size(); i++) {
251 this.fstates.elementAt(i).init4Simulate();
253 for(int i = 0; i < this.fedges.size(); i++) {
254 this.fedges.elementAt(i).init4Simulate();
258 public long process(Vector<CheckPoint> checkpoints,
259 Vector<SimExecutionNode> simexegraph) {
260 assert(this.scheduling != null);
263 this.processTime = 0;
266 this.init4Simulation();
268 // helper structures for building SimExecutionGraph
269 Hashtable<SimExecutionNode, Action> senode2action =
270 new Hashtable<SimExecutionNode, Action>();
271 SimExecutionNode[] lastseNodes = new SimExecutionNode[this.cores.size()];
272 Hashtable<Action, Long> action2exetime =
273 new Hashtable<Action, Long>();
274 Hashtable<TransTaskSimulator, SimExecutionNode> tttask2senode =
275 new Hashtable<TransTaskSimulator, SimExecutionNode>();
276 Hashtable<Integer, Long> obj2transtime =
277 new Hashtable<Integer, Long>();
278 Hashtable<Integer, SimExecutionEdge> obj2lastseedge =
279 new Hashtable<Integer, SimExecutionEdge>();
281 // first decide next task to execute on each core
283 for(i = 0; i < this.cores.size(); i++) {
284 CoreSimulator cs = this.cores.elementAt(i);
285 TaskSimulator task = cs.process();
287 this.tasks.add(task);
289 lastseNodes[i] = null;
292 // add STARTTASK checkpoint for all the initial tasks
293 CheckPoint cp = new CheckPoint(this.processTime,
295 for(i = 0; i < this.tasks.size(); i++) {
296 TaskSimulator task = this.tasks.elementAt(i);
297 int coreid = task.getCs().getCoreNum();
298 Action action = new Action(coreid,
301 cp.addAction(action);
302 if(!(task instanceof TransTaskSimulator)) {
303 cp.removeSpareCore(coreid);
304 SimExecutionNode seNode = new SimExecutionNode(coreid, this.processTime);
305 seNode.setSpareCores(cp.getSpareCores());
306 senode2action.put(seNode, action);
307 action2exetime.put(action, (long)-1);
308 lastseNodes[coreid] = seNode;
314 // if no more tasks on each core, simulation finish
315 if(this.tasks.size() == 0) {
319 // for each task in todo queue, decide the execution path of this time
320 // according to statistic information
321 long finishTime = Long.MAX_VALUE;
322 Vector<TaskSimulator> finishTasks = new Vector<TaskSimulator>();
323 for(i = 0; i < this.tasks.size(); i++) {
324 TaskSimulator task = this.tasks.elementAt(i);
326 long tempTime = task.getCurrentRun().getFinishTime();
327 if(tempTime < finishTime) {
328 finishTime = tempTime;
330 finishTasks.add(task);
331 } else if (tempTime == finishTime) {
332 finishTasks.add(task);
336 // advance to next finish point
337 this.processTime += finishTime;
338 cp = new CheckPoint(this.processTime,
340 for(i = 0; i < this.tasks.size(); i++) {
341 TaskSimulator task = this.tasks.elementAt(i);
342 if(!finishTasks.contains(task)) {
343 task.getCs().updateTask(finishTime);
344 if(!(task instanceof TransTaskSimulator)) {
345 cp.removeSpareCore(task.getCs().getCoreNum());
350 Action action = null;
351 for(i = 0; i < finishTasks.size(); i++) {
352 TaskSimulator task = finishTasks.elementAt(i);
353 this.tasks.removeElement(task);
354 if(task instanceof TransTaskSimulator) {
355 // handle TransTaskSimulator task's completion
356 finishTransTaskSimulator(task,
364 CoreSimulator cs = task.getCs();
365 Vector<TransTaskSimulator> tttasks = new Vector<TransTaskSimulator>();
367 Vector<ObjectSimulator> transObjs = null;
368 if(task.getCurrentRun().getExetype() == 0) {
369 // normal execution of a task
370 transObjs = finishTaskNormal(task,
376 } else if (task.getCurrentRun().getExetype() == 1) {
378 finishTaskAbnormal(cs,
384 } else if (task.getCurrentRun().getExetype() == 2) {
386 finishTaskAbnormal(cs,
394 // Choose a new task for this core
408 } // end of if(task instanceof TransTaskSimulator) else
412 } // end of while(true)
414 // add the end node into the SimExecutionGraph
415 SimExecutionNode seNode = new SimExecutionNode(this.coreNum, this.processTime);
416 simexegraph.addElement(seNode);
417 for(int j = 0; j < lastseNodes.length; j++) {
418 SimExecutionNode lastsenode = lastseNodes[j];
419 // create edges between previous senode on this core to this node
420 if(lastsenode != null) {
421 Action tmpaction = senode2action.get(lastsenode);
422 long weight = tmpaction != null?action2exetime.get(tmpaction):0; // TODO ????
423 SimExecutionEdge seEdge = new SimExecutionEdge(seNode,
424 lastsenode.getCoreNum(),
425 tmpaction != null?tmpaction.getTd():null,
427 tmpaction != null?tmpaction.getTaskParams():null);
428 lastsenode.addEdge(seEdge);
430 // setup data dependencies for the task
431 Vector<Integer> taskparams = seEdge.getTaskparams();
432 if(taskparams != null) {
433 for(int k = 0; k < taskparams.size(); k++) {
434 Integer tparam = taskparams.elementAt(k);
435 SimExecutionEdge lastedge = obj2lastseedge.get(tparam);
436 if(lastedge != null) {
437 if(lastedge.getCoreNum() != seEdge.getCoreNum()) {
438 // the obj is transferred from another core
439 // create an seEdge for this transfer
440 long transweight = obj2transtime.get(tparam);
441 SimExecutionEdge transseEdge = new SimExecutionEdge((SimExecutionNode)seEdge.getSource(),
442 lastedge.getCoreNum(),
443 null, // TODO: not sure if this is enough
446 if(((SimExecutionNode)seEdge.getSource()).getTimepoint() <
447 ((SimExecutionNode)lastedge.getTarget()).getTimepoint()) {
448 System.err.println("ScheduleSimulator:393");
451 lastedge.getTarget().addEdge(transseEdge);
452 transseEdge.addPredicate(lastedge);
453 seEdge.addPredicate(transseEdge);
455 seEdge.addPredicate(lastedge);
458 // update the last edge associated to the parameter obj
459 obj2lastseedge.put(tparam, seEdge);
464 lastseNodes[j] = null;
467 senode2action.clear();
468 senode2action = null;
470 action2exetime.clear();
471 action2exetime = null;
472 tttask2senode.clear();
473 tttask2senode = null;
474 obj2transtime.clear();
475 obj2transtime = null;
476 obj2lastseedge.clear();
477 obj2lastseedge = null;
479 int gid = this.scheduling.elementAt(0).getGid();
480 if(this.state.PRINTSCHEDULESIM) {
481 SchedulingUtil.printSimulationResult(this.state.outputdir + "SimGraph/" + "SimulatorResult_" + gid + ".dot",
486 if(!state.BAMBOOCOMPILETIME) {
487 System.out.println("Simulate scheduling #" + gid + ": ");
488 System.out.println("\tTotal execution time is: " + this.processTime);
489 System.out.println("\tUtility of cores: ");
490 for(int j = 0; j < this.cores.size(); j++) {
491 System.out.println("\t\tcore" + j + ": " + getUtility(j) + "%");
495 return this.processTime;
498 private void finishTransTaskSimulator(TaskSimulator task,
500 Hashtable<SimExecutionNode, Action> senode2action,
501 SimExecutionNode[] lastseNodes,
502 Hashtable<Action, Long> action2exetime,
503 Hashtable<TransTaskSimulator, SimExecutionNode> tttask2senode,
504 Hashtable<Integer, Long> obj2transtime) {
505 TransTaskSimulator tmptask = (TransTaskSimulator)task;
506 // add ADDOBJ task to targetCore
507 int targetCoreNum = tmptask.getTargetCoreNum();
508 ObjectInfo objinfo = tmptask.refreshTask();
509 ObjectSimulator nobj = objinfo.obj;
510 FlagState fs = objinfo.fs;
511 int version = objinfo.version;
512 this.cores.elementAt(targetCoreNum).addObject(nobj, fs, version);
513 Action action = new Action(targetCoreNum, Action.ADDOBJ, 1, nobj.getCd());
514 cp.addAction(action);
516 // get the obj transfer time and associated senode
517 SimExecutionNode senode = tttask2senode.get(tmptask);
518 obj2transtime.put(nobj.getOid(), this.processTime - senode.getTimepoint());
520 if(!tmptask.isFinished()) {
521 // still have some objects to be transferred
522 this.tasks.add(task);
524 if(this.cores.elementAt(targetCoreNum).getRtask() == null) {
525 TaskSimulator newTask = this.cores.elementAt(targetCoreNum).process();
526 if(newTask != null) {
527 this.tasks.add(newTask);
528 // add a TASKSTART action into this checkpoint
529 action = new Action(targetCoreNum,
532 cp.addAction(action);
533 if(!(newTask instanceof TransTaskSimulator)) {
534 cp.removeSpareCore(targetCoreNum);
535 SimExecutionNode seNode = new SimExecutionNode(targetCoreNum, this.processTime);
536 seNode.setSpareCores(cp.getSpareCores());
537 senode2action.put(seNode, action);
538 action2exetime.put(action, (long)-1);
540 SimExecutionNode lastsenode = lastseNodes[targetCoreNum];
541 // create edges between previous senode on this core to this node
542 if(lastsenode != null) {
543 Action tmpaction = senode2action.get(lastsenode);
544 SimExecutionEdge seEdge = null;
545 if(tmpaction == null) {
546 seEdge = new SimExecutionEdge(seNode,
547 lastsenode.getCoreNum(),
552 long weight = action2exetime.get(tmpaction);
553 seEdge = new SimExecutionEdge(seNode,
554 lastsenode.getCoreNum(),
557 tmpaction.getTaskParams());
559 lastsenode.addEdge(seEdge);
561 lastseNodes[targetCoreNum] = seNode;
567 private Vector<ObjectSimulator> finishTaskNormal(TaskSimulator task,
569 Vector<TransTaskSimulator> tttasks,
570 Hashtable<SimExecutionNode, Action> senode2action,
571 SimExecutionNode[] lastseNodes,
572 Hashtable<Action, Long> action2exetime) {
573 Vector<ObjectSimulator> totransObjs = new Vector<ObjectSimulator>();
574 CoreSimulator cs = task.getCs();
575 int corenum = cs.getCoreNum();
576 Hashtable<Integer, Queue<ObjectInfo>> transObjQueues =
577 new Hashtable<Integer, Queue<ObjectInfo>>();
578 Action action = null;
579 if(task.getCurrentRun().getNewObjs() == null) {
580 // task finish without new objects
581 action = new Action(corenum,
584 // get the execution time of this task
585 SimExecutionNode lastsenode = lastseNodes[corenum];
586 Action startaction = senode2action.get(lastsenode);
587 action2exetime.put(startaction, cp.getTimepoint() - lastsenode.getTimepoint());
590 // task finish with new objects
591 action = new Action(corenum,
594 // get the execution time of this task
595 SimExecutionNode lastsenode = lastseNodes[corenum];
596 Action startaction = senode2action.get(lastsenode);
597 action2exetime.put(startaction, cp.getTimepoint() - lastsenode.getTimepoint());
599 // get the infomation of how to send new objects
600 Vector<ObjectSimulator> nobjs = task.getCurrentRun().getNewObjs();
601 for(int j = 0; j < nobjs.size(); j++) {
602 ObjectSimulator nobj = nobjs.elementAt(j);
603 totransObjs.add(nobj);
605 action.addNewObj(nobj.getCd(), Integer.valueOf(1));
606 // send the new object to target core according to pre-decide scheduling
607 Queue<Integer> cores = cs.getTargetCores(nobj.getCurrentFS());
609 // this obj will reside on this core
612 Integer targetCore = cores.poll();
613 if(targetCore == corenum) {
614 // this obj will reside on this core
617 if(!transObjQueues.containsKey(targetCore)) {
618 transObjQueues.put(targetCore, new LinkedList<ObjectInfo>());
620 Queue<ObjectInfo> tmpqueue = transObjQueues.get(targetCore);
621 tmpqueue.add(new ObjectInfo(nobj));
624 // enqueue this core again
625 cores.add(targetCore);
628 // check if this object becoming shared or not
629 Vector<Integer> allycores = cs.getAllyCores(nobj.getCurrentFS());
630 if(allycores != null) {
631 nobj.setShared(true);
632 // TODO, temporarily send to at most 2 cores
633 int numtosend = allycores.size() > 2?2:allycores.size();
634 for(int k = 0; k < numtosend; ++k) {
635 Integer allyCore = allycores.elementAt(k);
636 if(allyCore == corenum) {
639 if(!transObjQueues.containsKey(allyCore)) {
640 transObjQueues.put(allyCore, new LinkedList<ObjectInfo>());
642 Queue<ObjectInfo> tmpqueue = transObjQueues.get(allyCore);
643 ObjectInfo nobjinfo = new ObjectInfo(nobj);
644 if(!tmpqueue.contains(nobjinfo)) {
645 tmpqueue.add(nobjinfo);
655 cp.addAction(action);
657 // group the new objects need to transfer
658 Vector<ObjectSimulator> transObjs = cs.finishTask();
659 if(transObjs != null) {
660 totransObjs.addAll(transObjs);
661 for(int j = 0; j < transObjs.size(); j++) {
662 ObjectSimulator tobj = transObjs.elementAt(j);
663 // send the object to target core according to pre-decide scheduling
664 Queue<Integer> cores = cs.getTargetCores(tobj.getCurrentFS());
665 tobj.setCurrentFS(cs.getTargetFState(tobj.getCurrentFS()));
667 // this obj will reside on this core
670 Integer targetCore = cores.poll();
671 if(targetCore == corenum) {
672 // this obj will reside on this core
675 if(!transObjQueues.containsKey(targetCore)) {
676 transObjQueues.put(targetCore, new LinkedList<ObjectInfo>());
678 Queue<ObjectInfo> tmpqueue = transObjQueues.get(targetCore);
679 tmpqueue.add(new ObjectInfo(tobj));
682 cores.add(targetCore);
685 // check if this object becoming shared or not
686 Vector<Integer> allycores = cs.getAllyCores(tobj.getCurrentFS());
687 if(allycores != null) {
688 tobj.setShared(true);
689 // TODO, temporarily send to at most 2 cores
690 int numtosend = allycores.size() > 2?2:allycores.size();
691 for(int k = 0; k < numtosend; ++k) {
692 Integer allyCore = allycores.elementAt(k);
693 if(allyCore == corenum) {
696 if(!transObjQueues.containsKey(allyCore)) {
697 transObjQueues.put(allyCore, new LinkedList<ObjectInfo>());
699 Queue<ObjectInfo> tmpqueue = transObjQueues.get(allyCore);
700 ObjectInfo nobjinfo = new ObjectInfo(tobj);
701 if(!tmpqueue.contains(nobjinfo)) {
702 tmpqueue.add(nobjinfo);
713 // add 'transport' tasks
714 Iterator it_entries = transObjQueues.entrySet().iterator();
715 while(it_entries.hasNext()) {
716 Entry<Integer, Queue<ObjectInfo>> tmpentry = (Entry<Integer, Queue<ObjectInfo>>)it_entries.next();
717 Integer tmpCoreNum = tmpentry.getKey();
718 Queue<ObjectInfo> nobjs = tmpentry.getValue();
719 TransTaskSimulator tmptask = new TransTaskSimulator(cs, tmpCoreNum, nobjs);
720 this.tasks.add(tmptask);
721 tttasks.add(tmptask);
726 transObjQueues = null;
731 private void generateNewTask(CoreSimulator cs,
733 Vector<ObjectSimulator> nobjs,
734 Vector<TransTaskSimulator> tttasks,
735 Hashtable<SimExecutionNode, Action> senode2action,
736 SimExecutionNode[] lastseNodes,
737 Hashtable<Action, Long> action2exetime,
738 Hashtable<TransTaskSimulator, SimExecutionNode> tttask2senode,
739 Hashtable<Integer, Long> obj2transtime,
740 Hashtable<Integer, SimExecutionEdge> obj2lastseedge) {
741 TaskSimulator newTask = cs.process();
742 int corenum = cs.getCoreNum();
743 SimExecutionEdge seEdge = null;
744 if(newTask != null) {
745 this.tasks.add(newTask);
746 // add a TASKSTART action into this checkpoint
747 Action action = new Action(corenum,
750 cp.addAction(action);
751 if(!(newTask instanceof TransTaskSimulator)) {
752 cp.removeSpareCore(cs.getCoreNum());
753 SimExecutionNode seNode = new SimExecutionNode(corenum, this.processTime);
754 seNode.setSpareCores(cp.getSpareCores());
755 senode2action.put(seNode, action);
756 action2exetime.put(action, (long)-1);
757 SimExecutionNode lastsenode = lastseNodes[corenum];
758 // create edges between previous senode on this core to this node
759 if(lastsenode != null) {
760 Action tmpaction = senode2action.get(lastsenode);
761 long weight = tmpaction != null?action2exetime.get(tmpaction):0;
762 seEdge = new SimExecutionEdge(seNode,
763 lastsenode.getCoreNum(),
764 tmpaction!= null?tmpaction.getTd():null,
766 tmpaction!=null?tmpaction.getTaskParams():null);
767 lastsenode.addEdge(seEdge);
769 lastseNodes[corenum] = seNode;
770 for(int tmpindex = 0; tmpindex < tttasks.size(); tmpindex++) {
771 tttask2senode.put(tttasks.elementAt(tmpindex), seNode);
774 } else if(tttasks.size() > 0) {
775 SimExecutionNode seNode = new SimExecutionNode(corenum, this.processTime);
776 //seNode.setSpareCores(cp.getSpareCores());
777 // no action associated here
778 SimExecutionNode lastsenode = lastseNodes[corenum];
779 // create edges between previous senode on this core to this node
780 if(lastsenode != null) {
781 Action tmpaction = senode2action.get(lastsenode);
782 long weight = action2exetime.get(tmpaction);
783 seEdge = new SimExecutionEdge(seNode,
784 lastsenode.getCoreNum(),
787 tmpaction.getTaskParams());
788 lastsenode.addEdge(seEdge);
790 lastseNodes[corenum] = seNode;
791 for(int tmpindex = 0; tmpindex < tttasks.size(); tmpindex++) {
792 tttask2senode.put(tttasks.elementAt(tmpindex), seNode);
796 // setup data dependencies for the task
797 Vector<Integer> taskparams = seEdge.getTaskparams();
798 if(taskparams != null) {
799 for(int i = 0; i < taskparams.size(); i++) {
800 Integer tparam = taskparams.elementAt(i);
801 SimExecutionEdge lastedge = obj2lastseedge.get(tparam);
802 if(lastedge != null) {
803 if(lastedge.getCoreNum() != seEdge.getCoreNum()) {
804 // the obj is transferred from another core
805 // create an seEdge for this transfer
806 long weight = obj2transtime.get(tparam);
807 SimExecutionEdge transseEdge = new SimExecutionEdge((SimExecutionNode)seEdge.getSource(),
808 lastedge.getCoreNum(),
809 null, // TODO: not sure if this is enough
812 if(((SimExecutionNode)seEdge.getSource()).getTimepoint() <
813 ((SimExecutionNode)lastedge.getTarget()).getTimepoint()) {
814 System.err.println("ScheduleSimulator:757");
817 lastedge.getTarget().addEdge(transseEdge);
818 transseEdge.addPredicate(lastedge);
819 seEdge.addPredicate(transseEdge);
821 seEdge.addPredicate(lastedge);
824 // update the last edge associated to the parameter obj
825 obj2lastseedge.put(tparam, seEdge);
830 // set seEdge as the last execution edge for all newly created objs
832 for(int i = 0; i < nobjs.size(); i++) {
833 ObjectSimulator nobj = nobjs.elementAt(i);
834 obj2lastseedge.put(nobj.getOid(), seEdge);
840 private void finishTaskAbnormal(CoreSimulator cs,
842 Hashtable<SimExecutionNode, Action> senode2action,
843 SimExecutionNode[] lastseNodes,
844 Hashtable<Action, Long> action2exetime,
846 Action action = new Action(cs.getCoreNum(),
849 cp.addAction(action);
852 // remove the corresponding action on the starting SimExecutionNode
853 SimExecutionNode lastsenode = lastseNodes[cs.getCoreNum()];
854 /*if(lastsenode.getInedgeVector().size() > 0) {
855 //SimExecutionEdge inseedge = (SimExecutionEdge)lastsenode.getinedge(0);
856 //lastseNodes[cs.getCoreNum()] = (SimExecutionNode)inseedge.getSource();
858 lastseNodes[cs.getCoreNum()] = null;
860 Action tmpaction = senode2action.remove(lastsenode);
861 action2exetime.remove(tmpaction);
864 public class CheckPoint {
865 private long timepoint;
866 private Vector<Action> actions;
867 private Vector<Integer> spareCores;
869 public CheckPoint(long timepoint,
872 this.timepoint = timepoint;
873 this.actions = new Vector<Action>();
874 this.spareCores = new Vector<Integer>();
875 for(int i = 0; i < corenum; i++) {
876 this.spareCores.add(i);
880 public Vector<Action> getActions() {
884 public void addAction(Action action) {
885 this.actions.add(action);
888 public void removeSpareCore(int core) {
889 for(int i = 0; i < this.spareCores.size(); i++) {
890 if(this.spareCores.elementAt(i) == core) {
891 for(int j = i; j < this.spareCores.size() - 1; j++) {
892 this.spareCores.setElementAt(this.spareCores.elementAt(j + 1), j);
894 this.spareCores.remove(this.spareCores.size() - 1);
900 public long getTimepoint() {
904 public Vector<Integer> getSpareCores() {
909 public class Action {
910 public static final int ADDOBJ = 0;
911 public static final int TASKFINISH = 1;
912 public static final int TFWITHOBJ = 2;
913 public static final int TASKSTART = 3;
914 public static final int TASKABORT = 4;
915 public static final int TASKREMOVE = 5;
919 private TaskDescriptor td;
920 private Vector<Integer> taskparams;
921 private Hashtable<ClassDescriptor, Integer> nObjs;
923 private ClassDescriptor transObj;
925 public Action(int corenum,
927 this.coreNum = corenum;
930 this.taskparams = null;
931 if(this.type == TFWITHOBJ) {
932 this.nObjs = new Hashtable<ClassDescriptor, Integer>();
937 this.transObj = null;
940 public Action(int corenum,
943 assert(this.type != ADDOBJ);
945 this.coreNum = corenum;
947 this.td = ts.getTd();
948 Vector<Queue<ObjectSimulator>> paraQueues = ts.getParaQueues();
949 if(this.type == TASKSTART) {
950 this.taskparams = new Vector<Integer>();
951 for(int i = 0; i < paraQueues.size(); i++) {
952 ObjectSimulator tpara = paraQueues.elementAt(i).peek();
953 this.taskparams.add(tpara.getOid());
956 this.taskparams = null;
959 if(this.type == TFWITHOBJ) {
960 this.nObjs = new Hashtable<ClassDescriptor, Integer>();
965 this.transObj = null;
968 public Action(int corenum,
971 ClassDescriptor transObj) {
972 assert(type == ADDOBJ);
973 this.coreNum = corenum;
976 this.taskparams = null;
977 this.nObjNum = objNum;
978 this.transObj = transObj;
981 public void addNewObj(ClassDescriptor cd,
983 assert(this.type == TFWITHOBJ);
985 if(this.nObjs.containsKey(cd)) {
986 Integer sum = this.nObjs.get(cd) + num;
987 this.nObjs.put(cd, sum);
989 this.nObjs.put(cd, num);
993 public int getCoreNum() {
997 public int getType() {
1001 public int getNObjNum() {
1005 public ClassDescriptor getTransObj() {
1009 public TaskDescriptor getTd() {
1013 public Vector<Integer> getTaskParams() {
1014 return this.taskparams;
1017 public Hashtable<ClassDescriptor, Integer> getNObjs() {