Opened 10 years ago

Closed 10 years ago

Last modified 7 years ago

#3214 closed defect (fixed)

Failure to generate or compile code when functions returning arrays or arrays of arrays are involved

Reported by: Gustaf Thorslund Owned by: Mahder Alemseged Gebremedhin
Priority: high Milestone: 1.9.4
Component: Backend Version: trunk
Keywords: array, function, algorithm Cc: Per Östlund, Lennart Ochel, Willi Braun, Mahder Alemseged Gebremedhin

Description

This continues the array and array of arrays story from ticket #3212 and #3213, so you may call it matrix revolutions if you like.

In the following code the first two models (RAITest and RAETest) works fine, and if simulated for more than one second they could probably plot a stairway to heaven. The fourth model (AIAITest) does on the other hand create a bit of headache since it generates code that later gives compilation warnings and asserted simulation. For this reason a third model (RAIArrayTest) have been introduced, and it fails a bit earlier when trying to generate code. Unless it's the same bug, this ticket is for AIAITest and new ticket should probably be created for RAIArrayTest.

package ArrayReturnTest
  constant Integer N = 3;
  type E = enumeration (e1, e2, e3);
  type AI = Real[N];
  type AE = Real[E];
  type AIAI = AI[N];
  type AIAE = AE[N];
  
  function RAI
    input Real x;
    output AI a;
  algorithm
    a := {1, 2, 3} * x;
  end RAI;

  function RAE
    input Real x;
    output AE a;
  algorithm
    a := {1, 2, 3} * x;
  end RAE;

  function NextAIAI
    input AIAI state;
    input Real t;
    output AIAI next;
  algorithm
    for i loop
      next[i] := state[i] + RAI(t)*i;
    end for;
  end NextAIAI;
  
  model RAITest // Works fine
    AI state(start = {0,0,0});
  algorithm
    when sample(0, 0.1) then
      state := state + RAI(time);
    end when;
  end RAITest;

  model RAETest // Works fine
    AE state(start = {0,0,0});
  algorithm
    when sample(0, 0.1) then
      state := state + RAE(time);
    end when;
  end RAETest;

  model RAIArrayTest // Fails during code generation
    AI s[N](each start = 0, each fixed = true);
  algorithm
    when sample(0, 0.1) then
      for i loop
  s[i] := s[i] + RAI(time);
      end for;
    end when;
  end RAIArrayTest;
  
  model AIAITest // Fails when trying to compile generated code
    AIAI state(each start = 0, each fixed = true);
  algorithm
    when sample(0, 0.1) then
      state := NextAIAI(state, time);
    end when;
  end AIAITest;
  
end ArrayReturnTest;

Here is what happens when trying to compile the code generated by AIAITest:

gcc   -fPIC -O0 -falign-functions -march=native     -I"/home/gustaf/local/openmodelica/include/omc/c" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o ArrayReturnTest.AIAITest_functions.o ArrayReturnTest.AIAITest_functions.c
ArrayReturnTest.AIAITest_functions.c: In function ‘omc_ArrayReturnTest_NextAIAI’:
ArrayReturnTest.AIAITest_functions.c:32:165: warning: passing argument 2 of ‘copy_real_array_data’ from incompatible pointer type
       copy_real_array_data(add_alloc_real_array(tmp1, mul_alloc_real_array_scalar(omc_ArrayReturnTest_RAI(threadData, _t), ((modelica_real)(modelica_integer)_i))), &(*real_array_element_addr(&_next, 1, /* modelica_integer */ (modelica_integer)_i)));
                                                                                                                                                                     ^
In file included from /home/gustaf/local/openmodelica/include/omc/c/util/modelica.h:88:0,
                 from ArrayReturnTest.AIAITest_functions.h:4,
                 from ArrayReturnTest.AIAITest_functions.c:1:
/home/gustaf/local/openmodelica/include/omc/c/util/real_array.h:84:13: note: expected ‘struct real_array_t *’ but argument is of type ‘modelica_real *’
 extern void copy_real_array_data(const real_array_t source, real_array_t* dest);
             ^

When trying to simulate it the following happens:

$ ./ArrayReturnTest.AIAITest 
base_array.c: array dimensions sizes are NULL!
ArrayReturnTest.AIAITest: util/real_array.c:98: copy_real_array_data: Assertion `base_array_ok(dest)' failed.
Aborted

Kind of sad to see the main actor die at the end...

Anyhow, as said earlier there is a way to fail earlier, even before entering lethal missions. This is where RAIArrayTest comes in. Trying to generate code for it fails with:

build$ omc ../ArrayReturnTest.mo -i=ArrayReturnTest.RAIArrayTest -s -d=initialization
function ArrayReturnTest.RAI
  input Real x;
  output Real[3] a;
algorithm
  a := {x, 2.0 * x, 3.0 * x};
end ArrayReturnTest.RAI;

class ArrayReturnTest.RAIArrayTest
  Real s[1,1](start = 0.0, fixed = true);
  Real s[1,2](start = 0.0, fixed = true);
  Real s[1,3](start = 0.0, fixed = true);
  Real s[2,1](start = 0.0, fixed = true);
  Real s[2,2](start = 0.0, fixed = true);
  Real s[2,3](start = 0.0, fixed = true);
  Real s[3,1](start = 0.0, fixed = true);
  Real s[3,2](start = 0.0, fixed = true);
  Real s[3,3](start = 0.0, fixed = true);
algorithm
  when sample(0.0, 0.1) then
    for i in 1:3 loop
      s[i] := {s[i,1], s[i,2], s[i,3]} + ArrayReturnTest.RAI(time);
    end for;
  end when;
end ArrayReturnTest.RAIArrayTest;
Error processing file: ../ArrayReturnTest.mo
Error: Too few equations, under-determined system. The model has 1 equation(s) and 9 variable(s).
Error: Internal error Transformation Module PFPlusExt index Reduction Method Pantelides failed!

# Error encountered! Exiting...
# Please check the error message and the flags.

Execution failed!

$ omc --version
1.9.2+dev (r25040)

Change History (15)

comment:1 by Gustaf Thorslund, 10 years ago

Cc: Per Östlund Lennart Ochel Willi Braun added
Keywords: array function algorithm added

comment:2 by Per Östlund, 10 years ago

I have fixed #3212 and #3213, but this is a completely different issue that one of the back end developers needs to look at. The first two bugs were due to not handling non-integer ranges and subscripts properly, but that is not the case here since removing the enumeration makes no difference (it's not even used in RAIArrayTest or AIAITest anyway).

comment:3 by Per Östlund, 10 years ago

Cc: Mahder Alemseged Gebremedhin added

Added Cc for Mahder, he likes array :)

in reply to:  2 comment:4 by Gustaf Thorslund, 10 years ago

Replying to perost:

I have fixed #3212 and #3213, but this is a completely different issue that one of the back end developers needs to look at. The first two bugs were due to not handling non-integer ranges and subscripts properly, but that is not the case here since removing the enumeration makes no difference (it's not even used in RAIArrayTest or AIAITest anyway).

Yes, I agree this is different. Since RAIArrayTest and AIAITest failed I didn't write RAETest or AEAETest, but I'll try them once this ticket have been solved.

comment:5 by Mahder Alemseged Gebremedhin, 10 years ago

Owner: changed from somebody to Mahder Alemseged Gebremedhin
Status: newaccepted

comment:6 by Mahder Alemseged Gebremedhin, 10 years ago

AIAITest should be working after r25108.

It had two issues. the first one is what you saw in the error messages. That was just an old code in code generation I hadn't updated after the new changes.

The second problem was with NextAIAI. We were not filling missing subs in the FrontEnd. I am surprised how I didn't see this before. Anyway both issues are fixed now hopefully.

Can you verify if the results are what you expect? If so then we can move to RAIArrayTest :)

in reply to:  3 comment:7 by Mahder Alemseged Gebremedhin, 10 years ago

Replying to perost:

Added Cc for Mahder, he likes array :)

Hate them actually :)

comment:8 by Mahder Alemseged Gebremedhin, 10 years ago

Can you check RAIArrayTest as well? Seems like it was also fixed by the changes in FrontEnd :)

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

Was testcases added for r25108?

comment:10 by Mahder Alemseged Gebremedhin, 10 years ago

No. Not yet. Let's see if Gustaf confirms his results are as expected. Then I can close the ticket and add the models as testcases for it.

comment:11 by Gustaf Thorslund, 10 years ago

AIAITest and RAIArrayTest both works fine now (just tested with r25113). I also tried some enumerated mutations. RAEArrayTest works fine, but AEAETest fails flattening NextAEAE. I can write some tests you can come over and look at before I commit them.

comment:12 by Gustaf Thorslund, 10 years ago

Resolution: fixed
Status: acceptedclosed

Test case added in r25123.

The previous comment about AEAETest was a bug in my test case. Since this bug have never been enumeration type specific, those parts have been removed from the test.

Last edited 10 years ago by Mahder Alemseged Gebremedhin (previous) (diff)

comment:13 by Gustaf Thorslund, 10 years ago

In r25132 the test case was renamed a bit and added to TESTFILES in Makefile.

comment:14 by Dietmar Winkler, 9 years ago

Milestone: Futurepre1.9.4

It doesn't make sense to keep closed ticket in the "Future" milestone that were simply forgotten to assign to the correct milestone in the past.

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

Milestone: pre1.9.41.9.4

Removing the pre1.9.4 milestone in favor of 1.9.4.

Note: See TracTickets for help on using tickets.