1 package dol.visitor.cell;
3 import java.util.ArrayList;
4 import java.util.Vector;
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;
15 public class CellMapping {
21 CellMapping(ProcessNetwork pn, String mapping, boolean ppu, int nrSPU) {
29 if (mapping.compareTo("structural") == 0) {
30 System.out.println("Cell: use structural mapping");
31 structuralMapping(pn, ppu);
35 else if (mapping.compareTo("random") == 0) {
36 System.out.println("Cell: use random mapping");
37 randomMapping(pn, ppu);
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);
46 _maxSPU = _SPUList.size();
47 ppu = this._PPU.size() == 0 ? false : true;
51 else if (mapping.compareTo("ppu") == 0) {
52 System.out.println("Cell: Map all processes to the PPU");
58 System.out.println("WARNING: Cell: use default mapping");
59 structuralMapping(pn, ppu);
62 System.out.println("Cell: Nr of SPE is " + _maxSPU);
65 System.out.println("Cell: Mapped some processes to the PPE");
70 * Returns a list which process is mapped to which spu
72 * @return the process list
74 public ArrayList<Vector<Process>> getSPUList() {
79 * Returns list of all processes which are mapped to an spu
81 public Vector<Process> getAllSpuProcess() {
82 Vector<Process> processList = new Vector<Process>();
84 for (Vector<Process> spu : _SPUList) {
85 for (Process p : spu) {
93 * Returns a list of all base processes which occur on the spu's
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)) {
102 pListName.add(basename);
109 * Returns a list of all processes which are mapped to the ppu
111 public Vector<Process> getPPU() {
116 * Returns a list of all base processes which occur on the ppu
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)) {
125 pListName.add(basename);
132 * Return the number of processes which are mapped to the ppu
134 public int getNrPPUProcess() {
139 * Returns the number of processes which are mapped to the SPU
141 public int getNrSPUProcess() {
142 return getAllSpuProcess().size();
146 * Returns the number of SPU which are used
148 public int getNrSPE() {
149 return _SPUList.size();
153 * Performs a random mapping of the processes
156 * the process network
158 * true if the sinks and sources should be mapped to the PPU
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>();
165 for (Process p : x.getProcessList()) {
166 // A process to map to the PPU
167 if ((p.getNumOfInports() == 0 || p.getNumOfOutports() == 0)
172 // A process to map to the SPU
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>();
181 // Have to add the process to an already used SPU
183 spu.get(addSPU % _maxSPU).add(p);
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
201 * the process network
203 * true if the sinks and sources should be mapped to the PPU
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>();
209 Vector<Process> spu_temp = new Vector<Process>();
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)
219 // A process to map to the SPU
225 System.out.println("Total processes: " + spu_temp.size());
227 // Do the structural mapping for all the SPE processes
228 int nrSPUProcess = spu_temp.size();
229 for (int i = 0; i < _maxSPU; i++) {
231 if (spu_temp.size() <= 0)
234 Vector<Process> pList = new Vector<Process>();
235 for (int j = 0; j < Math.ceil(((double) (nrSPUProcess - i))
237 pList.add(spu_temp.remove(0));
243 System.out.println("End Total processes: " + spu_temp.size());
250 * Maps the processes depending on the architectural and mapping files
254 protected void predefinedMapping(ProcessNetwork x) {
256 ArrayList<Vector<Process>> spu = new ArrayList<Vector<Process>>();
259 Vector<Process> ppu = new Vector<Process>();
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());
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())) {
272 if (b.getProcessor().getName().equals("ppu")) {
273 System.out.println("Mapped process " + p.getName()
277 // Maps to one of the SPU, they are written in the format
280 int index = Integer.valueOf(b.getProcessor()
281 .getName().replaceAll(".*_", ""));
283 while (spu.size() <= index) {
284 Vector<Process> spu_temp = new Vector<Process>();
288 spu.get(index).add(p);
289 System.out.println("Mapped process " + p.getName()
290 + " to the SPU_" + index);
302 * Performs a mapping of all processes to the PPU.
304 * @param x the process network
306 protected void allPPUMapping(ProcessNetwork x) {
307 ArrayList<Vector<Process>> spu = new ArrayList<Vector<Process>>();
308 Vector<Process> ppu = new Vector<Process>();
310 // Each process is allocated to the PPE or the SPE (as general)
311 for (Process p : x.getProcessList()) {
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();