Silverfrost Forums

Welcome to our forums

Calling Fortran functions in Matlab (64-bit)

1 Oct 2020 1:54 #26429

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.

5 Oct 2020 8:14 #26436

As it is mentioned in the link, a compatible compiler is required and Silverfrost FTN95 is not supported by MATLAB.

5 Oct 2020 10:46 #26437

In your Stock Overflow post you say

'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'?

6 Oct 2020 9:30 #26439

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.

6 Oct 2020 10:01 #26440

In that time, how many timed would you have called the FTN95 compiled routine?

6 Oct 2020 10:54 #26441

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.

7 Oct 2020 5:51 #26444

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.

7 Oct 2020 10:10 #26446

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.

7 Oct 2020 10:36 #26447

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.

Please login to reply.