Skip to content

Commit

Permalink
Report results to main LfGeneratorContext.
Browse files Browse the repository at this point in the history
  • Loading branch information
petervdonovan committed Aug 10, 2022
1 parent 32163c7 commit 38f325b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
30 changes: 17 additions & 13 deletions org.lflang/src/org/lflang/federated/generator/FedGenerator.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.lflang.federated.generator;

import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -29,6 +31,9 @@
import org.lflang.TargetProperty.CoordinationType;
import org.lflang.federated.launcher.FedLauncher;
import org.lflang.federated.launcher.FedLauncherFactory;
import org.lflang.generator.CodeMap;
import org.lflang.generator.GeneratorResult;
import org.lflang.generator.GeneratorResult.Status;
import org.lflang.generator.GeneratorUtils;
import org.lflang.generator.LFGenerator;
import org.lflang.generator.LFGeneratorContext;
Expand Down Expand Up @@ -128,8 +133,8 @@ public boolean doGenerate(Resource resource, LFGeneratorContext context) throws
);
}

compileFederates(context);

Map<Path, CodeMap> codeMapMap = compileFederates(context);
context.finish(Status.COMPILED, fileConfig.name, fileConfig, codeMapMap);
return false;
}

Expand Down Expand Up @@ -163,7 +168,7 @@ public void createLauncher(
// System.out.println(PythonInfoGenerator.generateFedRunInfo(fileConfig));
}

private void compileFederates(LFGeneratorContext context) {
private Map<Path, CodeMap> compileFederates(LFGeneratorContext context) {
// FIXME: Use the appropriate resource set instead of always using standalone
Injector inj = new LFStandaloneSetup()
.createInjectorAndDoEMFRegistration();
Expand All @@ -182,17 +187,16 @@ private void compileFederates(LFGeneratorContext context) {
);
var compileThreadPool = Executors.newFixedThreadPool(numOfCompileThreads);
System.out.println("******** Using "+numOfCompileThreads+" threads to compile the program.");
Map<Path, CodeMap> codeMapMap = new HashMap<>();

for(FederateInstance fed : federates) {
compileThreadPool.execute(new Runnable() {
@Override
public void run() {
SubContext cont = new SubContext(context, 0, 0); // Is there a way to quantify progress when compilation is in parallel?
Resource res = rs.getResource(URI.createFileURI(
fileConfig.getFedSrcPath().resolve(fed.name + ".lf").toAbsolutePath().toString()
), true);
gen.doGenerate(res, fsa, cont);
}
compileThreadPool.execute(() -> {
SubContext cont = new SubContext(context, 0, 0); // Is there a way to quantify progress when compilation is in parallel?
Resource res = rs.getResource(URI.createFileURI(
fileConfig.getFedSrcPath().resolve(fed.name + ".lf").toAbsolutePath().toString()
), true);
gen.doGenerate(res, fsa, cont);
codeMapMap.putAll(cont.getResult().getCodeMaps());
});
}

Expand All @@ -206,7 +210,7 @@ public void run() {
} catch (Exception e) {
Exceptions.sneakyThrow(e);
}

return codeMapMap;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions org.lflang/src/org/lflang/generator/SubContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class SubContext implements LFGeneratorContext {
private final LFGeneratorContext containingContext;
private final int startPercentProgress;
private final int endPercentProgress;
private GeneratorResult result = null;

/**
* Initializes the context within {@code containingContext} of the process that extends from
Expand Down Expand Up @@ -68,12 +69,12 @@ public ErrorReporter getErrorReporter() {

@Override
public void finish(GeneratorResult result) {
// Do nothing. A build process is not finished until the outermost containing context is finished.
this.result = result;
}

@Override
public GeneratorResult getResult() {
throw new UnsupportedOperationException("Only the outermost context can have a final result.");
return result;
}

@Override
Expand Down

1 comment on commit 38f325b

@Soroosh129
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀🚀🚀

Please sign in to comment.