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 = 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 if(!selectedScheduling.contains(index)) {
113 selectedScheduling.add(index);
114 selectedSimExeGraphs.add(simexegraph);
123 it_scheduling = null;
126 System.out.print("Selected schedulings with least exectution time " + processTime + ": \n\t");
127 for(int i = 0; i < selectedScheduling.size(); i++) {
128 int gid = schedulings.elementAt(selectedScheduling.elementAt(i)).elementAt(0).getGid();
129 System.out.print(gid + ", ");
131 System.out.println();
136 public int getCoreNum() {
140 public void setCoreNum(int corenum) {
141 this.coreNum = corenum;
142 if(this.cores != null) {
145 this.cores = new Vector<CoreSimulator>(this.coreNum);
146 for(int i = 0; i < this.coreNum; i++) {
147 this.cores.add(new CoreSimulator(FIFORSchedule.getFIFORSchedule(), i));
149 if(this.scheduling != null) {
154 public int getUtility(int index) {
155 return (this.cores.elementAt(index).getActiveTime() * 100) / this.processTime;
158 public Vector<Schedule> getScheduling() {
162 public void setScheduling(Vector<Schedule> scheduling) {
163 this.scheduling = scheduling;
164 if(this.tasks == null) {
165 this.tasks = new Vector<TaskSimulator>();
169 if(this.cores != null) {
170 for(int i = 0; i < this.coreNum; i++) {
171 CoreSimulator core = this.cores.elementAt(i);
173 core.setRSchedule(FIFORSchedule.getFIFORSchedule());
176 this.cores = new Vector<CoreSimulator>(this.coreNum);
177 for(int i = 0; i < this.coreNum; i++) {
178 this.cores.add(new CoreSimulator(FIFORSchedule.getFIFORSchedule(), i));
185 public void applyScheduling() {
186 assert(this.state != null);
188 for(int i = 0; i < this.scheduling.size(); i++) {
189 Schedule temp = this.scheduling.elementAt(i);
190 CoreSimulator cs = this.cores.elementAt(temp.getCoreNum());
191 cs.deployTasks(temp.getTasks());
192 cs.setTargetCSimulator(temp.getTargetCoreTable());
193 cs.setAllyCSimulator(temp.getAllyCoreTable());
194 cs.setTargetFState(temp.getTargetFStateTable());
196 // inject a Startup Object to each core
197 for(int i = 0; i < this.coreNum; i++) {
198 ClassDescriptor startupobject=(ClassDescriptor)state.getClassSymbolTable().get(TypeUtil.StartupClass);
199 FlagState fsstartup = (FlagState)taskanalysis.getRootNodes(startupobject).elementAt(0);
200 ObjectSimulator newObj = new ObjectSimulator(startupobject, fsstartup);
201 this.cores.elementAt(i).addObject(newObj);
205 public Vector<TaskSimulator> getTasks() {
209 public int process(Vector<CheckPoint> checkpoints,
210 Vector<SimExecutionEdge> simexegraph) {
211 assert(this.scheduling != null);
214 this.processTime = 0;
216 // helper structures for building SimExecutionGraph
217 Hashtable<SimExecutionNode, Action> senode2action =
218 new Hashtable<SimExecutionNode, Action>();
219 SimExecutionNode[] lastseNodes = new SimExecutionNode[this.cores.size()];
220 Hashtable<Action, Integer> action2exetime =
221 new Hashtable<Action, Integer>();
222 Hashtable<TransTaskSimulator, SimExecutionNode> tttask2senode =
223 new Hashtable<TransTaskSimulator, SimExecutionNode>();
224 Hashtable<Integer, Integer> obj2transtime =
225 new Hashtable<Integer, Integer>();
226 Hashtable<Integer, SimExecutionEdge> obj2lastseedge =
227 new Hashtable<Integer, SimExecutionEdge>();
229 // first decide next task to execute on each core
231 for(i = 0; i < this.cores.size(); i++) {
232 CoreSimulator cs = this.cores.elementAt(i);
233 TaskSimulator task = cs.process();
235 this.tasks.add(task);
237 lastseNodes[i] = null;
240 // add STARTTASK checkpoint for all the initial tasks
241 CheckPoint cp = new CheckPoint(this.processTime,
243 for(i = 0; i < this.tasks.size(); i++) {
244 TaskSimulator task = this.tasks.elementAt(i);
245 int coreid = task.getCs().getCoreNum();
246 Action action = new Action(coreid,
249 cp.addAction(action);
250 if(!(task instanceof TransTaskSimulator)) {
251 cp.removeSpareCore(coreid);
252 SimExecutionNode seNode = new SimExecutionNode(coreid, this.processTime);
253 seNode.setSpareCores(cp.getSpareCores());
254 senode2action.put(seNode, action);
255 action2exetime.put(action, -1);
256 lastseNodes[coreid] = seNode;
262 // if no more tasks on each core, simulation finish
263 if(this.tasks.size() == 0) {
267 // for each task in todo queue, decide the execution path of this time
268 // according to statistic information
269 int finishTime = Integer.MAX_VALUE;
270 Vector<TaskSimulator> finishTasks = new Vector<TaskSimulator>();
271 for(i = 0; i < this.tasks.size(); i++) {
272 TaskSimulator task = this.tasks.elementAt(i);
274 int tempTime = task.getCurrentRun().getFinishTime();
275 if(tempTime < finishTime) {
276 finishTime = tempTime;
278 finishTasks.add(task);
279 } else if (tempTime == finishTime) {
280 finishTasks.add(task);
284 // advance to next finish point
285 this.processTime += finishTime;
286 cp = new CheckPoint(this.processTime,
288 for(i = 0; i < this.tasks.size(); i++) {
289 TaskSimulator task = this.tasks.elementAt(i);
290 if(!finishTasks.contains(task)) {
291 task.getCs().updateTask(finishTime);
292 if(!(task instanceof TransTaskSimulator)) {
293 cp.removeSpareCore(task.getCs().getCoreNum());
298 Action action = null;
299 for(i = 0; i < finishTasks.size(); i++) {
300 TaskSimulator task = finishTasks.elementAt(i);
301 this.tasks.removeElement(task);
302 if(task instanceof TransTaskSimulator) {
303 // handle TransTaskSimulator task's completion
304 finishTransTaskSimulator(task,
313 CoreSimulator cs = task.getCs();
314 Vector<TransTaskSimulator> tttasks = new Vector<TransTaskSimulator>();
316 Vector<ObjectSimulator> transObjs = null;
317 if(task.getCurrentRun().getExetype() == 0) {
318 // normal execution of a task
319 transObjs = finishTaskNormal(task,
325 } else if (task.getCurrentRun().getExetype() == 1) {
327 finishTaskAbnormal(cs,
333 } else if (task.getCurrentRun().getExetype() == 2) {
335 finishTaskAbnormal(cs,
343 // Choose a new task for this core
358 }// end of if(task instanceof TransTaskSimulator) else
362 } // end of while(true)
364 // add the end node into the SimExecutionGraph
365 SimExecutionNode seNode = new SimExecutionNode(this.coreNum, this.processTime);
366 for(int j = 0; j < lastseNodes.length; j++) {
367 SimExecutionNode lastsenode = lastseNodes[j];
368 // create edges between previous senode on this core to this node
369 if(lastsenode != null) {
370 Action tmpaction = senode2action.get(lastsenode);
371 int weight = tmpaction != null? action2exetime.get(tmpaction) : 0; // TODO ????
372 SimExecutionEdge seEdge = new SimExecutionEdge(seNode,
373 lastsenode.getCoreNum(),
374 tmpaction != null? tmpaction.getTd():null,
376 tmpaction != null? tmpaction.getTaskParams():null);
377 lastsenode.addEdge(seEdge);
379 // setup data dependencies for the task
380 Vector<Integer> taskparams = seEdge.getTaskparams();
381 if(taskparams != null) {
382 for(int k = 0; k < taskparams.size(); k++) {
383 Integer tparam = taskparams.elementAt(k);
384 SimExecutionEdge lastedge = obj2lastseedge.get(tparam);
385 if(lastedge != null) {
386 if(lastedge.getCoreNum() != seEdge.getCoreNum()) {
387 // the obj is transferred from another core
388 // create an seEdge for this transfer
389 int transweight = obj2transtime.get(tparam);
390 SimExecutionEdge transseEdge = new SimExecutionEdge((SimExecutionNode)seEdge.getSource(),
391 lastedge.getCoreNum(),
392 null, // TODO: not sure if this is enough
395 if(((SimExecutionNode)seEdge.getSource()).getTimepoint() <
396 ((SimExecutionNode)lastedge.getTarget()).getTimepoint()) {
397 System.err.println("ScheduleSimulator:393");
400 lastedge.getTarget().addEdge(transseEdge);
401 simexegraph.add(transseEdge);
402 transseEdge.addPredicate(lastedge);
403 seEdge.addPredicate(transseEdge);
405 seEdge.addPredicate(lastedge);
408 // update the last edge associated to the parameter obj
409 obj2lastseedge.put(tparam, seEdge);
413 simexegraph.add(seEdge); // add the seEdge afger all corresponding transfer edges
415 lastseNodes[j] = null;
418 senode2action.clear();
419 senode2action = null;
421 action2exetime.clear();
422 action2exetime = null;
423 tttask2senode.clear();
424 tttask2senode = null;
425 obj2transtime.clear();
426 obj2transtime = null;
427 obj2lastseedge.clear();
428 obj2lastseedge = null;
430 int gid = this.scheduling.elementAt(0).getGid();
431 if(this.state.PRINTSCHEDULESIM) {
432 SchedulingUtil.printSimulationResult(this.state.outputdir + "SimulatorResult_" + gid + ".dot",
437 System.out.println("Simulate scheduling #" + gid + ": ");
438 System.out.println("\tTotal execution time is: " + this.processTime);
439 System.out.println("\tUtility of cores: ");
440 for(int j = 0; j < this.cores.size(); j++) {
441 System.out.println("\t\tcore" + j + ": " + getUtility(j) + "%");
444 return this.processTime;
447 private void finishTransTaskSimulator(TaskSimulator task,
449 Vector<SimExecutionEdge> simexegraph,
450 Hashtable<SimExecutionNode, Action> senode2action,
451 SimExecutionNode[] lastseNodes,
452 Hashtable<Action, Integer> action2exetime,
453 Hashtable<TransTaskSimulator, SimExecutionNode> tttask2senode,
454 Hashtable<Integer, Integer> obj2transtime) {
455 TransTaskSimulator tmptask = (TransTaskSimulator)task;
456 // add ADDOBJ task to targetCore
457 int targetCoreNum = tmptask.getTargetCoreNum();
458 ObjectInfo objinfo = tmptask.refreshTask();
459 ObjectSimulator nobj = objinfo.obj;
460 FlagState fs = objinfo.fs;
461 int version = objinfo.version;
462 this.cores.elementAt(targetCoreNum).addObject(nobj, fs, version);
463 Action action = new Action(targetCoreNum, Action.ADDOBJ, 1, nobj.getCd());
464 cp.addAction(action);
466 // get the obj transfer time and associated senode
467 SimExecutionNode senode = tttask2senode.get(tmptask);
468 obj2transtime.put(nobj.getOid(), this.processTime - senode.getTimepoint());
470 if(!tmptask.isFinished()) {
471 // still have some objects to be transferred
472 this.tasks.add(task);
474 if(this.cores.elementAt(targetCoreNum).getRtask() == null) {
475 TaskSimulator newTask = this.cores.elementAt(targetCoreNum).process();
476 if(newTask != null) {
477 this.tasks.add(newTask);
478 // add a TASKSTART action into this checkpoint
479 action = new Action(targetCoreNum,
482 cp.addAction(action);
483 if(!(newTask instanceof TransTaskSimulator)) {
484 cp.removeSpareCore(targetCoreNum);
485 SimExecutionNode seNode = new SimExecutionNode(targetCoreNum, this.processTime);
486 seNode.setSpareCores(cp.getSpareCores());
487 senode2action.put(seNode, action);
488 action2exetime.put(action, -1);
490 SimExecutionNode lastsenode = lastseNodes[targetCoreNum];
491 // create edges between previous senode on this core to this node
492 if(lastsenode != null) {
493 Action tmpaction = senode2action.get(lastsenode);
494 SimExecutionEdge seEdge = null;
495 if(tmpaction == null) {
496 seEdge = new SimExecutionEdge(seNode,
497 lastsenode.getCoreNum(),
502 int weight = action2exetime.get(tmpaction);
503 seEdge = new SimExecutionEdge(seNode,
504 lastsenode.getCoreNum(),
507 tmpaction.getTaskParams());
509 lastsenode.addEdge(seEdge);
510 simexegraph.add(seEdge);
512 lastseNodes[targetCoreNum] = seNode;
518 private Vector<ObjectSimulator> finishTaskNormal(TaskSimulator task,
520 Vector<TransTaskSimulator> tttasks,
521 Hashtable<SimExecutionNode, Action> senode2action,
522 SimExecutionNode[] lastseNodes,
523 Hashtable<Action, Integer> action2exetime) {
524 Vector<ObjectSimulator> totransObjs = new Vector<ObjectSimulator>();
525 CoreSimulator cs = task.getCs();
526 int corenum = cs.getCoreNum();
527 Hashtable<Integer, Queue<ObjectInfo>> transObjQueues =
528 new Hashtable<Integer, Queue<ObjectInfo>>();
529 Action action = null;
530 if(task.getCurrentRun().getNewObjs() == null) {
531 // task finish without new objects
532 action = new Action(corenum,
535 // get the execution time of this task
536 SimExecutionNode lastsenode = lastseNodes[corenum];
537 Action startaction = senode2action.get(lastsenode);
538 action2exetime.put(startaction, cp.getTimepoint() - lastsenode.getTimepoint());
541 // task finish with new objects
542 action = new Action(corenum,
545 // get the execution time of this task
546 SimExecutionNode lastsenode = lastseNodes[corenum];
547 Action startaction = senode2action.get(lastsenode);
548 action2exetime.put(startaction, cp.getTimepoint() - lastsenode.getTimepoint());
550 // get the infomation of how to send new objects
551 Vector<ObjectSimulator> nobjs = task.getCurrentRun().getNewObjs();
552 for(int j = 0; j < nobjs.size(); j++) {
553 ObjectSimulator nobj = nobjs.elementAt(j);
554 totransObjs.add(nobj);
556 action.addNewObj(nobj.getCd(), Integer.valueOf(1));
557 // send the new object to target core according to pre-decide scheduling
558 Queue<Integer> cores = cs.getTargetCores(nobj.getCurrentFS());
560 // this obj will reside on this core
563 Integer targetCore = cores.poll();
564 if(targetCore == corenum) {
565 // this obj will reside on this core
568 if(!transObjQueues.containsKey(targetCore)) {
569 transObjQueues.put(targetCore, new LinkedList<ObjectInfo>());
571 Queue<ObjectInfo> tmpqueue = transObjQueues.get(targetCore);
572 tmpqueue.add(new ObjectInfo(nobj));
575 // enqueue this core again
576 cores.add(targetCore);
579 // check if this object becoming shared or not
580 Vector<Integer> allycores = cs.getAllyCores(nobj.getCurrentFS());
581 if(allycores != null) {
582 nobj.setShared(true);
583 for(int k = 0; k < allycores.size(); ++k) {
584 Integer allyCore = allycores.elementAt(k);
585 if(allyCore == corenum) {
588 if(!transObjQueues.containsKey(allyCore)) {
589 transObjQueues.put(allyCore, new LinkedList<ObjectInfo>());
591 Queue<ObjectInfo> tmpqueue = transObjQueues.get(allyCore);
592 ObjectInfo nobjinfo = new ObjectInfo(nobj);
593 if(!tmpqueue.contains(nobjinfo)) {
594 tmpqueue.add(nobjinfo);
604 cp.addAction(action);
606 // group the new objects need to transfer
607 Vector<ObjectSimulator> transObjs = cs.finishTask();
608 if(transObjs != null) {
609 totransObjs.addAll(transObjs);
610 for(int j = 0; j < transObjs.size(); j++) {
611 ObjectSimulator tobj = transObjs.elementAt(j);
612 // send the object to target core according to pre-decide scheduling
613 Queue<Integer> cores = cs.getTargetCores(tobj.getCurrentFS());
614 tobj.setCurrentFS(cs.getTargetFState(tobj.getCurrentFS()));
616 // this obj will reside on this core
619 Integer targetCore = cores.poll();
620 if(targetCore == corenum) {
621 // this obj will reside on this core
624 if(!transObjQueues.containsKey(targetCore)) {
625 transObjQueues.put(targetCore, new LinkedList<ObjectInfo>());
627 Queue<ObjectInfo> tmpqueue = transObjQueues.get(targetCore);
628 tmpqueue.add(new ObjectInfo(tobj));
631 cores.add(targetCore);
634 // check if this object becoming shared or not
635 Vector<Integer> allycores = cs.getAllyCores(tobj.getCurrentFS());
636 if(allycores != null) {
637 tobj.setShared(true);
638 for(int k = 0; k < allycores.size(); ++k) {
639 Integer allyCore = allycores.elementAt(k);
640 if(allyCore == corenum) {
643 if(!transObjQueues.containsKey(allyCore)) {
644 transObjQueues.put(allyCore, new LinkedList<ObjectInfo>());
646 Queue<ObjectInfo> tmpqueue = transObjQueues.get(allyCore);
647 ObjectInfo nobjinfo = new ObjectInfo(tobj);
648 if(!tmpqueue.contains(nobjinfo)) {
649 tmpqueue.add(nobjinfo);
660 // add 'transport' tasks
661 Iterator it_entries = transObjQueues.entrySet().iterator();
662 while(it_entries.hasNext()) {
663 Entry<Integer, Queue<ObjectInfo>> tmpentry = (Entry<Integer, Queue<ObjectInfo>>)it_entries.next();
664 Integer tmpCoreNum = tmpentry.getKey();
665 Queue<ObjectInfo> nobjs = tmpentry.getValue();
666 TransTaskSimulator tmptask = new TransTaskSimulator(cs, tmpCoreNum, nobjs);
667 this.tasks.add(tmptask);
668 tttasks.add(tmptask);
673 transObjQueues = null;
678 private void generateNewTask(CoreSimulator cs,
680 Vector<ObjectSimulator> nobjs,
681 Vector<TransTaskSimulator> tttasks,
682 Vector<SimExecutionEdge> simexegraph,
683 Hashtable<SimExecutionNode, Action> senode2action,
684 SimExecutionNode[] lastseNodes,
685 Hashtable<Action, Integer> action2exetime,
686 Hashtable<TransTaskSimulator, SimExecutionNode> tttask2senode,
687 Hashtable<Integer, Integer> obj2transtime,
688 Hashtable<Integer, SimExecutionEdge> obj2lastseedge) {
689 TaskSimulator newTask = cs.process();
690 int corenum = cs.getCoreNum();
691 SimExecutionEdge seEdge = null;
692 if(newTask != null) {
693 this.tasks.add(newTask);
694 // add a TASKSTART action into this checkpoint
695 Action action = new Action(corenum,
698 cp.addAction(action);
699 if(!(newTask instanceof TransTaskSimulator)) {
700 cp.removeSpareCore(cs.getCoreNum());
701 SimExecutionNode seNode = new SimExecutionNode(corenum, this.processTime);
702 seNode.setSpareCores(cp.getSpareCores());
703 senode2action.put(seNode, action);
704 action2exetime.put(action, -1);
705 SimExecutionNode lastsenode = lastseNodes[corenum];
706 // create edges between previous senode on this core to this node
707 if(lastsenode != null) {
708 Action tmpaction = senode2action.get(lastsenode);
709 int weight = tmpaction != null? action2exetime.get(tmpaction):0;
710 seEdge = new SimExecutionEdge(seNode,
711 lastsenode.getCoreNum(),
712 tmpaction!= null?tmpaction.getTd():null,
714 tmpaction!=null?tmpaction.getTaskParams():null);
715 lastsenode.addEdge(seEdge);
717 lastseNodes[corenum] = seNode;
718 for(int tmpindex = 0; tmpindex < tttasks.size(); tmpindex++) {
719 tttask2senode.put(tttasks.elementAt(tmpindex), seNode);
722 } else if(tttasks.size() > 0) {
723 SimExecutionNode seNode = new SimExecutionNode(corenum, this.processTime);
724 //seNode.setSpareCores(cp.getSpareCores());
725 // no action associated here
726 SimExecutionNode lastsenode = lastseNodes[corenum];
727 // create edges between previous senode on this core to this node
728 if(lastsenode != null) {
729 Action tmpaction = senode2action.get(lastsenode);
730 int weight = action2exetime.get(tmpaction);
731 seEdge = new SimExecutionEdge(seNode,
732 lastsenode.getCoreNum(),
735 tmpaction.getTaskParams());
736 lastsenode.addEdge(seEdge);
738 lastseNodes[corenum] = seNode;
739 for(int tmpindex = 0; tmpindex < tttasks.size(); tmpindex++) {
740 tttask2senode.put(tttasks.elementAt(tmpindex), seNode);
744 // setup data dependencies for the task
745 Vector<Integer> taskparams = seEdge.getTaskparams();
746 if(taskparams != null) {
747 for(int i = 0; i < taskparams.size(); i++) {
748 Integer tparam = taskparams.elementAt(i);
749 SimExecutionEdge lastedge = obj2lastseedge.get(tparam);
750 if(lastedge != null) {
751 if(lastedge.getCoreNum() != seEdge.getCoreNum()) {
752 // the obj is transferred from another core
753 // create an seEdge for this transfer
754 int weight = obj2transtime.get(tparam);
755 SimExecutionEdge transseEdge = new SimExecutionEdge((SimExecutionNode)seEdge.getSource(),
756 lastedge.getCoreNum(),
757 null, // TODO: not sure if this is enough
760 if(((SimExecutionNode)seEdge.getSource()).getTimepoint() <
761 ((SimExecutionNode)lastedge.getTarget()).getTimepoint()) {
762 System.err.println("ScheduleSimulator:757");
765 lastedge.getTarget().addEdge(transseEdge);
766 simexegraph.add(transseEdge);
767 transseEdge.addPredicate(lastedge);
768 seEdge.addPredicate(transseEdge);
770 seEdge.addPredicate(lastedge);
773 // update the last edge associated to the parameter obj
774 obj2lastseedge.put(tparam, seEdge);
778 simexegraph.add(seEdge); // add the seEdge afger all corresponding transfer edges
780 // set seEdge as the last execution edge for all newly created objs
782 for(int i = 0; i < nobjs.size(); i++) {
783 ObjectSimulator nobj = nobjs.elementAt(i);
784 obj2lastseedge.put(nobj.getOid(), seEdge);
790 private void finishTaskAbnormal(CoreSimulator cs,
792 Hashtable<SimExecutionNode, Action> senode2action,
793 SimExecutionNode[] lastseNodes,
794 Hashtable<Action, Integer> action2exetime,
796 Action action = new Action(cs.getCoreNum(),
799 cp.addAction(action);
802 // remove the corresponding action on the starting SimExecutionNode
803 SimExecutionNode lastsenode = lastseNodes[cs.getCoreNum()];
804 /*if(lastsenode.getInedgeVector().size() > 0) {
805 //SimExecutionEdge inseedge = (SimExecutionEdge)lastsenode.getinedge(0);
806 //lastseNodes[cs.getCoreNum()] = (SimExecutionNode)inseedge.getSource();
808 lastseNodes[cs.getCoreNum()] = null;
810 Action tmpaction = senode2action.remove(lastsenode);
811 action2exetime.remove(tmpaction);
814 public class CheckPoint {
815 private int timepoint;
816 private Vector<Action> actions;
817 private Vector<Integer> spareCores;
819 public CheckPoint(int timepoint,
822 this.timepoint = timepoint;
823 this.actions = new Vector<Action>();
824 this.spareCores = new Vector<Integer>();
825 for(int i = 0; i < corenum; i++) {
826 this.spareCores.add(i);
830 public Vector<Action> getActions() {
834 public void addAction(Action action) {
835 this.actions.add(action);
838 public void removeSpareCore(int core) {
839 for(int i = 0 ; i < this.spareCores.size(); i++) {
840 if(this.spareCores.elementAt(i) == core) {
841 for(int j = i; j < this.spareCores.size() - 1; j++) {
842 this.spareCores.setElementAt(this.spareCores.elementAt(j + 1), j);
844 this.spareCores.remove(this.spareCores.size() - 1);
850 public int getTimepoint() {
854 public Vector<Integer> getSpareCores() {
859 public class Action {
860 public static final int ADDOBJ = 0;
861 public static final int TASKFINISH = 1;
862 public static final int TFWITHOBJ = 2;
863 public static final int TASKSTART = 3;
864 public static final int TASKABORT = 4;
865 public static final int TASKREMOVE = 5;
869 private TaskDescriptor td;
870 private Vector<Integer> taskparams;
871 private Hashtable<ClassDescriptor, Integer> nObjs;
873 private ClassDescriptor transObj;
875 public Action(int corenum,
877 this.coreNum = corenum;
880 this.taskparams = null;
881 if(this.type == TFWITHOBJ) {
882 this.nObjs = new Hashtable<ClassDescriptor, Integer>();
887 this.transObj = null;
890 public Action(int corenum,
893 assert(this.type != ADDOBJ);
895 this.coreNum = corenum;
897 this.td = ts.getTd();
898 Vector<Queue<ObjectSimulator>> paraQueues = ts.getParaQueues();
899 if(this.type == TASKSTART) {
900 this.taskparams = new Vector<Integer>();
901 for(int i = 0; i < paraQueues.size(); i++) {
902 ObjectSimulator tpara = paraQueues.elementAt(i).peek();
903 this.taskparams.add(tpara.getOid());
906 this.taskparams = null;
909 if(this.type == TFWITHOBJ) {
910 this.nObjs = new Hashtable<ClassDescriptor, Integer>();
915 this.transObj = null;
918 public Action(int corenum,
921 ClassDescriptor transObj) {
922 assert(type == ADDOBJ);
923 this.coreNum = corenum;
926 this.taskparams = null;
927 this.nObjNum = objNum;
928 this.transObj = transObj;
931 public void addNewObj(ClassDescriptor cd,
933 assert(this.type == TFWITHOBJ);
935 if(this.nObjs.containsKey(cd)) {
936 Integer sum = this.nObjs.get(cd) + num;
937 this.nObjs.put(cd, sum);
939 this.nObjs.put(cd, num);
943 public int getCoreNum() {
947 public int getType() {
951 public int getNObjNum() {
955 public ClassDescriptor getTransObj() {
959 public TaskDescriptor getTd() {
963 public Vector<Integer> getTaskParams() {
964 return this.taskparams;
967 public Hashtable<ClassDescriptor, Integer> getNObjs() {