4. My compiler doesn’t support %VAL(). Must I know the dimensions of the inputs and outputs at compilation?
No. Though for some odd reason the API Guide from The MathWorks certainly makes it sound like it. There are three ways to do it, the first can be used in either FORTRAN77 or Fortran90. The second requires the allocatable arrays of Fortran90, and the third uses the POINTER data type of Fortran9x, and may or may not be portable across platforms.
In order to demonstrate these techniques, I have written a series of MEX files that perform vector-vector multiplication: a’*a if a is column vector.
Method 1: Use two gateway procedures
The key to getting around it is using two Fortran procedures. In the first procedure, the gateway, the dimensions of the input are queried and stored. Then they are passed to the second procedure along with the pointers to the inputs and outputs, and the number of inputs and outputs. In this second procedure, both the number and dimensions of the input arguments are known at the invocation of the subroutine, and so are allocated in the subroutine declarations. If the above attempt at explanation seems worthless, hopefully this will help.vvmult.f is the fixed-format source. This method should work in f77 as well as f9x.
Method 2: Use ALLOCATABLE arrays
In this method, only one gateway subroutine is needed. After the dimensions of the input arguments are known, arrays are allocated to store them and the contents of the input arguments are placed in them. vvmult.f90 uses this method.
Method 3: Use POINTERs
I have never used this method in production code, so I cannot personally vouch for it. The third method creates pointers to point to the memory occupied by the arguments. One big advantage of this method is that it does not require allocating ‘double’ memory to hold your data. This method was suggested to me by Paul Lee, who pointed to the webpage describing the method here. I have implemented this method in the file vvmult_ptr.f90.