Using MEX file with the main program of Fortran code

조회 수: 1 (최근 30일)
Ardhana Wicaksono
Ardhana Wicaksono 2019년 12월 12일
댓글: Ardhana Wicaksono 2019년 12월 16일
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

답변 (1개)

James Tursa
James Tursa 2019년 12월 12일
편집: James Tursa 2019년 12월 12일
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
James Tursa
James Tursa 2019년 12월 12일
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 2019년 12월 16일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Fortran with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by