Skip to content

Commit

Permalink
- Added +d=openmp, which slows down simulation in most cases (at leas…
Browse files Browse the repository at this point in the history
…t on 2-CPU systems)

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@10112 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Oct 14, 2011
1 parent 3c35df1 commit b7c50d4
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 16 deletions.
106 changes: 100 additions & 6 deletions Compiler/BackEnd/SimCode.mo
Expand Up @@ -2094,7 +2094,7 @@ algorithm
// new variables
ModelInfo modelInfo;
list<SimEqSystem> allEquations;
list<SimEqSystem> odeEquations; // --> functionODE
list<list<SimEqSystem>> odeEquations; // --> functionODE
list<SimEqSystem> algebraicEquations; // --> functionAlgebraics
list<SimEqSystem> residualEquations; // --> initial_residual
list<SimEqSystem> initialEquations; // --> initial_function
Expand Down Expand Up @@ -2151,6 +2151,8 @@ algorithm

// equation generation for euler, dassl2, rungekutta
(odeEquations,algebraicEquations,allEquations) = createEquationsForSystems(ifcpp,systs,shared,helpVarInfo,{},{},{});

odeEquations = makeEqualLengthLists(odeEquations,RTOpts.noProc());

// Assertions and crap
initialEquations = BackendDAEUtil.foldEqSystem(dlow2,createInitialEquations,{});
Expand All @@ -2172,7 +2174,7 @@ algorithm
varlst2 = listAppend(varlst,varlst1);
vars = BackendDAEUtil.listVar(varlst2);
(allEquations,divLst) = listMap1_2(allEquations,addDivExpErrorMsgtoSimEqSystem,(vars,varlst2,BackendDAE.ONLY_VARIABLES()));
(odeEquations,_) = listMap1_2(odeEquations,addDivExpErrorMsgtoSimEqSystem,(vars,varlst2,BackendDAE.ONLY_VARIABLES()));
(odeEquations,_) = listListMap1_2(odeEquations,addDivExpErrorMsgtoSimEqSystem,(vars,varlst2,BackendDAE.ONLY_VARIABLES()));
(algebraicEquations,_) = listMap1_2(algebraicEquations,addDivExpErrorMsgtoSimEqSystem,(vars,varlst2,BackendDAE.ONLY_VARIABLES()));
(residualEquations,_) = listMap1_2(residualEquations,addDivExpErrorMsgtoSimEqSystem,(vars,varlst2,BackendDAE.ALL()));
(initialEquations,_) = listMap1_2(initialEquations,addDivExpErrorMsgtoSimEqSystem,(vars,varlst2,BackendDAE.ALL()));
Expand Down Expand Up @@ -2200,7 +2202,7 @@ algorithm
recordDecls,
externalFunctionIncludes,
allEquations,
{odeEquations},
odeEquations,
algebraicEquations,
residualEquations,
initialEquations,
Expand Down Expand Up @@ -2240,10 +2242,10 @@ protected function createEquationsForSystems
input BackendDAE.EqSystems systs;
input BackendDAE.Shared shared;
input list<HelpVarInfo> helpVarInfo;
input list<SimEqSystem> odeEquations;
input list<list<SimEqSystem>> odeEquations;
input list<SimEqSystem> algebraicEquations;
input list<SimEqSystem> allEquations;
output list<SimEqSystem> oodeEquations;
output list<list<SimEqSystem>> oodeEquations;
output list<SimEqSystem> oalgebraicEquations;
output list<SimEqSystem> oallEquations;
algorithm
Expand All @@ -2266,7 +2268,7 @@ algorithm
odeEquations1 = createEquations(false, false, ifcpp, false, false, syst, shared, blt_states1, helpVarInfo);
algebraicEquations1 = createEquations(ifcpp, false, ifcpp, false, false, syst, shared, contBlocks1, helpVarInfo);
allEquations1 = createEquations(true, false, true, false, false, syst, shared, comps, helpVarInfo);
odeEquations = listAppend(odeEquations,odeEquations1);
odeEquations = odeEquations1::odeEquations;
algebraicEquations = listAppend(algebraicEquations,algebraicEquations1);
allEquations = listAppend(allEquations,allEquations1);
(odeEquations,algebraicEquations,allEquations) = createEquationsForSystems(ifcpp,systs,shared,helpVarInfo,odeEquations,algebraicEquations,allEquations);
Expand Down Expand Up @@ -2737,6 +2739,7 @@ algorithm
omhome := Settings.getInstallationDirectoryPath();
omhome := System.trim(omhome, "\""); // Remove any quotation marks from omhome.
cflags := System.getCFlags();
cflags := Debug.bcallret2(RTOpts.debugFlag("openmp"),stringAppend,cflags," -fopenmp",cflags);
ldflags := System.getLDFlags();
senddatalibs := System.getSendDataLibs();
platform := System.platform();
Expand Down Expand Up @@ -9792,6 +9795,49 @@ algorithm
end match;
end listMap1_2;

public function listListMap1_2 "
Takes a list and a function over the elements and an additional argument returning a tuple of
two types, which is applied for each element producing two new lists.
See also listMap_2."
input list<list<Type_a>> inTypeALst;
input FuncTypeType_aToType_bType_c inFuncTypeTypeAToTypeBTypeC;
input Type_d extraArg;
output list<list<Type_b>> outTypeBLst;
output list<Type_c> outTypeCLst;
replaceable type Type_a subtypeof Any;
replaceable type Type_d subtypeof Any;
partial function FuncTypeType_aToType_bType_c
input Type_a inTypeA;
input Type_d extraArg;
output Type_b outTypeB;
output list<Type_c> outTypeC;
replaceable type Type_b subtypeof Any;
replaceable type Type_c subtypeof Any;
end FuncTypeType_aToType_bType_c;
replaceable type Type_b subtypeof Any;
replaceable type Type_c subtypeof Any;
algorithm
(outTypeBLst,outTypeCLst):=
match (inTypeALst,inFuncTypeTypeAToTypeBTypeC,extraArg)
local
list<Type_b> f1_1;
list<Type_c> f2_1;
list<list<Type_b>> r1_1;
list<Type_c> r2_1,r2_2;
list<Type_a> f;
list<list<Type_a>> r;
FuncTypeType_aToType_bType_c fn;
case ({},_,_) then ({},{});
case ((f :: r),fn,extraArg)
equation
(f1_1,f2_1) = listMap1_2(f,fn,extraArg);
(r1_1,r2_1) = listListMap1_2(r,fn,extraArg);
r2_2 = listAppend(f2_1,r2_1);
then
((f1_1 :: r1_1),r2_2);
end match;
end listListMap1_2;

protected function solve
input DAE.Exp lhs;
input DAE.Exp rhs;
Expand Down Expand Up @@ -12120,4 +12166,52 @@ algorithm
end matchcontinue;
end fileName2fileIndex;

protected function makeEqualLengthLists
input list<list<A>> lst;
input Integer i;
output list<list<A>> olst;
replaceable type A subtypeof Any;
algorithm
olst := matchcontinue (lst,i)
local
Integer n;
list<A> l;
case (lst,_)
equation
false = RTOpts.debugFlag("openmp") or RTOpts.debugFlag("pthreads");
l = List.flatten(lst);
then l::{};
case (lst,0) then lst;
case (lst,1)
equation
l = List.flatten(lst);
then l::{};
case (lst,i)
equation
n = listLength(lst);
n = intDiv(n,i) + Util.if_(intMod(n,i)>0,1,0);
then makeEqualLengthLists2(lst,n,n,{},{});
end matchcontinue;
end makeEqualLengthLists;

protected function makeEqualLengthLists2
input list<list<A>> lst;
input Integer i;
input Integer n;
input list<A> acc1;
input list<list<A>> acc2;
output list<list<A>> olst;
replaceable type A subtypeof Any;
algorithm
olst := match (lst,i,n,acc1,acc2)
local
list<A> l;
case ({},i,n,{},acc2) then acc2;
case ({},i,n,acc1,acc2) then acc1::acc2;
case (lst,0,n,{},acc2) then makeEqualLengthLists2(lst,n,n,{},acc2);
case (lst,0,n,acc1,acc2) then makeEqualLengthLists2(lst,n,n,{},acc1::acc2);
case (l::lst,i,n,acc1,acc2) then makeEqualLengthLists2(lst,i-1,n,listAppend(l,acc1),acc2);
end match;
end makeEqualLengthLists2;

end SimCode;
6 changes: 6 additions & 0 deletions Compiler/FrontEnd/ModelicaBuiltin.mo
Expand Up @@ -705,6 +705,12 @@ function setCompiler
external "builtin";
end setCompiler;

function setCXXCompiler
input String compiler;
output Boolean success;
external "builtin";
end setCXXCompiler;

function verifyCompiler
output Boolean compilerWorks;
external "builtin";
Expand Down
6 changes: 6 additions & 0 deletions Compiler/Script/CevalScript.mo
Expand Up @@ -1253,6 +1253,12 @@ algorithm
then
(cache,Values.BOOL(true),st);

case (cache,env,"setCXXCompiler",{Values.STRING(str)},st,msg)
equation
System.setCXXCompiler(str);
then
(cache,Values.BOOL(true),st);

case (cache,env,"setCompilerFlags",{Values.STRING(str)},st,msg)
equation
System.setCFlags(str);
Expand Down
41 changes: 31 additions & 10 deletions Compiler/susan_codegen/SimCode/SimCodeC.tpl
Expand Up @@ -1010,26 +1010,46 @@ template functionWhenReinitStatementElse(list<WhenOperator> reinits, Text &preEx
>>
end functionWhenReinitStatementElse;

template functionODE_system(list<SimEqSystem> derivativEquations, Integer n)
::=
let &varDecls = buffer ""
let odeEqs = derivativEquations |> eq => equation_(eq, contextSimulationNonDiscrete, &varDecls /*BUFC*/); separator="\n"
<<
static void functionODE_system<%n%>(int omc_thread_number)
{
<%varDecls%>
<%odeEqs%>
}
>>
end functionODE_system;

template functionODE(list<list<SimEqSystem>> derivativEquations, Text method)
"Generates function in simulation file."
::=
let () = System.tmpTickReset(0)
let &varDecls = buffer "" /*BUFD*/
let odeEquations = (derivativEquations |> eqs => (eqs |> eq =>
equation_(eq, contextSimulationNonDiscrete, &varDecls /*BUFC*/); separator="\n")
;separator="\n")
let funcs = derivativEquations |> eqs hasindex i1 fromindex 0 => functionODE_system(eqs,i1) ; separator="\n"
let nFuncs = listLength(derivativEquations)
let funcNames = derivativEquations |> eqs hasindex i1 fromindex 0 => 'functionODE_system<%i1%>' ; separator=",\n"
let &varDecls2 = buffer "" /*BUFD*/
let stateContPartInline = (derivativEquations |> eqs => (eqs |> eq =>
equation_(eq, contextInlineSolver, &varDecls2 /*BUFC*/); separator="\n")
;separator="\n")
<<
<%funcs%>
static void (*functionODE_systems[<%nFuncs%>])(int) = {
<%funcNames%>
};

int functionODE()
{
state mem_state;
<%varDecls%>
int id,th_id;
state mem_state; /* We need to have separate memory pools for separate systems... */
mem_state = get_memory_state();
<%odeEquations%>
<% if RTOpts.debugFlag("openmp") then '#pragma omp parallel for private(id,th_id) schedule(<%match noProc() case 0 then "dynamic" else "static"%>)' %>
for (id=0; id<<%nFuncs%>; id++) {
th_id = omp_get_thread_num();
functionODE_systems[id](th_id);
}
restore_memory_state(mem_state);

return 0;
Expand Down Expand Up @@ -2206,11 +2226,12 @@ end initValXml;
template commonHeader()
::=
<<
<% if acceptMetaModelicaGrammar() then '#define __OPENMODELICA__METAMODELICA'%>
<% if acceptMetaModelicaGrammar() then "#define __OPENMODELICA__METAMODELICA"%>
<% if RTOpts.debugFlag("openmp") then "#include <omp.h>" else "#define omp_get_thread_num() 0" %>
#include "modelica.h"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <errno.h>
>>
end commonHeader;

Expand Down
7 changes: 7 additions & 0 deletions Compiler/susan_codegen/SimCode/SimCodeTV.mo
Expand Up @@ -2007,6 +2007,13 @@ package RTOpts
function acceptMetaModelicaGrammar
output Boolean outBoolean;
end acceptMetaModelicaGrammar;
function debugFlag
input String flag;
output Boolean outBoolean;
end debugFlag;
function noProc
output Integer n;
end noProc;
end RTOpts;

package Settings
Expand Down

0 comments on commit b7c50d4

Please sign in to comment.