dol: initial dol commit
[jump.git] / dol / src / dol / visitor / cell / CellMapping.java
diff --git a/dol/src/dol/visitor/cell/CellMapping.java b/dol/src/dol/visitor/cell/CellMapping.java
new file mode 100644 (file)
index 0000000..d5421c5
--- /dev/null
@@ -0,0 +1,324 @@
+package dol.visitor.cell;
+
+import java.util.ArrayList;
+import java.util.Vector;
+
+import dol.datamodel.architecture.Architecture;
+import dol.datamodel.mapping.ComputationBinding;
+import dol.datamodel.mapping.Mapping;
+import dol.datamodel.pn.Process;
+import dol.datamodel.pn.ProcessNetwork;
+import dol.main.UserInterface;
+import dol.parser.xml.archischema.ArchiXmlParser;
+import dol.parser.xml.mapschema.MapXmlParser;
+
+public class CellMapping {
+
+    /**
+     * Constructor
+     * @param pn
+     */
+    CellMapping(ProcessNetwork pn, String mapping, boolean ppu, int nrSPU) {
+        _pn = pn;
+
+        _maxSPU = nrSPU;
+
+        // Select the mapping
+
+        // Structural mapping
+        if (mapping.compareTo("structural") == 0) {
+            System.out.println("Cell: use structural mapping");
+            structuralMapping(pn, ppu);
+        }
+
+        // Random mapping
+        else if (mapping.compareTo("random") == 0) {
+            System.out.println("Cell: use random mapping");
+            randomMapping(pn, ppu);
+        }
+
+        // Predefined
+        else if (mapping.compareTo("predefined") == 0) {
+            System.out.println("Cell: Use predefined mapping.");
+            System.out.println("      All other parameters are ignored.");
+            predefinedMapping(pn);
+
+            _maxSPU = _SPUList.size();
+            ppu = this._PPU.size() == 0 ? false : true;
+        }
+
+        // All to PPU
+        else if (mapping.compareTo("ppu") == 0) {
+            System.out.println("Cell: Map all processes to the PPU");
+            allPPUMapping(pn);
+        }
+
+        // Default mapping
+        else {
+            System.out.println("WARNING: Cell: use default mapping");
+            structuralMapping(pn, ppu);
+        }
+
+        System.out.println("Cell: Nr of SPE is " + _maxSPU);
+
+        if (ppu) {
+            System.out.println("Cell: Mapped some processes to the PPE");
+        }
+    }
+
+    /**
+     * Returns a list which process is mapped to which spu
+     *
+     * @return the process list
+     */
+    public ArrayList<Vector<Process>> getSPUList() {
+        return _SPUList;
+    }
+
+    /**
+     * Returns list of all processes which are mapped to an spu
+     */
+    public Vector<Process> getAllSpuProcess() {
+        Vector<Process> processList = new Vector<Process>();
+
+        for (Vector<Process> spu : _SPUList) {
+            for (Process p : spu) {
+                processList.add(p);
+            }
+        }
+        return processList;
+    }
+
+    /**
+     * Returns a list of all base processes which occur on the spu's
+     */
+    public Vector<Process> getAllSpuBaseProcess() {
+        Vector<Process> pList = new Vector<Process>();
+        Vector<String> pListName = new Vector<String>();
+        for (Process process : getAllSpuProcess()) {
+            String basename = process.getBasename();
+            if (!pListName.contains(basename)) {
+                pList.add(process);
+                pListName.add(basename);
+            }
+        }
+        return pList;
+    }
+
+    /**
+     * Returns a list of all processes which are mapped to the ppu
+     */
+    public Vector<Process> getPPU() {
+        return _PPU;
+    }
+
+    /**
+     * Returns a list of all base processes which occur on the ppu
+     */
+    public Vector<Process> getAllPPUBaseProcess() {
+        Vector<Process> pList = new Vector<Process>();
+        Vector<String> pListName = new Vector<String>();
+        for (Process process : _PPU) {
+            String basename = process.getBasename();
+            if (!pListName.contains(basename)) {
+                pList.add(process);
+                pListName.add(basename);
+            }
+        }
+        return pList;
+    }
+
+    /**
+     * Return the number of processes which are mapped to the ppu
+     */
+    public int getNrPPUProcess() {
+        return _PPU.size();
+    }
+
+    /**
+     * Returns the number of processes which are mapped to the SPU
+     */
+    public int getNrSPUProcess() {
+        return getAllSpuProcess().size();
+    }
+
+    /**
+     * Returns the number of SPU which are used
+     */
+    public int getNrSPE() {
+        return _SPUList.size();
+    }
+
+    /**
+     * Performs a random mapping of the processes
+     *
+     * @param x
+     *            the process network
+     * @param ppu_choose
+     *            true if the sinks and sources should be mapped to the PPU
+     */
+    protected void randomMapping(ProcessNetwork x, boolean ppu_choose) {
+        ArrayList<Vector<Process>> spu = new ArrayList<Vector<Process>>();
+        Vector<Process> ppu = new Vector<Process>();
+
+        int addSPU = 0;
+        for (Process p : x.getProcessList()) {
+            // A process to map to the PPU
+            if ((p.getNumOfInports() == 0 || p.getNumOfOutports() == 0)
+                    && ppu_choose) {
+                ppu.add(p);
+            }
+
+            // A process to map to the SPU
+            else {
+                // As long as there is a free SPU, add the process to this SPU
+                if (addSPU < _maxSPU) {
+                    Vector<Process> pList = new Vector<Process>();
+                    pList.add(p);
+                    spu.add(pList);
+                }
+
+                // Have to add the process to an already used SPU
+                else {
+                    spu.get(addSPU % _maxSPU).add(p);
+                }
+
+                addSPU++;
+            }
+        }
+
+        _SPUList = spu;
+        _PPU = ppu;
+    }
+
+    /**
+     * Performs a structural mapping of the processes. That means that the
+     * function tries to keep the structural order of the process network. The
+     * order is given depending on the occurrence of the process in the
+     * structure file.
+     *
+     * @param x
+     *            the process network
+     * @param ppu_choose
+     *            true if the sinks and sources should be mapped to the PPU
+     */
+    protected void structuralMapping(ProcessNetwork x, boolean ppu_choose) {
+        ArrayList<Vector<Process>> spu = new ArrayList<Vector<Process>>();
+        Vector<Process> ppu = new Vector<Process>();
+
+        Vector<Process> spu_temp = new Vector<Process>();
+
+        // Each process is allocated to the PPE or the SPE (as general)
+        for (Process p : x.getProcessList()) {
+            // A process to map to the PPU
+            if ((p.getNumOfInports() == 0 || p.getNumOfOutports() == 0)
+                    && ppu_choose) {
+                ppu.add(p);
+            }
+
+            // A process to map to the SPU
+            else {
+                spu_temp.add(p);
+            }
+        }
+
+        System.out.println("Total processes: " + spu_temp.size());
+
+        // Do the structural mapping for all the SPE processes
+        int nrSPUProcess = spu_temp.size();
+        for (int i = 0; i < _maxSPU; i++) {
+
+            if (spu_temp.size() <= 0)
+                break;
+
+            Vector<Process> pList = new Vector<Process>();
+            for (int j = 0; j < Math.ceil(((double) (nrSPUProcess - i))
+                    / _maxSPU); j++) {
+                pList.add(spu_temp.remove(0));
+            }
+
+            spu.add(pList);
+        }
+
+        System.out.println("End Total processes: " + spu_temp.size());
+
+        _SPUList = spu;
+        _PPU = ppu;
+    }
+
+    /**
+     * Maps the processes depending on the architectural and mapping files
+     *
+     * @param x
+     */
+    protected void predefinedMapping(ProcessNetwork x) {
+        // The SPUs
+        ArrayList<Vector<Process>> spu = new ArrayList<Vector<Process>>();
+
+        // The PPU
+        Vector<Process> ppu = new Vector<Process>();
+
+        ArchiXmlParser archParser = new ArchiXmlParser();
+        //System.out.println(_ui.getPlatformFileName());
+        Architecture arch = archParser.doParse(_ui.getPlatformFileName());
+        MapXmlParser mappingParser = new MapXmlParser(x, arch);
+        Mapping mapping = mappingParser.doParse(_ui.getMappingFileName());
+
+        // Go through all process and check where to add
+        for (Process p : x.getProcessList()) {
+            for (ComputationBinding b : mapping.getCompBindList()) {
+                if (b.getProcess().getName().equals(p.getName())) {
+                    // Mapping to PPU
+                    if (b.getProcessor().getName().equals("ppu")) {
+                        System.out.println("Mapped process " + p.getName()
+                                + " to the PPU");
+                        ppu.add(p);
+                    }
+                    // Maps to one of the SPU, they are written in the format
+                    // spu_0, spu_1, ...
+                    else {
+                        int index = Integer.valueOf(b.getProcessor()
+                                .getName().replaceAll(".*_", ""));
+
+                        while (spu.size() <= index) {
+                            Vector<Process> spu_temp = new Vector<Process>();
+                            spu.add(spu_temp);
+                        }
+
+                        spu.get(index).add(p);
+                        System.out.println("Mapped process " + p.getName()
+                                + " to the SPU_" + index);
+                    }
+                }
+            }
+        }
+
+        _SPUList = spu;
+        _PPU = ppu;
+    }
+
+
+    /**
+     * Performs a mapping of all processes to the PPU.
+     *
+     * @param x the process network
+     */
+    protected void allPPUMapping(ProcessNetwork x) {
+        ArrayList<Vector<Process>> spu = new ArrayList<Vector<Process>>();
+        Vector<Process> ppu = new Vector<Process>();
+
+        // Each process is allocated to the PPE or the SPE (as general)
+        for (Process p : x.getProcessList()) {
+            ppu.add(p);
+        }
+
+        _SPUList = spu;
+        _PPU = ppu;
+    }
+
+    protected ArrayList<Vector<Process>> _SPUList = null;
+    protected Vector<Process> _PPU = null;
+    protected ProcessNetwork _pn;
+    protected int _maxSPU = 6;
+    protected UserInterface _ui = UserInterface.getInstance();
+}