#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 , 11 years ago
comment:2 by , 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:4 by , 11 years ago
Owner: | changed from | to
---|---|
Status: | new → accepted |
comment:5 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | accepted → closed |
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 , 11 years ago
Resolution: | fixed |
---|---|
Status: | closed → reopened |
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 , 10 years ago
Milestone: | 1.9.1 → 1.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 , 10 years ago
Milestone: | 1.9.2 → 1.9.3 |
---|
Milestone changed to 1.9.3 since 1.9.2 was released.
comment:10 by , 9 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
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.
TestOnesLarge, front-end time:
There is some exponential-time algorithm in there. I will see if I can find it.