Skip to content

Commit 302ba78

Browse files
committed
+ Preparing the run-time for multi-thread execution.
+ Moved performSimulation from simulation runtime to generated code. + Fixed omp undefined references. + Generate extra equation info. Just the crefs used in equation. To build graphs later. + Hope nothing fails. git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@15913 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent c88e38a commit 302ba78

File tree

10 files changed

+571
-211
lines changed

10 files changed

+571
-211
lines changed

Compiler/BackEnd/SimCodeUtil.mo

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11937,6 +11937,7 @@ algorithm
1193711937
PriorityQueue.T q;
1193811938
list<tuple<Integer, list<SimCode.SimEqSystem>>> prios;
1193911939
list<list<SimCode.SimEqSystem>> lst;
11940+
String eq_str;
1194011941

1194111942
case (lst, _)
1194211943
equation
@@ -11947,6 +11948,8 @@ algorithm
1194711948
case (lst, 1)
1194811949
equation
1194911950
l = List.flatten(lst);
11951+
eq_str = Tpl.tplString2(SimCodeDump.dumpEqsSys, l, false);
11952+
print(eq_str);
1195011953
then l::{};
1195111954
case (lst, _)
1195211955
equation

Compiler/FrontEnd/ExpressionDump.mo

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,13 @@ algorithm
472472
s := Tpl.tplString2(ExpressionDumpTpl.dumpExp, e, "\"");
473473
end printExpStr;
474474

475+
public function printCrefsFromExpStr
476+
input DAE.Exp e;
477+
output String s;
478+
algorithm
479+
s := Tpl.tplString2(ExpressionDumpTpl.dumpExpCrefs, e, "");
480+
end printCrefsFromExpStr;
481+
475482
public function printExp2Str
476483
"function: printExp2Str
477484
Helper function to printExpStr."

Compiler/Template/CodegenC.tpl

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,35 @@ template simulationFile(SimCode simCode, String guid)
213213
#ifdef __cplusplus
214214
}
215215
#endif
216+
217+
/*! Moved from Simulation runtime to support openmp properly.
218+
This way threads can be launched if neccesary
219+
(i.e. if the generated code linked with openmp).
220+
Otherwise normal execution takes place. SimulationruntimeC.lib
221+
doesn't need to be linked with -fopenmp.*/
222+
int performSimulation_optional_thread(DATA* data, SOLVER_INFO* solverInfo)
223+
{
224+
225+
SIMULATION_INFO *simInfo = &(data->simulationInfo);
226+
solverInfo->currentTime = simInfo->startTime;
227+
228+
int retValue = 0;
229+
230+
#ifdef USE_DEBUG_OUTPUT
231+
printAllVarsDebug(data, 0);
232+
#endif
233+
234+
omp_set_num_threads(4);
235+
236+
#pragma omp parallel
237+
{
238+
retValue = main_simulation_loop(data, solverInfo, simInfo);
239+
} /* end #pragma omp parallel*/
240+
241+
return retValue;
242+
}
243+
244+
216245

217246
/* forward the main in the simulation runtime */
218247
extern int _main_SimulationRuntime(int argc, char**argv, DATA *data);
@@ -245,11 +274,23 @@ template simulationFileHeader(SimCode simCode)
245274
#include "simulation_runtime.h"
246275
#include "omc_error.h"
247276
#include "model_help.h"
277+
#include "solver_main.h"
248278

249279
#include <assert.h>
250280
#include <string.h>
251281

252282
#include "<%fileNamePrefix%>_functions.h"
283+
284+
/*! Defintions could not go in header. GNU89 C does't use inline for
285+
ODR. can not be static because they are used in SimulationRuntime.lib.
286+
So defined here. Declaration is in _function.h*/
287+
#ifdef _OPENMP
288+
#include <omp.h>
289+
#else
290+
#define omp_get_max_threads() 1
291+
int omp_get_thread_num() { return 0; }
292+
void omp_set_num_threads() {}
293+
#endif
253294

254295
>>
255296
end match
@@ -3124,6 +3165,8 @@ template commonHeader(String filePrefix)
31243165
#include <omp.h>
31253166
#else
31263167
#define omp_get_max_threads() 1
3168+
int omp_get_thread_num();
3169+
void omp_set_num_threads();
31273170
#endif
31283171
#include "modelica.h"
31293172
#include <stdio.h>

Compiler/Template/ExpressionDumpTpl.tpl

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ template dumpExpList(list<DAE.Exp> expl, String stringDelimiter, String expDelim
161161
::= (expl |> exp => dumpExp(exp, stringDelimiter) ;separator=expDelimiter)
162162
end dumpExpList;
163163
164+
template dumpExpListCrefs(list<DAE.Exp> expl, String stringDelimiter, String expDelimiter)
165+
::= (expl |> exp => dumpExpCrefs(exp, stringDelimiter) ;separator=expDelimiter)
166+
end dumpExpListCrefs;
167+
164168
template dumpCref(DAE.ComponentRef cref)
165169
::=
166170
match cref
@@ -448,6 +452,135 @@ template dumpNamedPattern(tuple<Pattern, String, Type> pattern)
448452
::= match pattern case (pat, id, _) then '<%id%> = <%dumpPattern(pat)%>'
449453
end dumpNamedPattern;
450454

455+
456+
457+
template dumpExpCrefs(DAE.Exp exp, String stringDelimiter)
458+
::=
459+
match exp
460+
case ICONST(__) then ''
461+
case RCONST(__) then ''
462+
case SCONST(__) then ''
463+
case BCONST(__) then ''
464+
case ENUM_LITERAL(__) then AbsynDumpTpl.dumpPath(name)
465+
case CREF(__) then dumpCref(componentRef)
466+
case e as BINARY(__) then
467+
let lhs_str = dumpExpCrefs(exp1, stringDelimiter)
468+
let rhs_str = dumpExpCrefs(exp2, stringDelimiter)
469+
'<%lhs_str%> <%rhs_str%>'
470+
case e as UNARY(__) then
471+
let exp_str = dumpOperand(exp, e, false)
472+
let op_str = dumpUnaryOp(operator)
473+
'<%op_str%><%exp_str%>'
474+
case e as LBINARY(__) then
475+
let lhs_str = dumpExpCrefs(exp1, stringDelimiter)
476+
let rhs_str = dumpExpCrefs(exp2, stringDelimiter)
477+
'<%lhs_str%> <%rhs_str%>'
478+
case e as LUNARY(__) then
479+
let lhs_str = dumpExpCrefs(exp, stringDelimiter)
480+
'<%lhs_str%>'
481+
case e as RELATION(__) then
482+
let lhs_str = dumpExpCrefs(exp1, stringDelimiter)
483+
let rhs_str = dumpExpCrefs(exp2, stringDelimiter)
484+
'<%lhs_str%> <%rhs_str%>'
485+
case IFEXP(__) then
486+
let cond_str = dumpExpCrefs(expCond, stringDelimiter)
487+
let then_str = dumpExpCrefs(expThen, stringDelimiter)
488+
let else_str = dumpExpCrefs(expElse, stringDelimiter)
489+
'<%cond_str%> <%then_str%> <%else_str%>'
490+
case CALL(attr=attr as CALL_ATTR(builtin=true)) then
491+
let argl = dumpExpListCrefs(expLst, stringDelimiter, " ")
492+
'<%argl%>'
493+
case CALL(__) then
494+
let argl = dumpExpListCrefs(expLst, stringDelimiter, " ")
495+
'<%argl%>'
496+
case PARTEVALFUNCTION(__) then
497+
let func_str = AbsynDumpTpl.dumpPathNoQual(path)
498+
let argl = dumpExpList(expList, stringDelimiter, ", ")
499+
'function <%func_str%>(<%argl%>)'
500+
case ARRAY(__) then
501+
let expl = dumpExpList(array, stringDelimiter, ", ")
502+
'<%if typeinfo() then (if scalar then "/* scalar */ " else "/* non-scalar */ ")%>{<%expl%>}'
503+
case MATRIX(__) then
504+
let mat_str = (matrix |> row => dumpExpList(row, stringDelimiter, ", ") ;separator="}, {")
505+
'<%if typeinfo() then '/* matrix <%unparseType(ty) %> */ '%>{{<%mat_str%>}}'
506+
case e as RANGE(__) then
507+
let start_str = dumpOperand(start, e, false)
508+
let step_str = match step case SOME(step) then '<%dumpOperand(step, e, false)%>:'
509+
let stop_str = dumpOperand(stop, e, false)
510+
'<%start_str%>:<%step_str%><%stop_str%>'
511+
case TUPLE(__) then
512+
let tuple_str = dumpExpList(PR, stringDelimiter, ", ")
513+
'(<%tuple_str%>)'
514+
case CAST(__) then
515+
let exp_str = dumpExpCrefs(exp, stringDelimiter)
516+
'(<%exp_str%>)'
517+
case ASUB(__) then
518+
let needs_paren = parenthesizeSubExp(exp)
519+
let lparen = if needs_paren then "("
520+
let rparen = if needs_paren then ")"
521+
let exp_str = dumpExp(exp, stringDelimiter)
522+
let sub_str = dumpExpList(sub, stringDelimiter, ", ")
523+
'<%lparen%><%exp_str%><%rparen%>[<%sub_str%>]'
524+
case TSUB(__) then
525+
let needs_paren = parenthesizeSubExp(exp)
526+
let lparen = if needs_paren then "("
527+
let rparen = if needs_paren then ")"
528+
let exp_str = dumpExp(exp, stringDelimiter)
529+
'<%lparen%><%exp_str%><%rparen%>[<%ix%>]'
530+
case SIZE(__) then
531+
let exp_str = dumpExp(exp, stringDelimiter)
532+
let dim_str = match sz case SOME(dim) then ', <%dumpExp(dim, stringDelimiter)%>'
533+
'size(<%exp_str%><%dim_str%>)'
534+
case CODE(__) then
535+
let code_str = Dump.printCodeStr(code)
536+
'$Code(<%code_str%>)'
537+
case EMPTY(__) then
538+
let name_str = dumpCref(name)
539+
'<EMPTY(scope: <%scope%>, name: <%name_str%>, ty: <%tyStr%>)>'
540+
case REDUCTION(reductionInfo = REDUCTIONINFO(path = name)) then
541+
let name_str = AbsynDumpTpl.dumpPathNoQual(name)
542+
let exp_str = dumpExp(expr, stringDelimiter)
543+
let iter_str = (iterators |> it => dumpReductionIterator(it, stringDelimiter) ;separator=", ")
544+
'<%name_str%>(<%exp_str%> for <%iter_str%>)'
545+
case LIST(__) then
546+
let expl_str = dumpExpList(valList, stringDelimiter, ", ")
547+
'List(<%expl_str%>)'
548+
case CONS(__) then
549+
let car_str = dumpExp(car, stringDelimiter)
550+
let cdr_str = dumpExp(cdr, stringDelimiter)
551+
'listCons(<%car_str%>, <%cdr_str%>)'
552+
case META_TUPLE(__) then
553+
let tuple_str = dumpExpList(listExp, stringDelimiter, ", ")
554+
'Tuple(<%tuple_str%>)'
555+
case META_OPTION(exp = SOME(exp)) then 'SOME(<%dumpExp(exp, stringDelimiter)%>)'
556+
case META_OPTION(__) then 'NONE()'
557+
case METARECORDCALL(__) then
558+
let name_str = AbsynDumpTpl.dumpPath(path)
559+
let args_str = dumpExpList(args, stringDelimiter, ", ")
560+
'<%name_str%>(<%args_str%>)'
561+
case MATCHEXPRESSION(__) then
562+
let match_ty = dumpMatchType(matchType)
563+
let inputs_str = dumpExpList(inputs, stringDelimiter, ", ")
564+
let case_str = (cases |> c => dumpMatchCase(c) ;separator="\n")
565+
<<
566+
<%match_ty%> (<%inputs_str%>)
567+
<%case_str%>
568+
end <%match_ty%>
569+
>>
570+
case BOX(__) then
571+
'#(<%dumpExp(exp, stringDelimiter)%>)'
572+
case UNBOX(__) then
573+
'unbox(<%dumpExp(exp, stringDelimiter)%>)'
574+
case SHARED_LITERAL(__) then
575+
let ty_str = dumpType(ty)
576+
'#SHARED_LITERAL_<%index%>(<%ty_str%>)#'
577+
case PATTERN(__) then dumpPattern(pattern)
578+
else errorMsg("ExpressionDumpTpl.dumpExp: Unknown expression.")
579+
end dumpExpCrefs;
580+
581+
582+
583+
451584
template errorMsg(String errMessage)
452585
::=
453586
let() = Tpl.addTemplateError(errMessage)

Compiler/Template/SimCodeDump.tpl

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,95 @@ template printEquationExpStrEscaped(EquationExp eq)
365365
'<%printExpStrEscaped(lhs)%> = <%printExpStrEscaped(rhs)%>'
366366
end printEquationExpStrEscaped;
367367

368+
369+
template dumpEqsSys(list<SimEqSystem> eqs, Boolean withOperations)
370+
::= eqs |> eq hasindex i0 =>
371+
match eq
372+
case e as SES_RESIDUAL(__) then
373+
<<
374+
<equation index="<%eqIndex(eq)%>">
375+
<residual><%printExpStrEscaped(e.exp)%></residual>
376+
</equation><%\n%>
377+
>>
378+
case e as SES_SIMPLE_ASSIGN(__) then
379+
'<%eqIndex(eq)%> simple_assign <%crefStr(e.cref)%> : <%printCrefsFromExpStr(e.exp)%><%\n%>'
380+
case e as SES_ARRAY_CALL_ASSIGN(__) then
381+
<<
382+
<equation index="<%eqIndex(eq)%>">
383+
<assign type="array">
384+
<lhs><%crefStr(e.componentRef)%></lhs>
385+
<rhs><%printExpStrEscaped(e.exp)%></rhs>
386+
</assign>
387+
</equation><%\n%>
388+
>>
389+
case e as SES_ALGORITHM(statements={}) then 'empty algorithm<%\n%>'
390+
case e as SES_ALGORITHM(statements=first::_)
391+
then
392+
<<
393+
<equation index="<%eqIndex(eq)%>">
394+
<statement>
395+
<%e.statements |> stmt => escapeModelicaStringToXmlString(ppStmtStr(stmt,2)) %>
396+
</statement>
397+
</equation><%\n%>
398+
>>
399+
case e as SES_LINEAR(__) then
400+
'<%eqIndex(eq)%> linear <%e.vars |> SIMVAR(name=cr) => '<%crefStr(cr)%>' ; separator = " " %> : <%beqs |> exp => '<%printExpStrEscaped(exp)%>' ; separator = " " %><%\n%>'
401+
case e as SES_NONLINEAR(__) then
402+
<<
403+
<%dumpEqsSys(SimCodeUtil.sortEqSystems(e.eqs),withOperations)%>
404+
<%eqIndex(eq)%> non_linear <%e.eqs |> eq => '<%eqIndex(eq)%>' ; separator = " " %><%\n%>
405+
>>
406+
/*
407+
<<
408+
<%dumpEqsSys(SimCodeUtil.sortEqSystems(e.eqs),withOperations)%>
409+
<%eqIndex(eq)%>
410+
<%e.crefs |> cr => '<var><%crefStr(cr)%></var>' ; separator = "\n" %>
411+
<%e.eqs |> eq => '<eq index="<%eqIndex(eq)%>"/>' ; separator = "\n" %>
412+
</nonlinear>
413+
</equation><%\n%>
414+
>>
415+
*/
416+
case e as SES_MIXED(__) then
417+
<<
418+
<%dumpEqs(fill(e.cont,1),withOperations)%>
419+
<%dumpEqs(e.discEqs,withOperations)%><%\n%>
420+
<equation index="<%eqIndex(eq)%>">
421+
<mixed>
422+
<continuous index="<%eqIndex(e.cont)%>" />
423+
<%e.discVars |> SIMVAR(name=cr) => '<var><%crefStr(cr)%></var>' ; separator = ","%>
424+
<%e.discEqs |> eq => '<discrete index="<%eqIndex(eq)%>" />'%>
425+
</mixed>
426+
</equation>
427+
>>
428+
case e as SES_WHEN(__) then
429+
<<
430+
<equation index="<%eqIndex(eq)%>">
431+
<when>
432+
<%conditions |> cond => '<cond><%crefStr(cond)%></cond>' ; separator="\n" %>
433+
<lhs><%crefStr(e.left)%></lhs>
434+
<rhs><%printExpStrEscaped(e.right)%></rhs>
435+
</when>
436+
</equation><%\n%>
437+
>>
438+
case e as SES_IFEQUATION(__) then
439+
let branches = ifbranches |> (_,eqs) => dumpEqsSys(eqs,withOperations)
440+
let elsebr = dumpEqsSys(elsebranch,withOperations)
441+
<<
442+
<%branches%>
443+
<%elsebr%>
444+
<equation index="<%eqIndex(eq)%>">
445+
<ifequation /> <!-- TODO: Fix me -->
446+
</equation><%\n%>
447+
>>
448+
else error(sourceInfo(),"dumpEqs: Unknown equation")
449+
end dumpEqsSys;
450+
451+
452+
453+
454+
455+
456+
368457
end SimCodeDump;
369458

370459
// vim: filetype=susan sw=2 sts=2

Compiler/Template/SimCodeTV.mo

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2447,6 +2447,10 @@ package ExpressionDump
24472447
input DAE.Exp e;
24482448
output String s;
24492449
end printExpStr;
2450+
function printCrefsFromExpStr
2451+
input DAE.Exp e;
2452+
output String s;
2453+
end printCrefsFromExpStr;
24502454
end ExpressionDump;
24512455

24522456
package Config

SimulationRuntime/c/simulation/solver/model_help.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,18 @@ void updateDiscreteSystem(DATA *data)
108108
*/
109109
void updateContinuousSystem(DATA *data)
110110
{
111-
functionODE(data);
112-
functionAlgebraics(data);
113-
output_function(data);
114-
function_storeDelayed(data);
115-
storePreValues(data);
111+
112+
// printf("continuous %d \n", omp_get_thread_num());
113+
// fflush(stdout);
114+
115+
if(omp_get_thread_num() == 0)
116+
{
117+
functionODE(data);
118+
functionAlgebraics(data);
119+
output_function(data);
120+
function_storeDelayed(data);
121+
storePreValues(data);
122+
}
116123
}
117124

118125
/*! \fn saveZeroCrossings

0 commit comments

Comments
 (0)