how to retain a variable in memory in mex function after its execution

Hi everybody,
I want to use a mex fucntion in Matlab. My requirement is that i have a matrix in Matlab that should be processed by a mex function using different arguments generated in loop in Matlab. In order to save execution time i do not want to pass the matrix to the mex function in every loop as the matrix is very large.
Is it possible to transfer the matrix for once to a fixed location in memory, so that the mex function can access it during every execution and is retained at the same location even after the call to the mex function.

 Réponse acceptée

Matt J
Matt J le 11 Mai 2014
Modifié(e) : Matt J le 11 Mai 2014
Is it possible to transfer the matrix for once to a fixed location in memory, so that the mex function can access it during every execution and is retained at the same location even after the call to the mex function.
There is never any transfer of matrix data when you call a mex. When you send a matrix to a mex function, you are not passing it by value, i.e., copying all of its data to a separate workspace. The mex gateway function receives an mxArray pointer which points to where the data already resides in the MATLAB workspace. And it is that data that gets used as input.
Now it is possible that time is consumed when fresh memory is allocated for the output of the computations, but that is at the discretion of the author of the mex file. He/she can put the result anywhere desired, either in freshly allocated memory or in space already occupied by existing matrices.

4 commentaires

Matt J
Matt J le 11 Mai 2014
Modifié(e) : Matt J le 11 Mai 2014
mohsan niaz Commented:
Thank you Matt for your response,
Perhaps then my problem is that within the mex function i also have to create matrices which are of the same size as the matrix given as argument to the mex function. Every time i call the mex function from Matlab it has to create the double matrices from the command "mxCreateDoubleMatrix". This memory allocation may take a long time. So is it possible to do only once and reuse the allocated memory again and again.
Matt J
Matt J le 11 Mai 2014
Modifié(e) : Matt J le 11 Mai 2014
Every time i call the mex function from Matlab it has to create the double matrices from the command "mxCreateDoubleMatrix".
Yes, you can allocate the matrix in MATLAB. Then, everytime you call the mex function, pass its mxArray pointer as one of the prhs[i]. Use that pointer to overwrite the matrix data as needed.
No, do not do this! Never overwrite data of an input variable. The variable could contain shared data and you would destroy the internal logic.
In general, I'd agree with that, Jan. But in this instance things are well-controlled. We know that the input variable will not be shared.

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 11 Mai 2014
You can use mxMakeMemoryPersistent or mxMakeArrayPersistent as well as the usual static property of C-variables.

1 commentaire

Thanks Mat and Jan for your answers. Both your answers were very helpful. As a matter of fact the way i have simplified the problem is that i don't create a vector in mex fucntion rather i create them in Matlab workspace and pass their pointers to mex function to manipulate them.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Write C Functions Callable from MATLAB (MEX Files) dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by