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 Owned by: mahge930
Priority: high Milestone: 1.9.4
Component: Backend Version: trunk
Keywords: array, function, algorithm Cc: perost, lochel, wbraun, mahge930

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 Changed 10 years ago by gustaf

  • Cc perost lochel wbraun added
  • Keywords array function algorithm added

comment:2 follow-up: Changed 10 years ago by 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).

comment:3 follow-up: Changed 10 years ago by perost

  • Cc mahge930 added

Added Cc for Mahder, he likes array :)

comment:4 in reply to: ↑ 2 Changed 10 years ago by gustaf

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 Changed 10 years ago by mahge930

  • Owner changed from somebody to mahge930
  • Status changed from new to accepted

comment:6 Changed 10 years ago by mahge930

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 :)

comment:7 in reply to: ↑ 3 Changed 10 years ago by mahge930

Replying to perost:

Added Cc for Mahder, he likes array :)

Hate them actually :)

comment:8 Changed 10 years ago by mahge930

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

comment:9 Changed 10 years ago by sjoelund.se

Was testcases added for r25108?

comment:10 Changed 10 years ago by mahge930

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 Changed 10 years ago by gustaf

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 Changed 10 years ago by gustaf

  • Resolution set to fixed
  • Status changed from accepted to closed

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 mahge930 (previous) (diff)

comment:13 Changed 10 years ago by gustaf

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

comment:14 Changed 9 years ago by dietmarw

  • Milestone changed from Future to pre1.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 Changed 7 years ago by sjoelund.se

  • Milestone changed from pre1.9.4 to 1.9.4

Removing the pre1.9.4 milestone in favor of 1.9.4.

Note: See TracTickets for help on using tickets.