3

I am relatively new to making different programing languages communicate with each other and would appreciate some help. Basically I have a Fortran code and a Matlab code. Both codes are first initialized and then have to run sequentially. Each code requires input from the other one. When this process has repeated often enough some convergence criteria is reached and the iteration is terminated. To make things more complicated the Fortran code not only requires input from Matlab but also from its own previous iteration. The same holds true for Matlab. So as far as I can see it is best to keep both programs open throughout the iteration process as I have a lot of variables and therefore can’t just write them in a text file to hand them over to the other programme and preserve them for the next iteration.

So essentially I’m trying to do something like this:

Initialise variable sets A,B,C and D

Fortran:

Input: A and B
Calculations …
Output: A (variables have now new values) and D

Matlab:

Input: C and D
Calculations …
Output: C (variables have now new values) and B

Repeat Fortran and Matlab until convergence criteria is reached.

So my questions are: How to make Matlab and Fortran communicate with each other and pass variables to one and another? And how can each code trigger the other one but then wait for the other code to finish its calculation first before continuing?

Dorit
  • 31
  • 1
  • I don't know how complex are the calculations done in the Fortran or Matlab environment, but there are both languages oriented for calculations (not so much for interfacing), so it may be much less hassle to "rewrite" the less complex part of code from one language to the other and only use one language (might be faster to execute too). – Hoki Oct 28 '14 at 09:24
  • What do you mean by Matlab code? MEX Level-2 functions? – Peter Petrik Oct 28 '14 at 09:27
  • @Hoki unfortunately rewriting the m-files or Fortran environment is not an option as they are both very complex. The top-level .f file calls many subroutines, similarly the top-level .m file calls many other m-files. – Dorit Oct 28 '14 at 12:42
  • @Peter As of now I only have .m files. Can MEX Level-2 functions be used for my application? – Dorit Oct 28 '14 at 12:43

1 Answers1

0

The keywords for your favourite search engine are "fortran mex". MATLAB has a very good documentation/tutorial, you can start here:

A MEX-file lets you call a Fortran subroutine from MATLAB

But I believe it only works if you call Fortran subroutines from Matlab. You cannot easily call Matlab .m function from Fortran code. So your "main" program has to be Matlab .m script which calls Fortran subroutines defined in the MEX file (which is actually a dynamic library).

Peter Petrik
  • 9,701
  • 5
  • 41
  • 65
  • If I call Fortran subroutines from Matlab what will happen to the fortran-variables after the call is completed? I mean how can I make sure that variable set A (from the original post) will be available in the next Fortran call? I guess one way of doing this would be to transfer all variables from Fortran to Matlab and then again from Matlab to Fortran, but I have a lot of variables that would need to be passed back and forth. Is there a more elegant way of doing this? – Dorit Oct 28 '14 at 14:23
  • @Dorit: I don't know much about Fortran, but with C/C++ MEX-files you can declare variables as global and they will remain available the next time you call the MEX function (also see [`mexMakeMemoryPersistent`](http://www.mathworks.com/help/matlab/apiref/mexmakememorypersistent.html)), as long as you don't clear it from memory (`clear mex` or `clear all` will unload the shared library from memory). There are ways to lock the MEX-function to prevent it from being forcefully cleared, causing memory leaks (see [`mexLock`](http://www.mathworks.com/help/matlab/apiref/mexlock.html)) – Amro Oct 28 '14 at 15:32