View previous topic :: View next topic |
Author |
Message |
Moji
Joined: 30 Sep 2020 Posts: 27
|
Posted: Thu Oct 01, 2020 2:54 pm Post subject: Calling Fortran functions in Matlab (64-bit) |
|
|
Hi,
I have a Fortran DLL (compiled with FTN95 64-bit) and I want to call the exported function of this file in MATLAB (2019). I created a test case, but unfortunately it crashes every time as I call the Fortran function inside the MATLAB. The full description of the test case is described in this page. How can I overcome this issue or is there any available solution for calling Fortran functions in MATLAB (64-bit version)?
I should also mention that in the process that I tested, C++ is used to generate a DLL from Fortran DLL (Silverfrost FTN95 is not in the list of MATLAB supported compilers).
P.S.: I don't know whether it is relevant or not, but my operating system is Windows 10. |
|
Back to top |
|
|
John-Silver
Joined: 30 Jul 2013 Posts: 1520 Location: Aerospace Valley
|
Posted: Fri Oct 02, 2020 9:00 pm Post subject: |
|
|
Try reading up here:
https://www.mathworks.com/help/matlab/write-fortran-mex-files.html _________________ ''Computers (HAL and MARVIN excepted) are incredibly rigid. They question nothing. Especially input data.Human beings are incredibly trusting of computers and don't check input data. Together cocking up even the simplest calculation ... " |
|
Back to top |
|
|
Moji
Joined: 30 Sep 2020 Posts: 27
|
Posted: Mon Oct 05, 2020 9:14 am Post subject: |
|
|
As it is mentioned in the link, a compatible compiler is required and Silverfrost FTN95 is not supported by MATLAB. |
|
Back to top |
|
|
Robert
Joined: 29 Nov 2006 Posts: 446 Location: Manchester
|
Posted: Mon Oct 05, 2020 11:46 pm Post subject: |
|
|
In your Stock Overflow post you say
Quote: | "i.e. it usually works at the beginning and the results are correct but after a while MATLAB crashes with/without an error message. " |
How long is 'after a while'? |
|
Back to top |
|
|
Moji
Joined: 30 Sep 2020 Posts: 27
|
Posted: Tue Oct 06, 2020 10:30 am Post subject: |
|
|
The time range is between 15 secondes and 2 minutes.
I also observed in task manager the process of MATLAB during this time. The main memory and CPU usage increase just as it starts to crash . Specially the main memory that is allocated to MATLAB rises from 600MB to 800MB. |
|
Back to top |
|
|
Robert
Joined: 29 Nov 2006 Posts: 446 Location: Manchester
|
Posted: Tue Oct 06, 2020 11:01 am Post subject: |
|
|
In that time, how many timed would you have called the FTN95 compiled routine? |
|
Back to top |
|
|
Moji
Joined: 30 Sep 2020 Posts: 27
|
Posted: Tue Oct 06, 2020 11:54 am Post subject: |
|
|
The crash happens definitely, even if I call the routine only once. However, before it crashes I can call it as many times as possible and the results are shown and they are all correct.
Even unloading the dll doesn't prevent the crash. |
|
Back to top |
|
|
John-Silver
Joined: 30 Jul 2013 Posts: 1520 Location: Aerospace Valley
|
Posted: Tue Oct 06, 2020 9:40 pm Post subject: |
|
|
Moji, in an earlier comment you wrote:
Quote: | a compatible compiler is required and Silverfrost FTN95 is not supported by MATLAB |
... so how can you expect it to work ? _________________ ''Computers (HAL and MARVIN excepted) are incredibly rigid. They question nothing. Especially input data.Human beings are incredibly trusting of computers and don't check input data. Together cocking up even the simplest calculation ... " |
|
Back to top |
|
|
Moji
Joined: 30 Sep 2020 Posts: 27
|
Posted: Wed Oct 07, 2020 6:51 am Post subject: |
|
|
John, I am not sure whether you read the stack overflow page that I shared along with the problem statement or not. To overcome compiler issue, I used a C++ code, which is then compiled by Visual Studio 2015 (it is supported by MATLAB) to a DLL. This C++ code calls the FORTRAN routine from another DLL file (this DLL file is generated by Silverfrost FTN95 64-bit) and pass the value.
First, I tried to use static library of the FORTRAN routine in C++, but I didn't succeed, then I tried to call it from DLL file (as described above). I couldn't think of any other way to do that. |
|
Back to top |
|
|
Robert
Joined: 29 Nov 2006 Posts: 446 Location: Manchester
|
Posted: Wed Oct 07, 2020 11:10 am Post subject: |
|
|
Quote: | The crash happens definitely, even if I call the routine only once. However, before it crashes I can call it as many times as possible and the results are shown and they are all correct.
Even unloading the dll doesn't prevent the crash. |
I am still unclear what is happening. Is this the situation:
You can call the code you wrote using FTN95. It works and returns the correct answer. You can call it multiple times. At some point (and I am not sure how long in terms of calls to your FTN95 code this is) after a call to the code MATLAB increases its memory use and then crashes. |
|
Back to top |
|
|
Moji
Joined: 30 Sep 2020 Posts: 27
|
Posted: Wed Oct 07, 2020 11:36 am Post subject: |
|
|
Quote: | and I am not sure how long in terms of calls to your FTN95 code this is |
answer: after the first call
You got it almost right. Calling the FORTRAN routine is like activating a time bomb that can not be canceled. Once the FORTRAN function is called, then MATLAB will crash for sure. But until this crash shows itself, MATLAB works fine. I mean, in meanwhile you can do other calculations or you can even call this FORTRAN function more times.
I had this feeling that the root of this crash is related to garbage collection, but I don't have knowledge in this regard. |
|
Back to top |
|
|
|