|
| 1 | +--- |
| 2 | +title: Patmos |
| 3 | +description: Developing LF Programs for Patmos. |
| 4 | +--- |
| 5 | +# Overview |
| 6 | +Lingua Franca's C-runtime supports [Patmos](https://github.com/t-crest/patmos), |
| 7 | +a bare-metal execution platform that is optimized for time-predictable execution. |
| 8 | +The time-predictability aspect of Patmos makes it easier to obtain a worst-case |
| 9 | +execution time (WCET) for reactions. |
| 10 | + |
| 11 | +### Compiling and Running Reactors |
| 12 | +Patmos can run in an FPGA, but there are also two |
| 13 | +simulators available: |
| 14 | + |
| 15 | +1. `pasim` a software ISA simulator that is written in C++. |
| 16 | +2. `patemu` a cycle-accurate hardware emulator generated from the hardware description. |
| 17 | + |
| 18 | +To execute reactions on Patmos, the [Patmos toolchain](https://github.com/t-crest/patmos) needs |
| 19 | +to be installed. The web page contains a quick start, detailed information including how to |
| 20 | +perform WCET analysis is available in the |
| 21 | +[Patmos Reference Handbook](http://patmos.compute.dtu.dk/patmos_handbook.pdf). |
| 22 | + |
| 23 | +To execute the "hello world" reactor on Patmos use the LF compiler to generate the C code. |
| 24 | +Compile the reactor with the Patmos compiler (in `src-gen`): |
| 25 | + |
| 26 | + patmos-clang Minimal.c -o Minimal.elf |
| 27 | + |
| 28 | +The reactor can be executed on the SW simulator with: |
| 29 | + |
| 30 | + pasim Minimal.elf |
| 31 | + |
| 32 | +As Patmos is a bare metal runtime that has no notion of calendar time, its start time |
| 33 | +is considered the epoch and the following output will be observed: |
| 34 | + |
| 35 | +``` |
| 36 | +Start execution at time Thu Jan 1 00:00:00 1970 |
| 37 | +plus 640000 nanoseconds. |
| 38 | +Hello World. |
| 39 | +Elapsed logical time (in nsec): 0 |
| 40 | +Elapsed physical time (in nsec): 3970000 |
| 41 | +``` |
| 42 | + |
| 43 | +The reactor can also be executed on the hardware emulator of Patmos: |
| 44 | + |
| 45 | + patemu Minimal.elf |
| 46 | + |
| 47 | +This execution is considerably slower than the SW simulator, as the concrete hardware |
| 48 | +of Patmos is simulated cycle-accurate. |
| 49 | + |
| 50 | +### Worst-Case Execution Time Analysis |
| 51 | + |
| 52 | +Following example is a code fragment from |
| 53 | +[Wcet.lf](https://github.com/lf-lang/lingua-franca/blob/master/xtext/org.icyphy.linguafranca/src/test/C/src/Wcet.lf). |
| 54 | + |
| 55 | +```lf-c |
| 56 | +reactor Work { |
| 57 | + input in1: int; |
| 58 | + input in2: int; |
| 59 | + output out:int; |
| 60 | + reaction(in1, in2) -> out {= |
| 61 | + int ret; |
| 62 | + if (in1 > 10) { |
| 63 | + ret = in2 * in1; |
| 64 | + } else { |
| 65 | + ret = in2 + in1; |
| 66 | + } |
| 67 | + lf_set(out, ret); |
| 68 | + =} |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +We want to perform WCET analysis of the single reaction of the Work reactor. |
| 73 | +This reaction, depending on the input data, will either perform a multiplication, |
| 74 | +which is more expensive in Patmos, or an addition. The WCET analysis shall consider |
| 75 | +the multiplication path as the worst-case path. To generate the information for |
| 76 | +WCET analysis by the compiler we have to compile the application as follows: |
| 77 | + |
| 78 | + patmos-clang -O2 -mserialize=wcet.pml Wcet.c |
| 79 | + |
| 80 | +We investigate the C source code `Wcet.c` and find that the reaction we |
| 81 | +are interested is named `reaction_function1`. Therefore, we invoke WCET analysis |
| 82 | +as follows: |
| 83 | + |
| 84 | + platin wcet -i wcet.pml -b a.out -e reaction_function1 --report |
| 85 | + |
| 86 | +This results in following report: |
| 87 | + |
| 88 | +``` |
| 89 | +... |
| 90 | +[platin] INFO: Finished run WCET analysis (platin) in 62 ms |
| 91 | +[platin] INFO: best WCET bound: 242 cycles |
| 92 | +--- |
| 93 | +- analysis-entry: reaction_function1 |
| 94 | + source: platin |
| 95 | + cycles: 242 |
| 96 | +... |
| 97 | +``` |
| 98 | + |
| 99 | +The analysis gives the WCET of 242 clock cycles for the reaction, |
| 100 | +which includes clock cycles for data cache misses. |
| 101 | +Further details on the WCET analysis |
| 102 | +tool `platin` and e.g., how to annotate loop bounds can be found in the |
| 103 | +[Patmos Reference Handbook](http://patmos.compute.dtu.dk/patmos_handbook.pdf). |
| 104 | + |
| 105 | +Note, that the WCET analysis of a reaction does only include the code of the |
| 106 | +reaction function, not the cache miss cost of calling the function from |
| 107 | +the scheduler or the cache miss cost when returning to the scheduler. |
0 commit comments