fix bugs in memory model in multi-core version
[IRC.git] / Robust / src / Analysis / Scheduling / Schedule.java
1 package Analysis.Scheduling;
2
3 import java.util.Hashtable;
4 import java.util.LinkedList;
5 import java.util.Queue;
6 import java.util.Vector;
7
8 import Analysis.TaskStateAnalysis.FlagState;
9 import IR.TaskDescriptor;
10
11 /** This class holds flag transition diagram(s) can be put on one core.
12  */
13 public class Schedule {
14   private int gid;
15   private int coreNum;
16   private Vector<TaskDescriptor> tasks;
17   private Hashtable<TaskDescriptor, Integer> td2num;
18   private Hashtable<FlagState, Queue<Integer>> targetCores;
19   private Hashtable<FlagState, FlagState> targetFState;   // only affected by transimit edges
20   private Hashtable<FlagState, Vector<Integer>> allyCores;
21   private Hashtable<TaskDescriptor, Vector<FlagState>> td2fs;
22
23   public Schedule(int coreNum,
24                   int gid) {
25     this.gid = gid;
26     this.coreNum = coreNum;
27     this.tasks = null;
28     this.td2num = null;
29     this.targetCores = null;
30     this.targetFState = null;
31     this.allyCores = null;
32     this.td2fs = null;
33   }
34
35   public int getGid() {
36       return gid;
37   }
38
39   public int getCoreNum() {
40     return coreNum;
41   }
42
43   public Hashtable<FlagState, Queue<Integer>> getTargetCoreTable() {
44     return targetCores;
45   }
46
47   public Queue<Integer> getTargetCores(FlagState fstate) {
48     if(targetCores == null) {
49       return null;
50     }
51     return targetCores.get(fstate);
52   }
53
54   public Hashtable<FlagState, FlagState> getTargetFStateTable() {
55     return targetFState;
56   }
57
58   public FlagState getTargetFState(FlagState fstate) {
59     if(targetFState == null) {
60       return null;
61     }
62     return targetFState.get(fstate);
63   }
64
65   public Hashtable<FlagState, Vector<Integer>> getAllyCoreTable() {
66     return this.allyCores;
67   }
68
69   public Vector<Integer> getAllyCores(FlagState fstate) {
70     if(this.allyCores == null) {
71       return null;
72     }
73     return this.allyCores.get(fstate);
74   }
75
76   public Hashtable<TaskDescriptor, Vector<FlagState>> getTd2FsTable() {
77     return this.td2fs;
78   }
79
80   public Vector<FlagState> getFStates4TD(TaskDescriptor td) {
81     if(this.td2fs == null) {
82       return null;
83     }
84     return this.td2fs.get(td);
85   }
86
87   public void addTargetCore(FlagState fstate, 
88                             Integer targetCore) {
89     if(this.targetCores == null) {
90       this.targetCores = new Hashtable<FlagState, Queue<Integer>>();
91     }
92     if(!this.targetCores.containsKey(fstate)) {
93       this.targetCores.put(fstate, new LinkedList<Integer>());
94     }
95     this.targetCores.get(fstate).add(targetCore);     // there may have some duplicate items,
96                                                       // which reflects probabilities.
97   }
98
99   public void addTargetCore(FlagState fstate, 
100                             Integer targetCore, 
101                             FlagState tfstate) {
102     if(this.targetCores == null) {
103       this.targetCores = new Hashtable<FlagState, Queue<Integer>>();
104     }
105     if(!this.targetCores.containsKey(fstate)) {
106       this.targetCores.put(fstate, new LinkedList<Integer>());
107     }
108     this.targetCores.get(fstate).add(targetCore);
109     if(this.targetFState == null) {
110       this.targetFState = new Hashtable<FlagState, FlagState>();
111     }
112     this.targetFState.put(fstate, tfstate);
113   }
114
115   public void addAllyCore(FlagState fstate, 
116                           Integer targetCore) {
117     if(this.allyCores == null) {
118       this.allyCores = new Hashtable<FlagState, Vector<Integer>>();
119     }
120     if(!this.allyCores.containsKey(fstate)) {
121       this.allyCores.put(fstate, new Vector<Integer>());
122     }
123     if((this.coreNum != targetCore.intValue()) && (!this.allyCores.get(fstate).contains(targetCore))) {
124       this.allyCores.get(fstate).add(targetCore); 
125     }
126   }
127
128   public void addFState4TD(TaskDescriptor td, 
129                            FlagState fstate) {
130     if(this.td2fs == null) {
131       this.td2fs = new Hashtable<TaskDescriptor, Vector<FlagState>>();
132     }
133     if(!this.td2fs.containsKey(td)) {
134       this.td2fs.put(td, new Vector<FlagState>());
135     }
136     if(!this.td2fs.get(td).contains(fstate)) {
137       this.td2fs.get(td).addElement(fstate);
138     }
139   }
140
141   public Vector<TaskDescriptor> getTasks() {
142     return tasks;
143   }
144
145   public void addTask(TaskDescriptor task) {
146     if(this.tasks == null) {
147       this.tasks = new Vector<TaskDescriptor>();
148       this.td2num = new Hashtable<TaskDescriptor, Integer>();
149     }
150     if(!this.tasks.contains(task)) {
151       this.tasks.add(task);
152       this.td2num.put(task, 1);
153     } else {
154       this.td2num.put(task, this.td2num.get(task).intValue()+1);
155     }
156   }
157   
158   public int getTaskNum(TaskDescriptor task) {
159     return this.td2num.get(task);
160   }
161 }