From 0b00905a9317ac03f68a1839e04762d3a9138f54 Mon Sep 17 00:00:00 2001 From: AlexisDrogoul Date: Sun, 8 May 2022 23:50:23 +0700 Subject: [PATCH] Addresses #3376. Please test ! --- .../gama/headless/runtime/Application.java | 21 +-- .../runtime/LocalSimulationRuntime.java | 174 ------------------ .../headless/runtime/SimulationRuntime.java | 4 +- 3 files changed, 8 insertions(+), 191 deletions(-) delete mode 100644 msi.gama.headless/src/msi/gama/headless/runtime/LocalSimulationRuntime.java diff --git a/msi.gama.headless/src/msi/gama/headless/runtime/Application.java b/msi.gama.headless/src/msi/gama/headless/runtime/Application.java index fd577573a4..c40461b452 100644 --- a/msi.gama.headless/src/msi/gama/headless/runtime/Application.java +++ b/msi.gama.headless/src/msi/gama/headless/runtime/Application.java @@ -10,14 +10,11 @@ ********************************************************************************************************/ package msi.gama.headless.runtime; -import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; -import java.io.InputStreamReader; import java.io.OutputStreamWriter; -import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -60,6 +57,7 @@ import msi.gama.kernel.model.IModel; import msi.gama.lang.gaml.validation.GamlModelBuilder; import msi.gama.runtime.GAMA; +import msi.gama.runtime.concurrent.GamaExecutorService; import msi.gaml.compilation.GamlCompilationError; import ummisco.gama.dev.utils.DEBUG; @@ -180,17 +178,6 @@ private static void showHelp() { DEBUG.OFF(); } - /** - * Check parameters. - * - * @param args - * the args - * @return true, if successful - */ - private boolean checkParameters(final List args) { - return checkParameters(args, false); - } - /** * Check parameters. * @@ -368,8 +355,8 @@ public Object start(final IApplicationContext context) throws Exception { } else if (args.contains(BUILD_XML_PARAMETER)) { buildXML(args); } else if (args.contains(SOCKET_PARAMETER)) { -// GamaListener.newInstance(this.socket, this); - GamaListener gl=new GamaListener(this.socket,this); + // GamaListener.newInstance(this.socket, this); + GamaListener gl = new GamaListener(this.socket, this); } else { runSimulation(args); } @@ -589,6 +576,8 @@ public void runBatchSimulation(final String experimentName, final String pathToM final List errors = new ArrayList<>(); final IModel mdl = builder.compile(URI.createFileURI(pathToModel), errors); + GamaExecutorService.CONCURRENCY_THRESHOLD.set(processorQueue.getNumberOfThreads()); + final IExperimentPlan expPlan = mdl.getExperiment(experimentName); expPlan.setHeadless(true); diff --git a/msi.gama.headless/src/msi/gama/headless/runtime/LocalSimulationRuntime.java b/msi.gama.headless/src/msi/gama/headless/runtime/LocalSimulationRuntime.java deleted file mode 100644 index a8d10dcbdd..0000000000 --- a/msi.gama.headless/src/msi/gama/headless/runtime/LocalSimulationRuntime.java +++ /dev/null @@ -1,174 +0,0 @@ -/******************************************************************************************************* - * - * LocalSimulationRuntime.java, in msi.gama.headless, is part of the source code of the GAMA modeling and simulation - * platform (v.1.8.2). - * - * (c) 2007-2022 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU) - * - * Visit https://github.com/gama-platform/gama for license information and contacts. - * - ********************************************************************************************************/ -package msi.gama.headless.runtime; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; - -import msi.gama.headless.job.IExperimentJob; -import ummisco.gama.dev.utils.DEBUG; - -/** - * The Class LocalSimulationRuntime. - */ -public class LocalSimulationRuntime /* extends Observable */ implements SimulationRuntime { - - static { - DEBUG.ON(); - } - - /** The simulations. */ - private final Map simulations; - - /** The queue. */ - private final ArrayList queue; - - /** The started. */ - private final ArrayList started; - - /** The allocated processor. */ - private int allocatedProcessor; - - /** The is trace kept. */ - private boolean isTraceKept; - - /** - * Instantiates a new local simulation runtime. - */ - public LocalSimulationRuntime() { - this(UNDEFINED_QUEUE_SIZE); - } - - /** - * Instantiates a new local simulation runtime. - * - * @param numberOfCoresAsked - * the number of cores asked - */ - public LocalSimulationRuntime(final int numberOfCoresAsked) { - simulations = new HashMap<>(); - queue = new ArrayList<>(); - started = new ArrayList<>(); - // loadedModels = new HashMap<>(); - // availableLoadedModels = new HashMap<>(); - this.allocatedProcessor = getAvailableCores(numberOfCoresAsked); - } - - /** - * Gets the available cores. - * - * @param asked - * the asked - * @return the available cores - */ - private static int getAvailableCores(final int asked) { - final int max = Runtime.getRuntime().availableProcessors(); - final int cpus = Math.max(1, Math.min(max, asked)); - DEBUG.LOG("Number of cpus used:" + cpus + " (available: " + max + ")"); - return cpus; - } - - @Override - public void pushSimulation(final IExperimentJob s) { - final IExperimentJobThread f = new IExperimentJobThread(s); - simulations.put(s.getExperimentID(), f); - if (started.size() < allocatedProcessor) { - this.startSimulation(f); - } else { - queue.add(f); - } - } - - /** - * Start simulation. - * - * @param s - * the s - */ - private void startSimulation(final IExperimentJobThread s) { - started.add(s); - s.start(); - // this.notifyListener(); - } - - /** - * Close simulation. - * - * @param s - * the s - */ - public void closeSimulation(final IExperimentJobThread s) { - started.remove(s); - if (queue.size() > 0) { - final IExperimentJobThread p = queue.get(0); - queue.remove(p); - this.startSimulation(p); - } - if (!this.isTraceKept) { simulations.remove(s.getIExperimentJob().getExperimentID()); } - // this.notifyListener(); - } - - @Override - public boolean isPerformingSimulation() { return started.size() > 0 || queue.size() > 0; } - - /** - * The Class IExperimentJobThread. - */ - class IExperimentJobThread extends Thread { - - /** The si. */ - private IExperimentJob si = null; - - /** - * Gets the experiment job. - * - * @return the experiment job - */ - IExperimentJob getIExperimentJob() { return si; } - - /** - * Instantiates a new experiment job thread. - * - * @param sim - * the sim - */ - public IExperimentJobThread(final IExperimentJob sim) { - si = sim; - } - - @Override - public void run() { - try (final DebugStream file = new DebugStream(si)) { - si.loadAndBuild(); - si.playAndDispose(); - } catch (final Exception e) { - DEBUG.ERR(e); - } finally { - closeSimulation(this); - } - } - - } - - @Override - public void setNumberOfThreads(final int numberOfThread) { - this.allocatedProcessor = numberOfThread; - - } - - @Override - public void execute(Runnable r) { - // TODO Auto-generated method stub - - } - -} diff --git a/msi.gama.headless/src/msi/gama/headless/runtime/SimulationRuntime.java b/msi.gama.headless/src/msi/gama/headless/runtime/SimulationRuntime.java index a2d2e6da7d..6e2a37c59b 100644 --- a/msi.gama.headless/src/msi/gama/headless/runtime/SimulationRuntime.java +++ b/msi.gama.headless/src/msi/gama/headless/runtime/SimulationRuntime.java @@ -59,7 +59,7 @@ public void close() throws IOException { * the s */ void execute(final Runnable r); - + /** * Push simulation. * @@ -83,4 +83,6 @@ public void close() throws IOException { */ void setNumberOfThreads(int numberOfThread); + int getNumberOfThreads(); + }