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

Call an Intel Fortran DLL from MATLAB

eos_pengwern
Beginner
3,171 Views

I need to call aDLL written in IVF V10.1 from MATLAB. In principle everything works well, except I can't seem to pass real arrays back and forth.

MATLAB's arrays are pretty much like Fortran allocatable arrays; indices start at 1, the arrays can be multi-dimensional, and are stored in column-major order. However, MATLAB expects all external DLLs to be written in C, so converts all data to C-format on the way out and on the way back. My Fortran DLL, in turn, uses ISO_C_BINDING and declares all data types as C-compatible.

C of course doesn't support multidimensional allocatable arrays, but this shouldn't in theory be a problem. All that should be passing back and forth between MATLAB and Fortran are pointers to the first element of each array, and the arrays themselves should never actually need to be converted. Naturally the parameters that I pass from MATLAB to Fortran include the array dimensions, so that Fortran can allocate the arrays correctly.

In any case, what actually happens is that integer values are passed correctly, so I can allocate arrays with the correct dimensions. However, the real values within the matrices themselved get completely corrupted. The values that Fortran receives from MATLAB are all wrong (typically by 199 orders of magnitude), and the values that MATLAB received back from Fortran are similarly wrong (though not quite so dramatically).

As well as using ISO_C_BINDING, I have tried out the compiler attributes C and STDCALL, but neither seem to affect the behavior in any way. So, what am I missing?

0 Kudos
24 Replies
eos_pengwern
Beginner
569 Views

It's a pleasure to be able to answer a question for once, rather than always asking them...

Stephen.

0 Kudos
sivaselvan
Beginner
569 Views

Stephen,

This was a very interesting thread to read. Thanks to you and to Steve.

Just out of curiosity, could you have called the Intel Fortran DLL by wrapping the function in a mex function? Is there a performance advantage to using the loadlibrary approach?

Thank you.

Siva

0 Kudos
eos_pengwern
Beginner
569 Views

Siva,

At the time I did this, the version of IVF I was using wasn't supported by the version of MATLAB I was using, so the MEX route wasn't available. I haven't checked to see whether that has been resolved in the meantime.

Besides, my ultimate plan is to replace the parts of my application still written in MATLAB with a new GUI written in C#, and with the DLL/loadlibrary approach I'll be able to do this without interfering with the Fortran modules at all.

As for the performance difference, I wouldn't expect much, since the choice of interface would only affect the 'overhead' time in transferring execution from one language to the other. If you have an application where the thread is passed back and forth frequently then it may have an impact (though I have no idea which method would be quicker), but if the calculationtime in Fortran is large relative to the overhead (i.e. if it is a 'big' calculation) then it shouldn't make much difference.

Stephen.

0 Kudos
sivaselvan
Beginner
569 Views

Stephen,

Thank you for your response.

Siva

0 Kudos
Reply