fortran subroutine includes several other subroutines

3 visualizzazioni (ultimi 30 giorni)
Hello
I want to write a Mex file to import data from a Fortran 77 subroutine in MatLab. However, this subroutine includes several other subroutines. How should I consider other subroutines in the main subroutine in my Mex file? any guidance is highly appreciated.
Btw, does anyone have a simple guidance as to how to write a mex file for fortran (other than twotimes doc in matlab)?
Bests
Pooneh
  3 Commenti
Pooneh Shah Malekpoor
Pooneh Shah Malekpoor il 1 Nov 2021
Modificato: Pooneh Shah Malekpoor il 1 Nov 2021
Hello Johnny
Thanks for your reply.
Could you please share a simple example of a Mex file which includes several subroutines (in addition to the main one)?
Bests
Pooneh
Johnny Himbele
Johnny Himbele il 2 Nov 2021
Hi Pooneh,
For example, matsq.F has one Mex function and one subroutine. Run the following command in MATLAB Command Window
edit([matlabroot '/extern/examples/refbook/matsq.F']);
Here is a brief example of multiple subroutines with a Mex function.
#include "fintrf.h"
C Gateway routine
subroutine mexFunction(nlhs, plhs, nrhs, prhs)
implicit none
C Arguments declarations
mwPointer plhs(*), prhs(*)
integer nlhs, nrhs
C Call subroutines
call myFunc1(input1,input2,output1,output2)
call myFunc2(input3,input4,input5,output3)
return
end
subroutine myFunc1(input1,input2,output1,output2)
C Put your code here
return end
subroutine myFunc2(input3,input4,input5,output3)
C Put your code here
call myFunc3(input6,output4)
return end
subroutine myFunc3(input6,output4)
C Put your code here
return end
Thank you,
Johnny

Accedi per commentare.

Risposte (1)

James Tursa
James Tursa il 12 Nov 2021
Modificato: James Tursa il 14 Nov 2021
Note that the timestwo.F example that comes with MATLAB is a bad example of Fortran code. It has several bugs in it that could lead to a crash, such as:
  • Does not check if the input type is double
  • Does not check if the input type is sparse
  • Does not check if the input type is complex
  • Copies arrays of numbers into a scalar location
  • Reads arrays of numbers from a scalar location
  • Uses 'size' for a variable name even though this is a Fortran intrinsic function name
  • Comments are lined up with code, making it very hard to read
  • Uses mwPointer type for mrows and ncols even though doc states the type should be mwSize
  • Passes a literal 0 to an API function when a variable of type integer*4 should be used per the doc
I have given these issues to the TMW folks but nothing seems to have been done about it. I think I need to create my own version of this routine and post it in Answers for people to see the corrected code. The matsq.F file has similar problems that can crash MATLAB, but at least it tries to get the array stuff protected (even though it employs two completely unnecessary deep data copies in the process).
Getting back to your original question, if the subroutines are in the same file then just compile as normal. If the subroutines are in different files then just list those file names next to your main file name in the mex command. I.e.,
mex mymain.F mysubroutine1.F mysubroutine2.F
You can also precompile code and then just list the object files in the mex build. E.g., on Windows you could do this:
mex -c mysubroutine1.F
mex -c mysubroutine2.F
The above lines compile the subroutine files into object files, but does not try to link them. Then later on you can compile your main routine into a mex function using these object files instead of the original source files:
mex mymain.F mysubroutine1.obj mysubroutine2.obj
This technique can be useful if you are debugging and modifying mymain.F several times inbetween testing as it can speed up the mex routine build process.

Community Treasure Hunt

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

Start Hunting!

Translated by