How to define lower limit at while code generation?

조회 수: 2 (최근 30일)
Emre Doruk
Emre Doruk 2021년 11월 22일
편집: Ryan Livingston 2021년 12월 2일
Hi,
I am trying to generate C++ code for my function with Codegen. It has variable data size but has limits,min 100, max 1000. I know I can define upto limit but is it possible to define down limit?
Thanks for your help,

채택된 답변

Ryan Livingston
Ryan Livingston 2021년 11월 24일
There is unfortunately no way to define a lower bound for a size. In this case, if you define the upper bound, Coder will assume the size could be anything up to that bound.
  댓글 수: 2
Emre Doruk
Emre Doruk 2021년 11월 29일
Hi Ryan,
Thanks for your answer. Is there a way for work around? Because lack of down limit causes unneccessary complexity in generated code. I try to handle it with defining fixed size (expected max size) and manage this size issue in C++ code. But I am not sure is this way has better for performance.
Ryan Livingston
Ryan Livingston 2021년 12월 2일
편집: Ryan Livingston 2021년 12월 2일
That fixed size approach could work. In some cases you can also give value range hints to MATLAB Coder by using MATLAB assert to prune dead code. This may help but isn't a guarantee. For example
function y = foo(x)
t = zeros(x);
s = size(t,1);
assert(s > 100);
if s < 100
y = 1;
else
y = -1;
end
end
>> codegen foo -args 1 -config:lib -report
generates
double foo(double x)
{
(void)x;
return -1.0;
}
So you can use assert to give hints near the usage of the size lower bound. Since you can't specify lower bounds on the matrix size, the assert needs to be based on a variable whose value is the size rather than doing assert(size(t,1) > 100).
I'll give the warning that these asserts are hints which may or may not result in code optimization based on Coder heuristics. They can be useful but I'd warn against going down a rabbit hole without direct evidence of benefit in your use case.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by