﻿id	summary	reporter	owner	description	type	status	priority	milestone	component	version	resolution	keywords	cc
5110	Inefficient translation of models with arrays	Rüdiger Franke	Rüdiger Franke	"See the following test example, for which OpenModelica generates inefficient code. Increasing `n` leads to huge increases in memory and CPU usage, until OpenModelica is not able to translate the model anymore.
{{{#!mo
package VectorTest
  constant Integer n = 10;

  function mysum
    input Real[:] u;
    output Real y;
  algorithm
    y := sum(u);
  end mysum;

  function myfor
    input Real[:] u;
    input Real[size(u, 1)] previous_x;
    output Real[size(u, 1)] x;
  algorithm
    for i in 1:size(u,1) loop
      x[i] := previous_x[i] + u[i];
    end for;
  end myfor;

  model m
    input Real[n] u(each start = 1);
    Real[size(u,1)] x1, x2;
    output Real y0, y1, y2;
  equation
    when Clock() then
      for i in 1:size(u,1) loop
        x1[i] = previous(x1[i]) + u[i];
      end for;
      x2 = myfor(u, previous(x2));
    end when;
    y0 = sum(u);
    y1 = mysum(u);
    y2 = mysum(x2);
  end m;

end VectorTest;
}}}
The bad code for y0 is:
{{{#!mo
SIMPLE_ASSIGN
y0 = u[1] + u[2] + u[3] + u[4] + u[5] + u[6] + u[7] + u[8] + u[9] + u[10]
}}}
Better code preserving arrays and for loops is generated when using functions (sometimes it's tricky to really get such code though):
{{{#!mo
SIMPLE_ASSIGN
y1 = VectorTest.mysum(u)
}}}
Unfortunately not in all cases:
{{{#!mo
ARRAY_CALL_ASSIGN
x2 = VectorTest.myfor(u, {previous(x2[1]), previous(x2[2]), previous(x2[3]), previous(x2[4]), previous(x2[5]), previous(x2[6]), previous(x2[7]), previous(x2[8]), previous(x2[9]), previous(x2[10])})
}}}

As a short term solution, it appears doable to place all array equations in functions. But OpenModelica must not roll out arrays when calling these functions -- see equation for x2 above.

In principle, a model can be formulated with functions and sorted equations, so that the front- and backend could just pass it through to the code generation. This would be a starting point for efficient code generation for models with arrays.

In the example at hand, at least the `previous` and maybe also the `sum` operator should preserve arrays. "	enhancement	closed	critical	1.14.0	Backend		fixed		Willi Braun Francesco Casella Per Östlund Karim Adbdelhak
