mxUnshareArray doesn't seem to work

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 Comments

What version of MATLAB are you using?

Sign in to comment.

 Accepted Answer

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

More Answers (1)

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 Comments

Thanks James, I tried a different approach using the new Matlab::data api, with no success.
We may want to move our discussion to this post since it is the new api and there is even an example for that at Matlab documentation (that doesn't work).
So, I did check this out and unfortunately mxUnshareArray has suffered the same fate as mxCreateSharedDataCopy for R2018a. It is not supported anymore, and mex will prevent you from linking to this function.
Actually I see that I can use mxUnshareArray and mxCreateSharedDataCopy (but it doesn't avoid the copy of the data)
What is the command that you are using to compile with?
mex -setup c++
mex ./resources/mexFunctions/example.cpp
but I'm using Mac
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 on 5 Jun 2018
Edited: James Tursa on 5 Jun 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.

Sign in to comment.

Categories

Asked:

on 25 Apr 2018

Edited:

on 5 Jun 2018

Community Treasure Hunt

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

Start Hunting!