Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28848 Discussions

Querying SIZEs of arrays passed to a Fortran routine by Matlab (& others)

Simon_C
Novice
709 Views

Everyone,

I am working on a Fortran dll from which a subroutine can be called either
by a Fortran program, or from another language/applcation, such as Matlab.

A few one dimensional integer and real work arrays that are being passed in/out of the called subroutine do not have their sizes specified as arguments of the routine - just for simplicity really - and they are declared within the
Fortran routine in the dll as, for example:

INTEGER, INTENT(INOUT) :: iUser(:)
REAL (KIND=8), INTENT(INOUT) :: User(:)

If the subroutine is being called from the dll by another Fortran program
or routine then I can check whether the two arrays are correctly sized by
using the SIZE intrinsic function within the routine being called from the
dll. However, my colleague who is working on the C-based wrapper for Matlab
tells me that, when called from Matlab (or other languages we are working with such as Python and R), the Fortran routine will have no way of knowing how big iUser and User are. Consequently, in this case the SIZE intrinsic just returns zero no matter what the sizes of the two arrays.

Will this (ie, a size of 0) always be the case? Is this result compiler-specific?

Of course, code can be written in the wrapper to ensure iUser and User are the correct sizes, but on the other hand I'd like to retain the SIZE test if only to cover the case whether the dll subroutine is being called by a Fortran program.

I'd be grateful if someone would comment, and let me know whether my understanding is correct or not. I can see that this problem is fairly trivial, and can be side-stepped by making the array sizes additional arguments of the called routine, but on the other hand I'd like to make sure I understand the issues/behaviour correctly.

Thank you.

Simon C.

0 Kudos
2 Replies
FortranFan
Honored Contributor III
698 Views

@Simon_C ,

Disclaimer: I have not kept up with MATLAB, I have not had access to it for a while, and I forget now what it supports when it comes to Fortran 90 and newer features.  So I am unsure how assumed-shape arrays (what you define as User(:) i.e., with the colon) are handled.

My hunch is there is no easy way out - assuming interoperability with C is the underlying mechanism of working with Fortran from MATLAB, one can either author an additional layer in C to interoperate with Fortran procedures that have assumed-shape array arguments or assumed-size arrays can be used with another mechanism to make the array sizes known to the Fortran subprograms.

You will note it is with assume-shape arrays the descriptors providing the SIZE (and SHAPE) information become available to the Fortran subroutine.

But now, a standard Fortran processor is only aware of a companion C processor, not specific applications such as MATLAB.  And it is possible under the hood a C processor is what MATLAB uses now to work with user Fortran code.  When it comes to a companion C processor, note in the absence of an additional binding layer that is propped up by ISO_Fortran_binding.h header, it is only assumed-size arrays (what you would define as arr(*) i.e., those defined with asterisk *   instead of colon that are interoperable.  With assumed-size arrays though, you cannot use the SIZE intrinsic in Fortran; the array size (and/or shape) has to be provided separately such as with additional arguments to the Fortran procedure that you mentioned.

 

0 Kudos
FortranFan
Honored Contributor III
697 Views

@Simon_C ,

In case you haven't looked into "an additional binding layer that is propped up by ISO_Fortran_binding.h" yet, you can review this comment at the Fortran Discourse site (a good one for general discussions around Fortran) for a quick example of the code needed in C to work with assumed-shape arrays.

0 Kudos
Reply