Why do large data buffers get created when I use MATLAB Coder 2.2 (R2012a) when I use logical indexing operations?

1 view (last 30 days)
I am generating code from several MATLAB functions using MATLAB Coder 2.2 (R2012a). I have four functions called "func1.m", "func2.m", "func3.m" and "func4.m". They all call an auxiliary function called "CallFunc.m". The output of "CallFunc.m" is then passed through a logical indexing operator to remove negative numbers as follows:
function a = func1(b)
...
a = CallFunc(b);
a(a<0) = 0;
...
end
The issue is that my four call site functions all use ASSERT statements for the size of the input variable "b", which gets reflected in my generated code. This is set up as follows:
Function Name -- Maximum Input Array Size
func1 -- 10
func2 -- 50
func3 -- 100
func4 -- 1511111
When I generate code, MATLAB Coder 2.2 (R2012a) assumes that the worst-case data size going into "CallFunc" is therefore 1511111. The PROJECT_types.h file then generates buffers of size 1511111 for all four of my functions.
I tried to inline "CallFunc.m" to resolve this, but somehow adding the following statement to my "CallFunc.m" did not work:
coder.inline('always');

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 6 Sep 2012
MATLAB Coder 2.2 (R2012a) does not generate efficient C/C++ code for logical indexing operations, and this is a work in progress by our development team. Currently, logical indexing creates a copy of the original array and populates it with the elements that pass the logical test. However, the worst-case data size is still assigned to this temporary variable.
As a workaround, you can replace the logical indexing operation as follows:
for i = 1:numel(a)
if ( a(i) < 0 )
a(i) = 0;
end
end
Also, you will still need to keep the call to CODER.INLINE in the body of any functions that uses this array "a" (for example, CallFunc.m) as follows:
function a = CallFunc(b)
coder.inline('always');
...
end

More Answers (0)

Products


Release

R2012a

Community Treasure Hunt

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

Start Hunting!