-
Notifications
You must be signed in to change notification settings - Fork 299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
State record with integer variable is not handled correctly in external function #8591
Comments
This probably a result of a package constant not being evaluated by the external function handling. See this comment from @perost in #8738. I guess. in this specific case, your library (the dll) was not found by |
No, The issue seems to be two things. The first is that the names don't match, i.e. we generate a struct called The other issue is that they actually are incompatible, becuase we use So if you have a model such as: record R
Real x;
Integer y;
Integer z;
end R;
function f
output R r;
external "C" f_impl(r) annotation(Include="#include \"f.h\"");
end f;
model M
R r;
Integer y; // Need this or the record won't be output in the result for some reason
equation
r = f();
y = r.y;
end M; and then the external C code: typedef struct ExternalR {
double x;
int y;
int z;
} ExternalR;
void f_impl(ExternalR *r)
{
r->x = 1.0;
r->y = 2;
r->z = 3;
} The simulating this gives |
Ahh. I saw the same message when the dll was not found in #8738 and together with you comment assumed it was caused by it. It is too much to hope for :(. Now that I read the description properly I can see that I was way off since the message I got was not just a warning but an actual error as well. Thanks for taking the time to check. I guess this is something to be discussed properly because we can not just change |
Yeah, the only practical solution I can think of would be to generate separate declarations for external structs and convert them when passing them back and forth. It's odd that no one has noticed this before but I guess external records aren't that common, and padding sometimes hides the issue too (the reason why the record has two integers in my example). |
@mahge do you think you can handle this?
Indeed, I'm only aware of this happening in ExternalMedia. Which has ben around for 15 yrs, but we've not been able to deal with it in omc until recently.
This is really diabolic :) |
@perost we need to sort this out to support ExternalMedia in OMC, which is something many people are asking (including myself). Are you now working on this? It would be good to have at least a workaround to make some progress. Thanks! |
No, I assumed @mahge would look into it since he's worked on the code generation for records in the past. |
@mahge can you do that? It turns out this is also needed to run Buildings, so it is really top priority. Thanks! |
Devmeeting discussion: @mahge cleaned up the source code to use modelica_int everywhere. In order to ensure external function compilance, this is then defined as There could be some problem if one writes 10000000000, which is an Integer constant that doesn't fit 32-bits. However, in all practical cases, 10000000000 means 10000000000.0, so we just issue a warning and convert the large integer literal into a Real Modelica variable. |
@mahge do you have a tentative date for the merging of the fixes with Modelica_int into master? |
No problem 🙂 It was not meant to be. It was actually surprising that it worked for as much as it did. It is not theoretically impossible but I did not expect it to work at first. We at least know exactly what would break if we ever need to do it again. I hope not. We will go with the alternative. With some help from the NF in finding these records, I think we can do it without having to traverse or generate more that we need. This story concerns a fox that tries to eat grapes from a vine but cannot reach them. Rather than admit defeat, he states they are undesirable. The expression "sour grapes" originated from this fable. |
Nondum matura est! (it is not yet ripe) 😄 |
Minor update: with current ExternalMedia master modelica-3rdparty/ExternalMedia@97fb739 and OM 1.19.2, |
@bilderbuchi the current implementation uses 32 bits instead of 64 (or the other way round), so the result is totally unpredictable until we have a proper solution. @mahge is working on that. |
Update after today's devmeeting: @mahge thought about it. Completely rewriting the code generation to use A reasonable solution is to flag external functions in the frontend, and then use special code generation for them only. @perost, @mahge, please agree on a solution that is not too cumbersome to implement, so we can finally get ExternalMedia to work in OMEdit. Thanks! |
Outcome of today's discussion at the OSMC devmeeting:
Plan for action
|
Working for the case in the ticket (including the MWE posted by Per) but there are still broken things in the testsuite. At least as of right now.
It is good enough to support ExternaMedia as it is today. Assuming it is not using anything more complicated than simple types in records.
It breaks some things we have in the testsuite. Last check had 578 tests failing. Changes like this cannot be done just for that model. The changes we make affect everything sometimes, so I have to follow up and fix the broken parts.
When do you plan to have a release of ExternaMedia? If it not absolutely urgent I want to go back to the previous fix and test out @adrpo's suggestions before we merge this one. If that works, it is probably better. |
Ouch, I didn't get it. This changes the scenario significantly, because it could still take a lot of time to fix them.
It doesn't.
@mahge I totally agree. ExternalMedia is ready to be released, whether or not this issue works in OpenModelica. Support for ExternalMedia can wait a couple more weeks for 1.20.0 to be branched off. If then it works, we could have a 1.20.1 with that fix only in one or two months, so it is available in a released version. Thanks! |
I confirm that I am getting the correct results with ExternalMedia on the latest nightly builds. Thanks @mahge! We will proceed ASAP with a new release of the library and add it to the library testsuite. |
Download the ExternalMedia 3.3.1 library from the Github release page: https://github.com/modelica-3rdparty/ExternalMedia/releases/download/v3.3.1/ExternalMedia_v3.3.1.zip
If I run
ExternalMedia.Test.TestMedium.TestState
with the latest nightly build on Windows, I get this warningThe simulation runs successfully, but the result displayed in OMEdit for
state.phase
, which is supposed to be an integer value of one, is instead zero at time zero, 1.4e14 during the simulation, and 6.2e12 at the final instant. Clearly something goes wrong as that integer is returned from the external function and passed back to the Modelica environment.@mahge, can you please check? The results in Linux may be different, but probably still plain wrong.
The text was updated successfully, but these errors were encountered: