Editing code for mex function to support 64 bit indexing

조회 수: 1 (최근 30일)
Marc Laub
Marc Laub 2021년 1월 15일
댓글: Marc Laub 2021년 1월 17일
Hey,
I want to speed up my code by using a mex function, I actually already did but the probem is that MatLab coder only supports 32bit indexing and when my function input gets to large, MatLab crashes.
I already tried:
but this doesnt help me because the topic is about what to change when a matrix that needs more than 32 bit indexing is created in the mex function.
In my case the matrix is generated in MatLab and given to the mex function, which gives some outputs. The same matrix can change its size in MatLab and it is returned to the mex function repeatedly.
I wrote some minimal working code to illustrade the problem:
nums=46340;
flashymatrixname=false(nums);
flashymatrixname(1:nums+1:end)=true;
flashyarrayname=ones(nums,1);
flashyconstname=1;
while length(flashyarrayname)<46342
[flashyarrayname2]=testfunction1(flashymatrixname, flashyarrayname,flashyconstname);
flashymatrixname(end+1,:)=false;
flashymatrixname(:,end+1)=false;
flashymatrixname(end,end)=true;
flashyarrayname(end+1)=1;
end
function [flashyarrayname2]=testfunction1(flashymatrixname, flashyarrayname,flashyconstname)
flashyarrayname2=NaN(length(flashyarrayname),1);
parfor i=1:length(flashyarrayname)
tempvar1=flashyarrayname(flashymatrixname(:,i));
tempvar2=flashyarrayname(i);
[flashyarrayname2(i)]=testfunction2(tempvar1,tempvar2,flashyconstname);
end
end
function [some_value]=testfunction2(tempvar1,tempvar2,flashyconstname)
some_value=flashyconstname*(1/tempvar2-1./tempvar1);
end
I generted a mex function form testfunction1 with MatLab Coder and it reproduces the error.
I than searched all files in the codegen/mex/testfunction1 folder and subfolders for the flashy names of the matrix and the array according to the link above.
The problem was that I cant find where the matrix size is explicitly declared so I could change it, the size must be somehow given by matlab. In the coder_test_api.cpp are declarations such as:
static void emlrt_marshallIn(const emlrtStack *sp, const mxArray *flashymatrixname, const char_T *identifier, coder::array<boolean_T, 2U> &y)
so somehow the size which is limited to 32 bit indexing and which I need to change is somehow overgiven by " *flashymatrixname" and I dont know how to change it, since the workaround from the link with replacing Int_32 by mwsize does not apply, since its not declared.
So maybe somebody could help me with this. It would also bee important in what kind of files I would need t replace such declaration, I guess i should only change it in the files that Coder created and not in some other files that Coder needs also, like some library headers or so....
Many Thanks in advance,
Best regards
  댓글 수: 2
Ryan Livingston
Ryan Livingston 2021년 1월 17일
Coder generated MEX files also rely on some runtime libraries which similarly assume 32-bit maximum sizes. The task of editing the generated code to work with 64-bit array sizes will be hampered by that if any of those functions are leveraged in your case. In general, the 32-bit assumption will be wide fairly pervasive in the generated MEX.
If the MEX does give you a speedup, a different strategy to consider is to have the MEX work on chunks of the MATLAB array. Then, you'd call the MEX file in a loop passing in various 32-bit-sized chunks. Admittedly, depending on your algorithm this may be difficult or impossible.
I've also made an internal note of this request for 64-bit array support.
Marc Laub
Marc Laub 2021년 1월 17일
The only bibraries I use for compiling are mathutil and emlrt, so just to understand you correct, I would need those libraries 64bit based, otherwise my plan fails at this point already? People that write 64bit mex functions on there own without coder use those libraries instead of the ones Coder uses in the MatLabroot\version\extern\lib ?
The mex with open mp gives a speedup by a factor of 20-30. I'll try to chunk the problematic boolean matrix into pieces, each one indexable with 32bit, and hope that the chunking does not cost more time than mex gives me.

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by