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.
C1002 compiler is out of heap space
error can sometimes be avoided by using Dymola in 64bit mode viaAdvanced.CompileWith64 = 2
and possibly also closing other applications (Chrome, Outlook etc), or using a different computer with more memory. – Whitehot