Skip to content

Commit

Permalink
Removes the dependency to SimulationPopulation in SimulationRunner
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisDrogoul committed Aug 19, 2021
1 parent 9c3db49 commit 8790a80
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
Expand Up @@ -43,7 +43,7 @@ public class SimulationPopulation extends GamaPopulation<SimulationAgent> {

public SimulationPopulation(final ExperimentAgent agent, final ISpecies species) {
super(agent, species);
runner = new SimulationRunner(this);
runner = SimulationRunner.of(this);
}

public int getMaxNumberOfConcurrentSimulations() {
Expand Down
26 changes: 15 additions & 11 deletions msi.gama.core/src/msi/gama/runtime/concurrent/SimulationRunner.java
Expand Up @@ -25,24 +25,31 @@

public class SimulationRunner {

final SimulationPopulation population;
final Map<SimulationAgent, Callable<Boolean>> runnables;
static final Function<SimulationAgent, Boolean> STEP = each -> each.getScope().step(each).passed();
final int concurrency;

private int activeThreads;

public SimulationRunner(final SimulationPopulation pop) {
population = pop;
runnables = new LinkedHashMap<>();
final IExperimentPlan plan = population.getHost().getSpecies();
public static SimulationRunner of(final SimulationPopulation pop) {
int concurrency = 0;
final IExperimentPlan plan = pop.getHost().getSpecies();
if (plan.isHeadless() && !plan.isBatch()) {
concurrency = 1;
} else {
concurrency = GamaExecutorService.getParallelism(population.getHost().getScope(), plan.getConcurrency(),
concurrency = GamaExecutorService.getParallelism(pop.getHost().getScope(), plan.getConcurrency(),
Caller.SIMULATION);
}
return withConcurrency(concurrency);
}

public static SimulationRunner withConcurrency(final int concurrency) {
return new SimulationRunner(concurrency < 0 ? 1 : concurrency);
}

private SimulationRunner(final int concurrency) {
this.concurrency = concurrency;
runnables = new LinkedHashMap<>();
}

public void remove(final SimulationAgent agent) {
Expand All @@ -65,13 +72,10 @@ public void step() {

private int computeNumberOfThreads() {
final ExecutorService executor = getExecutor();
if (executor instanceof ForkJoinPool) {
// getActiveThreadCount() always overestimates the number of threads
if (executor instanceof ForkJoinPool) // getActiveThreadCount() always overestimates the number of threads
return Math.min(concurrency, ((ForkJoinPool) executor).getActiveThreadCount());
}
if (executor instanceof ThreadPoolExecutor) {
if (executor instanceof ThreadPoolExecutor)
return Math.min(concurrency, ((ThreadPoolExecutor) executor).getActiveCount());
}
return 1;
}

Expand Down

0 comments on commit 8790a80

Please sign in to comment.