Main Content

coder.gpu.iterations

Pragma that provides information to the code generator for making parallelization decisions on variable bound loops

Description

example

coder.gpu.iterations(AVG_NUM_ITER) pragma can be used to specify the average number of iterations (AVG_NUM_ITER) for a variable-bound for-loop that immediately follows it. This value is used to provide heuristics towards making parallelization decisions for imperfect loops. This pragma does not affect fixed-bound for-loops.

This is a code generation function. It has no effect in MATLAB®.

Examples

collapse all

This example shows how to use the coder.gpu.iterations pragma to augment information used by the code generator to make parallelization decisions.

Consider the following MATLAB entry-point function myFun containing a simple nested loop.

function [a, c] = myFun(b, N1)

coder.gpu.kernelfun();
a = coder.nullcopy(zeros(1, N1));
c = coder.nullcopy(b);

for i = 1:N1             % Loop1
    a(i) = 1;
    
    for j = 1:20          % Loop2
        c(i,j) = 2 * b(i,j);
    end
end

end

In this case, Loop 1 is an imperfect loop, preventing the code generator from parallelizing the outer loop Loop 1.

Modify the entry-point function by using the coder.gpu.iterations pragma to inform the code generator the average number of iterations that the loop is expected to execute.

function [a, c] = myFun(b, N1)

coder.gpu.kernelfun();
a = coder.nullcopy(zeros(1, N1));
c = coder.nullcopy(b);

coder.gpu.iterations(25); % AVG_NUM_ITER
for i = 1:N1             % Loop1
    a(i) = 1;
    
    for j = 1:20          % Loop2
        c(i,j) = 2 * b(i,j);
    end
end

end

Loop 1 is parallelized when the AVG_NUM_ITER > 20 (Loop2 bound) regardless of the value of N1.

Input Arguments

collapse all

Specify the average number of iterations (AVG_NUM_ITER) for a variable-bound for-loop that immediately follows the coder.gpu.iterations pragma.

Introduced in R2019a