Permalink
Browse files

Fixes #2025. Fixes #738.

Generalization of the 'parallel:' facet to grid, species, ask and
experiment. Chnages in the API of IScope for supporting parallel
operations. Changes in QuadTree and GamaGraph for solving sync problems.
Changes in the step method of agents (now divided in 3 sub-methods:
preStep(), doStep(), postStep()). Addition of the
msi.gama.runtime.concurrent package and several classes dedicated to
concurrent runs.

Signed-off-by: AlexisDrogoul <alexis.drogoul@gmail.com>
  • Loading branch information...
1 parent b2fba48 commit 683d6c4cb769afa53192441d095360db40e34506 @AlexisDrogoul AlexisDrogoul committed Oct 16, 2016
Showing with 1,397 additions and 1,466 deletions.
  1. +4 −6 msi.gama.core/src/msi/gama/common/Activator.java
  2. +2 −30 msi.gama.core/src/msi/gama/common/GamaPreferences.java
  3. +29 −27 msi.gama.core/src/msi/gama/common/util/RandomUtils.java
  4. +6 −2 msi.gama.core/src/msi/gama/kernel/experiment/BatchAgent.java
  5. +14 −27 msi.gama.core/src/msi/gama/kernel/experiment/ExperimentAgent.java
  6. +71 −12 msi.gama.core/src/msi/gama/kernel/experiment/ExperimentPlan.java
  7. +0 −92 msi.gama.core/src/msi/gama/kernel/experiment/ExperimentPopulation.java
  8. +2 −7 msi.gama.core/src/msi/gama/kernel/experiment/ExperimentScheduler.java
  9. +0 −2 msi.gama.core/src/msi/gama/kernel/experiment/IExperimentPlan.java
  10. +17 −34 msi.gama.core/src/msi/gama/kernel/simulation/SimulationAgent.java
  11. +18 −78 msi.gama.core/src/msi/gama/kernel/simulation/SimulationPopulation.java
  12. +57 −9 msi.gama.core/src/msi/gama/metamodel/agent/AbstractAgent.java
  13. +32 −32 msi.gama.core/src/msi/gama/metamodel/agent/GamlAgent.java
  14. +2 −2 msi.gama.core/src/msi/gama/metamodel/agent/IMacroAgent.java
  15. +28 −43 msi.gama.core/src/msi/gama/metamodel/agent/MinimalAgent.java
  16. +15 −30 msi.gama.core/src/msi/gama/metamodel/population/GamaPopulation.java
  17. +24 −37 msi.gama.core/src/msi/gama/metamodel/topology/GamaQuadTree.java
  18. +5 −84 msi.gama.core/src/msi/gama/metamodel/topology/grid/GamaSpatialMatrix.java
  19. +0 −5 msi.gama.core/src/msi/gama/metamodel/topology/grid/IGrid.java
  20. +3 −3 msi.gama.core/src/msi/gama/outputs/AbstractOutputManager.java
  21. +1 −1 msi.gama.core/src/msi/gama/outputs/FileOutput.java
  22. +2 −2 msi.gama.core/src/msi/gama/outputs/InspectDisplayOutput.java
  23. +1 −1 msi.gama.core/src/msi/gama/outputs/LayeredDisplayOutput.java
  24. +2 −2 msi.gama.core/src/msi/gama/outputs/MonitorOutput.java
  25. +5 −6 msi.gama.core/src/msi/gama/outputs/layers/AgentLayer.java
  26. +1 −9 msi.gama.core/src/msi/gama/outputs/layers/EventLayer.java
  27. +4 −5 msi.gama.core/src/msi/gama/outputs/layers/GraphicLayer.java
  28. +1 −2 msi.gama.core/src/msi/gama/outputs/layers/OverlayLayer.java
  29. +5 −4 msi.gama.core/src/msi/gama/outputs/layers/SpeciesLayer.java
  30. +1 −1 msi.gama.core/src/msi/gama/outputs/layers/SpeciesLayerStatement.java
  31. +49 −43 msi.gama.core/src/msi/gama/runtime/ExecutionContext.java
  32. +156 −156 msi.gama.core/src/msi/gama/runtime/ExecutionScope.java
  33. +0 −23 msi.gama.core/src/msi/gama/runtime/GAMA.java
  34. +25 −38 msi.gama.core/src/msi/gama/runtime/IExecutionContext.java
  35. +97 −19 msi.gama.core/src/msi/gama/runtime/IScope.java
  36. +0 −148 msi.gama.core/src/msi/gama/runtime/ParallelAgentRunner.java
  37. +14 −44 msi.gama.core/src/msi/gama/runtime/TemporaryScope.java
  38. +68 −0 msi.gama.core/src/msi/gama/runtime/concurrent/AgentSpliterator.java
  39. +211 −0 msi.gama.core/src/msi/gama/runtime/concurrent/GamaExecutorService.java
  40. +35 −0 msi.gama.core/src/msi/gama/runtime/concurrent/ParallelAgentExecuter.java
  41. +98 −0 msi.gama.core/src/msi/gama/runtime/concurrent/ParallelAgentRunner.java
  42. +31 −0 msi.gama.core/src/msi/gama/runtime/concurrent/ParallelAgentStepper.java
  43. +84 −0 msi.gama.core/src/msi/gama/runtime/concurrent/SimulationRunner.java
  44. +6 −2 msi.gama.core/src/msi/gama/util/GAML.java
  45. +2 −2 msi.gama.core/src/msi/gama/util/GamaListFactory.java
  46. +0 −13 msi.gama.core/src/msi/gama/util/graph/AbstractGraphEdgeAgent.java
  47. +3 −3 msi.gama.core/src/msi/gama/util/graph/AbstractGraphNodeAgent.java
  48. +5 −4 msi.gama.core/src/msi/gama/util/graph/GamaGraph.java
  49. +1 −2 msi.gama.core/src/msi/gama/util/random/MersenneTwisterRNG.java
  50. +13 −1 msi.gama.core/src/msi/gaml/expressions/GlobalVariableExpression.java
  51. +1 −3 msi.gama.core/src/msi/gaml/expressions/PrimitiveOperator.java
  52. +8 −72 msi.gama.core/src/msi/gaml/operators/Containers.java
  53. +2 −2 msi.gama.core/src/msi/gaml/operators/Graphs.java
  54. +2 −2 msi.gama.core/src/msi/gaml/operators/Spatial.java
  55. +2 −2 msi.gama.core/src/msi/gaml/operators/System.java
  56. +1 −1 msi.gama.core/src/msi/gaml/species/AbstractSpecies.java
  57. +16 −2 msi.gama.core/src/msi/gaml/species/GamlSpecies.java
  58. +2 −0 msi.gama.core/src/msi/gaml/species/ISpecies.java
  59. +9 −35 msi.gama.core/src/msi/gaml/statements/AskStatement.java
  60. +2 −4 msi.gama.core/src/msi/gaml/statements/CaptureStatement.java
  61. +5 −5 msi.gama.core/src/msi/gaml/statements/CreateStatement.java
  62. +2 −3 msi.gama.core/src/msi/gaml/statements/ReleaseStatement.java
  63. +1 −2 msi.gama.core/src/msi/gaml/statements/UserCommandStatement.java
  64. +4 −4 msi.gama.core/src/msi/gaml/types/GamaMatrixType.java
  65. +8 −4 msi.gama.core/src/msi/gaml/variables/NumberVariable.java
  66. +4 −6 msi.gama.core/src/msi/gaml/variables/Variable.java
  67. +2 −2 msi.gama.models/models/Features/3D Visualization/models/Building Elevation.gaml
  68. +1 −122 ....models/models/Toy Models/Ants (Foraging and Sorting)/models/Ant Foraging (Multi-Simulation).gaml
  69. +14 −12 msi.gama.models/models/Toy Models/Flood Simulation/models/Hydrological Model.gaml
  70. +4 −3 msi.gama.models/models/Toy Models/Life/Life.gaml
  71. +12 −9 msi.gaml.architecture.simplebdi/src/msi/gaml/architecture/simplebdi/PerceiveStatement.java
  72. +1 −1 msi.gaml.extensions.fipa/src/msi/gaml/extensions/fipa/FIPASkill.java
  73. +1 −0 ummisco.gama.annotations/src/msi/gama/common/interfaces/IKeyword.java
  74. +1 −1 ummisco.gama.serialize/src/ummisco/gama/serializer/gamaType/converters/GamaBasicTypeConverter.java
  75. +0 −1 ummisco.gama.ui.experiment/src/ummisco/gama/ui/commands/RuntimeExceptionHandler.java
  76. +3 −3 ummisco.gama.ui.experiment/src/ummisco/gama/ui/menus/AgentsMenu.java
  77. +30 −56 ummisco.gama.ui.experiment/src/ummisco/gama/ui/views/console/InteractiveConsoleView.java
  78. +1 −3 ummisco.gama.ui.experiment/src/ummisco/gama/ui/views/inspectors/AgentInspectView.java
  79. +1 −2 ummisco.gama.ui.shared/src/ummisco/gama/ui/parameters/AgentEditor.java
  80. +3 −3 ...extensions.maths/src/ummisco/gaml/extensions/maths/ode/statements/SystemOfEquationsStatement.java
  81. +1 −2 ummisco.gaml.extensions.sound/src/ummisco/gaml/extensions/sound/PauseSoundStatement.java
  82. +1 −2 ummisco.gaml.extensions.sound/src/ummisco/gaml/extensions/sound/ResumeSoundStatement.java
  83. +1 −2 ummisco.gaml.extensions.sound/src/ummisco/gaml/extensions/sound/StartSoundStatement.java
  84. +1 −2 ummisco.gaml.extensions.sound/src/ummisco/gaml/extensions/sound/StopSoundStatement.java
@@ -3,19 +3,17 @@
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
+import msi.gama.runtime.concurrent.GamaExecutorService;
import msi.gaml.compilation.kernel.GamaBundleLoader;
public class Activator implements BundleActivator {
@Override
public void start(final BundleContext context) throws Exception {
/* Early build of the contributions made by plugins to GAMA */
- new Thread(new Runnable() {
-
- @Override
- public void run() {
- GamaBundleLoader.preBuildContributions();
- }
+ new Thread(() -> {
+ GamaBundleLoader.preBuildContributions();
+ GamaExecutorService.startUp();
}).start();
}
@@ -70,6 +70,7 @@
public static final String UI = "Presentation";
public static final String EXPERIMENTS = "Experiments";
public static final String SIMULATIONS = "Simulations";
+ public static final String CONCURRENCY = "Concurrency";
public static final String EXPERIMENTAL = "Performances";
public static final String DISPLAY = "Displays";
public static final String EDITOR = "Editors";
@@ -750,36 +751,7 @@ private static String getDefaultRPath() {
false, IType.BOOL).in(EXPERIMENTAL).group(DISPLAY);
public static final Entry<Integer> CORE_OUTPUT_DELAY = create("core.output_delay",
"Delay (in ms) between the opening of display views (increase if you experience freezes when opening displays, esp. Java2D displays)",
- 200, IType.INT).between(0, 1000).in(EXPERIMENTAL).group(DISPLAY);;
- public static final Entry<Boolean> MULTITHREADED_SIMULATIONS = create("core.multithreaded_simulations",
- "Run multiple simulations in multiple threads", true, IType.BOOL).activates("core.threads_number")
- .in(EXPERIMENTAL).group(SIMULATIONS);
- public static final Entry<Integer> NUMBERS_OF_THREADS = create("core.threads_number",
- "Max. number of threads to use (available processors: " + Runtime.getRuntime().availableProcessors() + ")",
- 4, IType.INT).between(1, null).in(EXPERIMENTAL).group(SIMULATIONS);
- public static final Entry<Boolean> GRID_OPTIMIZATION = create("core.grid_optimization",
- "Enable parallel grid operations", false, IType.BOOL).in(EXPERIMENTAL).group(SIMULATIONS)
- .activates("core.grid_threads");
- public static final Entry<Integer> NUMBERS_OF_GRID_THREADS = create("core.grid_threads",
- "Max. number of threads to use", 4, IType.INT).between(1, null).in(EXPERIMENTAL).group(SIMULATIONS)
- .addChangeListener(new IPreferenceChangeListener<Integer>() {
-
- @Override
- public boolean beforeValueChange(final Integer newValue) {
- return true;
- }
-
- @Override
- public void afterValueChange(final Integer newValue) {
- GAMA.setConcurrencyLevel(newValue);
- System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism",
- String.valueOf(newValue));
- }
- });
- public static final Entry<Integer> SEQUENTIAL_THRESHOLD = create("core.sequential_threshold",
- "Number under which agents will always be executed sequentially", 20, IType.INT).between(1, null)
- .in(EXPERIMENTAL).group(SIMULATIONS);
-
+ 200, IType.INT).between(0, 1000).in(EXPERIMENTAL).group(DISPLAY);
public static final Entry<Boolean> CONSTANT_OPTIMIZATION = create("core.constant_optimization",
"Automatically optimize constant expressions", false, IType.BOOL).in(EXPERIMENTAL).group("Compilation");
public static final Entry<Boolean> AGENT_OPTIMIZATION = create("core.agent_optimization",
@@ -181,7 +181,7 @@ public int createPoisson(final double mean) {
int x = 0;
double t = 0.0;
while (true) {
- t -= FastMath.log(next()) / mean;
+ t -= Math.log(next()) / mean;
if (t > 1.0) {
break;
}
@@ -191,6 +191,8 @@ public int createPoisson(final double mean) {
}
+ public static boolean USE_BITWISE = true;
+
private byte[] createSeed(final Double s, final int length) {
this.seed = s;
Double realSeed = seed;
@@ -200,10 +202,11 @@ public int createPoisson(final double mean) {
if (realSeed < 1) {
realSeed *= Long.MAX_VALUE;
}
- long l = realSeed.longValue();
- // System.out.println("Initial seed: " + seed + "; normalized seed: " +
- // l);
-
+ long l;
+ if (!USE_BITWISE)
+ l = realSeed.longValue();
+ else
+ l = Double.doubleToRawLongBits(realSeed);
final byte[] result = new byte[length];
switch (length) {
case 4:
@@ -230,11 +233,9 @@ public int createPoisson(final double mean) {
public void dispose() {
seed = null;
generator = null;
- // uniform = null;
}
public byte[] generateSeed(final int length) {
- // byte[] result;
return createSeed(seed, length);
}
@@ -341,6 +342,10 @@ public double next() {
return generator.nextDouble();
}
+ public int nextInt() {
+ return generator.nextInt();
+ }
+
/**
* @param matrix
* @return
@@ -418,26 +423,6 @@ public static void testDrawRandomValues(final int min, final int max, final int
System.out.println();
}
- public static void main(final String[] args) {
- final RandomUtils r1 = new RandomUtils(100.0, "mersenne");
- final RandomUtils r2 = new RandomUtils(100.0, "m{ersenne");
- for (int i = 0; i < 2000; i++) {
- System.out.println("r1 " + r1.next() + " | r2 " + r2.next());
- }
- // drawRandomValues(-0.2, 0.2, 0.1);
- // drawRandomValues(4., 5., 0.2);
- // drawRandomValues(0, 100, 3);
- // drawRandomValues(-5, 5, 3);
- // RandomUtils r = new RandomUtils(100.0, "mersenne");
- // for ( int i = 0; i < 10000000; i++ ) {
- // double d = 0.0;
- // if ( r.between(0.0, 0.1) == 0.0 ) {
- // System.out.println("0.0 !");
- // }
- // }
- // System.out.println("Finished");
- }
-
private class BitString {
private static final int WORD_LENGTH = 32;
@@ -595,4 +580,21 @@ public int countSetBits() {
}
+ public static void main(final String[] args) {
+ USE_BITWISE = false;
+ RandomUtils r1 = new RandomUtils(1.0, "mersenne1");
+ RandomUtils r2 = new RandomUtils(1.0 * Math.pow(10, -50), "mersenne2");
+ RandomUtils r3 = new RandomUtils(1.1 * Math.pow(10, -50), "mersenne3");
+ for (int i = 0; i < 3; i++) {
+ System.out.println("r1 " + r1.nextInt() + " | r2 " + r2.nextInt() + " | r3 " + r3.nextInt());
+ }
+ USE_BITWISE = true;
+ r1 = new RandomUtils(1.0, "mersenne1");
+ r2 = new RandomUtils(1.0 * Math.pow(10, -50), "mersenne2");
+ r3 = new RandomUtils(1.1 * Math.pow(10, -50), "mersenne3");
+ for (int i = 0; i < 3; i++) {
+ System.out.println("r1 " + r1.nextInt() + " | r2 " + r2.nextInt() + " | r3 " + r3.nextInt());
+ }
+ }
+
}
@@ -167,6 +167,8 @@ public int getRunNumber() {
public Double launchSimulationsWithSolution(final ParametersSet sol) throws GamaRuntimeException {
// We first reset the currentSolution and the fitness values
final SimulationPopulation pop = getSimulationPopulation();
+ if (pop == null) // interrupted
+ return 0d;
currentSolution = new ParametersSet(sol);
fitnessValues.clear();
runNumber = runNumber + 1;
@@ -180,7 +182,9 @@ public Double launchSimulationsWithSolution(final ParametersSet sol) throws Gama
}
// We then create a number of simulations with the same solution
- final int numberOfCores = pop.getMaxNumberOfConcurrentSimulations();
+ int numberOfCores = pop.getMaxNumberOfConcurrentSimulations();
+ if (numberOfCores == 0)
+ numberOfCores = 1;
int repeatIndex = 0;
while (repeatIndex < getSeeds().length) {
for (int coreIndex = 0; coreIndex < numberOfCores; coreIndex++) {
@@ -202,7 +206,7 @@ public Double launchSimulationsWithSolution(final ParametersSet sol) throws Gama
// cycles += " " + simulation.getClock().getCycle();
// test the condition first in case it is paused
final boolean stopConditionMet = Cast.asBool(sim.getScope(),
- sim.getScope().evaluate(stopCondition, sim));
+ sim.getScope().evaluate(stopCondition, sim).getValue());
final boolean mustStop = stopConditionMet || agent.dead() || agent.getScope().isPaused();
if (mustStop) {
pop.unscheduleSimulation(agent);
@@ -57,7 +57,6 @@
import msi.gama.util.Guava;
import msi.gama.util.IList;
import msi.gama.util.TOrderedHashMap;
-import msi.gaml.descriptions.IDescription;
import msi.gaml.species.ISpecies;
import msi.gaml.statements.IExecutable;
import msi.gaml.types.GamaGeometryType;
@@ -526,25 +525,23 @@ public void setOnUserHold(final boolean state) {
}
@Override
- public boolean step(final IScope scope) {
+ protected boolean preStep(final IScope scope) {
clock.beginCycle();
- boolean result;
- // An experiment always runs in its own scope
- try {
- executer.executeBeginActions();
- result = super.step(this.scope);
- executer.executeEndActions();
- executer.executeOneShotActions();
- final IOutputManager outputs = getOutputManager();
- if (outputs != null) {
- outputs.step(scope);
- }
- } finally {
- clock.step(this.scope);
+ executer.executeBeginActions();
+ return super.preStep(scope);
+ }
- informStatus();
+ @Override
+ protected void postStep(final IScope scope) {
+ super.postStep(scope);
+ executer.executeEndActions();
+ executer.executeOneShotActions();
+ final IOutputManager outputs = getOutputManager();
+ if (outputs != null) {
+ outputs.step(scope);
}
- return result;
+ clock.step(this.scope);
+ informStatus();
}
@Override
@@ -623,16 +620,6 @@ public IExperimentAgent getExperiment() {
}
@Override
- public IDescription getExperimentContext() {
- return ExperimentAgent.this.getSpecies().getDescription();
- }
-
- @Override
- public IDescription getModelContext() {
- return ExperimentAgent.this.getSpecies().getModel().getDescription();
- }
-
- @Override
public Object getGlobalVarValue(final String name) {
if (ExperimentAgent.this.hasAttribute(name) || getSpecies().hasVar(name)) {
return super.getGlobalVarValue(name);
Oops, something went wrong.

0 comments on commit 683d6c4

Please sign in to comment.