Optimize Loops in Generated Code - MATLAB & Simulink (original) (raw)

Main Content

Transform loops in the generated code to suit your execution speed and memory requirements. Loop control objects instruct the code generator to optimize loops in the generated code.

You can append these transforms to a loop control object to optimize the generated loops:

Instruct the code generator to optimize loops in the generated code by:

Optimize Loops By Using Objects of coder.loop.Control

Create objects of coder.loop.Control in your MATLABĀ® code and append the required transformations to the object. For example, to apply the vectorize transform by using acoder.loop.Control object, follow this pattern:

function out = applyVectorize out = zeros(1,100);

loopObj = coder.loop.Control; loopObj = loopObj.vectorize('loopId'); loopObj.apply;

for loopId = 1:100 out = out + loopId; end

To generate code for this function for an IntelĀ® target processor, use these commands:

cfg = coder.config('lib'); cfg.InstructionSetExtensions = "SSE2"; codegen -config cfg applyVectorize -launchreport

The generated code uses the SSE2 SIMD instruction set.

void applyVectorize(double out[100]) { int i; int loopId; memset(&out[0], 0, 100U * sizeof(double)); for (loopId = 0; loopId < 100; loopId++) { for (i = 0; i <= 98; i += 2) { __m128d r; r = _mm_loadu_pd(&out[i]); _mm_storeu_pd(&out[i], _mm_add_pd(r, _mm_set1_pd((double)loopId + 1.0))); } } }

You can append multiple transforms to the same loop control object. Call theapply method before defining the loops in your code. For example, you can add a parallelize transform to the loop if a variableinputVal is greater than some threshold value.

... loopObj = coder.loop.Control; loopObj = loopObj.parallelize('i'); if inputVal > threshold loopObj = loopObj.vectorize('inputVal'); end ... loopObj.apply; for i = 1:10
for inputVal = 1:10 ... end end...

Optimize Loops By Calling Member Functions Independently

You can apply loop transformations by calling the loop optimization functions immediately before the loop itself. You can apply these functions to the loops in your code:

Follow the pattern shown here:

function out = applyInterchange out = rand(10,7);

coder.loop.interchange('loopA','loopB'); for loopA = 1:10 for loopB = 1:7 out(loopA,loopB) = out(loopA,loopB) + loopA; end end

Alternatively, if you want to declare the loop transform prior to the loop, you can store the loop object returned by thecoder.loop.interchange function call. However, you must call the apply method for the returned object before defining the loop.

function out = applyInterchange out = rand(10,7);

loopObj = coder.loop.interchange('loopA','loopB'); ...

loopObj.apply; for loopA = 1:10 for loopB = 1:7 out(loopA,loopB) = out(loopA,loopB) + loopA; end end

Generate code for these functions by running this command:

codegen -config:lib applyInterchange -launchreport

The generated code is shown here:

void applyInterchange(double out[70]) { int loopA; int loopB; if (!isInitialized_applyInterchange) { applyInterchange_initialize(); } b_rand(out); for (loopB = 0; loopB < 7; loopB++) { for (loopA = 0; loopA < 10; loopA++) { int out_tmp; out_tmp = loopA + 10 * loopB; out[out_tmp] += (double)loopA + 1.0; } } }

See Also

coder.loop.Control | coder.loop.interchange | coder.loop.parallelize | coder.loop.reverse | coder.loop.tile | coder.loop.unrollAndJam | coder.unroll | coder.loop.vectorize