Skip to content

Commit

Permalink
Merge branch 'master' into masterMW
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Walther committed May 29, 2015
2 parents 5b680ae + c7db961 commit 04f6fca
Show file tree
Hide file tree
Showing 13 changed files with 894 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Compiler/CMakeLists.txt
Expand Up @@ -22,7 +22,7 @@ SET(SRCMO Absyn.mo AbsynDep.mo Algorithm.mo Builtin.mo

SET(ALLMO ${SRCMO} DAEEXT.mo DynLoad.mo Print.mo System.mo Parser.mo
TaskGraphExt.mo Corba.mo Socket.mo ErrorExt.mo Settings.mo
UnitParserExt.mo SimulationResults.mo)
UnitParserExt.mo SimulationResults.mo Serializer.mo)

# RML
IF(NOT RML)
Expand Down
19 changes: 19 additions & 0 deletions Compiler/FrontEnd/Expression.mo
Expand Up @@ -1116,6 +1116,25 @@ algorithm
end match;
end unliftExp;

public function liftExp
input DAE.Exp inExp;
input DAE.Dimension inDimension;
output DAE.Exp outExp;
algorithm
outExp := DAE.ARRAY(Types.liftArray(typeof(inExp), inDimension),
false, List.fill(inExp, dimensionSize(inDimension)));
end liftExp;

public function liftExpList
input DAE.Exp inExp;
input list<DAE.Dimension> inDimensions;
output DAE.Exp outExp = inExp;
algorithm
for dim in listReverse(inDimensions) loop
outExp := liftExp(outExp, dim);
end for;
end liftExpList;

public function liftArrayRight "
This function adds an array dimension to a type on the right side, i.e.
liftArrayRigth(Real[2,3],SOME(4)) => Real[2,3,4].
Expand Down
78 changes: 78 additions & 0 deletions Compiler/FrontEnd/InstVar.mo
Expand Up @@ -590,6 +590,7 @@ algorithm
attr := inAttributes;
else
// Userdefined array type, e.g. type Point = Real[3].
type_mods := liftUserTypeMod(type_mods, inDimensions);
dims := listAppend(inDimensions, dims);
mod := Mod.merge(inMod, type_mods, inEnv, inPrefix);
attr := InstUtil.propagateClassPrefix(inAttributes, inPrefix);
Expand All @@ -615,6 +616,83 @@ algorithm
end try;
end instVar_dispatch;

protected function liftUserTypeMod
"This function adds dimensions to a modifier. This is a bit of a hack to make
modifiers on user-defined types behave as expected, e.g.:

type T = Real[3](start = {1, 2, 3});
T x[2]; // Modifier from T must be lifted to become [2, 3].
"
input DAE.Mod inMod;
input list<DAE.Dimension> inDims;
output DAE.Mod outMod = inMod;
algorithm
if listEmpty(inDims) then
return;
end if;

outMod := matchcontinue outMod
case DAE.MOD()
algorithm
// Only lift modifiers without 'each'.
if not SCode.eachBool(outMod.eachPrefix) then
outMod.eqModOption := liftUserTypeEqMod(outMod.eqModOption, inDims);
outMod.subModLst := list(liftUserTypeSubMod(s, inDims) for s in outMod.subModLst);
end if;
then
outMod;

else outMod;
end matchcontinue;
end liftUserTypeMod;

protected function liftUserTypeSubMod
input DAE.SubMod inSubMod;
input list<DAE.Dimension> inDims;
output DAE.SubMod outSubMod = inSubMod;
algorithm
outSubMod := match outSubMod
case DAE.NAMEMOD()
algorithm
outSubMod.mod := liftUserTypeMod(outSubMod.mod, inDims);
then
outSubMod;
end match;
end liftUserTypeSubMod;

protected function liftUserTypeEqMod
input Option<DAE.EqMod> inEqMod;
input list<DAE.Dimension> inDims;
output Option<DAE.EqMod> outEqMod;
protected
DAE.EqMod eq;
DAE.Type ty;
algorithm
if isNone(inEqMod) then
outEqMod := inEqMod;
return;
end if;

SOME(eq) := inEqMod;

eq := match eq
case DAE.TYPED()
algorithm
eq.modifierAsExp := Expression.liftExpList(eq.modifierAsExp, inDims);
eq.modifierAsValue := Util.applyOption1(eq.modifierAsValue,
ValuesUtil.liftValueList, inDims);
ty := Types.getPropType(eq.properties);
eq.properties := Types.setPropType(eq.properties,
Types.liftArrayListDims(ty, inDims));
then
eq;

else eq;
end match;

outEqMod := SOME(eq);
end liftUserTypeEqMod;

protected function addArrayVarEquation
input FCore.Cache inCache;
input FCore.Graph inEnv;
Expand Down
16 changes: 6 additions & 10 deletions Compiler/FrontEnd/Types.mo
Expand Up @@ -3786,10 +3786,9 @@ public function getPropType "author: LS
input DAE.Properties inProperties;
output DAE.Type outType;
algorithm
outType := match (inProperties)
local Type ty;
case DAE.PROP(type_ = ty) then ty;
case DAE.PROP_TUPLE(type_ = ty) then ty;
outType := match inProperties
case DAE.PROP() then inProperties.type_;
case DAE.PROP_TUPLE() then inProperties.type_;
end match;
end getPropType;

Expand All @@ -3798,12 +3797,9 @@ public function setPropType "Set the Type from Properties."
input DAE.Type ty;
output DAE.Properties outProperties;
algorithm
outProperties := match (inProperties,ty)
local
DAE.Const constFlag;
DAE.TupleConst tupleConst;
case (DAE.PROP(constFlag = constFlag),_) then DAE.PROP(ty,constFlag);
case (DAE.PROP_TUPLE(tupleConst = tupleConst),_) then DAE.PROP_TUPLE(ty,tupleConst);
outProperties := match inProperties
case DAE.PROP() then DAE.PROP(ty, inProperties.constFlag);
case DAE.PROP_TUPLE() then DAE.PROP_TUPLE(ty, inProperties.tupleConst);
end match;
end setPropType;

Expand Down
10 changes: 10 additions & 0 deletions Compiler/FrontEnd/ValuesUtil.mo
Expand Up @@ -2582,5 +2582,15 @@ algorithm
end for;
end arrayContainsEmpty;

public function liftValueList
input Values.Value inValue;
input list<DAE.Dimension> inDimensions;
output Values.Value outValue = inValue;
algorithm
for dim in listReverse(inDimensions) loop
outValue := makeArray(List.fill(outValue, Expression.dimensionSize(dim)));
end for;
end liftValueList;

annotation(__OpenModelica_Interface="frontend");
end ValuesUtil;
24 changes: 17 additions & 7 deletions Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -14835,13 +14835,15 @@ author: waurich TUD 2015-05"
protected
list<BackendDAE.Var> vars, states;
list<Integer> idcs;
array<Boolean> markVars;
algorithm
vars := BackendDAEUtil.getAllVarLst(inDAE);
states := List.filterOnTrue(vars, BackendVariable.isStateVar);
if listEmpty(states) then
highestDerivation := 0;
else
idcs := List.map2(states,getHighestDerivation1,BackendVariable.listVar1(states),0);
markVars := arrayCreate(listLength(states),false);
idcs := List.map3(states,getHighestDerivation1,BackendVariable.listVar1(states),markVars,0);
highestDerivation := List.fold(idcs,intMax,0);
end if;
end getHighestDerivation;
Expand All @@ -14850,22 +14852,30 @@ protected function getHighestDerivation1"checks if a state is the derivative of
author: waurich TUD 2015-05"
input BackendDAE.Var stateIn;
input BackendDAE.Variables allStates;
input array<Boolean> markVarsIn;
input Integer derivationIn;
output Integer derivationOut;
algorithm
derivationOut := matchcontinue(stateIn,allStates,derivationIn)
derivationOut := matchcontinue(stateIn,allStates,markVarsIn,derivationIn)
local
Integer index;
Integer index, pos;
array<Boolean> markVars;
BackendDAE.Var var;
DAE.ComponentRef derCref;
case(BackendDAE.VAR(varKind=BackendDAE.STATE(index=index,derName = SOME(derCref))),_,_)
case(BackendDAE.VAR(varKind=BackendDAE.STATE(index=index,derName = SOME(derCref))),_,_,_)
algorithm
// try to find the derivative in the states
({var},_) := BackendVariable.getVar(derCref, allStates);
({var},{pos}) := BackendVariable.getVar(derCref, allStates);
// has this var already been checked or is the derivative the var itself?
false := arrayGet(markVarsIn,pos);
false := BackendVariable.varEqual(stateIn,var);
true := false;
then getHighestDerivation1(var,allStates,derivationIn+1);
markVars := arrayUpdate(markVarsIn,pos,true);
then getHighestDerivation1(var,allStates,markVars,derivationIn+1);
else
algorithm
for i in List.intRange(arrayLength(markVarsIn)) loop
_ := arrayUpdate(markVarsIn,i,false);
end for;
then derivationIn+1;
end matchcontinue;
end getHighestDerivation1;
Expand Down
25 changes: 14 additions & 11 deletions Compiler/Template/CodegenCppHpcom.tpl
Expand Up @@ -421,6 +421,7 @@ template generateAdditionalConstructorBodyStatements(Option<tuple<Schedule,Sched
case ("openmp") then
let threadFuncs = arrayList(odeSchedule.threadTasks) |> tt hasindex i0 fromindex 0 => generateThread(i0, type, modelNamePrefixStr,"evaluateThreadFunc"); separator="\n"
<<
omp_set_dynamic(0);
<%threadFuncs%>
<%initlocksOde%>
<%initlocksDae%>
Expand Down Expand Up @@ -458,6 +459,7 @@ template generateAdditionalConstructorBodyStatements(Option<tuple<Schedule,Sched
case ("tbb") then
let tbbVars = generateTbbConstructorExtension(odeSchedule.tasks, daeSchedule.tasks, modelNamePrefixStr)
<<
omp_set_dynamic(1);
<%tbbVars%>
>>
else ""
Expand Down Expand Up @@ -744,7 +746,6 @@ template generateParallelEvaluate(list<SimEqSystem> allEquationsPlusWhen, Absyn.
{
this->_evaluateODE = evaluateODE;
this->_command = command;
omp_set_dynamic(1);
<%&varDecls%>
if(_evaluateODE)
{
Expand Down Expand Up @@ -1028,28 +1029,30 @@ template function_HPCOM_Thread(list<SimEqSystem> allEquationsPlusWhen, array<lis
let threadReleaseLocksDae = arrayList(threadTasksOde) |> tt hasindex i0 fromindex 0 => function_HPCOM_releaseThreadLocks(arrayGet(threadTasksDae, intAdd(i0, 1)), "_lockDae", i0, iType); separator="\n"

<<
if (omp_get_dynamic())
omp_set_dynamic(0);
#pragma omp parallel num_threads(<%arrayLength(threadTasksOde)%>)
{
int threadNum = omp_get_thread_num();
//Assign locks first
<%threadAssignLocksOde%>
<%threadAssignLocksDae%>
#pragma omp barrier
if(_evaluateODE)
{
//Assign locks first
<%threadAssignLocksOde%>
#pragma omp barrier
<%odeEqs%>
//Release locks after calculation
#pragma omp barrier
<%threadReleaseLocksOde%>
}
else
{
//Assign locks first
<%threadAssignLocksDae%>
#pragma omp barrier
<%daeEqs%>
//Release locks after calculation
#pragma omp barrier
<%threadReleaseLocksDae%>
}
#pragma omp barrier
//Release locks after calculation
<%threadReleaseLocksOde%>
<%threadReleaseLocksDae%>
}
>>
case ("mpi") then
Expand Down
7 changes: 0 additions & 7 deletions Compiler/Template/SimCodeTV.mo
Expand Up @@ -140,13 +140,6 @@ package builtin
output Real z;
end realDiv;

function listGet
replaceable type TypeVar subtypeof Any;
input list<TypeVar> lst;
input Integer index;
output TypeVar result;
end listGet;

function stringLength
input String str;
output Integer length;
Expand Down
62 changes: 62 additions & 0 deletions Compiler/Util/Serializer.mo
@@ -0,0 +1,62 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2014, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/


encapsulated package Serializer
"file: Serializer.mo
package: Serializer
description: External Stream Utilities
@author: Leonardo Laguna Ruiz [leonardo@wolfram.com]
@date: 2015-05-19
RCS: $Id$
This package provides functions to serialize MetaModelica data.
The external C implementation is in TOP/Compiler/runtime/Serializer.c"


public function outputFile<T> "
Prints the structure of the object."
input T object;
input String filename;
external "C" Serializer_outputFile(object,filename) annotation(Library = {"omcruntime"});
end outputFile;

public function bypass<T> "
Serializes the object and reads it back. This function is used for testing purposes."
input T object;
output T out_object;
external "C" out_object = Serializer_bypass(object) annotation(Library = {"omcruntime"});
end bypass;


annotation(__OpenModelica_Interface="util");
end Serializer;
3 changes: 2 additions & 1 deletion Compiler/boot/LoadCompilerSources.mos
Expand Up @@ -293,7 +293,8 @@ if true then /* Suppress output */
"../Util/System.mo",
"../Util/Util.mo",
"../Util/VarTransform.mo",
"../Util/VisualXML.mo"
"../Util/VisualXML.mo",
"../Util/Serializer.mo"
};
LoadCompilerSourcesRes:= OpenModelica.Scripting.loadFiles(files,numThreads=min(5,OpenModelica.Scripting.numProcessors()));
if not LoadCompilerSourcesRes then
Expand Down
2 changes: 1 addition & 1 deletion Compiler/runtime/CMakeLists.txt
Expand Up @@ -33,7 +33,7 @@ ADD_CUSTOM_TARGET(omc_communication ALL DEPENDS ${IDL_OUTPUT})
SET(CORBASRC ${IDL_OUTPUT} omc_communication_impl.cpp corbaimpl.cpp)
SET(SRC socketimpl.c printimpl.c systemimpl.c settingsimpl.c SimulationResults.c)
SET(CPPSRC unitparser.cpp unitparserext.cpp ptolemyio.cpp Dynload.cpp BackendDAEEXT.cpp ErrorMessage.cpp errorext.cpp
systemimplmisc.cpp ${CORBASRC})
systemimplmisc.cpp ${CORBASRC} serializer.cpp)
SET(OBJ ${SRC} ${CPPSRC})

IF(WIN32)
Expand Down

0 comments on commit 04f6fca

Please sign in to comment.