Main Content

Upgrade Fortran MEX Files to use 64-bit API

The steps in Upgrade MEX Files to Use 64-Bit API apply to Fortran and C/C++ source files. Fortran uses similar API signatures, identical mwSize / mwIndex types, and similar compilers and debuggers.

However, to make your Fortran source code 64-bit compatible, perform these additional tasks.

Use Fortran API Header File

To make your Fortran MEX file compatible with the 64-bit API, use the fintrf.h header file in your Fortran source files. Name your source files with an uppercase .F file extension. For more information about these requirements, see Components of Fortran MEX File.

Declare Fortran Pointers

Pointers are 32-bit or 64-bit addresses, based on machine type. This requirement is not directly tied to array dimensions, but you could encounter problems when moving 32-bit code to 64-bit machines as part of this conversion.

For more information, see Preprocessor Macros and mwPointer.

The C/C++ compiler automatically handles pointer size. In Fortran, MATLAB® uses the mwPointer type to handle this difference. For example, mxCreateDoubleMatrix returns an mwPointer:

mwPointer mxCreateDoubleMatrix(m, n, ComplexFlag)
mwSize m, n
integer*4 ComplexFlag

Require Fortran Type Declarations

Fortran uses implicit type definitions. Undeclared variables starting with letters I through N are implicitly declared type INTEGER. Variable names starting with other letters are implicitly declared type REAL*4. Using the implicit INTEGER type could work for 32-bit indices, but is not safe for large array dimension MEX files. To force you to declare all variables, add the IMPLICIT NONE statement to your Fortran subroutines. For example:

subroutine mexFunction(nlhs, plhs, nrhs, prhs)
implicit none

This statement helps identify 32-bit integers in your code that do not have explicit type declarations. Then, you can declare them as INTEGER*4 or mwSize / mwIndex, as appropriate. For more information on IMPLICIT NONE, refer to your Fortran compiler documentation.

Use Variables in Function Calls

If you use a number as an argument to a function, your Fortran compiler could assign the argument an incorrect type. On a 64-bit platform, an incorrect type can produce Out of Memory errors, segmentation violations, or incorrect results. For example, definitions for the argument types for the mxCreateDoubleMatrix function are:

mwPointer mxCreateDoubleMatrix(m, n, ComplexFlag)
mwSize m, n
integer*4 ComplexFlag

Suppose that you have a C/C++ MEX file with the following statement:

myArray = mxCreateDoubleMatrix(2, 3, mxREAL); 

Most C/C++ compilers interpret the number 2 as a 64-bit value. Some Fortran compilers cannot detect this requirement, and supply a 32-bit value. For example, an equivalent Fortran statement is:

myArray = mxCreateDoubleMatrix(2, 3, 0)

The compiler interprets the value of the ComplexFlag argument 0 correctly as type INTEGER*4. However, the compiler could interpret the argument 2 as a 32-bit value, even though the argument m is declared type mwSize.

A compiler-independent solution to this problem is to declare and use an mwSize / mwIndex variable instead of a literal value. For example, the following statements unambiguously call the mxCreateDoubleMatrix function in Fortran:

mwSize nrows, ncols
INTEGER*4 flag
nrows = 2
ncols = 3
flag = 0
myArray = mxCreateDoubleMatrix(nrows, ncols, flag)

Manage Reduced Fortran Compiler Warnings

Some Fortran compilers cannot detect as many type mismatches as similar C/C++ compilers. This inability can complicate the step Resolve -largeArrayDims Build Failures and Warnings by leaving more issues to find with your debugger in the step Execute 64-Bit MEX File and Compare Results with 32-Bit Version.

See Also

| |

Related Examples

More About

External Websites