How to create a ones array in mex ?

I am coding in c++ with mex interface. I need to create an array of ones of size 5 * 1. I am trying to use the mexCallMATLAB function but it gives me an error. Can someone please help me with this ?
Code : mexCallMATLAB(1,Ones,1,createOnes,"ones"); I was hoping that the output array will be displayed in Ones and the createOnes array is an array of size 5 * 1.
Error : Error using ones Size vector should be a row vector with real elements.
Please guide me. Thanks in advance

9 comentarios

Adam
Adam el 5 de En. de 2016
'ones' expects to be given a size, not an array that happens to be the size you want it to create. So it would need to be a vector of (5,1). I'm not familiar with mexCallMatlab so I don't know if this needs to be in an mwSize object or just a regular array though.
Gauraang  Khurana
Gauraang Khurana el 6 de En. de 2016
Thanks Adam, I figured it out and did accordingly.
I have another question, I am writing a c++ code in Matlab because my Matlab function was taking hours to run. Now, I am using 'mexCallMatlab' functions to transpose and multiple matrices. I am using it for almost every calculation. Will the time taken to run the code remain same or will it differ. If it is going to be same then there is no use.
So, to summarize my question. Is the time taken in computation in Matlab and mexCallMATLAB same ?
Stephen23
Stephen23 el 6 de En. de 2016
Editada: Stephen23 el 6 de En. de 2016
Before jumping into MEX code you should investigate why your "MATLAB function was taking hours to run". Slow code is often written by beginners who do not know how to write efficient vectorized code, and instead include nested loops, or incrementally increase array sizes within loops, etc. You should consider moving everything outside of the loops that does not change within the loops, and use multi-dimensional numeric arrays as much as possible and access them using indexing (without creating lots of temporary variables).
There is plenty of help and advice on this forum for speeding up code, so do some reading before deciding to MEX it all.
Walter Roberson
Walter Roberson el 6 de En. de 2016
Matrix multiplication of larger arrays is something that is done especially efficiently, by calling LAPACK or similar libraries.
Though with the transpose potentially slowing you down, you might even want to marshal the data and call into the libraries yourself. My recollection is that those libraries need the data in row major order, so if you just pass the untransposed data in to the libraries then that would be the equivalent of transposing and then having MATLAB transpose as it marshals the data.
Gauraang  Khurana
Gauraang Khurana el 6 de En. de 2016
Thanks for you input but none of you answered my question. Instead of writing Matlab code, I am writing C code in mex files. I call mexCallMATLAB function for all my calculations like addition of matrices,transpose, multiplication etc. Will the time taken be same by both the approaches ?
Adam
Adam el 6 de En. de 2016
Editada: Adam el 7 de En. de 2016
The time may not be the same precisely, but it will be of the same order of magnitude if you literally just delegate from C++ to Matlab for everything.
The idea of using mex to speed code up (though I agree with Stephen Cobeldick that there are other avenues to explore before mex) is that C++ is generally a more optimised language than Matlab so doing things in C++ can be faster than in Matlab. Calling back into Matlab from C++ will not achieve that because it is still Matlab doing the work. If anything it will be slower due to the overhead of keep calling back into Matlab I would think.
I agree with Stephen and Adam and others ... your proposed approach of using mexCallMATLAB is not the way to go. Adding A+B at the m-file level is without question going to be faster than passing A and B into a mex routine and then using mexCallMATLAB to call the "plus" function. If speed is an issue, the general advice is to avoid mexCallMATLAB if at all possible (e.g., hand-code your own C/C++ loops to do specific calculations).
Matrix transposing is a separate issue. If the transpose is involved in a matrix multiply and that is the only time you need the transpose, then including the transposing with the matrix multiply is the way to go because MATLAB will not physically do the transpose but will call the BLAS routines with appropriate flags, and the transpose will be accomplished with internal indexing changes. E.g., at the m-file level, with A and B being 2D matrices,
C = A'*B; % <-- A' is not physically formed. A flag is passed to BLAS routine
is better than
At = A'; % <-- A' is physically formed.
C = At*B;
But if you need the transpose for some other reason, then calling "transpose" or "ctranspose" (depending on your needs) will be pretty fast. There are ways to beat MATLAB's transpose functions for speed sometimes by using certain memory blocking schemes in a C/C++ mex routine, but I will not offer up that code here since I doubt this is where your real speed bottleneck is.
Walter Roberson
Walter Roberson el 7 de En. de 2016
I didn't know that the transpose would not actually be done in that case... interesting. Does the trick also work with A.'*B ?
James Tursa
James Tursa el 7 de En. de 2016
Editada: James Tursa el 7 de En. de 2016
Yes. The BLAS matrix multiply routines include character flag arguments for both A and B, 'N' for No transpose, 'T' for Transpose, and 'C' for Conjugate transpose. The MATLAB parser is smart enough to recognize this syntax at the m-file level and pass the flag instead of doing the physical transpose. Of course, I don't have access to MATLAB parser code, but this can easily be concluded based on timing results compared to equivalent mex code calling BLAS routines. Having said all that, I would remind you that MATLAB complex variables store the real part and imaginary part separately in memory, whereas the BLAS complex routines expect that data to be interleaved (ala Fortran). So rather than copy all the memory over to an interleaved state, call the complex matrix multiply, then copy the result to separated states, the multiply is accomplished with a series of real BLAS matrix multiply calls instead with appropriate factors for adding/subtracting the intermediate results. The factor (any real number) is an argument to the BLAS routine.
For LAPACK calls where one needs to call the complex routine, the memory copies into and out of an interleaved state must be done.
I don't know whether the MATLAB parser has gotten smart enough to recognize naked conjugates, however. E.g., something like this:
C = A * conj(B);
can be done at the BLAS calling level by simply passing a -1.0 as the factor argument to apply on the 2nd matrix imaginary part instead of a 1.0. I haven't checked lately to see if the MATLAB parser has gotten smart enough to do this or not. It used to physically do the conjugate and maybe still does.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Centro de ayuda y File Exchange.

Preguntada:

el 5 de En. de 2016

Editada:

el 7 de En. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by