Change size of input arguments without recompiling mex with codegen

8 Ansichten (letzte 30 Tage)
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!

Akzeptierte Antwort

Naman Saraf
Naman Saraf am 28 Jan. 2025
Bearbeitet: Naman Saraf am 28 Jan. 2025
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 Kommentare
Ram Kokku
Ram Kokku am 4 Feb. 2025
@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 am 4 Feb. 2025
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 26 Jan. 2025
You need to declare the inputs with coder.varsize
You might need to define a maximum array size.
  1 Kommentar
Alessandro
Alessandro am 26 Jan. 2025
Bearbeitet: Alessandro am 26 Jan. 2025
Thanks, can you please show how to do this? I read the matlab help of coder.varsize but is quite complicated

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by