Skip to content

Commit

Permalink
Address PR FIXMEs, clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
lsk567 committed Jun 14, 2022
1 parent 8a8e31b commit 58c9057
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 713 deletions.
7 changes: 0 additions & 7 deletions org.lflang/src/org/lflang/generator/c/CCmakeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.lflang.ErrorReporter;
import org.lflang.FileConfig;
import org.lflang.TargetConfig;
import org.lflang.TargetProperty.SchedulerOption;
import org.lflang.generator.CodeBuilder;
import org.lflang.util.FileUtil;

Expand Down Expand Up @@ -226,12 +225,6 @@ CodeBuilder generateCMakeCode(
cMakeCode.pr("add_link_options( "+compilerFlag+")");
}
}

if (targetConfig.schedulerType == SchedulerOption.QS) {
cMakeCode.newLine();
cMakeCode.pr("add_compile_definitions(SCHEDULER_QS)");
cMakeCode.newLine();
}

// Add the install option
cMakeCode.pr("install(");
Expand Down
2 changes: 0 additions & 2 deletions org.lflang/src/org/lflang/generator/c/CCoreFilesUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ private static List<String> getThreadSupportFiles(
"threaded/scheduler_" + scheduler + ".c",
"threaded/reactor_threaded.c"
));
if (scheduler == SchedulerOption.QS)
supportFiles.add("threaded/scheduler_QS.h");
return threading ? supportFiles : List.of("reactor.c");
}
}
57 changes: 41 additions & 16 deletions org.lflang/src/org/lflang/generator/c/CGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
import org.lflang.generator.TargetTypes;
import org.lflang.generator.TimerInstance;
import org.lflang.generator.TriggerInstance;
import org.lflang.generator.uclid.UclidScheduleGenerator;
import org.lflang.lf.Action;
import org.lflang.lf.ActionOrigin;
import org.lflang.lf.Delay;
Expand Down Expand Up @@ -558,28 +559,17 @@ public void doGenerate(Resource resource, LFGeneratorContext context) {
Exceptions.sneakyThrow(e);
}

// Generate scheduler-specific code. In the case of the QS scheduler,
// a schedule.h is generated. This is separated from the logic in
// pickScheduler(), since pickScheduler() does not generate new code.
generateCodeForScheduler();

// Create docker file.
if (targetConfig.dockerOptions != null && mainDef != null) {
dockerGenerator.addFile(
dockerGenerator.fromData(lfModuleName, federate.name, fileConfig));
}

System.out.println("Checking main instance in CGenerator.java.");
System.out.println(this.main);
System.out.println(main);

// Generate schedule.h if QS scheduler is used.
if (targetConfig.schedulerType == SchedulerOption.QS) {
var scheduleGenerator = new SmtScheduleGenerator(fileConfig, errorReporter, main, targetConfig);
var scheduleFile = fileConfig.getSrcGenPath() + File.separator + "schedule.h";
var scheduleCode = scheduleGenerator.generateScheduleCode();
try {
scheduleCode.writeToFile(scheduleFile);
} catch (IOException e) {
Exceptions.sneakyThrow(e);
}
}

if (targetConfig.useCmake) {
// If cmake is requested, generated the CMakeLists.txt
var cmakeGenerator = new CCmakeGenerator(targetConfig, fileConfig);
Expand Down Expand Up @@ -855,6 +845,25 @@ private void generateCodeForCurrentFederate(
}
}

/**
* Generate scheduler-specific code. In the case of the QS scheduler,
* a schedule.h is generated. This is separated from the logic in
* pickScheduler(), since pickScheduler() does not generate new code.
*/
private void generateCodeForScheduler() {
// Generate schedule.h if QS scheduler is used.
if (targetConfig.schedulerType == SchedulerOption.QS) {
var scheduleGenerator = new UclidScheduleGenerator(fileConfig, errorReporter, main, targetConfig);
var scheduleFile = fileConfig.getSrcGenPath() + File.separator + "schedule.h";
var scheduleCode = scheduleGenerator.generateScheduleCode();
try {
scheduleCode.writeToFile(scheduleFile);
} catch (IOException e) {
Exceptions.sneakyThrow(e);
}
}
}

protected CDockerGenerator getDockerGenerator() {
return new CDockerGenerator(isFederated, CCppMode, targetConfig);
}
Expand Down Expand Up @@ -897,6 +906,22 @@ private void pickScheduler() {
targetConfig.compileAdditionalSources.add(
"core" + File.separator + "utils" + File.separator + "semaphore.c"
);

// Perform a set of QS scheduler-specific operations.
if (targetConfig.schedulerType == SchedulerOption.QS) {
// Define a macro in CMake.
targetConfig.compileDefinitions.put("SCHEDULER_QS", "");
// Copy an additional header file for QS.
try {
FileUtil.copyFilesFromClassPath(
"/lib/c/reactor-c/core",
fileConfig.getSrcGenPath().resolve("core"),
List.of("threaded/scheduler_QS.h")
);
} catch (IOException e) {
Exceptions.sneakyThrow(e);
}
}
}

private boolean hasDeadlines(List<Reactor> reactors) {
Expand Down
57 changes: 28 additions & 29 deletions org.lflang/src/org/lflang/generator/c/CTriggerObjectsGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,8 @@ public static String generateInitializeTriggerObjects(
isFederated,
clockSyncIsOn
));
if (targetConfig.schedulerType == SchedulerOption.QS) {
code.pr(generateReactionInstanceList(
federate,
main,
isFederated
));
}
code.pr(generateSchedulerInitializer(
federate,
main,
targetConfig
));
Expand All @@ -185,6 +179,7 @@ public static String generateInitializeTriggerObjects(
* Generate code to initialize the scheduler for the threaded C runtime.
*/
public static String generateSchedulerInitializer(
FederateInstance federate,
ReactorInstance main,
TargetConfig targetConfig
) {
Expand All @@ -196,6 +191,16 @@ public static String generateSchedulerInitializer(
var numReactionsPerLevelJoined = Arrays.stream(numReactionsPerLevel)
.map(String::valueOf)
.collect(Collectors.joining(", "));
// Generate a list of reaction runtime instances.
// This is currently useful for QS, but could be
// utilized by other schedulers in the future.
if (targetConfig.schedulerType == SchedulerOption.QS) {
code.pr(generateReactionInstanceList(
federate,
main
));
}
// Print the initializers.
code.pr(String.join("\n",
"// Initialize the scheduler",
"size_t num_reactions_per_level["+numReactionsPerLevel.length+"] = ",
Expand Down Expand Up @@ -440,47 +445,41 @@ private static boolean setReactionPriorities(
}
return foundOne;
}
private static String generateReactionInstanceList(
FederateInstance currentFederate,
ReactorInstance reactor,
boolean isFederated) {
var code = new CodeBuilder();
// var numReactionsPerLevel = reactor.assignLevels().getNumReactionsPerLevel();
code.pr( "reaction_t **_lf_reaction_instances = (reaction_t**) calloc("+reactionNum+", sizeof(reaction_t*));");
generateReactionInstances(currentFederate,reactor, isFederated, code);
return code.toString();
}
private static void generateReactionInstances(
private static String generateReactionInstanceList(
FederateInstance currentFederate,
ReactorInstance reactor) {
var code = new CodeBuilder();
code.pr( "reaction_t **_lf_reaction_instances = (reaction_t**) calloc("+reactionNum+", sizeof(reaction_t*));");
generateReactionInstances(currentFederate, reactor, code);
return code.toString();
}
private static void generateReactionInstances(
FederateInstance currentFederate,
ReactorInstance reactor,
boolean isFederated,
CodeBuilder code
){
//FIXME: CHECK WHETHER THE PARENT OF REACTOR IS BANK
if (reactor != null && (reactor.isBank()||(reactor.getParent() != null&& reactor.getParent().isBank()))) {
//if(reactor != null && reactor.isBank()){
if (reactor != null &&
(reactor.isBank() || (reactor.getParent() != null && reactor.getParent().isBank()))) {
for (ReactionInstance r : reactor.reactions) {
List<Runtime> runtimes = r.getRuntimeInstances();

for (Runtime rt :runtimes) {
code.pr(String.join("", "_lf_reaction_instances["+reactionId+"] = ", "&", CUtil.reactionRef(rt.getReaction(), Integer.toString(rt.id)), ";"));
rt.reactionID = reactionId;
reactionId +=1;
reactionId += 1;
}
}
}else{
} else {
for (ReactionInstance r : reactor.reactions) {
code.pr(String.join("", "_lf_reaction_instances["+reactionId+"] = ", "&", CUtil.reactionRef(r), ";"));
for(Runtime rt: r.getRuntimeInstances()){
rt.reactionID = reactionId;
reactionId +=1;
reactionId += 1;
}
}
}

for (ReactorInstance child : reactor.children) {
for (ReactorInstance child : reactor.children) {
if (currentFederate.contains(child)) {
generateReactionInstances(currentFederate, child, isFederated, code);
generateReactionInstances(currentFederate, child, code);
}
}
}
Expand Down
Loading

0 comments on commit 58c9057

Please sign in to comment.