Borrar filtros
Borrar filtros

mxUnshareArray doesn't seem to work

5 visualizaciones (últimos 30 días)
Or Nahir
Or Nahir el 25 de Abr. de 2018
Editada: James Tursa el 5 de Jun. de 2018
Hi,
I'm trying to write a very simple function to learn how to write mex in place function.
I wrote the following function:
#include <matrix.h>
#include <mex.h>
void mexFunction(int nargout, mxArray *argout[], int nargin, const mxArray *argin[]) {
const int res = mxUnshareArray(const_cast<mxArray *>(argin[0]), true);
argout[0] = mxCreateSharedDataCopy(argin[0]);
double *data = (double *)mxGetData(argin[0]);
data[0] =res;
}
I ran it:
x = ones(1e8,1) * 10;
x = fun(x);
And I still see that the function copies x. I looked at the call time of this function and the same function without the mxUnshareArray. With the mxUnshareArray it takes much longer (due to the coping of the data).
  2 comentarios
James Tursa
James Tursa el 25 de Abr. de 2018
What version of MATLAB are you using?
Or Nahir
Or Nahir el 25 de Abr. de 2018
R2018a

Iniciar sesión para comentar.

Respuesta aceptada

Or Nahir
Or Nahir el 1 de Mayo de 2018
I found a solution using the new Matlab::data api.
I'm posting it on my other post. Please check it on:

Más respuestas (1)

James Tursa
James Tursa el 28 de Abr. de 2018
I haven't forgotten this Question, but I don't know enough to completely answer it yet. That being said, I will give it a start ...
In the first place, mxUnshareArray and mxCreateSharedDataCopy are both undocumented API functions. So you should include your own prototypes for these functions since they will not have prototypes in the matrix.h file. Secondly, there have been major changes to the mxArray and API library functions in R2018a, and it would not surprise me that things that used to work might not work anymore. For instance, I know that mxCreateSharedDataCopy is not supposed to work in R2018a. I haven't yet investigated if mxUnshareArray has suffered the same fate. And I don't yet know if using the -R2017b vs -R2018a option will make a difference. E.g., see this related thread:
If I find out any additional information on this topic I will update this post.
Btw, you don't need this line:
#include <matrix.h>
since mex.h includes matrix.h automatically.
  7 comentarios
James Tursa
James Tursa el 3 de Mayo de 2018
With that command, you are compiling & linking against the R2017b memory model API library, which in many cases will force a data copy to take place. To compile and link against the new memory model you have to include the -R2018a option in the mex command. But I have my doubts that this will actually make a difference for these undocumented functions.
James Tursa
James Tursa el 5 de Jun. de 2018
Editada: James Tursa el 5 de Jun. de 2018
Update: mxUnshareArray appears to still be in the API library, but the mex command deliberately prevents you from linking with it. Even if you could link with it, note that since R2015b MATLAB no longer passes original variable pointers into mex routines ... the prhs[0] variables will be shared data copies of the original variables. So if you have code that does mxUnshareArray(prhs[0]) or similar in an effort to "safely" modify a variable inplace downstream, this tactic wouldn't work.

Iniciar sesión para comentar.

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by