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 long 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 long simulate(Vector<Vector<Schedule>> schedulings,
63 Vector<Integer> selectedScheduling,
64 Vector<SimExecutionNode> selectedSimExeGraphs) {
65 long processTime = Long.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);
96 Iterator it_scheduling = schedulings.iterator();
98 while(it_scheduling.hasNext()) {
99 Vector<Schedule> scheduling =
100 (Vector<Schedule>)it_scheduling.next();
101 System.out.println("Scheduling index:" + scheduling.elementAt(0).getGid());
102 this.setScheduling(scheduling);
103 Vector<SimExecutionNode> simexegraph = new Vector<SimExecutionNode>();
104 Vector<CheckPoint> checkpoints = new Vector<CheckPoint>();
105 long tmpTime = process(checkpoints, simexegraph);
106 if(tmpTime < processTime) {
107 selectedScheduling.clear();
108 selectedScheduling.add(index);
109 selectedSimExeGraphs.clear();
110 selectedSimExeGraphs.add(simexegraph.elementAt(0));
111 processTime = tmpTime;
112 } else if(tmpTime == processTime) {
113 if(!selectedScheduling.contains(index)) {
114 selectedScheduling.add(index);
115 selectedSimExeGraphs.add(simexegraph.elementAt(0));
124 it_scheduling = null;
127 System.out.print("Selected schedulings with least exectution time " + processTime + ": \n\t");
128 for(int i = 0; i < selectedScheduling.size(); i++) {
129 int gid = schedulings.elementAt(selectedScheduling.elementAt(i)).elementAt(0).getGid();
130 System.out.print(gid + ", ");
132 System.out.println();
137 public int getCoreNum() {
141 public void setCoreNum(int corenum) {
142 this.coreNum = corenum;
143 if(this.cores != null) {
146 this.cores = new Vector<CoreSimulator>(this.coreNum);
147 for(int i = 0; i < this.coreNum; i++) {
148 this.cores.add(new CoreSimulator(FIFORSchedule.getFIFORSchedule(), i));
150 if(this.scheduling != null) {
155 public int getUtility(int index) {
156 return (int)(this.cores.elementAt(index).getActiveTime() * 100) / (int)this.processTime;
159 public Vector<Schedule> getScheduling() {
163 public void setScheduling(Vector<Schedule> scheduling) {
164 this.scheduling = scheduling;
165 if(this.tasks == null) {
166 this.tasks = new Vector<TaskSimulator>();
170 if(this.cores != null) {
171 for(int i = 0; i < this.coreNum; i++) {
172 CoreSimulator core = this.cores.elementAt(i);
174 core.setRSchedule(FIFORSchedule.getFIFORSchedule());
177 this.cores = new Vector<CoreSimulator>(this.coreNum);
178 for(int i = 0; i < this.coreNum; i++) {
179 this.cores.add(new CoreSimulator(FIFORSchedule.getFIFORSchedule(), i));
186 public void applyScheduling() {
187 assert(this.state != null);
189 for(int i = 0; i < this.scheduling.size(); i++) {
190 Schedule temp = this.scheduling.elementAt(i);
191 CoreSimulator cs = this.cores.elementAt(temp.getCoreNum());
192 cs.deployTasks(temp.getTasks());
193 cs.setTargetCSimulator(temp.getTargetCoreTable());
194 cs.setAllyCSimulator(temp.getAllyCoreTable());
195 cs.setTargetFState(temp.getTargetFStateTable());
197 // inject a Startup Object to each core
198 for(int i = 0; i < this.coreNum; i++) {
199 ClassDescriptor startupobject=(ClassDescriptor)state.getClassSymbolTable().get(TypeUtil.StartupClass);
200 FlagState fsstartup = (FlagState)taskanalysis.getRootNodes(startupobject).elementAt(0);
201 ObjectSimulator newObj = new ObjectSimulator(startupobject, fsstartup);
202 this.cores.elementAt(i).addObject(newObj);
206 public Vector<TaskSimulator> getTasks() {
210 public long process(Vector<CheckPoint> checkpoints,
211 Vector<SimExecutionNode> simexegraph) {
212 assert(this.scheduling != null);
215 this.processTime = 0;
217 // helper structures for building SimExecutionGraph
218 Hashtable<SimExecutionNode, Action> senode2action =
219 new Hashtable<SimExecutionNode, Action>();
220 SimExecutionNode[] lastseNodes = new SimExecutionNode[this.cores.size()];
221 Hashtable<Action, Long> action2exetime =
222 new Hashtable<Action, Long>();
223 Hashtable<TransTaskSimulator, SimExecutionNode> tttask2senode =
224 new Hashtable<TransTaskSimulator, SimExecutionNode>();
225 Hashtable<Integer, Long> obj2transtime =
226 new Hashtable<Integer, Long>();
227 Hashtable<Integer, SimExecutionEdge> obj2lastseedge =
228 new Hashtable<Integer, SimExecutionEdge>();
230 // first decide next task to execute on each core
232 for(i = 0; i < this.cores.size(); i++) {
233 CoreSimulator cs = this.cores.elementAt(i);
234 TaskSimulator task = cs.process();
236 this.tasks.add(task);
238 lastseNodes[i] = null;
241 // add STARTTASK checkpoint for all the initial tasks
242 CheckPoint cp = new CheckPoint(this.processTime,
244 for(i = 0; i < this.tasks.size(); i++) {
245 TaskSimulator task = this.tasks.elementAt(i);
246 int coreid = task.getCs().getCoreNum();
247 Action action = new Action(coreid,
250 cp.addAction(action);
251 if(!(task instanceof TransTaskSimulator)) {
252 cp.removeSpareCore(coreid);
253 SimExecutionNode seNode = new SimExecutionNode(coreid, this.processTime);
254 seNode.setSpareCores(cp.getSpareCores());
255 senode2action.put(seNode, action);
256 action2exetime.put(action, (long)-1);
257 lastseNodes[coreid] = seNode;
263 // if no more tasks on each core, simulation finish
264 if(this.tasks.size() == 0) {
268 // for each task in todo queue, decide the execution path of this time
269 // according to statistic information
270 long finishTime = Long.MAX_VALUE;
271 Vector<TaskSimulator> finishTasks = new Vector<TaskSimulator>();
272 for(i = 0; i < this.tasks.size(); i++) {
273 TaskSimulator task = this.tasks.elementAt(i);
275 long tempTime = task.getCurrentRun().getFinishTime();
276 if(tempTime < finishTime) {
277 finishTime = tempTime;
279 finishTasks.add(task);
280 } else if (tempTime == finishTime) {
281 finishTasks.add(task);
285 // advance to next finish point
286 this.processTime += finishTime;
287 cp = new CheckPoint(this.processTime,
289 for(i = 0; i < this.tasks.size(); i++) {
290 TaskSimulator task = this.tasks.elementAt(i);
291 if(!finishTasks.contains(task)) {
292 task.getCs().updateTask(finishTime);
293 if(!(task instanceof TransTaskSimulator)) {
294 cp.removeSpareCore(task.getCs().getCoreNum());
299 Action action = null;
300 for(i = 0; i < finishTasks.size(); i++) {
301 TaskSimulator task = finishTasks.elementAt(i);
302 this.tasks.removeElement(task);
303 if(task instanceof TransTaskSimulator) {
304 // handle TransTaskSimulator task's completion
305 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
357 }// end of if(task instanceof TransTaskSimulator) else
361 } // end of while(true)
363 // add the end node into the SimExecutionGraph
364 SimExecutionNode seNode = new SimExecutionNode(this.coreNum, this.processTime);
365 simexegraph.addElement(seNode);
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 long 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 long 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 transseEdge.addPredicate(lastedge);
402 seEdge.addPredicate(transseEdge);
404 seEdge.addPredicate(lastedge);
407 // update the last edge associated to the parameter obj
408 obj2lastseedge.put(tparam, seEdge);
413 lastseNodes[j] = null;
416 senode2action.clear();
417 senode2action = null;
419 action2exetime.clear();
420 action2exetime = null;
421 tttask2senode.clear();
422 tttask2senode = null;
423 obj2transtime.clear();
424 obj2transtime = null;
425 obj2lastseedge.clear();
426 obj2lastseedge = null;
428 int gid = this.scheduling.elementAt(0).getGid();
429 if(this.state.PRINTSCHEDULESIM) {
430 SchedulingUtil.printSimulationResult(this.state.outputdir + "SimulatorResult_" + gid + ".dot",
435 System.out.println("Simulate scheduling #" + gid + ": ");
436 System.out.println("\tTotal execution time is: " + this.processTime);
437 System.out.println("\tUtility of cores: ");
438 for(int j = 0; j < this.cores.size(); j++) {
439 System.out.println("\t\tcore" + j + ": " + getUtility(j) + "%");
442 return this.processTime;
445 private void finishTransTaskSimulator(TaskSimulator task,
447 Hashtable<SimExecutionNode, Action> senode2action,
448 SimExecutionNode[] lastseNodes,
449 Hashtable<Action, Long> action2exetime,
450 Hashtable<TransTaskSimulator, SimExecutionNode> tttask2senode,
451 Hashtable<Integer, Long> obj2transtime) {
452 TransTaskSimulator tmptask = (TransTaskSimulator)task;
453 // add ADDOBJ task to targetCore
454 int targetCoreNum = tmptask.getTargetCoreNum();
455 ObjectInfo objinfo = tmptask.refreshTask();
456 ObjectSimulator nobj = objinfo.obj;
457 FlagState fs = objinfo.fs;
458 int version = objinfo.version;
459 this.cores.elementAt(targetCoreNum).addObject(nobj, fs, version);
460 Action action = new Action(targetCoreNum, Action.ADDOBJ, 1, nobj.getCd());
461 cp.addAction(action);
463 // get the obj transfer time and associated senode
464 SimExecutionNode senode = tttask2senode.get(tmptask);
465 obj2transtime.put(nobj.getOid(), this.processTime - senode.getTimepoint());
467 if(!tmptask.isFinished()) {
468 // still have some objects to be transferred
469 this.tasks.add(task);
471 if(this.cores.elementAt(targetCoreNum).getRtask() == null) {
472 TaskSimulator newTask = this.cores.elementAt(targetCoreNum).process();
473 if(newTask != null) {
474 this.tasks.add(newTask);
475 // add a TASKSTART action into this checkpoint
476 action = new Action(targetCoreNum,
479 cp.addAction(action);
480 if(!(newTask instanceof TransTaskSimulator)) {
481 cp.removeSpareCore(targetCoreNum);
482 SimExecutionNode seNode = new SimExecutionNode(targetCoreNum, this.processTime);
483 seNode.setSpareCores(cp.getSpareCores());
484 senode2action.put(seNode, action);
485 action2exetime.put(action, (long)-1);
487 SimExecutionNode lastsenode = lastseNodes[targetCoreNum];
488 // create edges between previous senode on this core to this node
489 if(lastsenode != null) {
490 Action tmpaction = senode2action.get(lastsenode);
491 SimExecutionEdge seEdge = null;
492 if(tmpaction == null) {
493 seEdge = new SimExecutionEdge(seNode,
494 lastsenode.getCoreNum(),
499 long weight = action2exetime.get(tmpaction);
500 seEdge = new SimExecutionEdge(seNode,
501 lastsenode.getCoreNum(),
504 tmpaction.getTaskParams());
506 lastsenode.addEdge(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, Long> 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 // TODO, temporarily send to at most 2 cores
580 int numtosend = allycores.size() > 2 ? 2 : allycores.size();
581 for(int k = 0; k < numtosend; ++k) {
582 Integer allyCore = allycores.elementAt(k);
583 if(allyCore == corenum) {
586 if(!transObjQueues.containsKey(allyCore)) {
587 transObjQueues.put(allyCore, new LinkedList<ObjectInfo>());
589 Queue<ObjectInfo> tmpqueue = transObjQueues.get(allyCore);
590 ObjectInfo nobjinfo = new ObjectInfo(nobj);
591 if(!tmpqueue.contains(nobjinfo)) {
592 tmpqueue.add(nobjinfo);
602 cp.addAction(action);
604 // group the new objects need to transfer
605 Vector<ObjectSimulator> transObjs = cs.finishTask();
606 if(transObjs != null) {
607 totransObjs.addAll(transObjs);
608 for(int j = 0; j < transObjs.size(); j++) {
609 ObjectSimulator tobj = transObjs.elementAt(j);
610 // send the object to target core according to pre-decide scheduling
611 Queue<Integer> cores = cs.getTargetCores(tobj.getCurrentFS());
612 tobj.setCurrentFS(cs.getTargetFState(tobj.getCurrentFS()));
614 // this obj will reside on this core
617 Integer targetCore = cores.poll();
618 if(targetCore == corenum) {
619 // this obj will reside on this core
622 if(!transObjQueues.containsKey(targetCore)) {
623 transObjQueues.put(targetCore, new LinkedList<ObjectInfo>());
625 Queue<ObjectInfo> tmpqueue = transObjQueues.get(targetCore);
626 tmpqueue.add(new ObjectInfo(tobj));
629 cores.add(targetCore);
632 // check if this object becoming shared or not
633 Vector<Integer> allycores = cs.getAllyCores(tobj.getCurrentFS());
634 if(allycores != null) {
635 tobj.setShared(true);
636 // TODO, temporarily send to at most 2 cores
637 int numtosend = allycores.size() > 2 ? 2 : allycores.size();
638 for(int k = 0; k < numtosend; ++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 Hashtable<SimExecutionNode, Action> senode2action,
683 SimExecutionNode[] lastseNodes,
684 Hashtable<Action, Long> action2exetime,
685 Hashtable<TransTaskSimulator, SimExecutionNode> tttask2senode,
686 Hashtable<Integer, Long> obj2transtime,
687 Hashtable<Integer, SimExecutionEdge> obj2lastseedge) {
688 TaskSimulator newTask = cs.process();
689 int corenum = cs.getCoreNum();
690 SimExecutionEdge seEdge = null;
691 if(newTask != null) {
692 this.tasks.add(newTask);
693 // add a TASKSTART action into this checkpoint
694 Action action = new Action(corenum,
697 cp.addAction(action);
698 if(!(newTask instanceof TransTaskSimulator)) {
699 cp.removeSpareCore(cs.getCoreNum());
700 SimExecutionNode seNode = new SimExecutionNode(corenum, this.processTime);
701 seNode.setSpareCores(cp.getSpareCores());
702 senode2action.put(seNode, action);
703 action2exetime.put(action, (long)-1);
704 SimExecutionNode lastsenode = lastseNodes[corenum];
705 // create edges between previous senode on this core to this node
706 if(lastsenode != null) {
707 Action tmpaction = senode2action.get(lastsenode);
708 long weight = tmpaction != null? action2exetime.get(tmpaction):0;
709 seEdge = new SimExecutionEdge(seNode,
710 lastsenode.getCoreNum(),
711 tmpaction!= null?tmpaction.getTd():null,
713 tmpaction!=null?tmpaction.getTaskParams():null);
714 lastsenode.addEdge(seEdge);
716 lastseNodes[corenum] = seNode;
717 for(int tmpindex = 0; tmpindex < tttasks.size(); tmpindex++) {
718 tttask2senode.put(tttasks.elementAt(tmpindex), seNode);
721 } else if(tttasks.size() > 0) {
722 SimExecutionNode seNode = new SimExecutionNode(corenum, this.processTime);
723 //seNode.setSpareCores(cp.getSpareCores());
724 // no action associated here
725 SimExecutionNode lastsenode = lastseNodes[corenum];
726 // create edges between previous senode on this core to this node
727 if(lastsenode != null) {
728 Action tmpaction = senode2action.get(lastsenode);
729 long weight = action2exetime.get(tmpaction);
730 seEdge = new SimExecutionEdge(seNode,
731 lastsenode.getCoreNum(),
734 tmpaction.getTaskParams());
735 lastsenode.addEdge(seEdge);
737 lastseNodes[corenum] = seNode;
738 for(int tmpindex = 0; tmpindex < tttasks.size(); tmpindex++) {
739 tttask2senode.put(tttasks.elementAt(tmpindex), seNode);
743 // setup data dependencies for the task
744 Vector<Integer> taskparams = seEdge.getTaskparams();
745 if(taskparams != null) {
746 for(int i = 0; i < taskparams.size(); i++) {
747 Integer tparam = taskparams.elementAt(i);
748 SimExecutionEdge lastedge = obj2lastseedge.get(tparam);
749 if(lastedge != null) {
750 if(lastedge.getCoreNum() != seEdge.getCoreNum()) {
751 // the obj is transferred from another core
752 // create an seEdge for this transfer
753 long weight = obj2transtime.get(tparam);
754 SimExecutionEdge transseEdge = new SimExecutionEdge((SimExecutionNode)seEdge.getSource(),
755 lastedge.getCoreNum(),
756 null, // TODO: not sure if this is enough
759 if(((SimExecutionNode)seEdge.getSource()).getTimepoint() <
760 ((SimExecutionNode)lastedge.getTarget()).getTimepoint()) {
761 System.err.println("ScheduleSimulator:757");
764 lastedge.getTarget().addEdge(transseEdge);
765 transseEdge.addPredicate(lastedge);
766 seEdge.addPredicate(transseEdge);
768 seEdge.addPredicate(lastedge);
771 // update the last edge associated to the parameter obj
772 obj2lastseedge.put(tparam, seEdge);
777 // set seEdge as the last execution edge for all newly created objs
779 for(int i = 0; i < nobjs.size(); i++) {
780 ObjectSimulator nobj = nobjs.elementAt(i);
781 obj2lastseedge.put(nobj.getOid(), seEdge);
787 private void finishTaskAbnormal(CoreSimulator cs,
789 Hashtable<SimExecutionNode, Action> senode2action,
790 SimExecutionNode[] lastseNodes,
791 Hashtable<Action, Long> action2exetime,
793 Action action = new Action(cs.getCoreNum(),
796 cp.addAction(action);
799 // remove the corresponding action on the starting SimExecutionNode
800 SimExecutionNode lastsenode = lastseNodes[cs.getCoreNum()];
801 /*if(lastsenode.getInedgeVector().size() > 0) {
802 //SimExecutionEdge inseedge = (SimExecutionEdge)lastsenode.getinedge(0);
803 //lastseNodes[cs.getCoreNum()] = (SimExecutionNode)inseedge.getSource();
805 lastseNodes[cs.getCoreNum()] = null;
807 Action tmpaction = senode2action.remove(lastsenode);
808 action2exetime.remove(tmpaction);
811 public class CheckPoint {
812 private long timepoint;
813 private Vector<Action> actions;
814 private Vector<Integer> spareCores;
816 public CheckPoint(long timepoint,
819 this.timepoint = timepoint;
820 this.actions = new Vector<Action>();
821 this.spareCores = new Vector<Integer>();
822 for(int i = 0; i < corenum; i++) {
823 this.spareCores.add(i);
827 public Vector<Action> getActions() {
831 public void addAction(Action action) {
832 this.actions.add(action);
835 public void removeSpareCore(int core) {
836 for(int i = 0 ; i < this.spareCores.size(); i++) {
837 if(this.spareCores.elementAt(i) == core) {
838 for(int j = i; j < this.spareCores.size() - 1; j++) {
839 this.spareCores.setElementAt(this.spareCores.elementAt(j + 1), j);
841 this.spareCores.remove(this.spareCores.size() - 1);
847 public long getTimepoint() {
851 public Vector<Integer> getSpareCores() {
856 public class Action {
857 public static final int ADDOBJ = 0;
858 public static final int TASKFINISH = 1;
859 public static final int TFWITHOBJ = 2;
860 public static final int TASKSTART = 3;
861 public static final int TASKABORT = 4;
862 public static final int TASKREMOVE = 5;
866 private TaskDescriptor td;
867 private Vector<Integer> taskparams;
868 private Hashtable<ClassDescriptor, Integer> nObjs;
870 private ClassDescriptor transObj;
872 public Action(int corenum,
874 this.coreNum = corenum;
877 this.taskparams = null;
878 if(this.type == TFWITHOBJ) {
879 this.nObjs = new Hashtable<ClassDescriptor, Integer>();
884 this.transObj = null;
887 public Action(int corenum,
890 assert(this.type != ADDOBJ);
892 this.coreNum = corenum;
894 this.td = ts.getTd();
895 Vector<Queue<ObjectSimulator>> paraQueues = ts.getParaQueues();
896 if(this.type == TASKSTART) {
897 this.taskparams = new Vector<Integer>();
898 for(int i = 0; i < paraQueues.size(); i++) {
899 ObjectSimulator tpara = paraQueues.elementAt(i).peek();
900 this.taskparams.add(tpara.getOid());
903 this.taskparams = null;
906 if(this.type == TFWITHOBJ) {
907 this.nObjs = new Hashtable<ClassDescriptor, Integer>();
912 this.transObj = null;
915 public Action(int corenum,
918 ClassDescriptor transObj) {
919 assert(type == ADDOBJ);
920 this.coreNum = corenum;
923 this.taskparams = null;
924 this.nObjNum = objNum;
925 this.transObj = transObj;
928 public void addNewObj(ClassDescriptor cd,
930 assert(this.type == TFWITHOBJ);
932 if(this.nObjs.containsKey(cd)) {
933 Integer sum = this.nObjs.get(cd) + num;
934 this.nObjs.put(cd, sum);
936 this.nObjs.put(cd, num);
940 public int getCoreNum() {
944 public int getType() {
948 public int getNObjNum() {
952 public ClassDescriptor getTransObj() {
956 public TaskDescriptor getTd() {
960 public Vector<Integer> getTaskParams() {
961 return this.taskparams;
964 public Hashtable<ClassDescriptor, Integer> getNObjs() {