Overhead of calling MEX functions from another MEX file

조회 수: 1 (최근 30일)
Jan Stolarek
Jan Stolarek 2011년 12월 21일
I have an algorithm implemented in Matlab. It consists of many functions, each function in separate file and having its own unit tests made in xUnit. The algorithm is rather simple, but it is heavily based on bit operations, which makes it extremely slow in Matlab. I decided that I will reimplement it in C using MEX files. Now I'm facing two choices:
1. Create only one MEX file acting as an interface to the whole algorithm and implement all the functions as ordinary C functions.
2. Reimplement each function as a separate MEX file.
The first approach seems better in terms of performance, but it forces me to drop all the unit tests and I'm reluctant to do that since I'm new to MEX files and I still make some mistakes. The second approach allows me to unit-test every MEX function, but it will result in MEX functions calling other MEX functions. I'm afraid that this might impact performance, and after all performance is what I'm after. Should I be worried with the overhead of calling MEX from MEX?

채택된 답변

Titus Edelhofer
Titus Edelhofer 2011년 12월 21일
Hi Jan,
what about doing both? Having the overall algorithm spread over many mex files probably is no good idea since you will indeed see quite some overhead. But what you could do is:
  • put your parts of the algorithm into different .c files
  • write a mex interface for each of them for running your unit tests
  • have an overall mex file calling those subfunctions (and pass all the .c files to mex for compilation.
Titus
  댓글 수: 2
Jan Stolarek
Jan Stolarek 2011년 12월 21일
Yes, this sounds like an acceptable compromise between the two. I'm still thinking if this won't cause problems, since MEX and C functions will have different set of parameters, e.g. where MEX function gets mxArray, the C function needs a pointer to an array and two parameters with array dimensions.
I also see, that such a solution will increase the number of files in the project directory five times: one original .m file, one MEX source, one header, one C source and one compiled binary for each function!
Titus Edelhofer
Titus Edelhofer 2011년 12월 25일
Yes. Although there is no reason not to seperate e.g. the test files into a different folder ....

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB Compiler에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by