X-Git-Url: http://sraa.de/git/?a=blobdiff_plain;f=dol%2Fsrc%2Fdol%2Futil%2FApplicationGenerator.java;fp=dol%2Fsrc%2Fdol%2Futil%2FApplicationGenerator.java;h=a193614fce6be208091ab944f53f414acac54a06;hb=8c411cf24ed0eb889191aaeafd8fa1e69081df42;hp=0000000000000000000000000000000000000000;hpb=dea7a4fb1ed110d3ce6e6d9255103d724bd66c0e;p=jump.git diff --git a/dol/src/dol/util/ApplicationGenerator.java b/dol/src/dol/util/ApplicationGenerator.java new file mode 100644 index 0000000..a193614 --- /dev/null +++ b/dol/src/dol/util/ApplicationGenerator.java @@ -0,0 +1,914 @@ +/* $Id: ApplicationGenerator.java 1 2010-02-24 13:03:05Z haidw $ */ +package dol.util; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.util.Random; +import java.util.Vector; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import dol.datamodel.architecture.Processor; +import dol.datamodel.architecture.ReadPath; +import dol.datamodel.architecture.WritePath; +import dol.datamodel.mapping.CommunicationBinding; +import dol.datamodel.mapping.ComputationBinding; +import dol.datamodel.mapping.Mapping; +import dol.datamodel.mapping.Schedule; +import dol.datamodel.mapping.ScheduleEntry; +import dol.datamodel.mapping.SchedulingPolicy; +import dol.datamodel.pn.Channel; +import dol.datamodel.pn.Connection; +import dol.datamodel.pn.Port; +import dol.datamodel.pn.Process; +import dol.datamodel.pn.ProcessNetwork; +import dol.datamodel.pn.ProfilingConfiguration; +import dol.datamodel.pn.SourceCode; +import dol.visitor.xml.MapXmlVisitor; +import dol.visitor.xml.PNXmlVisitor; + +/** + * + */ +public class ApplicationGenerator { + /* + protected String _processes[] = + {"producer", "consumer"}; + + protected int _computation[] = + {1000, 1000}; + + protected int _communication[][] = + {{0, 4}, {-4, 0}}; + + protected String _binding[] = + {"tile_0.arm", "tile_0.arm"}; + + protected int _iterations = 1000; + */ + + /* + protected String _processes[] = + {"producer", "forwarder", "consumer"}; + + protected int _computation[] = + {1000, 1000, 1000}; + + protected int _communication[][] = + {{ 0, 4, 0}, + {-4, 0, 4}, + { 0, -4, 0}}; + + protected String _binding[] = + {"tile_0.arm", "tile_0.arm", "tile_0.arm"}; + + protected int _iterations = 10; + */ + + /* + protected String _processes[] = + {"stageA", "stageB", "stageC", "stageD", + "stageE", "stageF", "stageG", "stageH", + "stageI", "stageJ", "stageK", "stageL", + "stageM", "stageN", "stageO", "stageP"}; + + protected int _computation[] = + {1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, 16}; + + protected int _communication[][] = + {{ 0, 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {-1024, 0, 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + { 0, -1024, 0, 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + { 0, 0, -1024, 0, 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + { 0, 0, 0, -1024, 0, 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + { 0, 0, 0, 0, -1024, 0, 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + { 0, 0, 0, 0, 0, -1024, 0, 1024, 0, 0, 0, 0, 0, 0, 0, 0}, + { 0, 0, 0, 0, 0, 0, -1024, 0, 1024, 0, 0, 0, 0, 0, 0, 0}, + { 0, 0, 0, 0, 0, 0, 0, -1024, 0, 1024, 0, 0, 0, 0, 0, 0}, + { 0, 0, 0, 0, 0, 0, 0, 0, -1024, 0, 1024, 0, 0, 0, 0, 0}, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, -1024, 0, 1024, 0, 0, 0, 0}, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1024, 0, 1024, 0, 0, 0}, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1024, 0, 1024, 0, 0}, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1024, 0, 1024, 0}, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1024, 0, 1024}, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1024, 0}}; + + + //protected String _binding[] = + // {"tile_0.arm", "tile_0.arm", "tile_1.arm", "tile_1.arm", + // "tile_2.arm", "tile_2.arm", "tile_3.arm", "tile_3.arm", + // "tile_4.arm", "tile_4.arm", "tile_5.arm", "tile_5.arm", + // "tile_6.arm", "tile_6.arm", "tile_7.arm", "tile_7.arm"}; + + //protected String _binding[] = + // {"tile_0.arm", "tile_1.arm", "tile_2.arm", "tile_3.arm", + // "tile_4.arm", "tile_5.arm", "tile_6.arm", "tile_7.arm", + // "tile_0.arm", "tile_1.arm", "tile_2.arm", "tile_3.arm", + // "tile_4.arm", "tile_5.arm", "tile_6.arm", "tile_7.arm"}; + + protected String _binding[] = + {"tile_0.arm", "tile_1.arm", "tile_2.arm", "tile_3.arm", + "tile_4.arm", "tile_5.arm", "tile_6.arm", "tile_7.arm", + "tile_7.arm", "tile_6.arm", "tile_5.arm", "tile_4.arm", + "tile_3.arm", "tile_2.arm", "tile_1.arm", "tile_0.arm"}; + + protected int _iterations = 10; + */ + + protected String _processes[] = + {"splitAA", "splitBA", "splitBB", + "pipeAA", "pipeBA", "pipeCA", "pipeDA", + "pipeEA", "pipeFA", "pipeAB", "pipeBB", + "pipeCB", "pipeDB", "pipeEB", "pipeFB", + "mergeBA", "mergeBB", "mergeAA"}; + + protected int _armComputation[] = + {1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}; + protected int _magicComputation[] = + {1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}; + + + protected int _communication[][] = + {{ 0, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + { -4, 0, 0, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + { -4, 0, 0, 0, 0, 0, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + { 0, -4, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0}, + { 0, -4, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0}, + { 0, -4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0}, + { 0, 0, -4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0}, + { 0, 0, -4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0}, + { 0, 0, -4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0}, + { 0, 0, 0, -4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0}, + { 0, 0, 0, 0, -4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0}, + { 0, 0, 0, 0, 0, -4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0}, + { 0, 0, 0, 0, 0, 0, -4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0}, + { 0, 0, 0, 0, 0, 0, 0, -4, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0}, + { 0, 0, 0, 0, 0, 0, 0, 0, -4, 0, 0, 0, 0, 0, 0, 0, 4, 0}, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, -4, -4, -4, 0, 0, 0, 0, 0, 4}, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -4, -4, -4, 0, 0, 4}, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -4, -4, 0}}; + + + //working + protected String _binding[] = + {"tile_0.arm", "tile_1.arm", "tile_4.arm", + "tile_1.arm", "tile_2.arm", "tile_3.arm", "tile_4.arm", + "tile_5.arm", "tile_6.arm", "tile_1.arm", "tile_2.arm", + "tile_3.arm", "tile_4.arm", "tile_5.arm", "tile_6.arm", + "tile_3.arm", "tile_7.arm", "tile_0.arm"}; + + /* + protected String _binding[] = + {"tile_0.arm", "tile_1.arm", "tile_3.arm", + "tile_1.arm", "tile_1.arm", "tile_2.arm", "tile_2.arm", + "tile_3.arm", "tile_3.arm", "tile_4.arm", "tile_4.arm", + "tile_5.arm", "tile_5.arm", "tile_6.arm", "tile_6.arm", + "tile_7.arm", "tile_7.arm", "tile_0.arm"}; + + protected String _binding[] = + {"tile_0.arm", "tile_1.arm", "tile_7.arm", + "tile_1.arm", "tile_1.arm", "tile_2.arm", "tile_2.arm", + "tile_3.arm", "tile_3.arm", "tile_4.arm", "tile_4.arm", + "tile_5.arm", "tile_5.arm", "tile_6.arm", "tile_6.arm", + "tile_7.arm", "tile_3.arm", "tile_0.arm"}; + */ + + //not working + /* + protected String _binding[] = + {"tile_0.arm", "tile_1.arm", "tile_2.arm", "tile_3.arm", + "tile_4.arm", "tile_5.arm", "tile_6.arm", "tile_7.arm", + "tile_7.arm", "tile_6.arm", "tile_5.arm", "tile_4.arm", + "tile_3.arm", "tile_2.arm", "tile_1.arm", "tile_0.arm", + "tile_0.arm", "tile_0.arm"}; + */ + + protected int _iterations = 10; + + public String getStringEncoding(int i, int total) { + if (total > 26 * 26) { + System.out.println("\"total\" needs to be less than " + + 26 * 26 + "."); + return ""; + } else if (total <= 26) { + return Character.toString((char)('A' + (i % 26))); + } + return Character.toString((char)('A' + (i / 26))) + + Character.toString((char)('A' + (i % 26))); + } + + /** + * + * @param stages + * @param communication + */ + public void generateIndependentTasks(int numberOfTasks) { + _processes = new String[numberOfTasks]; + _armComputation = new int[numberOfTasks]; + _magicComputation = new int[numberOfTasks]; + _communication = new int[numberOfTasks][numberOfTasks]; + _binding = new String[numberOfTasks]; + _iterations = 1000; + + for (int i = 0; i < numberOfTasks; i++) { + _processes[i] = ((i < numberOfTasks / 2) ? "armProcess" + : "magicProcess") + + getStringEncoding(i, numberOfTasks); + System.out.println(_processes[i]); + _armComputation[i] = + (i < numberOfTasks / 2) ? 100000000 : 200000000; + _magicComputation[i] = + (i < numberOfTasks / 2) ? 200000000 : 100000000; + _binding[i] = (i < numberOfTasks / 2) ? "tile_0.arm" + : "tile_0.magic"; + } + + //no communication + for (int i = 0; i < numberOfTasks; i++) { + for (int j = 0; j < numberOfTasks; j++) { + _communication[i][j] = 0; + } + } + } + + /** + * + * @param pipelines + * @param stages + * @param communication + */ + public void generateIndpendentPipelines(int pipelines, int stages, int communication) { + Random random = new Random(); + _processes = new String[stages * pipelines]; + _armComputation = new int[stages * pipelines]; + _magicComputation = new int[stages * pipelines]; + _communication = new int[stages * pipelines][stages * pipelines]; + _binding = new String[stages * pipelines]; + _iterations = 1000; + + for (int i = 0; i < pipelines; i++) { + for (int j = 0; j < stages; j++) { + _processes[i * stages + j] = + ((i < pipelines / 2) ? "arm" : "magic") + + "stage" + + getStringEncoding(i, pipelines) + "x" + + getStringEncoding(j, stages); + System.out.println(_processes[i * stages + j]); + _armComputation[i * stages + j] = + 0 * random.nextInt(10000000) + + ((i < pipelines / 2) ? 100000000 : 200000000); + _magicComputation[i * stages + j] = + 0 * random.nextInt(10000000) + + ((i < pipelines / 2) ? 200000000 : 100000000); + _binding[i * stages + j] = "tile_0.arm"; + } + } + + for (int i = 0; i < pipelines; i++) { + for (int j = 0; j < stages; j++) { + for (int k = 0; k < pipelines; k++) { + for (int l = 0; l < stages; l++) { + _communication[i * stages + j][k * stages + l] = 0; + if (i == k && l == j + 1) { + _communication[i * stages + j][k * stages + l] = communication; + } else if (i == k && j == l + 1) { + _communication[i * stages + j][k * stages + l] = -communication; + } + } + } + } + } + } + + + /** + * + * @param pipelines + * @param stages + * @param communication + */ + public void generateIndpendentPipelines2(int pipelines, int stages, int communication) { + Random random = new Random(); + _processes = new String[stages * pipelines]; + _armComputation = new int[stages * pipelines]; + _magicComputation = new int[stages * pipelines]; + _communication = new int[stages * pipelines][stages * pipelines]; + _binding = new String[stages * pipelines]; + _iterations = 1000; + + for (int i = 0; i < pipelines; i++) { + for (int j = 0; j < stages; j++) { + _processes[i * stages + j] = + ((j < stages / 2) ? "arm" : "magic") + + "stage" + + getStringEncoding(i, pipelines) + "x" + + getStringEncoding(j, stages); + System.out.println(_processes[i * stages + j]); + _armComputation[i * stages + j] = + 0 * random.nextInt(10000000) + + ((j < stages / 2) ? 100000000 : 200000000); + _magicComputation[i * stages + j] = + 0 * random.nextInt(10000000) + + ((j < stages / 2) ? 200000000 : 100000000); + _binding[i * stages + j] = "tile_0.arm"; + } + } + + for (int i = 0; i < pipelines; i++) { + for (int j = 0; j < stages; j++) { + for (int k = 0; k < pipelines; k++) { + for (int l = 0; l < stages; l++) { + _communication[i * stages + j][k * stages + l] = 0; + if (i == k && l == j + 1) { + _communication[i * stages + j][k * stages + l] = communication; + } else if (i == k && j == l + 1) { + _communication[i * stages + j][k * stages + l] = -communication; + } + } + } + } + } + } + + + /** + * + * @param stages + * @param communication + */ + public void generatePipeline(int stages, int communication) { + _processes = new String[stages]; + _armComputation = new int[stages]; + _magicComputation = new int[stages]; + _communication = new int[stages][stages]; + _binding = new String[stages]; + //_iterations = 1000; + _iterations = 1000; + + for (int i = 0; i < stages; i++) { + _processes[i] = ((i % 8 < 4) ? "arm" : "magic") + + "stage" + getStringEncoding(i, stages); + System.out.println(_processes[i]); + _armComputation[i] = + (i % 8 < 4) ? 100 : 200; + _magicComputation[i] = + (i % 8 < 4) ? 200 : 100; + _binding[i] = "tile_0.arm"; + } + + for (int i = 0; i < stages; i++) { + for (int j = 0; j < stages; j++) { + _communication[i][j] = 0; + if (j == i + 1) { + _communication[i][j] = communication; + } else if (i == j + 1) { + _communication[i][j] = -communication; + } + } + } + } + + + /** + * + * @param pn + */ + public void generateProcessNetwork(ProcessNetwork pn) { + System.out.print("Generate process network. "); + int numberOfProcesses = 0; + int numberOfChannels = 0; + + for (int j = 0; j < _processes.length; j++) { + String processName = _processes[j]; + Process p = new Process(processName); + SourceCode srcCode = new SourceCode(processName); + srcCode.setLocality(processName + ".c"); + srcCode.setType("c"); + p.getSrcList().add(srcCode); + pn.getProcessList().add(p); + numberOfProcesses++; + for (int i = 0; i < 8; i++) { + ProfilingConfiguration c = new ProfilingConfiguration( + "BCED tile_" + i + ".arm"); + c.setValue(Integer.toString(_armComputation[j])); + p.getProfilingList().add(c); + c = new ProfilingConfiguration( + "BCED tile_" + i + ".magic"); + c.setValue(Integer.toString(_magicComputation[j])); + p.getProfilingList().add(c); + } + } + + //check whether communication matrix is symmetric + if (_communication[0].length != numberOfProcesses) { + System.out.println(); + System.out.println("Error: Size of communication matrix (" + + _communication[0].length + " columns) must match " + + "number of processes (" + numberOfProcesses + ")."); + System.exit(-1); + } + for (int row = 0; row < numberOfProcesses; row++) { + for (int col = 0; col < numberOfProcesses; col++) { + if (row == col && _communication[row][col] != 0) { + System.out.println(); + System.out.println("Error: No self-channels allowed. " + + "Problem for process " + row + " (" + + _processes[row] + ")."); + System.exit(-1); + } + if (_communication[row][col] != + -_communication[col][row]) { + System.out.println(); + System.out.println("Error: Communication matrix " + + "needs to be symmetric. Mismatch for row " + + row + " column " + col + "."); + System.exit(-1); + } + } + } + + for (int row = 0; row < numberOfProcesses; row++) { + for (int col = row; col < numberOfProcesses; col++) { + if (_communication[row][col] == 0) { + continue; + } + int src = row; + int dst = col; + + if (_communication[row][col] < 0) { + src = col; + dst = row; + } + + Channel c = new Channel("channel" + _processes[src] + + _processes[dst]); + c.setSize(Math.abs(2 * _communication[src][dst])); + c.setType("fifo"); + ProfilingConfiguration p = new ProfilingConfiguration( + "TotalReadData"); + p.setValue(Integer.toString(_communication[row][col])); + c.getProfilingList().add(p); + pn.getChannelList().add(c); + numberOfChannels++; + + Port channelPort0 = new Port("0", Port.INPORT); + Port channelPort1 = new Port("1", Port.OUTPORT); + c.getPortList().add(channelPort0); + c.getPortList().add(channelPort1); + + Connection c1 = new Connection( + "c1" + _processes[src] + _processes[dst]); + Connection c2 = new Connection( + "c2" + _processes[src] + _processes[dst]); + pn.getConnectionList().add(c1); + pn.getConnectionList().add(c2); + + Port p0 = new Port(Integer.toString(pn.getProcess( + _processes[src]).getPortList().size()), + Port.OUTPORT); + Port p1 = new Port(Integer.toString(pn.getProcess( + _processes[dst]).getPortList().size()), + Port.INPORT); + pn.getProcess(_processes[src]).getPortList().add(p0); + pn.getProcess(_processes[dst]).getPortList().add(p1); + + c1.setOriginPort(p0); + c1.setTargetPort(channelPort0); + c1.setOrigin(pn.getProcess(_processes[src])); + c1.setTarget(c); + p0.setPeerResource(c); + p0.setPeerPort(channelPort0); + channelPort0.setPeerResource(pn.getProcess( + _processes[src])); + channelPort0.setPeerPort(p0); + + c2.setOriginPort(channelPort1); + c2.setTargetPort(p1); + c2.setOrigin(c); + c2.setTarget(pn.getProcess(_processes[dst])); + channelPort1.setPeerResource(pn.getProcess( + _processes[dst])); + channelPort1.setPeerPort(p1); + p1.setPeerResource(c); + p1.setPeerPort(channelPort1); + + c.setOrigin(pn.getProcess(_processes[src])); + c.setTarget(pn.getProcess(_processes[dst])); + } + } + + System.out.println("Finished."); + } + + /** + * + * @param pn + */ + public void generateProcessNetworkXml(ProcessNetwork pn, + String filename) { + System.out.print("Generate process network XML. "); + StringBuffer buffer = new StringBuffer(); + pn.accept(new PNXmlVisitor(buffer)); + try { + java.io.BufferedWriter writer = + new java.io.BufferedWriter( + new java.io.FileWriter(filename)); + writer.write(buffer.toString()); + writer.close(); + } catch (java.io.IOException e) { + System.out.println(); + System.out.println("Error: Could not write XML file."); + System.exit(-1); + } + System.out.println("Finished."); + } + + /** + * + * @param pn + * @param path + */ + public void generateCode(ProcessNetwork pn, String path) { + System.out.print("Generate source code of processes. "); + + String newline = System.getProperty("line.separator"); + int maxProcessNameLength = 0; + for (String processName : _processes) { + maxProcessNameLength = Math.max(maxProcessNameLength, + processName.length()); + } + + String global = ""; + global += "#ifndef GLOBAL_H" + newline; + global += "#define GLOBAL_H" + newline; + global += newline; + + global += "#define ITERATIONS " + _iterations + newline; + global += newline; + + for (int i = 0; i < _processes.length; i++) { + global += "#define LOOPS_ARM_" + _processes[i].toUpperCase() + + " " + _armComputation[i] + newline; + global += "#define LOOPS_MAGIC_" + _processes[i].toUpperCase() + + " " + _magicComputation[i] + newline; + } + + for (int row = 0; row < _communication[0].length; row++) { + for (int col = row; col < _communication[0].length; col++) { + if (_communication[row][col] == 0) { + continue; + } + int src = row; + int dst = col; + + if (_communication[row][col] < 0) { + src = col; + dst = row; + } + + global += "#define TOKEN_SIZE_" + + _processes[src].toUpperCase() + "_" + + _processes[dst].toUpperCase() + " " + + _communication[src][dst] + newline; + } + } + global += newline; + global += "#endif" + newline; + + try { + BufferedWriter out = new BufferedWriter( + new FileWriter(path + "global.h")); + out.write(global); + out.close(); + } catch (Exception e) { + System.out.println(); + System.out.println("Error: While writing global.h file."); + System.exit(-1); + } + + + for (Process p : pn.getProcessList()) { + String state = p.getName().substring(0, 1).toUpperCase() + + p.getName().substring(1) + "_State"; + + String h = ""; + h += "#ifndef " + p.getName().toUpperCase() + "_H" + newline; + h += "#define " + p.getName().toUpperCase() + "_H" + newline; + h += newline; + h += "#include " + newline; + h += newline; + for (Port port : p.getPortList()) { + h += "#define PORT_" + p.getName().toUpperCase() + "_" + + (port.isInPort() ? "IN_" : "OUT_" ) + + port.getName() + " " + port.getName() + newline; + } + h += newline; + h += "typedef struct {" + newline; + h += " int iterations;" + newline; + h += "} " + state + ";" + newline; + h += newline; + h += "void " + p.getName() + "_init(DOLProcess *p);" + + newline; + h += "int " + p.getName() + "_fire(DOLProcess *p);" + newline; + h += newline; + h += "#endif" + newline; + + String c = ""; + String printPrefix = String.format(" printf(\"[%-" + + maxProcessNameLength + "s] ", p.getName()); + c += "#include " + newline; + c += newline; + c += "#include \"" + p.getName() + ".h\"" + newline; + c += "#include \"global.h\"" + newline; + c += newline; + c += "void " + p.getName() + "_init(DOLProcess *p) {" + + newline; + c += " " + state + " *state = (" + state + "*)p->local;" + + newline; + c += " state->iterations = 0;" + newline; + c += "}" + newline; + c += newline; + c += "int " + p.getName() + "_fire(DOLProcess *p) {" + + newline; + + int processIndex = getProcessIndex(p.getName()); + int maxBuffer = 0; + for (int i = 0; i < _communication[processIndex].length; i++) { + maxBuffer = Math.max(maxBuffer, + Math.abs(_communication[processIndex][i])); + } + c += " int buffer[" + ((maxBuffer + (4 - 1)) / 4) + "];" + newline; + c += " int i;" + newline; + c += " " + state + " *state = (" + state + "*)p->local;"; + c += newline + newline; + + for (Port port : p.getPortList()) { + if (port.isInPort()) { + int originIndex = getProcessIndex( + ((Channel)port.getPeerResource()).getOrigin() + .getName()); + String tokenSize = "TOKEN_SIZE_" + + _processes[originIndex].toUpperCase() + "_" + + _processes[processIndex].toUpperCase(); + c += printPrefix + "read %d bytes from " + + _processes[originIndex] + ".\\n\", " + tokenSize + + ");" + newline; + c += " DOL_read((void*)" + + "PORT_" + p.getName().toUpperCase() + "_IN_" + + port.getName() + ", buffer, " + tokenSize + + ", p);" + newline; + } + } + c += newline; + //c += " printf(\"[%-20s ] iteration %d/n\", \"" + // + p.getName() + "\", state->iterations);" + newline; + c += printPrefix + "iteration %d.\\n\" , " + + "state->iterations);" + newline; + c += newline; + c += "#ifdef __arm__" + newline; + c += " for (i = 0; i < LOOPS_ARM_" + p.getName().toUpperCase() + + "; i++) { ; }" + newline; + c += "#else" + newline; + c += " for (i = 0; i < LOOPS_MAGIC_" + p.getName().toUpperCase() + + "; i++) { ; }" + newline; + c += "#endif" + newline; + for (Port port : p.getPortList()) { + if (port.isOutPort()) { + int targetIndex = getProcessIndex( + ((Channel)port.getPeerResource()).getTarget() + .getName()); + String tokenSize = "TOKEN_SIZE_" + + _processes[processIndex].toUpperCase() + "_" + + _processes[targetIndex].toUpperCase(); + c += printPrefix + "write %d bytes to " + + _processes[targetIndex] + ".\\n\", " + tokenSize + + ");" + newline; + c += " DOL_write((void*)" + + "PORT_" + p.getName().toUpperCase() + "_OUT_" + + port.getName() + ", buffer, "+ tokenSize + + ", p);" + newline; + } + } + c += newline; + c += " state->iterations++;" + newline; + c += " if (state->iterations >= ITERATIONS) {" + + newline; + c += " DOL_detach(p);" + newline; + c += " return -1;" + newline; + c += " }" + newline; + c += newline; + c += " return 0;" + newline; + c += "}" + newline; + + try { + BufferedWriter out = new BufferedWriter( + new FileWriter(path + p.getName() + ".h")); + out.write(h); + out.close(); + out = new BufferedWriter( + new FileWriter(path + p.getName() + ".c")); + out.write(c); + out.close(); + } catch (Exception e) { + System.out.println(); + System.out.println("Error: While writing source files."); + System.exit(-1); + } + } + System.out.println("Finished."); + } + + + /** + * + * @param map + * @param pn + */ + public void generateMapping(ProcessNetwork pn, Mapping map) { + System.out.print("Generate mapping. "); + map.setPN(pn); + + //binding of processes + Vector processors = new Vector(); + for (int i = 0; i < _binding.length; i++) { + String processName = _processes[i]; + String processorName = _binding[i]; + Processor processor = null; + + boolean processorExists = false; + for (Processor proc : processors) { + if (proc.getName().equals(processorName)) { + processor = proc; + processorExists = true; + break; + } + } + + if (!processorExists) { + processor = new Processor(processorName); + processors.add(processor); + } + + ComputationBinding compBinding = new ComputationBinding( + processName + "binding"); + compBinding.setProcess(pn.getProcess(processName)); + compBinding.setProcessor(processor); + processor.getProcessList().add(pn.getProcess(processName)); + map.getCompBindList().add(compBinding); + } + + //schedules + for (Processor p : processors) { + Schedule s = new Schedule(p.getName() + "schedule"); + s.setSchedPolicy(SchedulingPolicy.FIFO); + s.setResource(p); + for (Process process : p.getProcessList()) { + ScheduleEntry entry = new ScheduleEntry( + process.getName()); + entry.setConsumer(process); + s.getEntryList().add(entry); + } + map.getScheduleList().add(s); + } + + //binding of channels + for (Channel c : pn.getChannelList()) { + String originProcess = c.getOrigin().getName(); + String targetProcess = c.getTarget().getName(); + + String originBinding = _binding[getProcessIndex( + originProcess)]; + String targetBinding = _binding[getProcessIndex( + targetProcess)]; + + Pattern pattern0 = Pattern.compile( + "tile_(\\d)\\.(arm)|tile_(\\d)\\.(magic)"); + Matcher matcher0 = pattern0.matcher(originBinding); + Pattern pattern1 = Pattern.compile( + "tile_(\\d)\\.(arm)|tile_(\\d)\\.(magic)"); + Matcher matcher1 = pattern1.matcher(targetBinding); + + if (!matcher0.matches() || !matcher1.matches()) { + System.out.println("binding needs to comply to the" + + " following regular expression: " + + "tile_(\\d)\\.(arm)|tile_(\\d)\\.(magic)"); + System.exit(-1); + } + + int srcIndex = Integer.valueOf(matcher0.group(1)); + int dstIndex = Integer.valueOf(matcher1.group(1)); + String srcProcessor = matcher0.group(2); + String dstProcessor = matcher1.group(2); + + String writePath = ""; + String readPath = ""; + + if (srcIndex == dstIndex) { + if (srcProcessor.equals("arm")) { + writePath = "tile_" + srcIndex + ".rdmtodxm"; + } else { + writePath = "tile_" + srcIndex + ".ddmtodxm"; + } + } else { + if (srcProcessor.equals("arm")) { + writePath = "tile_" + srcIndex + ".rdmtodnp_" + + dstIndex; + } else { + writePath = "tile_" + srcIndex + ".ddmtodnp_" + + dstIndex; + } + } + + if (dstProcessor.equals("arm")) { + readPath = "tile_" + dstIndex + ".rdmfromdxm"; + } else { + readPath = "tile_" + dstIndex + ".ddmfromdxm"; + } + + CommunicationBinding commBinding = new CommunicationBinding( + c.getName() + "binding"); + commBinding.setChannel(c); + commBinding.setWritePath(new WritePath(writePath)); + commBinding.setReadPath(new ReadPath(readPath)); + map.getCommBindList().add(commBinding); + } + System.out.println("Finished."); + } + + /** + * + * @param processName + * @return + */ + protected int getProcessIndex(String processName) { + for (int i = 0; i < _processes.length; i++) { + if (_processes[i].equals(processName)) { + return i; + } + } + System.out.println("Error: Could not find process " + + processName + "."); + System.exit(-1); + return 0; + } + + /** + * + * @param map + */ + public void generateMappingXml(Mapping map, String filename) { + System.out.print("Generate mapping XML. "); + StringBuffer buffer = new StringBuffer(); + map.accept(new MapXmlVisitor(buffer)); + try { + java.io.BufferedWriter writer = + new java.io.BufferedWriter( + new java.io.FileWriter(filename)); + writer.write(buffer.toString()); + writer.close(); + } catch (java.io.IOException e) { + System.out.println("Could not write XML file."); + System.exit(-1); + } + System.out.println("Finished."); + } + + /** + * + * @param args + */ + public static void main(String args[]) { + /* + Pattern pattern = Pattern.compile("tile_(\\d)\\."); + Matcher matcher = pattern.matcher("tile_3."); + matcher.matches(); + System.out.println(matcher.group(1)); + */ + + String appName = "exampleAuto"; + String path = System.getProperty("user.dir") + "/" + appName + "/"; + File dir = new File(path); + dir.mkdirs(); + dir = new File(path + "src/"); + dir.mkdirs(); + + System.out.println("Generate application \"" + appName + "\"" + + " in directory \"" + path + "\"."); + ApplicationGenerator appGen = new ApplicationGenerator(); + //appGen.generateIndpendentPipelines2(16, 4, 10000000); + appGen.generatePipeline(64, 10); + //appGen.generateIndependentTasks(64); + ProcessNetwork pn = new ProcessNetwork(appName); + appGen.generateProcessNetwork(pn); + appGen.generateProcessNetworkXml(pn, path + appName + ".xml"); + appGen.generateCode(pn, path + "src/"); + Mapping map = new Mapping("mapping"); + appGen.generateMapping(pn, map); + appGen.generateMappingXml(map, path + "mapping.xml"); + System.out.println("Done."); + } +} \ No newline at end of file