简体   繁体   English

在 Dymola 中编译大数组

[英]Compile large array in Dymola

Please consider the following small Modelica model and function:请考虑以下小型 Modelica 模型和功能:

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:这将在 Dymola 中编译和模拟,但查看dsmodel.c中的 C 代码,每个数组元素都被声明为一个新变量:

...
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.因此,如果我通过设置na=1000增加数组大小,我将声明 1000*2000 个变量。 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 .显示的示例仍然可以编译,即使它需要很长时间,但我更复杂的用例失败了编译器warning C4049: compiler limit, terminating line number emissionC1002 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.旁注:较大的示例也需要几分钟来检查,并且在模拟之后,在变量浏览器中展开变量时,GUI 将被阻塞很长时间。

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.使用 Dymola 2020 和 VisualStudio 2017。

Yes, at least the compilation issue can preliminarily be avoided as follows in Dymola 2020 (and possibly earlier versions):是的,至少可以在 Dymola 2020(可能还有更早的版本)中初步避免编译问题,如下所示:

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.我特别注意到 'd' 可以被评估并且根本不使用。

By default all Modelica Compilers will expand all arrays (there are some exceptions like arrays in records in functions) into scalars.默认情况下,所有 Modelica 编译器都会将所有数组(有一些例外,例如函数中记录中的数组)扩展为标量。 OpenModelica started some work on non-expanding arrays in the front-end and back-end. OpenModelica 在前端和后端开始了一些非扩展数组的工作。 See: http://www.ep.liu.se/ecp/157/071/ecp19157071.pdf参见: http : //www.ep.liu.se/ecp/157/071/ecp19157071.pdf

IIRC, some parts of the Modelica specification are written in a way that requires scalarization of all variables. IIRC,Modelica 规范的某些部分是以需要对所有变量进行标量化的方式编写的。 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.对于离散化 PDE 的某些情况,这似乎特别不必要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM