MATLAB: Advantages of mwsize (Cross-Platform Flexibility)

c codeMATLAB

Hello,
in the documentation there is the hint to use mwsize rather than int for Cross-Platform Flexibility. What is the actual advantage? The backround of my question is the following: I want to write (and partly already have written) C-Code which I wan to use in MATLAB. I tried to find a good workflow to extend the existing C-Code and use it in MATLAB. The big problem I came across is the debugging. I found this very helpful Blog entry: https://blogs.mathworks.com/developer/2018/06/19/mex-debugging-vscode/ nevertheless, it is pretty cumbersome to debug like this all the time. Especially because MATLAB might crash pretty often because bugs happen during development of new C-Code. Therefore, I thought it would be better to write the code in a C IDE and just put the wrapper mexFunction around my code at the end. My question is, if I loose a lot of performance if i do it this way?
Furthermore, a question arose regarding the 'MATLAB Support for Interleaved Complex API in MEX Functions'. Which API is preferred for high speed applications?

Best Answer

"Furthermore, a question arose regarding the 'MATLAB Support for Interleaved Complex API in MEX Functions'. Which API is preferred for high speed applications?"
This is somewhat of a moot point given that the choice is determined by the MATLAB version you are using. If you are using R2017b or earlier, then you will be using two separate Real/Imaginary data areas. If you are using R2018a or later, then you will be using a single interleaved Complex data area. There are no MATLAB versions that simultaneously support both methods. Compiling a mex routine in R2018a or later with the -R2017b memory model option simply forces the mex routine to do a copy-in/copy-out on all complex variables in the background. It does not change the underlying storage scheme of the variable data, which is always interleaved complex in R2018a and later.
As to which is faster, that depends on what you are doing. Note that the BLAS and LAPACK complex linear algebra library routines that MATLAB uses only support the interleaved complex data model (in every version of MATLAB, not just R2018a and later), so that drives the comments below. E.g.,
Matrix Multiply real * complex:
The R2017b separate storage scheme will be faster because the BLAS matrix multiply routines can be called directly without any intermediate data copying needed. I.e., the individual real*real and real*imaginary pieces can be done by making two calls to the real BLAS matrix multiply routine and the results stuffed directly into the MATLAB output variable. For the R2018a interleaved storage scheme to use the complex BLAS matrix multiply routine in this case, it must first deep copy the real variable into a complex variable with imaginary part 0, and then make the call. So extra wasted memory and time to do the intermediate deep data copy for R2018a and lots of extra unnecessary 0 multiplies.
Linear Algebra calls to complex LAPACK routines:
The R2018a interleaved storage scheme will be faster because the input can be passed directly to the LAPACK routine and the output stuffed directly into a MATLAB variable. No intermediate deep data copying needed. For the R2017b separate storage scheme to use the complex LAPACK routine, it must first deep copy the separate real/imaginary data areas into a single contiguous interleaved area and then pass that to the LAPACK routine. Then it must take the interleaved result and deep copy it into two separate real/imaginary data areas for output back to MATLAB. So extra wasted memory and time to do the intermediate deep data copies.