Change size of input arguments without recompiling mex with codegen

조회 수: 3 (최근 30일)
I compiled a simple function with matlab coder. The function takes three input arguments: xvec is [nx,1], yvec is [ny,1] and zmat is [nx,ny]. All is good the first time I run the mex function, but if I subsequently change the size of one of the input arrays (say I change nx from nx=8 to nx=7), the function crashes. Of course I can compile again the code and it runs, but I really want to avoid recompilation every time I change the dimension of the inputs!
MWE here:
clear
clc
close all
nx = 8;
ny = 3;
xvec = linspace(0,1,nx)';
yvec = linspace(0,1,ny)';
zmat = xvec.^2+yvec'; % [nx,ny]
%size(zmat)
disp('Calling myfun...')
res = myfun(xvec,yvec,zmat);
% MEX with codegen
disp('Compiling MEX...')
cfg = coder.config('mex');
cfg.GenerateReport = true;
codegen -config cfg myfun -args {xvec,yvec,zmat} -o myfun_mex
disp('Calling myfun_mex...')
res_mex = myfun_mex(xvec,yvec,zmat);
err_mex = abs(res-res_mex)
and here is the function:
function res = myfun(x,y,z)
res = sum(x)+sum(y)+sum(sum(z));
end
First time, I compile and it runs OK. Then, if I set nx=7, it crashes with the following error message:
```
Incorrect size for expression 'x': expected [8x1] but found [7x1].
Error in myfun_mex
Error in main (line 22)
res_mex = myfun_mex(xvec,yvec,zmat);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```
Any help is greatly appreciated, thanks!

채택된 답변

Naman Saraf
Naman Saraf 2025년 1월 28일
편집: Naman Saraf 2025년 1월 28일
Hi,
You can specify the inputs to be "unbounded" during code generation. MATLAB Coder then generates code that can accept inputs of different sizes at runtime without requiring to regenerate the code.
The command line way of achieving this is shown in the code below. You can also specify this using the MATLAB Coder App.
Please refer to the following page for additional details.
%====
clear
clc
close all
nx = 8;
ny = 3;
xvec = linspace(0,1,nx)';
yvec = linspace(0,1,ny)';
zmat = xvec.^2+yvec'; % [nx,ny]
%size(zmat)
disp('Calling myfun...')
res = myfun(xvec,yvec,zmat);
% MEX with codegen
disp('Compiling MEX...')
cfg = coder.config('mex');
cfg.GenerateReport = true;
% specify unbounded inputs for code generation
xvecCoder = coder.typeof(0, [inf,1]);
yvecCoder = coder.typeof(0, [inf,1]);
zmatCoder = coder.typeof(0, [inf,inf]);
codegen -config cfg myfun -args {xvecCoder,yvecCoder,zmatCoder} -o myfun_mex
disp('Calling myfun_mex...')
res_mex = myfun_mex(xvec,yvec,zmat);
err_mex = abs(res-res_mex)
% different inputs
nx = 7;
ny = 4;
xvecNew = linspace(0,1,nx)';
yvecNew = linspace(0,1,ny)';
zmatNew = xvec.^2+yvec'; % [nx,ny]
disp('Calling myfun_mex with new inputs ...')
res_mexNew = myfun_mex(xvecNew,yvecNew,zmatNew);
%====
  댓글 수: 3
Ram Kokku
Ram Kokku 2025년 2월 4일
@Alessandro, As of R2024b, GPU Coder does not support defining unbounded variables as GPU input. You are hitting that limitation. However, bounded variable size inputs are supported. so,
coder.typeof(0, [<large number>, 1], [1, 0], 'Gpu', true)
should work.
Alessandro
Alessandro 2025년 2월 4일
Ok, thanks! However, when using the Gpu coder app, I was able to specify the arrays xvec, yvec and zmat as double with size :inf *1 and :inf * :inf. I did not get any error. It seems that there is a discrepancy between the command line codegen and the app

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2025년 1월 26일
You need to declare the inputs with coder.varsize
You might need to define a maximum array size.
  댓글 수: 1
Alessandro
Alessandro 2025년 1월 26일
편집: Alessandro 2025년 1월 26일
Thanks, can you please show how to do this? I read the matlab help of coder.varsize but is quite complicated

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

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by