Opened 11 years ago

Closed 9 years ago

Last modified 9 years ago

#2690 closed defect (fixed)

Problems with functions returning multi-dimensional arrays in when statements

Reported by: Francesco Casella Owned by: Per Östlund
Priority: high Milestone: 1.9.4
Component: Backend Version: trunk
Keywords: Cc: federico.terraneo@…, alberto.leva@…

Description

Please consider the following test cases:

package TestFunctionArray
  function getVal
    input Integer nx, ny;
    output Real val[nx, ny];
  algorithm
    for i in 1:nx loop
      for j in 1:ny loop
        val[i, j] := i * j;
      end for;
    end for;
  end getVal;

  model TestOnes
    parameter Integer N = 2;
    parameter Real Ts = 0.0001;
    discrete Real v[N, N];
  equation
    when sample(0, Ts) then
      v = ones(N, N);
    end when;
    annotation(experiment(StartTime = 0, StopTime = 0.1, Tolerance = 1e-006, Interval = 0.0002));
  end TestOnes;

  model TestOnesLarge
    extends TestOnes(N = 50);
     annotation(experiment(StartTime = 0, StopTime = 0.1, Tolerance = 1e-006, Interval = 0.0002));
  end TestOnesLarge;

  model TestVal
    parameter Integer N = 2;
    parameter Real Ts = 0.0001;
    discrete Real v[N, N];
  equation
    when sample(0, Ts) then
      v = getVal(N, N);
    end when;
    annotation(experiment(StartTime = 0, StopTime = 0.1, Tolerance = 1e-006, Interval = 0.0002));
  end TestVal;

  model TestValLarge
    extends TestVal(N = 50);
    annotation(experiment(StartTime = 0, StopTime = 0.1, Tolerance = 1e-006, Interval = 0.0002));
  end TestValLarge;

  model TestAlgorithm
    parameter Integer N = 2;
    parameter Real Ts = 0.0001;
    discrete Real v[N, N];
  algorithm
    when sample(0, Ts) then
      v := getVal(N, N);
    end when;
    annotation(experiment(StartTime = 0, StopTime = 0.1, Tolerance = 1e-006, Interval = 0.0002));
  end TestAlgorithm;

  model TestAlgorithmLarge
    extends TestAlgorithm(N = 50);
    annotation(experiment(StartTime = 0, StopTime = 0.1, Tolerance = 1e-006, Interval = 0.0002));
  end TestAlgorithmLarge;
end TestFunctionArray;
  • TestOnes works fine
  • TestOnesLarge requires to kill the omc.exe process, otherwise it goes on forever in the compiling phase
  • TestVal generates incorrect C-code
  • TestValLarge again requires to kill the omc.exe process
  • TestAlgorithm works fine
  • TestAlgorithmLarge works fine, although it takes about 30 seconds to compile

Apparently there is something wrong with functions returning multi-dimensional arrays within when-statements in the back-end, but only if the when statement is used in the equation section. Note that such models are quite common when modelling 2D and 3D thermal systems.

I reckon there is not much testing involving multi-dimensional arrays in the testsuite, so maybe these cases reveal some design flaw in the back-end.

Change History (12)

comment:1 by Martin Sjölund, 11 years ago

TestOnesLarge, front-end time:

N=50.05s
N=100.1s
N=152.3s
N=164.8s
N=179.9s

There is some exponential-time algorithm in there. I will see if I can find it.

comment:2 by Martin Sjölund, 11 years ago

Static.createCrefArray is called 49140x for N=12. 106483x for N=13. Something is very wrong is Static.crefVectorize and createCrefArray2d.
Most of the actual time spent is by ExpressionSimplify, but this is just a result of the former functions being called too many times, I think.

This model is just as slow:

model M
  parameter Integer N = 13;
  discrete Real r[N,N];
equation
  r = ones(N,N);
end M;

While this one is fast (same changes to the original model seems to speed up the front-end):

model M
  parameter Integer N = 13;
  /* discrete */ Real r[N,N];
equation
  r = ones(N,N);
end M;

comment:3 by Martin Sjölund, 11 years ago

Note that with non-discrete Real, crefVectorize is called only 5 times.

comment:4 by Per Östlund, 11 years ago

Owner: changed from Adrian Pop to Per Östlund
Status: newaccepted

comment:5 by Per Östlund, 11 years ago

Resolution: fixed
Status: acceptedclosed

Fixed in r20555. crefVectorize seems to blow up if it doesn't get a spliced exp. The solution was to simply give it the spliced exp for the discrete case, as is done in the non-discrete case. I will investigate a bit more though and see if it's possible to fix crefVectorize so it doesn't for other cases.

comment:6 by federico.terraneo@…, 11 years ago

Resolution: fixed
Status: closedreopened

Although the fix did solve the exponential time required by omc, TestVal still generates incorrect C code, attached is the compiler error

Test.TestVal_06inz.c:140:61: error: passing 'modelica_real' (aka 'double') to parameter of incompatible type 'real_array_t' (aka 'struct base_array_s')
  $Pv$lB2$c2$rB = div_alloc_real_array(sub_alloc_real_array($P$PRE$Pv, tmp0), _OMC_LIT0);
                                                            ^~~~~~~~~
./Test.TestVal_model.h:29:19: note: expanded from macro '$P$PRE$Pv'
#define $P$PRE$Pv data->simulationInfo.realVarsPre[2]
                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/omc/c/util/real_array.h:168:61: note: passing argument to parameter 'a' here
extern real_array_t sub_alloc_real_array(const real_array_t a, const real_array_t b);

comment:7 by Martin Sjölund, 10 years ago

Milestone: 1.9.11.9.2

This ticket was not closed for 1.9.1, which has now been released. It was batch modified for milestone 1.9.2 (but maybe an empty milestone was more appropriate; feel free to change it).

comment:8 by Martin Sjölund, 10 years ago

Milestone: 1.9.21.9.3

Milestone changed to 1.9.3 since 1.9.2 was released.

comment:9 by Martin Sjölund, 9 years ago

Milestone: 1.9.31.9.4

Moved to new milestone 1.9.4

comment:10 by Per Östlund, 9 years ago

Resolution: fixed
Status: reopenedclosed

All of the models now compile and simulate for me, so I assume these issues were fixed some time ago. The issue with TestVal was probably caused by the flattening generating some erroneous type, which I might unknowingly have fixed when fixing some other cref vectorization issues a while ago.

comment:11 by Martin Sjölund, 9 years ago

Milestone: 1.9.41.9.4-1.9.x

Milestone renamed

comment:12 by Martin Sjölund, 9 years ago

Milestone: 1.9.4-1.9.x1.9.4

Milestone renamed

Note: See TracTickets for help on using tickets.