dol: initial dol commit
[jump.git] / dol / src / dol / visitor / cell / CellMapping.java
1 package dol.visitor.cell;
2
3 import java.util.ArrayList;
4 import java.util.Vector;
5
6 import dol.datamodel.architecture.Architecture;
7 import dol.datamodel.mapping.ComputationBinding;
8 import dol.datamodel.mapping.Mapping;
9 import dol.datamodel.pn.Process;
10 import dol.datamodel.pn.ProcessNetwork;
11 import dol.main.UserInterface;
12 import dol.parser.xml.archischema.ArchiXmlParser;
13 import dol.parser.xml.mapschema.MapXmlParser;
14
15 public class CellMapping {
16
17     /**
18      * Constructor
19      * @param pn
20      */
21     CellMapping(ProcessNetwork pn, String mapping, boolean ppu, int nrSPU) {
22         _pn = pn;
23
24         _maxSPU = nrSPU;
25
26         // Select the mapping
27
28         // Structural mapping
29         if (mapping.compareTo("structural") == 0) {
30             System.out.println("Cell: use structural mapping");
31             structuralMapping(pn, ppu);
32         }
33
34         // Random mapping
35         else if (mapping.compareTo("random") == 0) {
36             System.out.println("Cell: use random mapping");
37             randomMapping(pn, ppu);
38         }
39
40         // Predefined
41         else if (mapping.compareTo("predefined") == 0) {
42             System.out.println("Cell: Use predefined mapping.");
43             System.out.println("      All other parameters are ignored.");
44             predefinedMapping(pn);
45
46             _maxSPU = _SPUList.size();
47             ppu = this._PPU.size() == 0 ? false : true;
48         }
49
50         // All to PPU
51         else if (mapping.compareTo("ppu") == 0) {
52             System.out.println("Cell: Map all processes to the PPU");
53             allPPUMapping(pn);
54         }
55
56         // Default mapping
57         else {
58             System.out.println("WARNING: Cell: use default mapping");
59             structuralMapping(pn, ppu);
60         }
61
62         System.out.println("Cell: Nr of SPE is " + _maxSPU);
63
64         if (ppu) {
65             System.out.println("Cell: Mapped some processes to the PPE");
66         }
67     }
68
69     /**
70      * Returns a list which process is mapped to which spu
71      *
72      * @return the process list
73      */
74     public ArrayList<Vector<Process>> getSPUList() {
75         return _SPUList;
76     }
77
78     /**
79      * Returns list of all processes which are mapped to an spu
80      */
81     public Vector<Process> getAllSpuProcess() {
82         Vector<Process> processList = new Vector<Process>();
83
84         for (Vector<Process> spu : _SPUList) {
85             for (Process p : spu) {
86                 processList.add(p);
87             }
88         }
89         return processList;
90     }
91
92     /**
93      * Returns a list of all base processes which occur on the spu's
94      */
95     public Vector<Process> getAllSpuBaseProcess() {
96         Vector<Process> pList = new Vector<Process>();
97         Vector<String> pListName = new Vector<String>();
98         for (Process process : getAllSpuProcess()) {
99             String basename = process.getBasename();
100             if (!pListName.contains(basename)) {
101                 pList.add(process);
102                 pListName.add(basename);
103             }
104         }
105         return pList;
106     }
107
108     /**
109      * Returns a list of all processes which are mapped to the ppu
110      */
111     public Vector<Process> getPPU() {
112         return _PPU;
113     }
114
115     /**
116      * Returns a list of all base processes which occur on the ppu
117      */
118     public Vector<Process> getAllPPUBaseProcess() {
119         Vector<Process> pList = new Vector<Process>();
120         Vector<String> pListName = new Vector<String>();
121         for (Process process : _PPU) {
122             String basename = process.getBasename();
123             if (!pListName.contains(basename)) {
124                 pList.add(process);
125                 pListName.add(basename);
126             }
127         }
128         return pList;
129     }
130
131     /**
132      * Return the number of processes which are mapped to the ppu
133      */
134     public int getNrPPUProcess() {
135         return _PPU.size();
136     }
137
138     /**
139      * Returns the number of processes which are mapped to the SPU
140      */
141     public int getNrSPUProcess() {
142         return getAllSpuProcess().size();
143     }
144
145     /**
146      * Returns the number of SPU which are used
147      */
148     public int getNrSPE() {
149         return _SPUList.size();
150     }
151
152     /**
153      * Performs a random mapping of the processes
154      *
155      * @param x
156      *            the process network
157      * @param ppu_choose
158      *            true if the sinks and sources should be mapped to the PPU
159      */
160     protected void randomMapping(ProcessNetwork x, boolean ppu_choose) {
161         ArrayList<Vector<Process>> spu = new ArrayList<Vector<Process>>();
162         Vector<Process> ppu = new Vector<Process>();
163
164         int addSPU = 0;
165         for (Process p : x.getProcessList()) {
166             // A process to map to the PPU
167             if ((p.getNumOfInports() == 0 || p.getNumOfOutports() == 0)
168                     && ppu_choose) {
169                 ppu.add(p);
170             }
171
172             // A process to map to the SPU
173             else {
174                 // As long as there is a free SPU, add the process to this SPU
175                 if (addSPU < _maxSPU) {
176                     Vector<Process> pList = new Vector<Process>();
177                     pList.add(p);
178                     spu.add(pList);
179                 }
180
181                 // Have to add the process to an already used SPU
182                 else {
183                     spu.get(addSPU % _maxSPU).add(p);
184                 }
185
186                 addSPU++;
187             }
188         }
189
190         _SPUList = spu;
191         _PPU = ppu;
192     }
193
194     /**
195      * Performs a structural mapping of the processes. That means that the
196      * function tries to keep the structural order of the process network. The
197      * order is given depending on the occurrence of the process in the
198      * structure file.
199      *
200      * @param x
201      *            the process network
202      * @param ppu_choose
203      *            true if the sinks and sources should be mapped to the PPU
204      */
205     protected void structuralMapping(ProcessNetwork x, boolean ppu_choose) {
206         ArrayList<Vector<Process>> spu = new ArrayList<Vector<Process>>();
207         Vector<Process> ppu = new Vector<Process>();
208
209         Vector<Process> spu_temp = new Vector<Process>();
210
211         // Each process is allocated to the PPE or the SPE (as general)
212         for (Process p : x.getProcessList()) {
213             // A process to map to the PPU
214             if ((p.getNumOfInports() == 0 || p.getNumOfOutports() == 0)
215                     && ppu_choose) {
216                 ppu.add(p);
217             }
218
219             // A process to map to the SPU
220             else {
221                 spu_temp.add(p);
222             }
223         }
224
225         System.out.println("Total processes: " + spu_temp.size());
226
227         // Do the structural mapping for all the SPE processes
228         int nrSPUProcess = spu_temp.size();
229         for (int i = 0; i < _maxSPU; i++) {
230
231             if (spu_temp.size() <= 0)
232                 break;
233
234             Vector<Process> pList = new Vector<Process>();
235             for (int j = 0; j < Math.ceil(((double) (nrSPUProcess - i))
236                     / _maxSPU); j++) {
237                 pList.add(spu_temp.remove(0));
238             }
239
240             spu.add(pList);
241         }
242
243         System.out.println("End Total processes: " + spu_temp.size());
244
245         _SPUList = spu;
246         _PPU = ppu;
247     }
248
249     /**
250      * Maps the processes depending on the architectural and mapping files
251      *
252      * @param x
253      */
254     protected void predefinedMapping(ProcessNetwork x) {
255         // The SPUs
256         ArrayList<Vector<Process>> spu = new ArrayList<Vector<Process>>();
257
258         // The PPU
259         Vector<Process> ppu = new Vector<Process>();
260
261         ArchiXmlParser archParser = new ArchiXmlParser();
262         //System.out.println(_ui.getPlatformFileName());
263         Architecture arch = archParser.doParse(_ui.getPlatformFileName());
264         MapXmlParser mappingParser = new MapXmlParser(x, arch);
265         Mapping mapping = mappingParser.doParse(_ui.getMappingFileName());
266
267         // Go through all process and check where to add
268         for (Process p : x.getProcessList()) {
269             for (ComputationBinding b : mapping.getCompBindList()) {
270                 if (b.getProcess().getName().equals(p.getName())) {
271                     // Mapping to PPU
272                     if (b.getProcessor().getName().equals("ppu")) {
273                         System.out.println("Mapped process " + p.getName()
274                                 + " to the PPU");
275                         ppu.add(p);
276                     }
277                     // Maps to one of the SPU, they are written in the format
278                     // spu_0, spu_1, ...
279                     else {
280                         int index = Integer.valueOf(b.getProcessor()
281                                 .getName().replaceAll(".*_", ""));
282
283                         while (spu.size() <= index) {
284                             Vector<Process> spu_temp = new Vector<Process>();
285                             spu.add(spu_temp);
286                         }
287
288                         spu.get(index).add(p);
289                         System.out.println("Mapped process " + p.getName()
290                                 + " to the SPU_" + index);
291                     }
292                 }
293             }
294         }
295
296         _SPUList = spu;
297         _PPU = ppu;
298     }
299
300
301     /**
302      * Performs a mapping of all processes to the PPU.
303      *
304      * @param x the process network
305      */
306     protected void allPPUMapping(ProcessNetwork x) {
307         ArrayList<Vector<Process>> spu = new ArrayList<Vector<Process>>();
308         Vector<Process> ppu = new Vector<Process>();
309
310         // Each process is allocated to the PPE or the SPE (as general)
311         for (Process p : x.getProcessList()) {
312             ppu.add(p);
313         }
314
315         _SPUList = spu;
316         _PPU = ppu;
317     }
318
319     protected ArrayList<Vector<Process>> _SPUList = null;
320     protected Vector<Process> _PPU = null;
321     protected ProcessNetwork _pn;
322     protected int _maxSPU = 6;
323     protected UserInterface _ui = UserInterface.getInstance();
324 }