- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
Link Copied
- « Previous
-
- 1
- 2
- Next »
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It's a pleasure to be able to answer a question for once, rather than always asking them...
Stephen.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Stephen,
Thank you for your response.
Siva

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- « Previous
-
- 1
- 2
- Next »