Skip to content
This repository has been archived by the owner on Sep 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #67 from lf-lang/formatting
Browse files Browse the repository at this point in the history
Proper formatting of templates
  • Loading branch information
lhstrh authored Apr 26, 2024
2 parents a0fbe29 + b3b946a commit 65c5d90
Show file tree
Hide file tree
Showing 10 changed files with 264 additions and 267 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ target C

/** Print "Hello World!" in C. */
main reactor {
reaction(startup) {=
// Using a thread-safe print function provided by the runtime.
lf_print("Hello World!");
=}
reaction(startup) {=
// Using a thread-safe print function provided by the runtime.
lf_print("Hello World!");
=}
}
Original file line number Diff line number Diff line change
@@ -1,46 +1,44 @@
/**
* Simple demonstration of the sensor simulator (used in the Rhythm examples).
* This has no audio output, but just tests the ncurses interface.
* Simple demonstration of the sensor simulator (used in the Rhythm examples). This has no audio
* output, but just tests the ncurses interface.
*/
target C {
keepalive: true,
cmake-include: [
"include/ncurses-cmake-extension.txt", // Adds support for ncurses
"/lib/c/reactor-c/util/sensor_simulator.cmake"
],
files: [
"/lib/c/reactor-c/util/sensor_simulator.c",
"/lib/c/reactor-c/util/sensor_simulator.h"
]
keepalive: true,
cmake-include: [
"include/ncurses-cmake-extension.txt", // Adds support for ncurses
"/lib/c/reactor-c/util/sensor_simulator.cmake"],
files: ["/lib/c/reactor-c/util/sensor_simulator.c", "/lib/c/reactor-c/util/sensor_simulator.h"]
}

preamble {=
#include "sensor_simulator.h"
#include "sensor_simulator.h"
=}

main reactor {
preamble {=
const char* messages[] = {"Hello", "World"};
int num_messages = 2;
=}
timer t(0, 1 sec)
timer r(0, 2 sec)
physical action key: char*
preamble {=
const char* messages[] = {"Hello", "World"};
int num_messages = 2;
=}
timer t(0, 1 sec)
timer r(0, 2 sec)
physical action key: char*

reaction(startup) -> key {=
lf_print("Starting sensor simulator.");
start_sensor_simulator(messages, num_messages, 16, NULL, LOG_LEVEL_INFO);
register_sensor_key('\0', key);
=}
reaction(startup) -> key {=
lf_print("Starting sensor simulator.");
start_sensor_simulator(messages, num_messages, 16, NULL, LOG_LEVEL_INFO);
register_sensor_key('\0', key);
=}

reaction(t) {= show_tick("*"); =}
reaction(t) {=
show_tick("*");
=}

reaction(r) {=
lf_print("Elapsed logical time: %lld.", lf_time_logical_elapsed());
show_tick(".");
=}
reaction(r) {=
lf_print("Elapsed logical time: %lld.", lf_time_logical_elapsed());
show_tick(".");
=}

reaction(key) {=
lf_print("You typed '%s' at elapsed time %lld.", key->value, lf_time_logical_elapsed());
=}
reaction(key) {=
lf_print("You typed '%s' at elapsed time %lld.", key->value, lf_time_logical_elapsed());
=}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Other options: Python, Cpp, TypeScript, Rust.
target C

main reactor { // Import and/or declare reactor classes.
// Import and/or declare reactor classes.
main reactor {
}
85 changes: 42 additions & 43 deletions org.lflang.ui/src/org/lflang/ui/wizard/templates/c/src/Parallel.lf
Original file line number Diff line number Diff line change
@@ -1,62 +1,61 @@
/**
* Each instance of TakeTime takes 200 ms wall clock time to transport the input
* to the output. Four of them are instantiated. Note that without parallel
* execution, there is no way this program can keep up with real time since in
* every 200 msec cycle it has 800 msec of work to do. Given 4 workers, however,
* this program can complete 800 msec of work in about 225 msec.
* Each instance of TakeTime takes 200 ms wall clock time to transport the input to the output. Four
* of them are instantiated. Note that without parallel execution, there is no way this program can
* keep up with real time since in every 200 msec cycle it has 800 msec of work to do. Given 4
* workers, however, this program can complete 800 msec of work in about 225 msec.
*/
target C {
timeout: 2 sec,
workers: 1 // Change to 4 to see speed up.
timeout: 2 sec,
workers: 1 // Change to 4 to see speed up.
}

reactor Source {
timer t(0, 200 msec)
output out: int
state s: int = 0
timer t(0, 200 msec)
output out: int
state s: int = 0

reaction(t) -> out {=
lf_set(out, self->s);
self->s++;
=}
reaction(t) -> out {=
lf_set(out, self->s);
self->s++;
=}
}

reactor TakeTime {
input in: int
output out: int
input in: int
output out: int

reaction(in) -> out {=
lf_sleep(MSEC(200));
int offset = 0;
for (int i = 0; i < 100000000; i++) {
offset++;
}
lf_set(out, in->value + offset);
=}
reaction(in) -> out {=
lf_sleep(MSEC(200));
int offset = 0;
for (int i = 0; i < 100000000; i++) {
offset++;
}
lf_set(out, in->value + offset);
=}
}

reactor Destination(width: int = 4) {
state s: int = 400000000
input[width] in: int
state s: int = 400000000
input[width] in: int

reaction(in) {=
int sum = 0;
for (int i = 0; i < in_width; i++) {
sum += in[i]->value;
}
printf("Sum of received: %d.\n", sum);
if (sum != self->s) {
printf("ERROR: Expected %d.\n", self->s);
exit(1);
}
self->s += in_width;
=}
reaction(in) {=
int sum = 0;
for (int i = 0; i < in_width; i++) {
sum += in[i]->value;
}
printf("Sum of received: %d.\n", sum);
if (sum != self->s) {
printf("ERROR: Expected %d.\n", self->s);
exit(1);
}
self->s += in_width;
=}
}

main reactor Parallel(width: int = 4) {
a = new Source()
t = new[width] TakeTime()
(a.out)+ -> t.in
b = new Destination(width = width)
t.out -> b.in
a = new Source()
t = new[width] TakeTime()
(a.out)+ -> t.in
b = new Destination(width=width)
t.out -> b.in
}
95 changes: 44 additions & 51 deletions org.lflang.ui/src/org/lflang/ui/wizard/templates/c/src/Pipeline.lf
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
/**
* Basic pipeline pattern where a periodic source feeds a chain of reactors that
* can all execute in parallel at each logical time step.
* Basic pipeline pattern where a periodic source feeds a chain of reactors that can all execute in
* parallel at each logical time step.
*
* The workers argument specifies the number of worker workers, which enables
* the reactors in the chain to execute on multiple cores simultaneously.
* The workers argument specifies the number of worker workers, which enables the reactors in the
* chain to execute on multiple cores simultaneously.
*
* This uses the TakeTime reactor to perform computation. If you reduce the
* number of worker workers to 1, the execution time will be approximately four
* times as long.
* This uses the TakeTime reactor to perform computation. If you reduce the number of worker workers
* to 1, the execution time will be approximately four times as long.
*
* @author Edward A. Lee
* @author Marten Lohstroh
*/
target C {
workers: 4
workers: 4
}

/**
Expand All @@ -24,68 +23,62 @@ target C {
* @param init The first output.
* @param increment The increment between outputs
*/
reactor SendCount(
offset: time = 0,
period: time = 1 sec,
init: int = 0,
increment: int = 1
) {
state count: int = init
output out: int
timer t(offset, period)
reactor SendCount(offset: time = 0, period: time = 1 sec, init: int = 0, increment: int = 1) {
state count: int = init
output out: int
timer t(offset, period)

reaction(t) -> out {=
lf_set(out, self->count);
self->count += self->increment;
=}
reaction(t) -> out {=
lf_set(out, self->count);
self->count += self->increment;
=}
}

/**
* Receive an input and report the elapsed logical tag and the value of the
* input. Request stop when the 10th value has been received.
* Receive an input and report the elapsed logical tag and the value of the input. Request stop when
* the 10th value has been received.
*/
reactor Receive {
input in: int
input in: int

reaction(in) {=
lf_print("At elapsed tag (%lld, %d), received %d.",
lf_time_logical_elapsed(), lf_tag().microstep,
in->value
);
if (in->value >= 10) {
lf_request_stop();
}
=}
reaction(in) {=
lf_print("At elapsed tag (%lld, %d), received %d.",
lf_time_logical_elapsed(), lf_tag().microstep,
in->value
);
if (in->value >= 10) {
lf_request_stop();
}
=}
}

/**
* When triggered, take the specified amount of physical time before outputting
* the value of the trigger.
* When triggered, take the specified amount of physical time before outputting the value of the
* trigger.
*
* @param approximate_time The approximate amount of physical time to take for
* each input.
* @param approximate_time The approximate amount of physical time to take for each input.
*
* @input in A triggering input.
*
* @output out The triggering input.
*/
reactor TakeTime(approximate_time: time = 100 msec) {
input in: int
output out: int
input in: int
output out: int

reaction(in) -> out {=
instant_t start_time = lf_time_physical();
while (lf_time_physical() < start_time + self->approximate_time) {
// Do nothing.
}
lf_set(out, in->value);
=}
reaction(in) -> out {=
instant_t start_time = lf_time_physical();
while (lf_time_physical() < start_time + self->approximate_time) {
// Do nothing.
}
lf_set(out, in->value);
=}
}

main reactor {
r0 = new SendCount(period = 100 msec)
rp = new[4] TakeTime(approximate_time = 100 msec)
r5 = new Receive()
// Uncomment the "after" clause to expose parallelism. after 100 msec;
r0.out, rp.out -> rp.in, r5.in
r0 = new SendCount(period = 100 msec)
rp = new[4] TakeTime(approximate_time = 100 msec)
r5 = new Receive()
// Uncomment the "after" clause to expose parallelism. after 100 msec;
r0.out, rp.out -> rp.in, r5.in
}
Loading

0 comments on commit 65c5d90

Please sign in to comment.