Attempting to turn off Dynamic Memory allocation but struggling to set variable size upper bound

68 次查看(过去 30 天)
I'm attempting to turn OFF the dynamic memory allocation setting (attempt to set to NEVER) in codegen, but I'm getting an error on one of my variables:
Computed maximum size is not bounded.
Static memory allocation requires all sizes to be bounded.
The computed size is [1 x :?].
Please consider enabling dynamic memory allocation to allow unbounded sizes. <a href="matlab:helpview('ecoder','msginfo_Coder_builtins_DMTUnknownUpperBound');">More information</a>
This variable in question originally comes from an input to my entry point function which I defined with upper bounds, but somehow internally Matlab must believe that I'm resizing the variable arbitrarily. Does anyone have a strategy for how I can track down where in my logic Matlab has decided that the sizem of this variable has become unbounded again and what operations I should look for that might be interpreted as having "unbounding" my previously bounded array?
Basically, I've created an array of structs, and during a series of calculations, I'm pruning/subsetting the array by tossing out elements that I'm not interested in. I have a maxmimum number of structs (the original input array, which I can set at some arbitrary limit in the input definition of the Coder UI), and at various pruning steps, I get a vector of indices and I update the array to contain the new array created by the using the index array as the indices.
Something like:
obj = struct( 'X', 0 , 'Y', 0, 'Z', 0);
obj = repmat( obj, 1, 1000 ); % my upper bound
X = rand(1,1000)*128;
Y = rand(1,1000)*128;
Z = rand(1,1000);
for i=1:1000
obj(i).X = X(i);
obj(i).Y = Y(i);
obj(i).Z = Z(i);
end
function out = entry_point( obj, thresh )
idx = [ obj.Z ] > thresh;
out = obj(idx);
end
The above example works fine, but in my more complicated actual code, Coder decides that the "obj" variable above us unbounded for some reason. What are the language heuristics that make Coder believe that the variable has become unbounded when I specifically define the input as a strtucture with bounds of [ 1 1000 ]?

回答(2 个)

Fangjun Jiang
Fangjun Jiang 2024-12-3,14:00
编辑:Walter Roberson about 12 hours 前
Try allocating the variable in one shot, e.g.
obj=struct('x',num2cell(rand(1,1000)))

Sumukh
Sumukh about 13 hours 前
Based on the example code provided, it is possible that the code generator views the "obj" instance inside the "entry_point" function without the upper bounds specified using the "repmat" function. It is also possible that the "obj" array size is changing when pruning the array.
When dynamic allocation is disabled, the code generator uses static allocation. As a result, the arrays must be bounded within the code while initializing them at different instances, or by using 'asserts"/"coder.varsize". The code is analysed to get the upper bounds of the array and if it does not find it, it throws the error as stated.
To resolve this issue, instead of specifying the 1000 as a fixed-size dimension, it can be specified as a variable-size upper bound using "assert" function that ensures that there are bounds for static allocation. This also ensures that any pruning done to the array does not trigger an error, as it will be variable-sized with an upper bound. Please refer to the following documentation to understand how to use "assert":
You can try the following example code for using "assert":
assert(sz <= 1000)
obj = repmat(obj,1,sz);
Another way to ensure bounds is to use the "coder.varsize" command. "coder.varsize" can be used to set the bounds on the array dimensions after the declaration, as follows:
% Setting the 2nd dimesnsion of obj as variable-size upper-bound
coder.varsize("obj",[1 1000],[0 1]);
This function is not supported for arguments of "entry_point" functions, however, there are workarounds suggested to provide the variable-size to these input arguments, as stated in the "Limitations" section of the following documentation:
Sharing the complete code would help in debugging any issues that persist.
I hope this helps resolve the error.

类别

Help CenterFile Exchange 中查找有关 Deployment, Integration, and Supported Hardware 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by