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;
9 import java.util.Vector;
10 import java.util.Map.Entry;
12 import Analysis.TaskStateAnalysis.FlagState;
13 import Analysis.TaskStateAnalysis.TaskAnalysis;
14 import IR.ClassDescriptor;
16 import IR.TaskDescriptor;
19 public class ScheduleSimulator {
21 private Vector<Schedule> scheduling;
22 private Vector<CoreSimulator> cores;
23 private Vector<TaskSimulator> tasks;
24 private int processTime;
25 private int invoketime;
28 TaskAnalysis taskanalysis;
30 public ScheduleSimulator(int corenum,
32 TaskAnalysis taskanalysis) {
33 this.coreNum = corenum;
34 this.scheduling = null;
40 this.taskanalysis = taskanalysis;
43 public ScheduleSimulator(int corenum,
44 Vector<Schedule> scheduling,
46 TaskAnalysis taskanalysis) {
48 this.coreNum = corenum;
49 this.scheduling = scheduling;
50 this.cores = new Vector<CoreSimulator>(this.coreNum);
51 for(int i = 0; i < this.coreNum; i++) {
52 this.cores.add(new CoreSimulator(FIFORSchedule.getFIFORSchedule(), i));
54 this.tasks = new Vector<TaskSimulator>();
58 this.taskanalysis = taskanalysis;
62 public int simulate(Vector<Vector<Schedule>> schedulings,
63 Vector<Integer> selectedScheduling,
64 Vector<Vector<SimExecutionEdge>> selectedSimExeGraphs) {
65 int processTime = Integer.MAX_VALUE;
66 /*if(schedulings.size() > 1500) {
68 int upperbound = schedulings.size();
70 java.util.Random r = new java.util.Random(seed);
71 for(int ii = 0; ii < 1500; ii++) {
72 index = (int)((Math.abs((double)r.nextInt()
73 /(double)Integer.MAX_VALUE)) * upperbound);
74 System.out.println("Scheduling index:" + index);
75 Vector<Schedule> scheduling = schedulings.elementAt(index);
76 this.setScheduling(scheduling);
77 Vector<SimExecutionEdge> simexegraph = new Vector<SimExecutionEdge>();
78 Vector<CheckPoint> checkpoints = new Vector<CheckPoint>();
79 int tmpTime = this.process(checkpoints, simexegraph);
80 if(tmpTime < processTime) {
81 selectedScheduling.clear();
82 selectedScheduling.add(index);
83 selectedSimExeGraphs.clear();
84 selectedSimExeGraphs.add(simexegraph);
85 processTime = tmpTime;
86 } else if(tmpTime == processTime) {
87 selectedScheduling.add(index);
88 selectedSimExeGraphs.add(simexegraph);
95 Iterator it_scheduling = schedulings.iterator();
97 while(it_scheduling.hasNext()) {
98 Vector<Schedule> scheduling =
99 (Vector<Schedule>)it_scheduling.next();
100 System.out.println("Scheduling index:" + scheduling.elementAt(0).getGid());
101 this.setScheduling(scheduling);
102 Vector<SimExecutionEdge> simexegraph = new Vector<SimExecutionEdge>();
103 Vector<CheckPoint> checkpoints = new Vector<CheckPoint>();
104 int tmpTime = this.process(checkpoints, simexegraph);
105 if(tmpTime < processTime) {
106 selectedScheduling.clear();
107 selectedScheduling.add(index);
108 selectedSimExeGraphs.clear();
109 selectedSimExeGraphs.add(simexegraph);
110 processTime = tmpTime;
111 } else if(tmpTime == processTime) {
112 selectedScheduling.add(index);
113 selectedSimExeGraphs.add(simexegraph);
119 it_scheduling = null;
122 System.out.print("Selected schedulings with least exectution time " + processTime + ": \n\t");
123 for(int i = 0; i < selectedScheduling.size(); i++) {
124 int gid = schedulings.elementAt(selectedScheduling.elementAt(i)).elementAt(0).getGid();
125 System.out.print(gid + ", ");
127 System.out.println();
132 public int getCoreNum() {
136 public void setCoreNum(int corenum) {
137 this.coreNum = corenum;
138 if(this.cores != null) {
141 this.cores = new Vector<CoreSimulator>(this.coreNum);
142 for(int i = 0; i < this.coreNum; i++) {
143 this.cores.add(new CoreSimulator(FIFORSchedule.getFIFORSchedule(), i));
145 if(this.scheduling != null) {
150 public int getUtility(int index) {
151 return (this.cores.elementAt(index).getActiveTime() * 100) / this.processTime;
154 public Vector<Schedule> getScheduling() {
158 public void setScheduling(Vector<Schedule> scheduling) {
159 this.scheduling = scheduling;
160 if(this.tasks == null) {
161 this.tasks = new Vector<TaskSimulator>();
165 if(this.cores != null) {
166 for(int i = 0; i < this.coreNum; i++) {
167 CoreSimulator core = this.cores.elementAt(i);
169 core.setRSchedule(FIFORSchedule.getFIFORSchedule());
172 this.cores = new Vector<CoreSimulator>(this.coreNum);
173 for(int i = 0; i < this.coreNum; i++) {
174 this.cores.add(new CoreSimulator(FIFORSchedule.getFIFORSchedule(), i));
181 public void applyScheduling() {
182 assert(this.state != null);
184 for(int i = 0; i < this.scheduling.size(); i++) {
185 Schedule temp = this.scheduling.elementAt(i);
186 CoreSimulator cs = this.cores.elementAt(temp.getCoreNum());
187 cs.deployTasks(temp.getTasks());
188 cs.setTargetCSimulator(temp.getTargetCoreTable());
189 cs.setAllyCSimulator(temp.getAllyCoreTable());
190 cs.setTargetFState(temp.getTargetFStateTable());
192 // inject a Startup Object to each core
193 for(int i = 0; i < this.coreNum; i++) {
194 ClassDescriptor startupobject=(ClassDescriptor)state.getClassSymbolTable().get(TypeUtil.StartupClass);
195 FlagState fsstartup = (FlagState)taskanalysis.getRootNodes(startupobject).elementAt(0);
196 ObjectSimulator newObj = new ObjectSimulator(startupobject, fsstartup);
197 this.cores.elementAt(i).addObject(newObj);
201 public Vector<TaskSimulator> getTasks() {
205 public int process(Vector<CheckPoint> checkpoints,
206 Vector<SimExecutionEdge> simexegraph) {
207 assert(this.scheduling != null);
210 this.processTime = 0;
212 // helper structures for building SimExecutionGraph
213 Hashtable<SimExecutionNode, Action> senode2action =
214 new Hashtable<SimExecutionNode, Action>();
215 SimExecutionNode[] lastseNodes = new SimExecutionNode[this.cores.size()];
216 Hashtable<Action, Integer> action2exetime =
217 new Hashtable<Action, Integer>();
218 Hashtable<TransTaskSimulator, SimExecutionNode> tttask2senode =
219 new Hashtable<TransTaskSimulator, SimExecutionNode>();
220 Hashtable<Integer, Integer> obj2transtime =
221 new Hashtable<Integer, Integer>();
222 Hashtable<Integer, SimExecutionEdge> obj2lastseedge =
223 new Hashtable<Integer, SimExecutionEdge>();
225 // first decide next task to execute on each core
227 for(i = 0; i < this.cores.size(); i++) {
228 CoreSimulator cs = this.cores.elementAt(i);
229 TaskSimulator task = cs.process();
231 this.tasks.add(task);
233 lastseNodes[i] = null;
236 // add STARTTASK checkpoint for all the initial tasks
237 CheckPoint cp = new CheckPoint(this.processTime,
239 for(i = 0; i < this.tasks.size(); i++) {
240 TaskSimulator task = this.tasks.elementAt(i);
241 int coreid = task.getCs().getCoreNum();
242 Action action = new Action(coreid,
245 cp.addAction(action);
246 if(!(task instanceof TransTaskSimulator)) {
247 cp.removeSpareCore(coreid);
248 SimExecutionNode seNode = new SimExecutionNode(coreid, this.processTime);
249 seNode.setSpareCores(cp.getSpareCores());
250 senode2action.put(seNode, action);
251 action2exetime.put(action, -1);
252 lastseNodes[coreid] = seNode;
258 // if no more tasks on each core, simulation finish
259 if(this.tasks.size() == 0) {
263 // for each task in todo queue, decide the execution path of this time
264 // according to statistic information
265 int finishTime = Integer.MAX_VALUE;
266 Vector<TaskSimulator> finishTasks = new Vector<TaskSimulator>();
267 for(i = 0; i < this.tasks.size(); i++) {
268 TaskSimulator task = this.tasks.elementAt(i);
270 int tempTime = task.getCurrentRun().getFinishTime();
271 if(tempTime < finishTime) {
272 finishTime = tempTime;
274 finishTasks.add(task);
275 } else if (tempTime == finishTime) {
276 finishTasks.add(task);
280 // advance to next finish point
281 this.processTime += finishTime;
282 cp = new CheckPoint(this.processTime,
284 for(i = 0; i < this.tasks.size(); i++) {
285 TaskSimulator task = this.tasks.elementAt(i);
286 if(!finishTasks.contains(task)) {
287 task.getCs().updateTask(finishTime);
288 if(!(task instanceof TransTaskSimulator)) {
289 cp.removeSpareCore(task.getCs().getCoreNum());
294 Action action = null;
295 for(i = 0; i < finishTasks.size(); i++) {
296 TaskSimulator task = finishTasks.elementAt(i);
297 this.tasks.removeElement(task);
298 if(task instanceof TransTaskSimulator) {
299 // handle TransTaskSimulator task's completion
300 finishTransTaskSimulator(task,
309 CoreSimulator cs = task.getCs();
310 Vector<TransTaskSimulator> tttasks = new Vector<TransTaskSimulator>();
312 Vector<ObjectSimulator> transObjs = null;
313 if(task.getCurrentRun().getExetype() == 0) {
314 // normal execution of a task
315 transObjs = finishTaskNormal(task,
321 } else if (task.getCurrentRun().getExetype() == 1) {
323 finishTaskAbnormal(cs,
329 } else if (task.getCurrentRun().getExetype() == 2) {
331 finishTaskAbnormal(cs,
339 // Choose a new task for this core
354 }// end of if(task instanceof TransTaskSimulator) else
358 } // end of while(true)
360 // add the end node into the SimExecutionGraph
361 SimExecutionNode seNode = new SimExecutionNode(this.coreNum, this.processTime);
362 for(int j = 0; j < lastseNodes.length; j++) {
363 SimExecutionNode lastsenode = lastseNodes[j];
364 // create edges between previous senode on this core to this node
365 if(lastsenode != null) {
366 Action tmpaction = senode2action.get(lastsenode);
367 int weight = tmpaction != null? action2exetime.get(tmpaction) : 0; // TODO ????
368 SimExecutionEdge seEdge = new SimExecutionEdge(seNode,
369 lastsenode.getCoreNum(),
370 tmpaction != null? tmpaction.getTd():null,
372 tmpaction != null? tmpaction.getTaskParams():null);
373 lastsenode.addEdge(seEdge);
375 // setup data dependencies for the task
376 Vector<Integer> taskparams = seEdge.getTaskparams();
377 if(taskparams != null) {
378 for(int k = 0; k < taskparams.size(); k++) {
379 Integer tparam = taskparams.elementAt(k);
380 SimExecutionEdge lastedge = obj2lastseedge.get(tparam);
381 if(lastedge != null) {
382 if(lastedge.getCoreNum() != seEdge.getCoreNum()) {
383 // the obj is transferred from another core
384 // create an seEdge for this transfer
385 int transweight = obj2transtime.get(tparam);
386 SimExecutionEdge transseEdge = new SimExecutionEdge((SimExecutionNode)seEdge.getSource(),
387 lastedge.getCoreNum(),
388 null, // TODO: not sure if this is enough
391 if(((SimExecutionNode)seEdge.getSource()).getTimepoint() <
392 ((SimExecutionNode)lastedge.getTarget()).getTimepoint()) {
393 System.err.println("ScheduleSimulator:393");
396 lastedge.getTarget().addEdge(transseEdge);
397 simexegraph.add(transseEdge);
398 transseEdge.addPredicate(lastedge);
399 seEdge.addPredicate(transseEdge);
401 seEdge.addPredicate(lastedge);
404 // update the last edge associated to the parameter obj
405 obj2lastseedge.put(tparam, seEdge);
409 simexegraph.add(seEdge); // add the seEdge afger all corresponding transfer edges
411 lastseNodes[j] = null;
414 senode2action.clear();
415 senode2action = null;
417 action2exetime.clear();
418 action2exetime = null;
419 tttask2senode.clear();
420 tttask2senode = null;
421 obj2transtime.clear();
422 obj2transtime = null;
423 obj2lastseedge.clear();
424 obj2lastseedge = null;
426 int gid = this.scheduling.elementAt(0).getGid();
427 if(this.state.PRINTSCHEDULESIM) {
428 SchedulingUtil.printSimulationResult("SimulatorResult_" + gid + ".dot",
433 System.out.println("Simulate scheduling #" + gid + ": ");
434 System.out.println("\tTotal execution time is: " + this.processTime);
435 System.out.println("\tUtility of cores: ");
436 for(int j = 0; j < this.cores.size(); j++) {
437 System.out.println("\t\tcore" + j + ": " + getUtility(j) + "%");
440 return this.processTime;
443 private void finishTransTaskSimulator(TaskSimulator task,
445 Vector<SimExecutionEdge> simexegraph,
446 Hashtable<SimExecutionNode, Action> senode2action,
447 SimExecutionNode[] lastseNodes,
448 Hashtable<Action, Integer> action2exetime,
449 Hashtable<TransTaskSimulator, SimExecutionNode> tttask2senode,
450 Hashtable<Integer, Integer> obj2transtime) {
451 TransTaskSimulator tmptask = (TransTaskSimulator)task;
452 // add ADDOBJ task to targetCore
453 int targetCoreNum = tmptask.getTargetCoreNum();
454 ObjectInfo objinfo = tmptask.refreshTask();
455 ObjectSimulator nobj = objinfo.obj;
456 FlagState fs = objinfo.fs;
457 int version = objinfo.version;
458 this.cores.elementAt(targetCoreNum).addObject(nobj, fs, version);
459 Action action = new Action(targetCoreNum, Action.ADDOBJ, 1, nobj.getCd());
460 cp.addAction(action);
462 // get the obj transfer time and associated senode
463 SimExecutionNode senode = tttask2senode.get(tmptask);
464 obj2transtime.put(nobj.getOid(), this.processTime - senode.getTimepoint());
466 if(!tmptask.isFinished()) {
467 // still have some objects to be transferred
468 this.tasks.add(task);
470 if(this.cores.elementAt(targetCoreNum).getRtask() == null) {
471 TaskSimulator newTask = this.cores.elementAt(targetCoreNum).process();
472 if(newTask != null) {
473 this.tasks.add(newTask);
474 // add a TASKSTART action into this checkpoint
475 action = new Action(targetCoreNum,
478 cp.addAction(action);
479 if(!(newTask instanceof TransTaskSimulator)) {
480 cp.removeSpareCore(targetCoreNum);
481 SimExecutionNode seNode = new SimExecutionNode(targetCoreNum, this.processTime);
482 seNode.setSpareCores(cp.getSpareCores());
483 senode2action.put(seNode, action);
484 action2exetime.put(action, -1);
486 SimExecutionNode lastsenode = lastseNodes[targetCoreNum];
487 // create edges between previous senode on this core to this node
488 if(lastsenode != null) {
489 Action tmpaction = senode2action.get(lastsenode);
490 SimExecutionEdge seEdge = null;
491 if(tmpaction == null) {
492 seEdge = new SimExecutionEdge(seNode,
493 lastsenode.getCoreNum(),
498 int weight = action2exetime.get(tmpaction);
499 seEdge = new SimExecutionEdge(seNode,
500 lastsenode.getCoreNum(),
503 tmpaction.getTaskParams());
505 lastsenode.addEdge(seEdge);
506 simexegraph.add(seEdge);
508 lastseNodes[targetCoreNum] = seNode;
514 private Vector<ObjectSimulator> finishTaskNormal(TaskSimulator task,
516 Vector<TransTaskSimulator> tttasks,
517 Hashtable<SimExecutionNode, Action> senode2action,
518 SimExecutionNode[] lastseNodes,
519 Hashtable<Action, Integer> action2exetime) {
520 Vector<ObjectSimulator> totransObjs = new Vector<ObjectSimulator>();
521 CoreSimulator cs = task.getCs();
522 int corenum = cs.getCoreNum();
523 Hashtable<Integer, Queue<ObjectInfo>> transObjQueues =
524 new Hashtable<Integer, Queue<ObjectInfo>>();
525 Action action = null;
526 if(task.getCurrentRun().getNewObjs() == null) {
527 // task finish without new objects
528 action = new Action(corenum,
531 // get the execution time of this task
532 SimExecutionNode lastsenode = lastseNodes[corenum];
533 Action startaction = senode2action.get(lastsenode);
534 action2exetime.put(startaction, cp.getTimepoint() - lastsenode.getTimepoint());
537 // task finish with new objects
538 action = new Action(corenum,
541 // get the execution time of this task
542 SimExecutionNode lastsenode = lastseNodes[corenum];
543 Action startaction = senode2action.get(lastsenode);
544 action2exetime.put(startaction, cp.getTimepoint() - lastsenode.getTimepoint());
546 // get the infomation of how to send new objects
547 Vector<ObjectSimulator> nobjs = task.getCurrentRun().getNewObjs();
548 for(int j = 0; j < nobjs.size(); j++) {
549 ObjectSimulator nobj = nobjs.elementAt(j);
550 totransObjs.add(nobj);
552 action.addNewObj(nobj.getCd(), Integer.valueOf(1));
553 // send the new object to target core according to pre-decide scheduling
554 Queue<Integer> cores = cs.getTargetCores(nobj.getCurrentFS());
556 // this obj will reside on this core
559 Integer targetCore = cores.poll();
560 if(targetCore == corenum) {
561 // this obj will reside on this core
564 if(!transObjQueues.containsKey(targetCore)) {
565 transObjQueues.put(targetCore, new LinkedList<ObjectInfo>());
567 Queue<ObjectInfo> tmpqueue = transObjQueues.get(targetCore);
568 tmpqueue.add(new ObjectInfo(nobj));
571 // enqueue this core again
572 cores.add(targetCore);
575 // check if this object becoming shared or not
576 Vector<Integer> allycores = cs.getAllyCores(nobj.getCurrentFS());
577 if(allycores != null) {
578 nobj.setShared(true);
579 for(int k = 0; k < allycores.size(); ++k) {
580 Integer allyCore = allycores.elementAt(k);
581 if(allyCore == corenum) {
584 if(!transObjQueues.containsKey(allyCore)) {
585 transObjQueues.put(allyCore, new LinkedList<ObjectInfo>());
587 Queue<ObjectInfo> tmpqueue = transObjQueues.get(allyCore);
588 ObjectInfo nobjinfo = new ObjectInfo(nobj);
589 if(!tmpqueue.contains(nobjinfo)) {
590 tmpqueue.add(nobjinfo);
600 cp.addAction(action);
602 // group the new objects need to transfer
603 Vector<ObjectSimulator> transObjs = cs.finishTask();
604 if(transObjs != null) {
605 totransObjs.addAll(transObjs);
606 for(int j = 0; j < transObjs.size(); j++) {
607 ObjectSimulator tobj = transObjs.elementAt(j);
608 // send the object to target core according to pre-decide scheduling
609 Queue<Integer> cores = cs.getTargetCores(tobj.getCurrentFS());
610 tobj.setCurrentFS(cs.getTargetFState(tobj.getCurrentFS()));
612 // this obj will reside on this core
615 Integer targetCore = cores.poll();
616 if(targetCore == corenum) {
617 // this obj will reside on this core
620 if(!transObjQueues.containsKey(targetCore)) {
621 transObjQueues.put(targetCore, new LinkedList<ObjectInfo>());
623 Queue<ObjectInfo> tmpqueue = transObjQueues.get(targetCore);
624 tmpqueue.add(new ObjectInfo(tobj));
627 cores.add(targetCore);
630 // check if this object becoming shared or not
631 Vector<Integer> allycores = cs.getAllyCores(tobj.getCurrentFS());
632 if(allycores != null) {
633 tobj.setShared(true);
634 for(int k = 0; k < allycores.size(); ++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(tobj);
644 if(!tmpqueue.contains(nobjinfo)) {
645 tmpqueue.add(nobjinfo);
656 // add 'transport' tasks
657 Iterator it_entries = transObjQueues.entrySet().iterator();
658 while(it_entries.hasNext()) {
659 Entry<Integer, Queue<ObjectInfo>> tmpentry = (Entry<Integer, Queue<ObjectInfo>>)it_entries.next();
660 Integer tmpCoreNum = tmpentry.getKey();
661 Queue<ObjectInfo> nobjs = tmpentry.getValue();
662 TransTaskSimulator tmptask = new TransTaskSimulator(cs, tmpCoreNum, nobjs);
663 this.tasks.add(tmptask);
664 tttasks.add(tmptask);
668 transObjQueues = null;
673 private void generateNewTask(CoreSimulator cs,
675 Vector<ObjectSimulator> nobjs,
676 Vector<TransTaskSimulator> tttasks,
677 Vector<SimExecutionEdge> simexegraph,
678 Hashtable<SimExecutionNode, Action> senode2action,
679 SimExecutionNode[] lastseNodes,
680 Hashtable<Action, Integer> action2exetime,
681 Hashtable<TransTaskSimulator, SimExecutionNode> tttask2senode,
682 Hashtable<Integer, Integer> obj2transtime,
683 Hashtable<Integer, SimExecutionEdge> obj2lastseedge) {
684 TaskSimulator newTask = cs.process();
685 int corenum = cs.getCoreNum();
686 SimExecutionEdge seEdge = null;
687 if(newTask != null) {
688 this.tasks.add(newTask);
689 // add a TASKSTART action into this checkpoint
690 Action action = new Action(corenum,
693 cp.addAction(action);
694 if(!(newTask instanceof TransTaskSimulator)) {
695 cp.removeSpareCore(cs.getCoreNum());
696 SimExecutionNode seNode = new SimExecutionNode(corenum, this.processTime);
697 seNode.setSpareCores(cp.getSpareCores());
698 senode2action.put(seNode, action);
699 action2exetime.put(action, -1);
700 SimExecutionNode lastsenode = lastseNodes[corenum];
701 // create edges between previous senode on this core to this node
702 if(lastsenode != null) {
703 Action tmpaction = senode2action.get(lastsenode);
704 int weight = tmpaction != null? action2exetime.get(tmpaction):0;
705 seEdge = new SimExecutionEdge(seNode,
706 lastsenode.getCoreNum(),
707 tmpaction!= null?tmpaction.getTd():null,
709 tmpaction!=null?tmpaction.getTaskParams():null);
710 lastsenode.addEdge(seEdge);
712 lastseNodes[corenum] = seNode;
713 for(int tmpindex = 0; tmpindex < tttasks.size(); tmpindex++) {
714 tttask2senode.put(tttasks.elementAt(tmpindex), seNode);
717 } else if(tttasks.size() > 0) {
718 SimExecutionNode seNode = new SimExecutionNode(corenum, this.processTime);
719 seNode.setSpareCores(cp.getSpareCores());
720 // no action associated here
721 SimExecutionNode lastsenode = lastseNodes[corenum];
722 // create edges between previous senode on this core to this node
723 if(lastsenode != null) {
724 Action tmpaction = senode2action.get(lastsenode);
725 int weight = action2exetime.get(tmpaction);
726 seEdge = new SimExecutionEdge(seNode,
727 lastsenode.getCoreNum(),
730 tmpaction.getTaskParams());
731 lastsenode.addEdge(seEdge);
733 lastseNodes[corenum] = seNode;
734 for(int tmpindex = 0; tmpindex < tttasks.size(); tmpindex++) {
735 tttask2senode.put(tttasks.elementAt(tmpindex), seNode);
739 // setup data dependencies for the task
740 Vector<Integer> taskparams = seEdge.getTaskparams();
741 if(taskparams != null) {
742 for(int i = 0; i < taskparams.size(); i++) {
743 Integer tparam = taskparams.elementAt(i);
744 SimExecutionEdge lastedge = obj2lastseedge.get(tparam);
745 if(lastedge != null) {
746 if(lastedge.getCoreNum() != seEdge.getCoreNum()) {
747 // the obj is transferred from another core
748 // create an seEdge for this transfer
749 int weight = obj2transtime.get(tparam);
750 SimExecutionEdge transseEdge = new SimExecutionEdge((SimExecutionNode)seEdge.getSource(),
751 lastedge.getCoreNum(),
752 null, // TODO: not sure if this is enough
755 if(((SimExecutionNode)seEdge.getSource()).getTimepoint() <
756 ((SimExecutionNode)lastedge.getTarget()).getTimepoint()) {
757 System.err.println("ScheduleSimulator:757");
760 lastedge.getTarget().addEdge(transseEdge);
761 simexegraph.add(transseEdge);
762 transseEdge.addPredicate(lastedge);
763 seEdge.addPredicate(transseEdge);
765 seEdge.addPredicate(lastedge);
768 // update the last edge associated to the parameter obj
769 obj2lastseedge.put(tparam, seEdge);
773 simexegraph.add(seEdge); // add the seEdge afger all corresponding transfer edges
775 // set seEdge as the last execution edge for all newly created objs
777 for(int i = 0; i < nobjs.size(); i++) {
778 ObjectSimulator nobj = nobjs.elementAt(i);
779 obj2lastseedge.put(nobj.getOid(), seEdge);
785 private void finishTaskAbnormal(CoreSimulator cs,
787 Hashtable<SimExecutionNode, Action> senode2action,
788 SimExecutionNode[] lastseNodes,
789 Hashtable<Action, Integer> action2exetime,
791 Action action = new Action(cs.getCoreNum(),
794 cp.addAction(action);
797 // remove the corresponding action on the starting SimExecutionNode
798 SimExecutionNode lastsenode = lastseNodes[cs.getCoreNum()];
799 /*if(lastsenode.getInedgeVector().size() > 0) {
800 //SimExecutionEdge inseedge = (SimExecutionEdge)lastsenode.getinedge(0);
801 //lastseNodes[cs.getCoreNum()] = (SimExecutionNode)inseedge.getSource();
803 lastseNodes[cs.getCoreNum()] = null;
805 Action tmpaction = senode2action.remove(lastsenode);
806 action2exetime.remove(tmpaction);
809 public class CheckPoint {
810 private int timepoint;
811 private Vector<Action> actions;
812 private Vector<Integer> spareCores;
814 public CheckPoint(int timepoint,
817 this.timepoint = timepoint;
818 this.actions = new Vector<Action>();
819 this.spareCores = new Vector<Integer>();
820 for(int i = 0; i < corenum; i++) {
821 this.spareCores.add(i);
825 public Vector<Action> getActions() {
829 public void addAction(Action action) {
830 this.actions.add(action);
833 public void removeSpareCore(int core) {
834 for(int i = 0 ; i < this.spareCores.size(); i++) {
835 if(this.spareCores.elementAt(i) == core) {
836 for(int j = i; j < this.spareCores.size() - 1; j++) {
837 this.spareCores.setElementAt(this.spareCores.elementAt(j + 1), j);
839 this.spareCores.remove(this.spareCores.size() - 1);
845 public int getTimepoint() {
849 public Vector<Integer> getSpareCores() {
854 public class Action {
855 public static final int ADDOBJ = 0;
856 public static final int TASKFINISH = 1;
857 public static final int TFWITHOBJ = 2;
858 public static final int TASKSTART = 3;
859 public static final int TASKABORT = 4;
860 public static final int TASKREMOVE = 5;
864 private TaskDescriptor td;
865 private Vector<Integer> taskparams;
866 private Hashtable<ClassDescriptor, Integer> nObjs;
868 private ClassDescriptor transObj;
870 public Action(int corenum,
872 this.coreNum = corenum;
875 this.taskparams = null;
876 if(this.type == TFWITHOBJ) {
877 this.nObjs = new Hashtable<ClassDescriptor, Integer>();
882 this.transObj = null;
885 public Action(int corenum,
888 assert(this.type != ADDOBJ);
890 this.coreNum = corenum;
892 this.td = ts.getTd();
893 Vector<Queue<ObjectSimulator>> paraQueues = ts.getParaQueues();
894 this.taskparams = new Vector<Integer>();
895 if((this.type != TASKABORT) && (this.type != TASKREMOVE)) {
896 for(int i = 0; i < paraQueues.size(); i++) {
897 ObjectSimulator tpara = paraQueues.elementAt(i).peek();
898 this.taskparams.add(tpara.getOid());
902 if(this.type == TFWITHOBJ) {
903 this.nObjs = new Hashtable<ClassDescriptor, Integer>();
908 this.transObj = null;
911 public Action(int corenum,
914 ClassDescriptor transObj) {
915 assert(type == ADDOBJ);
916 this.coreNum = corenum;
919 this.taskparams = null;
920 this.nObjNum = objNum;
921 this.transObj = transObj;
924 public void addNewObj(ClassDescriptor cd,
926 assert(this.type == TFWITHOBJ);
928 if(this.nObjs.containsKey(cd)) {
929 Integer sum = this.nObjs.get(cd) + num;
930 this.nObjs.put(cd, sum);
932 this.nObjs.put(cd, num);
936 public int getCoreNum() {
940 public int getType() {
944 public int getNObjNum() {
948 public ClassDescriptor getTransObj() {
952 public TaskDescriptor getTd() {
956 public Vector<Integer> getTaskParams() {
957 return this.taskparams;
960 public Hashtable<ClassDescriptor, Integer> getNObjs() {