Editing code for mex function to support 64 bit indexing
2 次查看(过去 30 天)
显示 更早的评论
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
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.
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!