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 件のコメント

James Tursa
James Tursa 2018 年 4 月 25 日
What version of MATLAB are you using?
Or Nahir
Or Nahir 2018 年 4 月 25 日
R2018a

サインインしてコメントする。

 採用された回答

Or Nahir
Or Nahir 2018 年 5 月 1 日

0 投票

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

その他の回答 (1 件)

James Tursa
James Tursa 2018 年 4 月 28 日

0 投票

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 件のコメント

Or Nahir
Or Nahir 2018 年 4 月 29 日
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).
James Tursa
James Tursa 2018 年 4 月 30 日
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.
Or Nahir
Or Nahir 2018 年 5 月 1 日
Actually I see that I can use mxUnshareArray and mxCreateSharedDataCopy (but it doesn't avoid the copy of the data)
James Tursa
James Tursa 2018 年 5 月 1 日
What is the command that you are using to compile with?
Or Nahir
Or Nahir 2018 年 5 月 3 日
mex -setup c++
mex ./resources/mexFunctions/example.cpp
but I'm using Mac
James Tursa
James Tursa 2018 年 5 月 3 日
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 2018 年 6 月 5 日
編集済み: James Tursa 2018 年 6 月 5 日
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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeWrite C Functions Callable from MATLAB (MEX Files) についてさらに検索

質問済み:

2018 年 4 月 25 日

編集済み:

2018 年 6 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by