From 329fcf12e18915dc62c6213a5ecdbb832ef84c9d Mon Sep 17 00:00:00 2001 From: villberg Date: Mon, 16 Jun 2014 12:51:48 +0000 Subject: [PATCH] Some performance optimization refs #4765 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@29618 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../solver/EnumElementsVariableBase.java | 2 +- .../sysdyn/solver/EnumSizeVariableBase.java | 2 +- .../semantum/sysdyn/solver/Environment.java | 75 ++++++++++++++++--- .../src/fi/semantum/sysdyn/solver/Solver.java | 2 +- .../fi/semantum/sysdyn/solver/Variable.java | 11 ++- .../semantum/sysdyn/solver/VariableBase.java | 51 ++++++++++++- .../simantics/sysdyn/manager/GameResult.java | 26 ++----- .../sysdyn/manager/SimulateDurationJob.java | 6 ++ .../sysdyn/manager/SysdynGameExperiment.java | 10 +-- .../manager/SysdynGameExperimentBase.java | 12 +-- .../manager/SysdynGameExperimentInternal.java | 6 +- 11 files changed, 154 insertions(+), 49 deletions(-) diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/EnumElementsVariableBase.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/EnumElementsVariableBase.java index d80d5dfd..4ef4da1e 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/EnumElementsVariableBase.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/EnumElementsVariableBase.java @@ -29,7 +29,7 @@ public class EnumElementsVariableBase extends VariableBase { } @Override - public Object evaluate(IEnvironment environment, IExpression[] subscripts) { + public Object evaluate(IEnvironment environment, IExpression[] subscripts, int constantIndex) { return result; } diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/EnumSizeVariableBase.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/EnumSizeVariableBase.java index 6ed388f0..63db718a 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/EnumSizeVariableBase.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/EnumSizeVariableBase.java @@ -25,7 +25,7 @@ public class EnumSizeVariableBase extends VariableBase { } @Override - public Object evaluate(IEnvironment environment, IExpression[] subscripts) { + public Object evaluate(IEnvironment environment, IExpression[] subscripts, int constantIndex) { return value; } diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Environment.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Environment.java index 90f84a9b..f3a96b8d 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Environment.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Environment.java @@ -383,6 +383,7 @@ final public class Environment implements IEnvironment, ISystem { } double[] valueArray; + int[] valueIndices; public void addIndexed(int dimensions[], ArrayList keys, String prefix, int d) { @@ -462,13 +463,15 @@ final public class Environment implements IEnvironment, ISystem { keyIndexMap.put(key, index); } + valueIndices = new int[keys.size()]; + getValueIndices(); + return keys.toArray(new String[keys.size()]); } + + public int[] getValueIndices() { - // TODO: this is probably not smart at all, figure out a better way to obtain results - public double[] getValueArray() { - int offset = 0; Set used = new HashSet(); @@ -477,17 +480,14 @@ final public class Environment implements IEnvironment, ISystem { Variable v = model.assignmentArray[i].target; if(!used.add(v.base.name)) continue; for(int index=0;index entry : model.copies.entrySet()) { VariableBase base = entry.getValue(); if(!used.add(entry.getKey())) continue; for(int index=0;index used = new HashSet(); +// +// for (int i = 0; i < model.assignmentArray.length; i++) { +// Variable v = model.assignmentArray[i].target; +// if(!used.add(v.base.name)) continue; +// for(int index=0;index entry : model.copies.entrySet()) { +// VariableBase base = entry.getValue(); +// if(!used.add(entry.getKey())) continue; +// for(int index=0;index 1) { + + if(subscripts != null) { + + Array[] sub = SolverUtils.parseSubscripts(environment, subscripts); + if(!SolverUtils.isSlice(sub) && areSubscriptsConstant(subscripts)) return index(environment, subscripts); + + } + + return -1; + + } + + Object result = environment.getNamedValue(name); + if(result != null) return -1; + + if(!areSubscriptsConstant(subscripts)) return -1; + + return index(environment, subscripts); + + } + + } + + boolean areSubscriptsConstant(IExpression[] subscripts) { + if(subscripts == null) return true; + for(IExpression e : subscripts) { + if(!(e instanceof Constant)) return false; + } + return true; + } + + public Object evaluate(IEnvironment environment, IExpression[] subscripts, int constantIndex) { + + if(constantIndex >= 0 && environment instanceof Environment) { + Double result = (Double)environment.getValue(constantIndex); + if(result == null) + throw new UnassignedVariableException("No value for " + name); + if(SolverUtils.isArray(dimensions) && subscripts == null) { + return new Array().addElement(result); + } else { + return result; + } + } if(isStoredAsArray()) { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/GameResult.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/GameResult.java index dc08d760..8997ca07 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/GameResult.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/GameResult.java @@ -1,7 +1,7 @@ package org.simantics.sysdyn.manager; -import java.util.ArrayList; -import java.util.HashMap; +import gnu.trove.list.array.TDoubleArrayList; +import gnu.trove.map.hash.THashMap; import org.simantics.modelica.data.DataSet; import org.simantics.modelica.data.SimulationResult; @@ -13,30 +13,18 @@ import org.simantics.modelica.data.SimulationResult; */ public class GameResult extends SimulationResult { - private final SysdynGameExperimentBase base; - - public GameResult(SysdynGameExperimentBase sysdynGameExperiment, HashMap> results, String[] subscription) { + public GameResult(SysdynGameExperimentBase sysdynGameExperiment, THashMap results, String[] subscription) { - base = sysdynGameExperiment; // Get times - ArrayList timeList = results.get("time"); - double[] times = new double[timeList.size()]; - for(int i = 0; i < timeList.size(); i++) { - times[i] = timeList.get(i); - } + TDoubleArrayList timeList = results.get("time"); + double[] times = timeList.toArray(); String name; - double[] values; - ArrayList valueList; for(int k = 0; k < subscription.length; k++) { name = subscription[k]; - values = new double[timeList.size()]; - valueList = results.get(name); + TDoubleArrayList valueList = results.get(name); if(valueList.size() == timeList.size()) { - for(int i = 0; i < valueList.size(); i++) { - values[i] = valueList.get(i); - } - this.variables.add(new DataSet(name, times, values)); + this.variables.add(new DataSet(name, times, valueList.toArray())); } else { System.err.println("wrong amount of values " + name); } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SimulateDurationJob.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SimulateDurationJob.java index 0f183284..9ff49128 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SimulateDurationJob.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SimulateDurationJob.java @@ -31,6 +31,8 @@ class SimulateDurationJob extends Job { if(base == null || base.getState() != ExperimentState.STOPPED) return Status.OK_STATUS; + long start = System.nanoTime(); + base.changeState(ExperimentState.RUNNING); int nSteps = (int)(duration / base.stepLength); int work = 1 + nSteps * 3 + 2; // initialization + number of steps * number of phases per step + set result + call result listeners @@ -90,6 +92,10 @@ class SimulateDurationJob extends Job { e.printStackTrace(); System.err.println("SysdynGameExperiment simulateDuration failed: \n\t" + e.getMessage()); } + + long end = System.nanoTime(); + System.err.println("simulate duration took " + 1e-6*(end-start) + "ms."); + return Status.OK_STATUS; } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperiment.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperiment.java index d3f8f19f..6d8d4a92 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperiment.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperiment.java @@ -11,20 +11,21 @@ *******************************************************************************/ package org.simantics.sysdyn.manager; +import gnu.trove.list.array.TDoubleArrayList; +import gnu.trove.map.hash.THashMap; + import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.lang.reflect.Field; -import java.util.ArrayList; import java.util.HashMap; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.NullProgressMonitor; import org.simantics.Simantics; import org.simantics.databoard.Bindings; -import org.simantics.db.AsyncReadGraph; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.Session; @@ -35,7 +36,6 @@ import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.util.Layer0Utils; import org.simantics.db.layer0.variable.Variable; import org.simantics.db.layer0.variable.Variables; -import org.simantics.db.procedure.AsyncListener; import org.simantics.db.request.Read; import org.simantics.fmu.FMUControlJNI; import org.simantics.fmu.FMUJNIException; @@ -43,12 +43,10 @@ import org.simantics.modelica.IModelicaMonitor; import org.simantics.modelica.ModelicaManager; import org.simantics.modelica.SimulationLocation; import org.simantics.modeling.PartialIC; -import org.simantics.simulation.experiment.ExperimentState; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.simulation.SimulationJob.HeadlessModelicaMonitor; import org.simantics.sysdyn.solver.ISolver; import org.simantics.sysdyn.solver.ModelicaSolver; -import org.simantics.utils.datastructures.Quad; /** * Game experiment @@ -79,7 +77,7 @@ public class SysdynGameExperiment extends SysdynGameExperimentBase { if(control == null) control = new FMUControlJNI(); - results = new HashMap>(); + results = new THashMap(); } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperimentBase.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperimentBase.java index 91f8e118..421e75d2 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperimentBase.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperimentBase.java @@ -11,7 +11,9 @@ *******************************************************************************/ package org.simantics.sysdyn.manager; -import java.util.ArrayList; +import gnu.trove.list.array.TDoubleArrayList; +import gnu.trove.map.hash.THashMap; + import java.util.HashMap; import org.eclipse.core.runtime.IProgressMonitor; @@ -43,7 +45,7 @@ abstract public class SysdynGameExperimentBase extends OldSysdynExperiment { protected HashMap subscriptionIndexes; protected double[] currentValues; // Current values from FMU. Updated with updateSubscriptions - HashMap> results; + THashMap results; protected String[] subscription; @@ -92,8 +94,6 @@ abstract public class SysdynGameExperimentBase extends OldSysdynExperiment { public void setSubscribedResults(IProgressMonitor monitor, double time) throws FMUJNIException { -// System.err.println("setSubscribedResults " + time); - monitor.subTask("Get results (time = " + time + ")"); currentValues = getSolver().getSubscribedResults(currentValues); monitor.worked(1); @@ -110,9 +110,9 @@ abstract public class SysdynGameExperimentBase extends OldSysdynExperiment { private double lastResultTime = Double.NaN; public void setResults(double time, String key, Double value) { - ArrayList list = results.get(key); + TDoubleArrayList list = results.get(key); if(list == null) { - list = new ArrayList(); + list = new TDoubleArrayList(); results.put(key, list); } if(time == lastResultTime) { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperimentInternal.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperimentInternal.java index a3864dfe..1dfc9a8d 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperimentInternal.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperimentInternal.java @@ -11,8 +11,10 @@ *******************************************************************************/ package org.simantics.sysdyn.manager; +import gnu.trove.list.array.TDoubleArrayList; +import gnu.trove.map.hash.THashMap; + import java.io.IOException; -import java.util.ArrayList; import java.util.HashMap; import org.eclipse.core.runtime.IProgressMonitor; @@ -55,7 +57,7 @@ public class SysdynGameExperimentInternal extends SysdynGameExperimentBase { super.init(g); - results = new HashMap>(); + results = new THashMap(); solver = new InternalSolver(this, sysdynModel, true, new ISolverMonitor() { -- 2.47.1