필터 지우기
필터 지우기

mxUnshareArray doesn't seem to work

조회 수: 5 (최근 30일)
Or Nahir
Or Nahir 2018년 4월 25일
편집: James Tursa 2018년 6월 5일
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일
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일
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
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by