MATLAB Answers

Using MEX file with the main program of Fortran code

2 views (last 30 days)
Ardhana Wicaksono
Ardhana Wicaksono on 12 Dec 2019
Commented: Ardhana Wicaksono on 16 Dec 2019
Hello,
I want to use MEX file to pass the input from Matlab to the main program of a Fortran code that contains many subroutines.
Is it fine to write the MEX file solely based on the input and output of the main program? Can we neglect the existence of the subroutines since the operation is done within the Fortran code?
Thank you :D
-
Ardhana

  0 Comments

Sign in to comment.

Answers (1)

James Tursa
James Tursa on 12 Dec 2019
Edited: James Tursa on 12 Dec 2019
You would need to turn the PROGRAM line into a MEXFUNCTION line and add some code for getting the MATLAB variable data to/from the Fortran variables ... i.e., change the interface to the Fortran code. The entire code can be Fortran with as many subroutines as you like. See the doc for examples.

  3 Comments

Ardhana Wicaksono
Ardhana Wicaksono on 12 Dec 2019
James,
Thank you very much for your answer.
Therefore, if I have a main program titled MOTION, then I have to:
1) replace PROGRAM MOTION → subroutine mexFunction(nlhs, plhs, nrhs, prhs)
2) use mwPointer to state functions, arguments, and variables (following the sample in documentation, and desired input-output data)
Is this correct?
In my main program, I already included some initial computations before calling other subroutines, would this be fine as well?
-
Ardhana
James Tursa
James Tursa on 12 Dec 2019
Yes. That is the general concept. You can do all of your non-I/O Fortran stuff as usual. You can use whatever modern Fortran features you want (modules, etc) as long as your compiler can handle it. You can do computations before you do the I/O stuff. It is really only the I/O stuff between MATLAB and your Fortran code that you will need to write, and the doc examples can help you with this.
That being said, Fortran mex routines are harder to work with than C mex routines because of the difference in how pointers are supported. In C it is relatively straightforward to get data pointers and read the input variables ... in Fortran not so much. The doc will typically have you copy the data in before using it which wastes memory and time. If your variables are not too large then no big deal, but if your variables are large then this can be a serious performance drag. To avoid this you might be forced to use hacks.
My advice is to first just get your code running with the official API functions using the copy-in copy-out methods. Then if performance drags too much we can discuss how to use pointers effectively. Feel free to post some code and we can help with all of this (setting up the MATLAB/Fortran variable I/O, etc.).
CAVEAT: The MATLAB mex opts files have unfortunatly included the /fixed flag in their COMPFLAGS settings. This will force your Fortran compiler to treat all source files as fixed format even if they have the .f90 extension. Not good and not correct. I have submitted a bug report on this but I don't know if they will ever remove this silly flag. My advice is to manually edit your mex opts files and remove the /fixed flag from all of the COMPFLAGS lines.
Ardhana Wicaksono
Ardhana Wicaksono on 16 Dec 2019
James,
Sorry for the delayed response. Thank you very much, I appreciate your comprehensive answer on my question.
I have checked the MEX sample programs for Fortran, as written in doc, such as: timestwo.f, yprime.f, xtimesy.f, which work without problem. I have been trying to follow these examples in modifying my own code but there are still a lot of errors.
For the time being, I don't mind using the copy-in copy-out, even though it wastes memory and time, at least for the simple version of my code. Anyway, as you said, the substantial performace drag may occur once I uses the extended code containing thousand of variables. I will consider this point later on.
I am currently re-editing my mexFunction subroutine to resolve the errors. For your reference, please find my code attached (shipme.f), in which the last subroutine (STECON) is the one I would like to write on the MATLAB side. The input text file is simdat.txt, and the common.f.
Thank you again!
-
Ardhana

Sign in to comment.

Sign in to answer this question.

Tags