Editing code for mex function to support 64 bit indexing

2 visualizaciones (últimos 30 días)
Marc Laub
Marc Laub el 15 de En. de 2021
Comentada: Marc Laub el 17 de En. de 2021
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 comentarios
Ryan Livingston
Ryan Livingston el 17 de En. de 2021
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 el 17 de En. de 2021
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.

Iniciar sesión para comentar.

Respuestas (0)

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by