Compile large array in Dymola
Asked Answered
W

3

3

Please consider the following small Modelica model and function:

model VectorizeDemo
  parameter Integer na=5;
  final parameter Integer nb=2*na;
  final parameter Real a[na] = {2*i for i in 1:na};
  final parameter Real b[nb] = {3*i for i in 1:nb};
  Real c[na];
  Real d[na,nb];

protected 
  function myFun
    input Real A;
    input Real B;
    output Real C;
  algorithm 
    C:=tanh(A)*sin(B);
  end myFun;

equation 
  c = sin(a);
  //d = myFun(a,b);
  // inner loop first
  d = {myFun(a[i], b[j]) for j in 1:nb, i in 1:na};
end VectorizeDemo;

This will compile and simulate in Dymola, but looking at the C code in dsmodel.c every array element is declared as a new variable:

...
DeclareVariable("d[4, 10]", "", 38.0, 0.0,0.0,0.0,0,513)
DeclareVariable("d[5, 1]", "", 13.0, 0.0,0.0,0.0,0,513)
DeclareVariable("d[5, 2]", "", 16.0, 0.0,0.0,0.0,0,513)
DeclareVariable("d[5, 3]", "", 19.0, 0.0,0.0,0.0,0,513)
...

So, if I increase the array size by setting na=1000 I will have 1000*2000 variables declared. The shown example will still compile, even though it takes very long, but my more complex use case fails with compiler warning C4049: compiler limit, terminating line number emission or with C1002 compiler is out of heap space.

Sidenotes: The larger example will also take several minutes to check, and after simulation the GUI will be blocked for ages when unfolding the variables in the variable browser.

Is there any workaround, like rewriting my code or setting some flag? Temporarily increase heap space? I need to run the model only once. Any insight in what is happening would also be appreciated. Using Dymola 2020, with VisualStudio 2017.

Whitehot answered 9/8, 2019 at 11:8 Comment(2)
After thinking about it, this example from the ScalableTestsuite seems to do the same: github.com/casella/ScalableTestSuite/blob/master/…Whitehot
C1002 compiler is out of heap space error can sometimes be avoided by using Dymola in 64bit mode via Advanced.CompileWith64 = 2 and possibly also closing other applications (Chrome, Outlook etc), or using a different computer with more memory.Whitehot
P
2

By default all Modelica Compilers will expand all arrays (there are some exceptions like arrays in records in functions) into scalars. OpenModelica started some work on non-expanding arrays in the front-end and back-end. See: http://www.ep.liu.se/ecp/157/071/ecp19157071.pdf

Pinxit answered 9/8, 2019 at 12:29 Comment(0)
G
4

Yes, at least the compilation issue can preliminarily be avoided as follows in Dymola 2020 (and possibly earlier versions):

Real d[na,nb] annotation(__Dymola_HideArray=true);

However, there might be other possibilities as well - and the example doesn't fully clarify how it is intended be used. In particular I noted that 'd' can be evaluated and isn't used at all.

Glutenous answered 12/8, 2019 at 12:15 Comment(3)
The real use case is for creating plots for validation, evaluating is fine, hiding from the .mat file is not.Whitehot
Nice annotation. Is it equivalent to HideArray, which is documented in the Dymola User Manual 2?Marcoux
@Marcoux Yes, adding _Dymola in front of annotation-names is just done to conform to the standard.Glutenous
P
2

By default all Modelica Compilers will expand all arrays (there are some exceptions like arrays in records in functions) into scalars. OpenModelica started some work on non-expanding arrays in the front-end and back-end. See: http://www.ep.liu.se/ecp/157/071/ecp19157071.pdf

Pinxit answered 9/8, 2019 at 12:29 Comment(0)
U
2

IIRC, some parts of the Modelica specification are written in a way that requires scalarization of all variables. That is not necessary for many cases, but simpler for some common cases in index reduction. It seems particularly unnecessary for some cases of discretized PDE.

Unselfish answered 11/8, 2019 at 17:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.