C/Fortran callback to MATLAB
古いコメントを表示
I have a large library (~100K lines of code) written in modern Fortran, out of which I need to call only one function with the following C-signature,
#include <stdint.h>
void runFoo (
// nd
int32_t *
// getFuncFromMatlab(nd, Point(nd)): procedure pointer to the MATLAB function
, double (*)(int *, double [])
// input character vector
, char []
// the length of the input chracter vector
, size_t *
);
Note that this function contains a callback to MATLAB. In other words, MATLAB calls this function, and this DLL function has to call another function in MATLAB provided by the original calling MATLAB function. I can generate a DLL from this library, and it loads in MATLAB via lodlibrary() with the above C header. However, that is no help, and I believe I am seriously missing some fundamental step to make this callback work. What is the best approach to solve this problem? MATLAB mex file compilation is no help either, as it apprently cannot even understand the syntax of modern Fortran, for example, module keyword. appreciate you help very much.
As a side note, as much as I find MATLAB a great standalone computing tool, I also find it terrible for any interoperation with any other languages. Hope this issue gets fixed. Are there any interoperation capabilities in MATLAB that would be equivalent to the simple, yet extremely powerful ctypes package of Python?
19 件のコメント
Walter Roberson
2019 年 9 月 14 日
I believe that you are mistaken about support for modules.
If you are using Windows or Mac then Fortran support is by way of Intel Parallel Studio XE Fortran. For that compiler, files with extension .f90 are assumed to be Fortran 90/95 free form. You can specifically request that extensions beyond that be marked as errors by adding -std90 or -std95
If you are using Linux then support is through gfortran. You might need to add -std=f95
A.B.
2019 年 9 月 14 日
Walter Roberson
2019 年 9 月 14 日
編集済み: Walter Roberson
2020 年 3 月 30 日
Bruno Luong
2019 年 9 月 14 日
編集済み: Bruno Luong
2019 年 9 月 14 日
Does that mean Mex cannot compile the library?
Mex No (actually a MEX file is an DLL - so library - under windows just callable only under MATLAB), but MCC yes, see my post.
James Tursa
2019 年 9 月 14 日
Are you asking if the argument with this signature
double (*)(int *, double [])
which is a pointer to a function that takes an int pointer and double pointer as input arguments and returns a double, can point to an arbitrary MATLAB function like an m-file?
A.B.
2019 年 9 月 14 日
James Tursa
2019 年 9 月 15 日
編集済み: James Tursa
2019 年 9 月 15 日
Two quick comments:
1) Whether or not you can use modern Fortran features depends entirely on your compiler, not on MATLAB or mex. The only catch is you may need to go into the mex configuration files and remove the silly /fixed option that TMW keeps insisting on including.
2) I don't know of any way to directly interface a C function pointer with a MATLAB m-file. Maybe there is a way with some of the loadlibrary capabilities, but I don't use loadlibrary so can't advise on this. For mex routines, typically one would use mexCallMATLAB for this.
A.B.
2019 年 9 月 15 日
Bruno Luong
2019 年 9 月 15 日
編集済み: Bruno Luong
2019 年 9 月 15 日
Why can't you compile your library as *.lib then link it whith a C/C++/FORTRAN MEX interface function?
You need to figure out suitable name mangling, calling convention to suit both sides but that all you need to do (well beside of course programming the interface function).
James Tursa
2019 年 9 月 18 日
"You need to figure out suitable name mangling ..."
If you are using C++, I would highly advise using the extern "C" { ... } feature to avoid all name mangling.
A.B.
2020 年 3 月 21 日
James Tursa
2020 年 3 月 21 日
編集済み: James Tursa
2020 年 3 月 21 日
Is this function pointer argument:
double (*)(int *, double [])
supposed to call back and actually call a MATLAB m-file function at run time? I.e., You compile your library, but you don't know at compile time what the function pointer will point to. Then at run time, you pass in a string with the name of the MATLAB function you want that function pointer to use for a call back, and you want to make that connection at run time. Is that what you are trying to do? If so, this sounds doable and mexCallMATLAB would be the method to use, and I can write up a short example showing how to do this if needed.
HOWEVER, I would need to know what the function behind this function pointer is doing with the arguments. Specifically, is the data behind the int * and double [ ] input argument pointers read-only or is it supposed to get modified by the function. I need a much wider context of how this function will be used in your code to determine if there will need to be data copying going on behind those data pointers.
Maybe you could write up a short example using m-code and pseudo code or English to show what you want the code to do.
James Tursa
2020 年 3 月 31 日
編集済み: James Tursa
2020 年 3 月 31 日
One final question. These input arguments
double (*)(int *, double [])
are coming from native C/C++ memory via malloc or local variables etc. and not from MATLAB generated memory via mxMalloc etc.?
A.B.
2020 年 4 月 1 日
Walter Roberson
2020 年 4 月 16 日
[You deleted a question before my response could go up, and you do not permit contacts in your profile, so the below is in response to the question you asked, not to the above Question]
- move the dylib to the /usr/lib folder on macOS, which macOS did not allow
You have to sudo in order to do it, but it does allow that.
I am surprised to see that in the year 2020, MATLAB and Apple still cannot resolve their problems like grown-ups and come to an agreement to recognize and respect each other's software.
Whether they do that or not, the C DLL is not Apple's software and it is not Mathworks software either, it is your software. Mathworks might have permitted Apple to scan its software to ensure that it is "safe", but Mathworks could not possibly have given Apple permission to scan your software because your software did not exist at the time Mathworks had MATLAB certified.
As far as Apple is concerned, SIP did what it is supposed to do: Prevent an unknown untrusted DLL from being used. Apple's SIP does not know that you, the user, created the DLL yourself: as far as it knows, an untrusted browser extension might have silently downloaded a DLL and is trying to trick MATLAB into running it.
As far as Apple is concerned, anyone who wants to write software that creates DLLs that will be executed by a program covered by SIP, should become a registered Apple developer and go through the process of getting every DLL scanned and certified before use.
If this leads you to the suspicion that Apple believes that end users (not part of the Developer Program) should not be compiling software for their own use, then that is because that is Apple's goal. Apple is pushing for the position that you should either be paying Apple to develop the software you want, or paying a certified third-party developer to develop the software you want. Apple is fond of "walled gardens". It is not (for now) going to prevent you from compiling your own software, but it is going to ensure that (A) you are inconvenienced; and (B) that the software runs in a lower-security context.
A.B.
2020 年 4 月 16 日
Walter Roberson
2020 年 4 月 16 日
With regards to /usr/bin :
I find it hard to believe that Apple cannot trust MATLAB. This seems to be more of a corporation rivalry than anything else
Apple cares little what Mathworks thinks. Mathworks is not buying many systems from Apple, and not so many people buy Apple systems specifically to run MATLAB. Mathworks is just another customer of the Developer's Program as far as Apple is concerned.
Apple blew off NVIDIA, which is a $US100B+ company; Mathworks is small potatos by comparison. It isn't a matter of corporate rivalry: Mathworks is too small for Apple to notice. Apple makes decision, and Mathworks just has to live with the decisions.
A.B.
2020 年 4 月 16 日
採用された回答
その他の回答 (1 件)
Bruno Luong
2019 年 9 月 14 日
編集済み: Bruno Luong
2019 年 9 月 14 日
0 投票
Not sure what you mean by "Callback". Callback to me is kind of event trigger function like an SW interruption.
Anyway if you have the compiler, you can compile any MATLAB function in C shared library, and call it from external C program.
I have no idea about FORTRAN, but in the pass I have linked fortran (gcc,g77) with MSVSC/Intel-C, a bit tricky but doable.
カテゴリ
ヘルプ センター および File Exchange で Write C Functions Callable from MATLAB (MEX Files) についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!