Navigation Menu

Skip to content

Commit

Permalink
Fixes #2025. Fixes #738.
Browse files Browse the repository at this point in the history
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
AlexisDrogoul committed Oct 16, 2016
1 parent b2fba48 commit 683d6c4
Show file tree
Hide file tree
Showing 84 changed files with 1,397 additions and 1,466 deletions.
10 changes: 4 additions & 6 deletions msi.gama.core/src/msi/gama/common/Activator.java
Expand Up @@ -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();

}
Expand Down
32 changes: 2 additions & 30 deletions msi.gama.core/src/msi/gama/common/GamaPreferences.java
Expand Up @@ -70,6 +70,7 @@ public class GamaPreferences {
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";
Expand Down Expand Up @@ -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",
Expand Down
56 changes: 29 additions & 27 deletions msi.gama.core/src/msi/gama/common/util/RandomUtils.java
Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -200,10 +202,11 @@ private byte[] createSeed(final Double s, final int length) {
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:
Expand All @@ -230,11 +233,9 @@ private byte[] createSeed(final Double s, final int length) {
public void dispose() {
seed = null;
generator = null;
// uniform = null;
}

public byte[] generateSeed(final int length) {
// byte[] result;
return createSeed(seed, length);
}

Expand Down Expand Up @@ -341,6 +342,10 @@ public double next() {
return generator.nextDouble();
}

public int nextInt() {
return generator.nextInt();
}

/**
* @param matrix
* @return
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

}
8 changes: 6 additions & 2 deletions msi.gama.core/src/msi/gama/kernel/experiment/BatchAgent.java
Expand Up @@ -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;
Expand All @@ -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++) {
Expand All @@ -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);
Expand Down
41 changes: 14 additions & 27 deletions msi.gama.core/src/msi/gama/kernel/experiment/ExperimentAgent.java
Expand Up @@ -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;
Expand Down Expand Up @@ -526,25 +525,23 @@ public IPopulation<? extends IAgent> getPopulationFor(final ISpecies species) {
}

@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
Expand Down Expand Up @@ -622,16 +619,6 @@ public IExperimentAgent getExperiment() {
return ExperimentAgent.this;
}

@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)) {
Expand Down

0 comments on commit 683d6c4

Please sign in to comment.