Commit 1f4b3dc9 authored by Sascha Niklas Schneiders's avatar Sascha Niklas Schneiders
Browse files

added basic equation part optimizations for assignmentexpressions

parent d3f2eeae
......@@ -22,7 +22,7 @@
<Common-MontiCar.version>0.0.7</Common-MontiCar.version>
<Embedded-MontiArc.version>0.0.7</Embedded-MontiArc.version>
<Embedded-MontiArc-Behaviour.version>0.0.7</Embedded-MontiArc-Behaviour.version>
<Math.version>0.0.7</Math.version>
<Math.version>0.0.7.1</Math.version>
<Embedded-MontiArc-Math.version>0.0.7</Embedded-MontiArc-Math.version>
<tagging.version>0.0.1</tagging.version>
<!-- .. Libraries .................................................. -->
......
......@@ -17,7 +17,9 @@ import de.se_rwth.commons.logging.Log;
public class MathFunctionFixer {
public static void fixMathFunctions(MathExpressionSymbol mathExpressionSymbol, BluePrintCPP bluePrintCPP) {
boolean notHandled = true;
if (mathExpressionSymbol.isAssignmentExpression()) {
if (mathExpressionSymbol == null) {
notHandled = false;
} else if (mathExpressionSymbol.isAssignmentExpression()) {
fixMathFunctions((MathAssignmentExpressionSymbol) mathExpressionSymbol, bluePrintCPP);
notHandled = false;
} else if (mathExpressionSymbol.isMatrixExpression()) {
......@@ -156,8 +158,8 @@ public class MathFunctionFixer {
} else {
MathCommand mathCommand = bluePrintCPP.getMathCommandRegister().getMathCommand(mathExpressionSymbol.getNameToAccess());
if (mathCommand != null) {
if(MathConverter.curBackend.getBackendName().equals("OctaveBackend"))
bluePrintCPP.addAdditionalIncludeString("Helper");
if (MathConverter.curBackend.getBackendName().equals("OctaveBackend"))
bluePrintCPP.addAdditionalIncludeString("Helper");
mathCommand.convert(mathExpressionSymbol, bluePrintCPP);
}
if (fixForLoopAccess(mathExpressionSymbol, variable, bluePrintCPP)) {
......
......@@ -23,6 +23,8 @@ import java.util.List;
* @author Sascha Schneiders
*/
public class ComponentConverterMethodGeneration {
public static int currentGenerationIndex = 0;
public static void generateExecuteMethod(ExpandedComponentInstanceSymbol componentSymbol, BluePrintCPP bluePrint, MathStatementsSymbol mathStatementsSymbol, GeneratorCPP generatorCPP, List<String> includeStrings) {
Method method = new Method("execute", "void");
......@@ -60,25 +62,45 @@ public class ComponentConverterMethodGeneration {
}
}
if (mathStatementsSymbol != null) {
// add math implementation instructions to method
List<MathExpressionSymbol> newMathExpressionSymbols = new ArrayList<>();
MathOptimizer.currentBluePrint = bluePrint;
int counter = 0;
handleMathStatementGeneration(method, bluePrint, mathStatementsSymbol, generatorCPP, includeStrings);
}
bluePrint.addMethod(method);
}
for (MathExpressionSymbol mathExpressionSymbol : mathStatementsSymbol.getMathExpressionSymbols()) {
private static void handleMathStatementGeneration(Method method, BluePrintCPP bluePrint, MathStatementsSymbol mathStatementsSymbol, GeneratorCPP generatorCPP, List<String> includeStrings) {
// add math implementation instructions to method
List<MathExpressionSymbol> newMathExpressionSymbols = new ArrayList<>();
MathOptimizer.currentBluePrint = bluePrint;
int counter = 0;
List<MathExpressionSymbol> visitedMathExpressionSymbol = new ArrayList<>();
int lastIndex = 0;
for (currentGenerationIndex = 0; currentGenerationIndex < mathStatementsSymbol.getMathExpressionSymbols().size(); ++currentGenerationIndex) {
MathExpressionSymbol mathExpressionSymbol = mathStatementsSymbol.getMathExpressionSymbols().get(currentGenerationIndex);
if (!visitedMathExpressionSymbol.contains(mathExpressionSymbol)) {
if (generatorCPP.useAlgebraicOptimizations()) {
List<MathExpressionSymbol> precedingExpressions = new ArrayList<>();
for (int i = 0; i < counter; ++i)
precedingExpressions.add(mathStatementsSymbol.getMathExpressionSymbols().get(i));
newMathExpressionSymbols.add(MathOptimizer.applyOptimizations(mathExpressionSymbol, precedingExpressions));
if (mathExpressionSymbol != visitedMathExpressionSymbol)
newMathExpressionSymbols.add(MathOptimizer.applyOptimizations(mathExpressionSymbol, precedingExpressions, mathStatementsSymbol));
++counter;
}
MathFunctionFixer.fixMathFunctions(mathExpressionSymbol, bluePrint);
String result = ExecuteMethodGenerator.generateExecuteCode(mathExpressionSymbol, includeStrings);
TargetCodeMathInstruction instruction = new TargetCodeMathInstruction(result, mathExpressionSymbol);
method.addInstruction(instruction);
if (lastIndex == currentGenerationIndex - 1) {
//Log.error("ad");
Instruction lastInstruction = method.getInstructions().get(currentGenerationIndex);
method.getInstructions().remove(currentGenerationIndex);
method.addInstruction(lastInstruction);
}
visitedMathExpressionSymbol.add(mathExpressionSymbol);
System.out.println("lastIndex: "+lastIndex+" current: "+currentGenerationIndex);
lastIndex = currentGenerationIndex;
}
}
bluePrint.addMethod(method);
}
}
......@@ -23,7 +23,9 @@ public class ExecuteMethodGenerator {
/*if (mathExpressionSymbol.isAssignmentDeclarationExpression()) {
return generateExecuteCode((MathValueSymbol) mathExpressionSymbol, includeStrings);
} */
if (mathExpressionSymbol.isAssignmentExpression()) {
if (mathExpressionSymbol == null) {
} else if (mathExpressionSymbol.isAssignmentExpression()) {
result = ExecuteMethodGeneratorHandler.generateExecuteCode((MathAssignmentExpressionSymbol) mathExpressionSymbol, includeStrings);
} else if (mathExpressionSymbol.isCompareExpression()) {
result = ExecuteMethodGeneratorHandler.generateExecuteCode((MathCompareExpressionSymbol) mathExpressionSymbol, includeStrings);
......
package de.monticore.lang.monticar.generator.optimization;
import de.monticore.lang.math.math._symboltable.MathAssignmentOperator;
import de.monticore.lang.math.math._symboltable.MathStatementsSymbol;
import de.monticore.lang.math.math._symboltable.expression.MathArithmeticExpressionSymbol;
import de.monticore.lang.math.math._symboltable.expression.MathAssignmentExpressionSymbol;
import de.monticore.lang.math.math._symboltable.expression.MathExpressionSymbol;
import de.monticore.lang.math.math._symboltable.expression.MathNameExpressionSymbol;
import de.monticore.lang.math.math._symboltable.matrix.MathMatrixExpressionSymbol;
import de.monticore.lang.math.math._symboltable.matrix.MathMatrixNameExpressionSymbol;
import de.monticore.lang.monticar.generator.cpp.converter.ComponentConverterMethodGeneration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Sascha Schneiders
*/
public class MathAssignmentPartResultReuse implements MathOptimizationRule {
MathStatementsSymbol currentMathStatementsSymbol = null;
List<MathExpressionSymbol> encounteredSymbolInstances = new ArrayList<>();
Map<MathExpressionSymbol, String> symbolMap = new HashMap();
int currentId;
MathExpressionSymbol startMathExpressionSymbol = null;
@Override
public void optimize(MathExpressionSymbol mathExpressionSymbol, List<MathExpressionSymbol> precedingExpressions) {
if (mathExpressionSymbol.isAssignmentExpression()) {
if (mathExpressionSymbol == null) {
} else if (mathExpressionSymbol.isAssignmentExpression()) {
optimize((MathAssignmentExpressionSymbol) mathExpressionSymbol, precedingExpressions);
} else if (mathExpressionSymbol.isArithmeticExpression()) {
optimize((MathArithmeticExpressionSymbol) mathExpressionSymbol, precedingExpressions);
} else if (mathExpressionSymbol.isMatrixExpression()) {
optimize((MathMatrixExpressionSymbol) mathExpressionSymbol, precedingExpressions);
} else {
System.out.println("Symbol not handled: " + mathExpressionSymbol.getClass().getName() + " " + mathExpressionSymbol.getTextualRepresentation());
}
}
@Override
public void optimize(MathExpressionSymbol mathExpressionSymbol, List<MathExpressionSymbol> precedingExpressions, MathStatementsSymbol mathStatementsSymbol) {
currentMathStatementsSymbol = mathStatementsSymbol;
encounteredSymbolInstances.clear();
symbolMap.clear();
currentId = 0;
startMathExpressionSymbol = mathExpressionSymbol;
optimize(mathExpressionSymbol, precedingExpressions);
}
public void optimize(MathAssignmentExpressionSymbol mathExpressionSymbol, List<MathExpressionSymbol> precedingExpressions) {
optimize(mathExpressionSymbol.getExpressionSymbol(), precedingExpressions);
}
public void optimize(MathArithmeticExpressionSymbol mathExpressionSymbol, List<MathExpressionSymbol> precedingExpressions) {
optimize(mathExpressionSymbol.getLeftExpression(), precedingExpressions);
optimize(mathExpressionSymbol.getRightExpression(), precedingExpressions);
}
public void optimize(MathMatrixExpressionSymbol mathExpressionSymbol, List<MathExpressionSymbol> precedingExpressions) {
if (mathExpressionSymbol.isMatrixNameExpression()) {
optimize((MathMatrixNameExpressionSymbol) mathExpressionSymbol, precedingExpressions);
} else {
System.out.println("Symbol not handled: " + mathExpressionSymbol.getClass().getName() + " " + mathExpressionSymbol.getTextualRepresentation());
}
}
public void optimize(MathMatrixNameExpressionSymbol mathExpressionSymbol, List<MathExpressionSymbol> precedingExpressions) {
if (encounteredSymbolInstances.contains(mathExpressionSymbol)) {
System.out.println("Found Same Symbol");
String name = "";
if (!symbolMap.containsKey(mathExpressionSymbol)) {
symbolMap.put(mathExpressionSymbol, name = getReplacementName(currentId));
} else {
name = symbolMap.get(mathExpressionSymbol);
}
currentMathStatementsSymbol.replaceMathExpression(constructMathExpressionSymbolForName(name), mathExpressionSymbol);
currentMathStatementsSymbol.addMathExpressionBefore(constructMathExpressionSymbolReplacement(name, mathExpressionSymbol), startMathExpressionSymbol);
--ComponentConverterMethodGeneration.currentGenerationIndex;
} else {
encounteredSymbolInstances.add(mathExpressionSymbol);
System.out.println("Added " + mathExpressionSymbol.getTextualRepresentation() + " to encounterSymbolInstances");
optimize(mathExpressionSymbol.getMathMatrixAccessOperatorSymbol(), precedingExpressions);
}
}
public static String getReplacementName(int currentId) {
return "_I_" + currentId;
}
public static MathExpressionSymbol constructMathExpressionSymbolForName(String name) {
return new MathNameExpressionSymbol(name);
}
public static MathExpressionSymbol constructMathExpressionSymbolReplacement(String name, MathExpressionSymbol mathExpressionSymbol) {
MathAssignmentExpressionSymbol mathAssignmentExpressionSymbol = new MathAssignmentExpressionSymbol();
mathAssignmentExpressionSymbol.setNameOfMathValue("auto " + name);//Use auto for C++ automatic type deduction
mathAssignmentExpressionSymbol.setAssignmentOperator(new MathAssignmentOperator("="));
mathAssignmentExpressionSymbol.setExpressionSymbol(mathExpressionSymbol);
return mathAssignmentExpressionSymbol;
}
}
package de.monticore.lang.monticar.generator.optimization;
import de.monticore.lang.math.math._symboltable.MathStatementsSymbol;
import de.monticore.lang.math.math._symboltable.expression.*;
import de.monticore.lang.math.math._symboltable.matrix.MathMatrixArithmeticExpressionSymbol;
import de.monticore.lang.math.math._symboltable.matrix.MathMatrixExpressionSymbol;
......@@ -20,7 +21,9 @@ public class MathMatrixMultiplicationOrder implements MathOptimizationRule {
@Override
public void optimize(MathExpressionSymbol mathExpressionSymbol, List<MathExpressionSymbol> precedingExpressions) {
if (mathExpressionSymbol.isMatrixExpression()) {
if (mathExpressionSymbol == null) {
} else if (mathExpressionSymbol.isMatrixExpression()) {
optimize((MathMatrixExpressionSymbol) mathExpressionSymbol, precedingExpressions);
} else if (mathExpressionSymbol.isAssignmentExpression()) {
optimize((MathAssignmentExpressionSymbol) mathExpressionSymbol, precedingExpressions);
......@@ -43,6 +46,11 @@ public class MathMatrixMultiplicationOrder implements MathOptimizationRule {
}
}
@Override
public void optimize(MathExpressionSymbol mathExpressionSymbol, List<MathExpressionSymbol> precedingExpressions, MathStatementsSymbol mathStatementsSymbol) {
optimize(mathExpressionSymbol, precedingExpressions);
}
public void optimize(MathNameExpressionSymbol mathExpressionSymbol, List<MathExpressionSymbol> precedingExpressions) {
// do nothing
}
......
package de.monticore.lang.monticar.generator.optimization;
import de.monticore.lang.math.math._symboltable.MathStatementsSymbol;
import de.monticore.lang.math.math._symboltable.expression.*;
import de.monticore.lang.math.math._symboltable.matrix.MathMatrixArithmeticExpressionSymbol;
import de.monticore.lang.math.math._symboltable.matrix.MathMatrixExpressionSymbol;
......@@ -15,9 +16,13 @@ import java.util.List;
* @author Sascha Schneiders
*/
public class MathMultiplicationAddition implements MathOptimizationRule {
@Override
public void optimize(MathExpressionSymbol mathExpressionSymbol, List<MathExpressionSymbol> precedingExpressions) {
if (mathExpressionSymbol.isMatrixExpression()) {
if (mathExpressionSymbol == null){
}
else if (mathExpressionSymbol.isMatrixExpression()) {
optimize((MathMatrixExpressionSymbol) mathExpressionSymbol, precedingExpressions);
} else if (mathExpressionSymbol.isAssignmentExpression()) {
optimize((MathAssignmentExpressionSymbol) mathExpressionSymbol, precedingExpressions);
......@@ -40,6 +45,11 @@ public class MathMultiplicationAddition implements MathOptimizationRule {
}
}
@Override
public void optimize(MathExpressionSymbol mathExpressionSymbol, List<MathExpressionSymbol> precedingExpressions, MathStatementsSymbol mathStatementsSymbol) {
optimize(mathExpressionSymbol, precedingExpressions);
}
public void optimize(MathNameExpressionSymbol mathExpressionSymbol, List<MathExpressionSymbol> precedingExpressions) {
// do nothing
}
......
package de.monticore.lang.monticar.generator.optimization;
import de.monticore.lang.math.math._symboltable.MathStatementsSymbol;
import de.monticore.lang.math.math._symboltable.expression.MathExpressionSymbol;
import java.util.List;
......@@ -15,4 +16,10 @@ public interface MathOptimizationRule {
*/
void optimize(MathExpressionSymbol mathExpressionSymbol, List<MathExpressionSymbol> precedingExpressions);
/*
* Will explore all sub math operations and check whether the optimization rule can be applied.
* If the rule can be applied, it will rewrite the expression.
*/
void optimize(MathExpressionSymbol mathExpressionSymbol, List<MathExpressionSymbol> precedingExpressions, MathStatementsSymbol mathStatementsSymbol);
}
package de.monticore.lang.monticar.generator.optimization;
import de.monticore.lang.math.math._symboltable.MathStatementsSymbol;
import de.monticore.lang.math.math._symboltable.expression.*;
import de.monticore.lang.math.math._symboltable.matrix.MathMatrixArithmeticExpressionSymbol;
import de.monticore.lang.math.math._symboltable.matrix.MathMatrixExpressionSymbol;
......@@ -21,10 +22,14 @@ public class MathOptimizer {
optimizationRules.add(mathOptimizationRule);
}
public static MathExpressionSymbol applyOptimizations(MathExpressionSymbol mathExpressionSymbol, List<MathExpressionSymbol> precedingExpressions) {
public static MathExpressionSymbol applyOptimizations(MathExpressionSymbol mathExpressionSymbol, List<MathExpressionSymbol> precedingExpressions, MathStatementsSymbol mathStatementsSymbol) {
MathExpressionSymbol currentMathExpressionSymbol = mathExpressionSymbol;
if (mathStatementsSymbol == null) {
Log.error("MathStatementsSymbol is null but should not be at this point!");
}
MathExpressionSymbol lastMathExpressionSymbol = null;
for (MathOptimizationRule mathOptimizationRule : optimizationRules) {
mathOptimizationRule.optimize(currentMathExpressionSymbol, precedingExpressions);
mathOptimizationRule.optimize(currentMathExpressionSymbol, precedingExpressions, mathStatementsSymbol);
}
return currentMathExpressionSymbol;
}
......@@ -70,7 +75,10 @@ public class MathOptimizer {
MathExpressionSymbol currentAssignment = mathNameExpressionSymbol;
for (MathExpressionSymbol expressionSymbol : precedingExpressionSymbols) {
if (expressionSymbol.isAssignmentDeclarationExpression()) {
if(expressionSymbol==null){
}
else if (expressionSymbol.isAssignmentDeclarationExpression()) {
MathValueSymbol mathValueSymbol = (MathValueSymbol) expressionSymbol;
if (mathValueSymbol.getName().equals(mathNameExpressionSymbol.getNameToResolveValue())) {
currentAssignment = getCurrentAssignment(mathValueSymbol.getValue(), precedingExpressionSymbols);
......@@ -223,6 +231,7 @@ public class MathOptimizer {
static {
addOptimizationRule(new MathMultiplicationAddition());
addOptimizationRule(new MathMatrixMultiplicationOrder());
addOptimizationRule(new MathAssignmentPartResultReuse());
}
......
......@@ -307,4 +307,16 @@ public class MathOptimizerTest extends AbstractSymtabTest {
generatorCPP.setGenerationTargetPath("./target/generated-sources-cpp/paperMatrixModifier/l1");
generatorCPP.generateFiles(componentSymbol, symtab);
}
@Test
public void testMathAssignmentOptimization1() throws IOException{
TaggingResolver symtab = createSymTabAndTaggingResolver("src/test/resources");
ExpandedComponentInstanceSymbol componentSymbol = symtab.<ExpandedComponentInstanceSymbol>resolve("detection.normalizedLaplacianInstance", ExpandedComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentSymbol);
GeneratorCPP generatorCPP = new GeneratorCPP();
generatorCPP.setUseAlgebraicOptimizations(true);
generatorCPP.setGenerationTargetPath("./target/generated-sources-cpp/optimizer/l1");
generatorCPP.generateFiles(componentSymbol, symtab);
}
}
package detection;
component NormalizedLaplacianInstance{
ports in Q(-oo:oo)^{2500,2500} degree,
in Q(-oo:oo)^{2500,2500} W,
out Q(-oo:oo)^{2500,2500} nLaplacian;
instance NormalizedLaplacian<2500> normLaplaInst;
connect degree -> normLaplaInst.degree;
connect W -> normLaplaInst.W;
connect normLaplaInst.nLaplacian -> nLaplacian;
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment