dol: initial dol commit
[jump.git] / dol / src / dol / util / ApplicationGenerator.java
1 /* $Id: ApplicationGenerator.java 1 2010-02-24 13:03:05Z haidw $ */\r
2 package dol.util;\r
3 \r
4 import java.io.BufferedWriter;\r
5 import java.io.File;\r
6 import java.io.FileWriter;\r
7 import java.util.Random;\r
8 import java.util.Vector;\r
9 import java.util.regex.Matcher;\r
10 import java.util.regex.Pattern;\r
11 \r
12 import dol.datamodel.architecture.Processor;\r
13 import dol.datamodel.architecture.ReadPath;\r
14 import dol.datamodel.architecture.WritePath;\r
15 import dol.datamodel.mapping.CommunicationBinding;\r
16 import dol.datamodel.mapping.ComputationBinding;\r
17 import dol.datamodel.mapping.Mapping;\r
18 import dol.datamodel.mapping.Schedule;\r
19 import dol.datamodel.mapping.ScheduleEntry;\r
20 import dol.datamodel.mapping.SchedulingPolicy;\r
21 import dol.datamodel.pn.Channel;\r
22 import dol.datamodel.pn.Connection;\r
23 import dol.datamodel.pn.Port;\r
24 import dol.datamodel.pn.Process;\r
25 import dol.datamodel.pn.ProcessNetwork;\r
26 import dol.datamodel.pn.ProfilingConfiguration;\r
27 import dol.datamodel.pn.SourceCode;\r
28 import dol.visitor.xml.MapXmlVisitor;\r
29 import dol.visitor.xml.PNXmlVisitor;\r
30 \r
31 /**\r
32  *\r
33  */\r
34 public class ApplicationGenerator {\r
35     /*\r
36     protected String _processes[] =\r
37         {"producer", "consumer"};\r
38 \r
39     protected int _computation[] =\r
40         {1000, 1000};\r
41 \r
42     protected int _communication[][] =\r
43         {{0, 4}, {-4, 0}};\r
44 \r
45     protected String _binding[] =\r
46         {"tile_0.arm", "tile_0.arm"};\r
47 \r
48     protected int _iterations = 1000;\r
49     */\r
50 \r
51     /*\r
52     protected String _processes[] =\r
53         {"producer", "forwarder", "consumer"};\r
54 \r
55     protected int _computation[] =\r
56         {1000, 1000, 1000};\r
57 \r
58     protected int _communication[][] =\r
59         {{ 0,  4,  0},\r
60          {-4,  0,  4},\r
61          { 0, -4,  0}};\r
62 \r
63     protected String _binding[] =\r
64         {"tile_0.arm", "tile_0.arm", "tile_0.arm"};\r
65 \r
66     protected int _iterations = 10;\r
67     */\r
68 \r
69     /*\r
70     protected String _processes[] =\r
71         {"stageA", "stageB", "stageC", "stageD",\r
72          "stageE", "stageF", "stageG", "stageH",\r
73          "stageI", "stageJ", "stageK", "stageL",\r
74          "stageM", "stageN", "stageO", "stageP"};\r
75 \r
76     protected int _computation[] =\r
77         {1, 2, 3, 4, 5, 6, 7, 8,\r
78          9, 10, 11, 12, 13, 14, 15, 16};\r
79 \r
80     protected int _communication[][] =\r
81         {{    0,  1024,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0},\r
82          {-1024,     0,  1024,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0},\r
83          {    0, -1024,     0,  1024,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0},\r
84          {    0,     0, -1024,     0,  1024,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0},\r
85          {    0,     0,     0, -1024,     0,  1024,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0},\r
86          {    0,     0,     0,     0, -1024,     0,  1024,     0,     0,     0,     0,     0,     0,     0,     0,     0},\r
87          {    0,     0,     0,     0,     0, -1024,     0,  1024,     0,     0,     0,     0,     0,     0,     0,     0},\r
88          {    0,     0,     0,     0,     0,     0, -1024,     0,  1024,     0,     0,     0,     0,     0,     0,     0},\r
89          {    0,     0,     0,     0,     0,     0,     0, -1024,     0,  1024,     0,     0,     0,     0,     0,     0},\r
90          {    0,     0,     0,     0,     0,     0,     0,     0, -1024,     0,  1024,     0,     0,     0,     0,     0},\r
91          {    0,     0,     0,     0,     0,     0,     0,     0,     0, -1024,     0,  1024,     0,     0,     0,     0},\r
92          {    0,     0,     0,     0,     0,     0,     0,     0,     0,     0, -1024,     0,  1024,     0,     0,     0},\r
93          {    0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0, -1024,     0,  1024,     0,     0},\r
94          {    0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0, -1024,     0,  1024,     0},\r
95          {    0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0, -1024,     0,  1024},\r
96          {    0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0, -1024,     0}};\r
97 \r
98 \r
99     //protected String _binding[] =\r
100     //    {"tile_0.arm", "tile_0.arm", "tile_1.arm", "tile_1.arm",\r
101     //     "tile_2.arm", "tile_2.arm", "tile_3.arm", "tile_3.arm",\r
102     //     "tile_4.arm", "tile_4.arm", "tile_5.arm", "tile_5.arm",\r
103     //     "tile_6.arm", "tile_6.arm", "tile_7.arm", "tile_7.arm"};\r
104 \r
105     //protected String _binding[] =\r
106     //    {"tile_0.arm", "tile_1.arm", "tile_2.arm", "tile_3.arm",\r
107     //     "tile_4.arm", "tile_5.arm", "tile_6.arm", "tile_7.arm",\r
108     //     "tile_0.arm", "tile_1.arm", "tile_2.arm", "tile_3.arm",\r
109     //     "tile_4.arm", "tile_5.arm", "tile_6.arm", "tile_7.arm"};\r
110 \r
111     protected String _binding[] =\r
112         {"tile_0.arm", "tile_1.arm", "tile_2.arm", "tile_3.arm",\r
113          "tile_4.arm", "tile_5.arm", "tile_6.arm", "tile_7.arm",\r
114          "tile_7.arm", "tile_6.arm", "tile_5.arm", "tile_4.arm",\r
115          "tile_3.arm", "tile_2.arm", "tile_1.arm", "tile_0.arm"};\r
116 \r
117     protected int _iterations = 10;\r
118     */\r
119 \r
120     protected String _processes[] =\r
121         {"splitAA", "splitBA", "splitBB",\r
122          "pipeAA", "pipeBA", "pipeCA", "pipeDA",\r
123          "pipeEA", "pipeFA", "pipeAB", "pipeBB",\r
124          "pipeCB", "pipeDB", "pipeEB", "pipeFB",\r
125          "mergeBA", "mergeBB", "mergeAA"};\r
126 \r
127     protected int _armComputation[] =\r
128         {1, 2, 3, 4, 5, 6, 7, 8,\r
129          9, 10, 11, 12, 13, 14, 15, 16, 17, 18};\r
130     protected int _magicComputation[] =\r
131         {1, 2, 3, 4, 5, 6, 7, 8,\r
132          9, 10, 11, 12, 13, 14, 15, 16, 17, 18};\r
133 \r
134 \r
135     protected int _communication[][] =\r
136         {{    0,     4,     4,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0},\r
137          {   -4,     0,     0,     4,     4,     4,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0},\r
138          {   -4,     0,     0,     0,     0,     0,     4,     4,     4,     0,     0,     0,     0,     0,     0,     0,     0,     0},\r
139          {    0,    -4,     0,     0,     0,     0,     0,     0,     0,     4,     0,     0,     0,     0,     0,     0,     0,     0},\r
140          {    0,    -4,     0,     0,     0,     0,     0,     0,     0,     0,     4,     0,     0,     0,     0,     0,     0,     0},\r
141          {    0,    -4,     0,     0,     0,     0,     0,     0,     0,     0,     0,     4,     0,     0,     0,     0,     0,     0},\r
142          {    0,     0,    -4,     0,     0,     0,     0,     0,     0,     0,     0,     0,     4,     0,     0,     0,     0,     0},\r
143          {    0,     0,    -4,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     4,     0,     0,     0,     0},\r
144          {    0,     0,    -4,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     4,     0,     0,     0},\r
145          {    0,     0,     0,    -4,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     4,     0,     0},\r
146          {    0,     0,     0,     0,    -4,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     4,     0,     0},\r
147          {    0,     0,     0,     0,     0,    -4,     0,     0,     0,     0,     0,     0,     0,     0,     0,     4,     0,     0},\r
148          {    0,     0,     0,     0,     0,     0,    -4,     0,     0,     0,     0,     0,     0,     0,     0,     0,     4,     0},\r
149          {    0,     0,     0,     0,     0,     0,     0,    -4,     0,     0,     0,     0,     0,     0,     0,     0,     4,     0},\r
150          {    0,     0,     0,     0,     0,     0,     0,     0,    -4,     0,     0,     0,     0,     0,     0,     0,     4,     0},\r
151          {    0,     0,     0,     0,     0,     0,     0,     0,     0,    -4,    -4,    -4,     0,     0,     0,     0,     0,     4},\r
152          {    0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,    -4,    -4,    -4,     0,     0,     4},\r
153          {    0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,    -4,    -4,     0}};\r
154 \r
155 \r
156     //working\r
157     protected String _binding[] =\r
158         {"tile_0.arm", "tile_1.arm", "tile_4.arm",\r
159          "tile_1.arm", "tile_2.arm", "tile_3.arm", "tile_4.arm",\r
160          "tile_5.arm", "tile_6.arm", "tile_1.arm", "tile_2.arm",\r
161          "tile_3.arm", "tile_4.arm", "tile_5.arm", "tile_6.arm",\r
162          "tile_3.arm", "tile_7.arm", "tile_0.arm"};\r
163 \r
164     /*\r
165     protected String _binding[] =\r
166         {"tile_0.arm", "tile_1.arm", "tile_3.arm",\r
167          "tile_1.arm", "tile_1.arm", "tile_2.arm", "tile_2.arm",\r
168          "tile_3.arm", "tile_3.arm", "tile_4.arm", "tile_4.arm",\r
169          "tile_5.arm", "tile_5.arm", "tile_6.arm", "tile_6.arm",\r
170          "tile_7.arm", "tile_7.arm", "tile_0.arm"};\r
171 \r
172     protected String _binding[] =\r
173         {"tile_0.arm", "tile_1.arm", "tile_7.arm",\r
174          "tile_1.arm", "tile_1.arm", "tile_2.arm", "tile_2.arm",\r
175          "tile_3.arm", "tile_3.arm", "tile_4.arm", "tile_4.arm",\r
176          "tile_5.arm", "tile_5.arm", "tile_6.arm", "tile_6.arm",\r
177          "tile_7.arm", "tile_3.arm", "tile_0.arm"};\r
178     */\r
179 \r
180     //not working\r
181     /*\r
182     protected String _binding[] =\r
183         {"tile_0.arm", "tile_1.arm", "tile_2.arm", "tile_3.arm",\r
184          "tile_4.arm", "tile_5.arm", "tile_6.arm", "tile_7.arm",\r
185          "tile_7.arm", "tile_6.arm", "tile_5.arm", "tile_4.arm",\r
186          "tile_3.arm", "tile_2.arm", "tile_1.arm", "tile_0.arm",\r
187          "tile_0.arm", "tile_0.arm"};\r
188     */\r
189 \r
190     protected int _iterations = 10;\r
191 \r
192     public String getStringEncoding(int i, int total) {\r
193         if (total > 26 * 26) {\r
194             System.out.println("\"total\" needs to be less than "\r
195                     + 26 * 26 + ".");\r
196             return "";\r
197         } else if (total <= 26) {\r
198             return  Character.toString((char)('A' + (i % 26)));\r
199         }\r
200         return Character.toString((char)('A' + (i / 26)))\r
201             + Character.toString((char)('A' + (i % 26)));\r
202     }\r
203 \r
204     /**\r
205      *\r
206      * @param stages\r
207      * @param communication\r
208      */\r
209     public void generateIndependentTasks(int numberOfTasks) {\r
210           _processes = new String[numberOfTasks];\r
211           _armComputation = new int[numberOfTasks];\r
212           _magicComputation = new int[numberOfTasks];\r
213           _communication = new int[numberOfTasks][numberOfTasks];\r
214           _binding = new String[numberOfTasks];\r
215           _iterations = 1000;\r
216 \r
217           for (int i = 0; i < numberOfTasks; i++) {\r
218               _processes[i] = ((i < numberOfTasks / 2) ? "armProcess"\r
219                   : "magicProcess")\r
220                   + getStringEncoding(i, numberOfTasks);\r
221               System.out.println(_processes[i]);\r
222               _armComputation[i] =\r
223                   (i < numberOfTasks / 2) ? 100000000 : 200000000;\r
224               _magicComputation[i] =\r
225                   (i < numberOfTasks / 2) ? 200000000 : 100000000;\r
226               _binding[i] = (i < numberOfTasks / 2) ? "tile_0.arm"\r
227                       : "tile_0.magic";\r
228           }\r
229 \r
230           //no communication\r
231           for (int i = 0; i < numberOfTasks; i++) {\r
232               for (int j = 0; j < numberOfTasks; j++) {\r
233                   _communication[i][j] = 0;\r
234               }\r
235           }\r
236     }\r
237 \r
238     /**\r
239      *\r
240      * @param pipelines\r
241      * @param stages\r
242      * @param communication\r
243      */\r
244     public void generateIndpendentPipelines(int pipelines, int stages, int communication) {\r
245         Random random = new Random();\r
246         _processes = new String[stages * pipelines];\r
247         _armComputation = new int[stages * pipelines];\r
248         _magicComputation = new int[stages * pipelines];\r
249         _communication = new int[stages * pipelines][stages * pipelines];\r
250         _binding = new String[stages * pipelines];\r
251         _iterations = 1000;\r
252 \r
253         for (int i = 0; i < pipelines; i++) {\r
254             for (int j = 0; j < stages; j++) {\r
255                _processes[i * stages + j] =\r
256                    ((i < pipelines / 2) ? "arm" : "magic")\r
257                    + "stage"\r
258                    + getStringEncoding(i, pipelines) + "x"\r
259                    + getStringEncoding(j, stages);\r
260                System.out.println(_processes[i * stages + j]);\r
261               _armComputation[i * stages + j] =\r
262                   0 * random.nextInt(10000000) +\r
263                   ((i < pipelines / 2) ? 100000000 : 200000000);\r
264               _magicComputation[i * stages + j] =\r
265                   0 * random.nextInt(10000000) +\r
266                   ((i < pipelines / 2) ? 200000000 : 100000000);\r
267               _binding[i * stages + j] = "tile_0.arm";\r
268             }\r
269         }\r
270 \r
271         for (int i = 0; i < pipelines; i++) {\r
272             for (int j = 0; j < stages; j++) {\r
273                 for (int k = 0; k < pipelines; k++) {\r
274                     for (int l = 0; l < stages; l++) {\r
275                         _communication[i * stages + j][k * stages + l] = 0;\r
276                         if (i == k && l == j + 1) {\r
277                             _communication[i * stages + j][k * stages + l] = communication;\r
278                         } else if (i == k && j == l + 1) {\r
279                             _communication[i * stages + j][k * stages + l] = -communication;\r
280                         }\r
281                     }\r
282                 }\r
283             }\r
284         }\r
285     }\r
286 \r
287 \r
288     /**\r
289      *\r
290      * @param pipelines\r
291      * @param stages\r
292      * @param communication\r
293      */\r
294     public void generateIndpendentPipelines2(int pipelines, int stages, int communication) {\r
295         Random random = new Random();\r
296         _processes = new String[stages * pipelines];\r
297         _armComputation = new int[stages * pipelines];\r
298         _magicComputation = new int[stages * pipelines];\r
299         _communication = new int[stages * pipelines][stages * pipelines];\r
300         _binding = new String[stages * pipelines];\r
301         _iterations = 1000;\r
302 \r
303         for (int i = 0; i < pipelines; i++) {\r
304             for (int j = 0; j < stages; j++) {\r
305                _processes[i * stages + j] =\r
306                    ((j < stages / 2) ? "arm" : "magic")\r
307                    + "stage"\r
308                    + getStringEncoding(i, pipelines) + "x"\r
309                    + getStringEncoding(j, stages);\r
310                System.out.println(_processes[i * stages + j]);\r
311               _armComputation[i * stages + j] =\r
312                   0 * random.nextInt(10000000) +\r
313                   ((j < stages / 2) ? 100000000 : 200000000);\r
314               _magicComputation[i * stages + j] =\r
315                   0 * random.nextInt(10000000) +\r
316                   ((j < stages / 2) ? 200000000 : 100000000);\r
317               _binding[i * stages + j] = "tile_0.arm";\r
318             }\r
319         }\r
320 \r
321         for (int i = 0; i < pipelines; i++) {\r
322             for (int j = 0; j < stages; j++) {\r
323                 for (int k = 0; k < pipelines; k++) {\r
324                     for (int l = 0; l < stages; l++) {\r
325                         _communication[i * stages + j][k * stages + l] = 0;\r
326                         if (i == k && l == j + 1) {\r
327                             _communication[i * stages + j][k * stages + l] = communication;\r
328                         } else if (i == k && j == l + 1) {\r
329                             _communication[i * stages + j][k * stages + l] = -communication;\r
330                         }\r
331                     }\r
332                 }\r
333             }\r
334         }\r
335     }\r
336 \r
337 \r
338     /**\r
339      *\r
340      * @param stages\r
341      * @param communication\r
342      */\r
343     public void generatePipeline(int stages, int communication) {\r
344         _processes = new String[stages];\r
345         _armComputation = new int[stages];\r
346         _magicComputation = new int[stages];\r
347         _communication = new int[stages][stages];\r
348         _binding = new String[stages];\r
349         //_iterations = 1000;\r
350         _iterations = 1000;\r
351 \r
352         for (int i = 0; i < stages; i++) {\r
353             _processes[i] = ((i % 8 < 4) ? "arm" : "magic")\r
354                 + "stage" + getStringEncoding(i, stages);\r
355             System.out.println(_processes[i]);\r
356             _armComputation[i] =\r
357                 (i % 8 < 4) ? 100 : 200;\r
358             _magicComputation[i] =\r
359                 (i % 8 < 4) ? 200 : 100;\r
360             _binding[i] = "tile_0.arm";\r
361         }\r
362 \r
363         for (int i = 0; i < stages; i++) {\r
364             for (int j = 0; j < stages; j++) {\r
365                 _communication[i][j] = 0;\r
366                 if (j == i + 1) {\r
367                     _communication[i][j] = communication;\r
368                 } else if (i == j + 1) {\r
369                     _communication[i][j] = -communication;\r
370                 }\r
371             }\r
372         }\r
373     }\r
374 \r
375 \r
376     /**\r
377      *\r
378      * @param pn\r
379      */\r
380     public void generateProcessNetwork(ProcessNetwork pn) {\r
381         System.out.print("Generate process network. ");\r
382         int numberOfProcesses = 0;\r
383         int numberOfChannels = 0;\r
384 \r
385         for (int j = 0; j < _processes.length; j++) {\r
386             String processName = _processes[j];\r
387             Process p = new Process(processName);\r
388             SourceCode srcCode = new SourceCode(processName);\r
389             srcCode.setLocality(processName + ".c");\r
390             srcCode.setType("c");\r
391             p.getSrcList().add(srcCode);\r
392             pn.getProcessList().add(p);\r
393             numberOfProcesses++;\r
394             for (int i = 0; i < 8; i++) {\r
395                 ProfilingConfiguration c = new ProfilingConfiguration(\r
396                         "BCED tile_" + i + ".arm");\r
397                 c.setValue(Integer.toString(_armComputation[j]));\r
398                 p.getProfilingList().add(c);\r
399                 c = new ProfilingConfiguration(\r
400                         "BCED tile_" + i + ".magic");\r
401                 c.setValue(Integer.toString(_magicComputation[j]));\r
402                 p.getProfilingList().add(c);\r
403             }\r
404         }\r
405 \r
406         //check whether communication matrix is symmetric\r
407         if (_communication[0].length != numberOfProcesses) {\r
408             System.out.println();\r
409             System.out.println("Error: Size of communication matrix ("\r
410                     + _communication[0].length + " columns) must match "\r
411                     + "number of processes (" + numberOfProcesses + ").");\r
412             System.exit(-1);\r
413         }\r
414         for (int row = 0; row < numberOfProcesses; row++) {\r
415             for (int col = 0; col < numberOfProcesses; col++) {\r
416                 if (row == col && _communication[row][col] != 0) {\r
417                     System.out.println();\r
418                     System.out.println("Error: No self-channels allowed. "\r
419                             + "Problem for process " + row + " ("\r
420                             + _processes[row] + ").");\r
421                     System.exit(-1);\r
422                 }\r
423                 if (_communication[row][col] !=\r
424                     -_communication[col][row]) {\r
425                     System.out.println();\r
426                     System.out.println("Error: Communication matrix "\r
427                             + "needs to be symmetric. Mismatch for row "\r
428                             + row + " column " + col + ".");\r
429                     System.exit(-1);\r
430                 }\r
431             }\r
432         }\r
433 \r
434         for (int row = 0; row < numberOfProcesses; row++) {\r
435             for (int col = row; col < numberOfProcesses; col++) {\r
436                 if (_communication[row][col] == 0) {\r
437                     continue;\r
438                 }\r
439                 int src = row;\r
440                 int dst = col;\r
441 \r
442                 if (_communication[row][col] < 0) {\r
443                     src = col;\r
444                     dst = row;\r
445                 }\r
446 \r
447                 Channel c = new Channel("channel" + _processes[src]\r
448                         + _processes[dst]);\r
449                 c.setSize(Math.abs(2 * _communication[src][dst]));\r
450                 c.setType("fifo");\r
451                 ProfilingConfiguration p = new ProfilingConfiguration(\r
452                         "TotalReadData");\r
453                 p.setValue(Integer.toString(_communication[row][col]));\r
454                 c.getProfilingList().add(p);\r
455                 pn.getChannelList().add(c);\r
456                 numberOfChannels++;\r
457 \r
458                 Port channelPort0 = new Port("0", Port.INPORT);\r
459                 Port channelPort1 = new Port("1", Port.OUTPORT);\r
460                 c.getPortList().add(channelPort0);\r
461                 c.getPortList().add(channelPort1);\r
462 \r
463                 Connection c1 = new Connection(\r
464                         "c1" + _processes[src] + _processes[dst]);\r
465                 Connection c2 = new Connection(\r
466                         "c2" + _processes[src] + _processes[dst]);\r
467                 pn.getConnectionList().add(c1);\r
468                 pn.getConnectionList().add(c2);\r
469 \r
470                 Port p0 = new Port(Integer.toString(pn.getProcess(\r
471                         _processes[src]).getPortList().size()),\r
472                         Port.OUTPORT);\r
473                 Port p1 = new Port(Integer.toString(pn.getProcess(\r
474                         _processes[dst]).getPortList().size()),\r
475                         Port.INPORT);\r
476                 pn.getProcess(_processes[src]).getPortList().add(p0);\r
477                 pn.getProcess(_processes[dst]).getPortList().add(p1);\r
478 \r
479                 c1.setOriginPort(p0);\r
480                 c1.setTargetPort(channelPort0);\r
481                 c1.setOrigin(pn.getProcess(_processes[src]));\r
482                 c1.setTarget(c);\r
483                 p0.setPeerResource(c);\r
484                 p0.setPeerPort(channelPort0);\r
485                 channelPort0.setPeerResource(pn.getProcess(\r
486                         _processes[src]));\r
487                 channelPort0.setPeerPort(p0);\r
488 \r
489                 c2.setOriginPort(channelPort1);\r
490                 c2.setTargetPort(p1);\r
491                 c2.setOrigin(c);\r
492                 c2.setTarget(pn.getProcess(_processes[dst]));\r
493                 channelPort1.setPeerResource(pn.getProcess(\r
494                         _processes[dst]));\r
495                 channelPort1.setPeerPort(p1);\r
496                 p1.setPeerResource(c);\r
497                 p1.setPeerPort(channelPort1);\r
498 \r
499                 c.setOrigin(pn.getProcess(_processes[src]));\r
500                 c.setTarget(pn.getProcess(_processes[dst]));\r
501             }\r
502         }\r
503 \r
504         System.out.println("Finished.");\r
505     }\r
506 \r
507     /**\r
508      *\r
509      * @param pn\r
510      */\r
511     public void generateProcessNetworkXml(ProcessNetwork pn,\r
512             String filename) {\r
513         System.out.print("Generate process network XML. ");\r
514         StringBuffer buffer = new StringBuffer();\r
515         pn.accept(new PNXmlVisitor(buffer));\r
516         try {\r
517             java.io.BufferedWriter writer =\r
518                     new java.io.BufferedWriter(\r
519                     new java.io.FileWriter(filename));\r
520             writer.write(buffer.toString());\r
521             writer.close();\r
522         } catch (java.io.IOException e) {\r
523             System.out.println();\r
524             System.out.println("Error: Could not write XML file.");\r
525             System.exit(-1);\r
526         }\r
527         System.out.println("Finished.");\r
528     }\r
529 \r
530     /**\r
531      *\r
532      * @param pn\r
533      * @param path\r
534      */\r
535     public void generateCode(ProcessNetwork pn, String path) {\r
536         System.out.print("Generate source code of processes. ");\r
537 \r
538         String newline = System.getProperty("line.separator");\r
539         int maxProcessNameLength = 0;\r
540         for (String processName : _processes) {\r
541             maxProcessNameLength = Math.max(maxProcessNameLength,\r
542                     processName.length());\r
543         }\r
544 \r
545         String global = "";\r
546         global += "#ifndef GLOBAL_H" + newline;\r
547         global += "#define GLOBAL_H" + newline;\r
548         global += newline;\r
549 \r
550         global += "#define ITERATIONS    " + _iterations + newline;\r
551         global += newline;\r
552 \r
553         for (int i = 0; i < _processes.length; i++) {\r
554             global += "#define LOOPS_ARM_" + _processes[i].toUpperCase()\r
555                 + "    " + _armComputation[i] + newline;\r
556             global += "#define LOOPS_MAGIC_" + _processes[i].toUpperCase()\r
557                 + "    " + _magicComputation[i] + newline;\r
558         }\r
559 \r
560         for (int row = 0; row < _communication[0].length; row++) {\r
561             for (int col = row; col < _communication[0].length; col++) {\r
562                 if (_communication[row][col] == 0) {\r
563                     continue;\r
564                 }\r
565                 int src = row;\r
566                 int dst = col;\r
567 \r
568                 if (_communication[row][col] < 0) {\r
569                     src = col;\r
570                     dst = row;\r
571                 }\r
572 \r
573                 global += "#define TOKEN_SIZE_"\r
574                         + _processes[src].toUpperCase() + "_"\r
575                         + _processes[dst].toUpperCase() + "    "\r
576                         + _communication[src][dst] + newline;\r
577             }\r
578         }\r
579         global += newline;\r
580         global += "#endif" + newline;\r
581 \r
582         try {\r
583             BufferedWriter out = new BufferedWriter(\r
584                     new FileWriter(path + "global.h"));\r
585             out.write(global);\r
586             out.close();\r
587         } catch (Exception e) {\r
588             System.out.println();\r
589             System.out.println("Error: While writing global.h file.");\r
590             System.exit(-1);\r
591         }\r
592 \r
593 \r
594         for (Process p : pn.getProcessList()) {\r
595             String state = p.getName().substring(0, 1).toUpperCase()\r
596                     + p.getName().substring(1) + "_State";\r
597 \r
598             String h = "";\r
599             h += "#ifndef " + p.getName().toUpperCase() + "_H" + newline;\r
600             h += "#define " + p.getName().toUpperCase() + "_H" + newline;\r
601             h += newline;\r
602             h += "#include <dol.h>" + newline;\r
603             h += newline;\r
604             for (Port port : p.getPortList()) {\r
605                 h += "#define PORT_" + p.getName().toUpperCase() + "_"\r
606                        + (port.isInPort() ? "IN_" : "OUT_" )\r
607                        + port.getName() + " " + port.getName() + newline;\r
608             }\r
609             h += newline;\r
610             h += "typedef struct {" + newline;\r
611             h += "    int iterations;" + newline;\r
612             h += "} " + state + ";" + newline;\r
613             h += newline;\r
614             h += "void " + p.getName() + "_init(DOLProcess *p);"\r
615                     + newline;\r
616             h += "int " + p.getName() + "_fire(DOLProcess *p);" + newline;\r
617             h += newline;\r
618             h += "#endif" + newline;\r
619 \r
620             String c = "";\r
621             String printPrefix = String.format("    printf(\"[%-"\r
622                     + maxProcessNameLength + "s] ", p.getName());\r
623             c += "#include <stdio.h>" + newline;\r
624             c += newline;\r
625             c += "#include \"" + p.getName() + ".h\"" + newline;\r
626             c += "#include \"global.h\"" + newline;\r
627             c += newline;\r
628             c += "void " + p.getName() + "_init(DOLProcess *p) {"\r
629                     + newline;\r
630             c += "    " + state + " *state = (" + state + "*)p->local;"\r
631                     + newline;\r
632             c += "    state->iterations = 0;" + newline;\r
633             c += "}" + newline;\r
634             c += newline;\r
635             c += "int " + p.getName() + "_fire(DOLProcess *p) {"\r
636                     + newline;\r
637 \r
638             int processIndex = getProcessIndex(p.getName());\r
639             int maxBuffer = 0;\r
640             for (int i = 0; i < _communication[processIndex].length; i++) {\r
641                 maxBuffer = Math.max(maxBuffer,\r
642                         Math.abs(_communication[processIndex][i]));\r
643             }\r
644             c += "    int buffer[" + ((maxBuffer + (4 - 1)) / 4) + "];" + newline;\r
645             c += "    int i;" + newline;\r
646             c += "    " + state + " *state = (" + state + "*)p->local;";\r
647             c += newline + newline;\r
648 \r
649             for (Port port : p.getPortList()) {\r
650                 if (port.isInPort()) {\r
651                     int originIndex = getProcessIndex(\r
652                         ((Channel)port.getPeerResource()).getOrigin()\r
653                         .getName());\r
654                     String tokenSize = "TOKEN_SIZE_"\r
655                         + _processes[originIndex].toUpperCase() + "_"\r
656                         + _processes[processIndex].toUpperCase();\r
657                     c += printPrefix + "read %d bytes from "\r
658                         + _processes[originIndex] + ".\\n\", " + tokenSize\r
659                         + ");" + newline;\r
660                     c += "    DOL_read((void*)"\r
661                         + "PORT_" + p.getName().toUpperCase() + "_IN_"\r
662                         + port.getName() + ", buffer, " + tokenSize\r
663                         + ", p);" + newline;\r
664                 }\r
665             }\r
666             c += newline;\r
667             //c += "    printf(\"[%-20s ] iteration %d/n\", \""\r
668             //        + p.getName() + "\", state->iterations);" + newline;\r
669             c += printPrefix + "iteration %d.\\n\" , "\r
670                     + "state->iterations);" + newline;\r
671             c += newline;\r
672             c += "#ifdef __arm__" + newline;\r
673             c += "    for (i = 0; i < LOOPS_ARM_" + p.getName().toUpperCase()\r
674                 + "; i++) { ; }" + newline;\r
675             c += "#else" + newline;\r
676             c += "    for (i = 0; i < LOOPS_MAGIC_" + p.getName().toUpperCase()\r
677                 + "; i++) { ; }" + newline;\r
678             c += "#endif" + newline;\r
679             for (Port port : p.getPortList()) {\r
680                 if (port.isOutPort()) {\r
681                     int targetIndex = getProcessIndex(\r
682                             ((Channel)port.getPeerResource()).getTarget()\r
683                             .getName());\r
684                     String tokenSize = "TOKEN_SIZE_"\r
685                         + _processes[processIndex].toUpperCase() + "_"\r
686                         + _processes[targetIndex].toUpperCase();\r
687                     c += printPrefix + "write %d bytes to "\r
688                         + _processes[targetIndex] + ".\\n\", " + tokenSize\r
689                         + ");" + newline;\r
690                     c += "    DOL_write((void*)"\r
691                         + "PORT_" + p.getName().toUpperCase() + "_OUT_"\r
692                         + port.getName() + ", buffer, "+ tokenSize\r
693                         + ", p);" + newline;\r
694                 }\r
695             }\r
696             c += newline;\r
697             c += "    state->iterations++;" + newline;\r
698             c += "    if (state->iterations >= ITERATIONS) {"\r
699                     + newline;\r
700             c += "        DOL_detach(p);" + newline;\r
701             c += "        return -1;" + newline;\r
702             c += "    }" + newline;\r
703             c += newline;\r
704             c += "    return 0;" + newline;\r
705             c += "}" + newline;\r
706 \r
707             try {\r
708                 BufferedWriter out = new BufferedWriter(\r
709                         new FileWriter(path + p.getName() + ".h"));\r
710                 out.write(h);\r
711                 out.close();\r
712                 out = new BufferedWriter(\r
713                         new FileWriter(path + p.getName() + ".c"));\r
714                 out.write(c);\r
715                 out.close();\r
716             } catch (Exception e) {\r
717                 System.out.println();\r
718                 System.out.println("Error: While writing source files.");\r
719                 System.exit(-1);\r
720             }\r
721         }\r
722         System.out.println("Finished.");\r
723     }\r
724 \r
725 \r
726     /**\r
727      *\r
728      * @param map\r
729      * @param pn\r
730      */\r
731     public void generateMapping(ProcessNetwork pn, Mapping map) {\r
732         System.out.print("Generate mapping. ");\r
733         map.setPN(pn);\r
734 \r
735         //binding of processes\r
736         Vector<Processor> processors = new Vector<Processor>();\r
737         for (int i = 0; i < _binding.length; i++) {\r
738             String processName =  _processes[i];\r
739             String processorName = _binding[i];\r
740             Processor processor = null;\r
741 \r
742             boolean processorExists = false;\r
743             for (Processor proc : processors) {\r
744                 if (proc.getName().equals(processorName)) {\r
745                     processor = proc;\r
746                     processorExists = true;\r
747                     break;\r
748                 }\r
749             }\r
750 \r
751             if (!processorExists) {\r
752                 processor = new Processor(processorName);\r
753                 processors.add(processor);\r
754             }\r
755 \r
756             ComputationBinding compBinding = new ComputationBinding(\r
757                     processName + "binding");\r
758             compBinding.setProcess(pn.getProcess(processName));\r
759             compBinding.setProcessor(processor);\r
760             processor.getProcessList().add(pn.getProcess(processName));\r
761             map.getCompBindList().add(compBinding);\r
762         }\r
763 \r
764         //schedules\r
765         for (Processor p : processors) {\r
766             Schedule s = new Schedule(p.getName() + "schedule");\r
767             s.setSchedPolicy(SchedulingPolicy.FIFO);\r
768             s.setResource(p);\r
769             for (Process process : p.getProcessList()) {\r
770                 ScheduleEntry entry = new ScheduleEntry(\r
771                         process.getName());\r
772                 entry.setConsumer(process);\r
773                 s.getEntryList().add(entry);\r
774             }\r
775             map.getScheduleList().add(s);\r
776         }\r
777 \r
778         //binding of channels\r
779         for (Channel c : pn.getChannelList()) {\r
780             String originProcess = c.getOrigin().getName();\r
781             String targetProcess = c.getTarget().getName();\r
782 \r
783             String originBinding = _binding[getProcessIndex(\r
784                     originProcess)];\r
785             String targetBinding = _binding[getProcessIndex(\r
786                     targetProcess)];\r
787 \r
788             Pattern pattern0 = Pattern.compile(\r
789                     "tile_(\\d)\\.(arm)|tile_(\\d)\\.(magic)");\r
790             Matcher matcher0 = pattern0.matcher(originBinding);\r
791             Pattern pattern1 = Pattern.compile(\r
792                     "tile_(\\d)\\.(arm)|tile_(\\d)\\.(magic)");\r
793             Matcher matcher1 = pattern1.matcher(targetBinding);\r
794 \r
795             if (!matcher0.matches() || !matcher1.matches()) {\r
796                 System.out.println("binding needs to comply to the"\r
797                         + " following regular expression: "\r
798                         + "tile_(\\d)\\.(arm)|tile_(\\d)\\.(magic)");\r
799                 System.exit(-1);\r
800             }\r
801 \r
802             int srcIndex = Integer.valueOf(matcher0.group(1));\r
803             int dstIndex = Integer.valueOf(matcher1.group(1));\r
804             String srcProcessor = matcher0.group(2);\r
805             String dstProcessor = matcher1.group(2);\r
806 \r
807             String writePath = "";\r
808             String readPath = "";\r
809 \r
810             if (srcIndex == dstIndex) {\r
811                 if (srcProcessor.equals("arm")) {\r
812                     writePath = "tile_" + srcIndex + ".rdmtodxm";\r
813                 } else {\r
814                     writePath = "tile_" + srcIndex + ".ddmtodxm";\r
815                 }\r
816             } else {\r
817                 if (srcProcessor.equals("arm")) {\r
818                     writePath = "tile_" + srcIndex + ".rdmtodnp_"\r
819                             + dstIndex;\r
820                 } else {\r
821                     writePath = "tile_" + srcIndex + ".ddmtodnp_"\r
822                             + dstIndex;\r
823                 }\r
824             }\r
825 \r
826             if (dstProcessor.equals("arm")) {\r
827                 readPath = "tile_" + dstIndex + ".rdmfromdxm";\r
828             } else {\r
829                 readPath = "tile_" + dstIndex + ".ddmfromdxm";\r
830             }\r
831 \r
832             CommunicationBinding commBinding = new CommunicationBinding(\r
833                     c.getName() + "binding");\r
834             commBinding.setChannel(c);\r
835             commBinding.setWritePath(new WritePath(writePath));\r
836             commBinding.setReadPath(new ReadPath(readPath));\r
837             map.getCommBindList().add(commBinding);\r
838         }\r
839         System.out.println("Finished.");\r
840     }\r
841 \r
842     /**\r
843      *\r
844      * @param processName\r
845      * @return\r
846      */\r
847     protected int getProcessIndex(String processName) {\r
848         for (int i = 0; i < _processes.length; i++) {\r
849             if (_processes[i].equals(processName)) {\r
850                 return i;\r
851             }\r
852         }\r
853         System.out.println("Error: Could not find process "\r
854                 + processName + ".");\r
855         System.exit(-1);\r
856         return 0;\r
857     }\r
858 \r
859     /**\r
860      *\r
861      * @param map\r
862      */\r
863     public void generateMappingXml(Mapping map, String filename) {\r
864         System.out.print("Generate mapping XML. ");\r
865         StringBuffer buffer = new StringBuffer();\r
866         map.accept(new MapXmlVisitor(buffer));\r
867         try {\r
868             java.io.BufferedWriter writer =\r
869                     new java.io.BufferedWriter(\r
870                     new java.io.FileWriter(filename));\r
871             writer.write(buffer.toString());\r
872             writer.close();\r
873         } catch (java.io.IOException e) {\r
874             System.out.println("Could not write XML file.");\r
875             System.exit(-1);\r
876         }\r
877         System.out.println("Finished.");\r
878     }\r
879 \r
880     /**\r
881      *\r
882      * @param args\r
883      */\r
884     public static void main(String args[]) {\r
885         /*\r
886         Pattern pattern = Pattern.compile("tile_(\\d)\\.");\r
887         Matcher matcher = pattern.matcher("tile_3.");\r
888         matcher.matches();\r
889         System.out.println(matcher.group(1));\r
890         */\r
891 \r
892         String appName = "exampleAuto";\r
893         String path = System.getProperty("user.dir") + "/" + appName + "/";\r
894         File dir = new File(path);\r
895         dir.mkdirs();\r
896         dir = new File(path + "src/");\r
897         dir.mkdirs();\r
898 \r
899         System.out.println("Generate application \"" + appName + "\""\r
900                 + " in directory \"" + path + "\".");\r
901         ApplicationGenerator appGen = new ApplicationGenerator();\r
902         //appGen.generateIndpendentPipelines2(16, 4, 10000000);\r
903         appGen.generatePipeline(64, 10);\r
904         //appGen.generateIndependentTasks(64);\r
905         ProcessNetwork pn = new ProcessNetwork(appName);\r
906         appGen.generateProcessNetwork(pn);\r
907         appGen.generateProcessNetworkXml(pn, path + appName + ".xml");\r
908         appGen.generateCode(pn, path + "src/");\r
909         Mapping map = new Mapping("mapping");\r
910         appGen.generateMapping(pn, map);\r
911         appGen.generateMappingXml(map, path + "mapping.xml");\r
912         System.out.println("Done.");\r
913     }\r
914 }