forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Calling Fortran functions in Matlab (64-bit)

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> 64-bit
View previous topic :: View next topic  
Author Message
Moji



Joined: 30 Sep 2020
Posts: 6

PostPosted: Thu Oct 01, 2020 2:54 pm    Post subject: Calling Fortran functions in Matlab (64-bit) Reply with quote

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
View user's profile Send private message
John-Silver



Joined: 30 Jul 2013
Posts: 1451
Location: Aerospace Valley

PostPosted: Fri Oct 02, 2020 9:00 pm    Post subject: Reply with quote

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 ... Smile "
Back to top
View user's profile Send private message
Moji



Joined: 30 Sep 2020
Posts: 6

PostPosted: Mon Oct 05, 2020 9:14 am    Post subject: Reply with quote

As it is mentioned in the link, a compatible compiler is required and Silverfrost FTN95 is not supported by MATLAB.
Back to top
View user's profile Send private message
Robert



Joined: 29 Nov 2006
Posts: 346
Location: Manchester

PostPosted: Mon Oct 05, 2020 11:46 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Moji



Joined: 30 Sep 2020
Posts: 6

PostPosted: Tue Oct 06, 2020 10:30 am    Post subject: Reply with quote

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
View user's profile Send private message
Robert



Joined: 29 Nov 2006
Posts: 346
Location: Manchester

PostPosted: Tue Oct 06, 2020 11:01 am    Post subject: Reply with quote

In that time, how many timed would you have called the FTN95 compiled routine?
Back to top
View user's profile Send private message Visit poster's website
Moji



Joined: 30 Sep 2020
Posts: 6

PostPosted: Tue Oct 06, 2020 11:54 am    Post subject: Reply with 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.
Back to top
View user's profile Send private message
John-Silver



Joined: 30 Jul 2013
Posts: 1451
Location: Aerospace Valley

PostPosted: Tue Oct 06, 2020 9:40 pm    Post subject: Reply with quote

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 ... Smile "
Back to top
View user's profile Send private message
Moji



Joined: 30 Sep 2020
Posts: 6

PostPosted: Wed Oct 07, 2020 6:51 am    Post subject: Reply with quote

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
View user's profile Send private message
Robert



Joined: 29 Nov 2006
Posts: 346
Location: Manchester

PostPosted: Wed Oct 07, 2020 11:10 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Moji



Joined: 30 Sep 2020
Posts: 6

PostPosted: Wed Oct 07, 2020 11:36 am    Post subject: Reply with quote

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 Smile 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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> 64-bit All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group