Hi
I'm looking for a sample code of how to use "coder. unroll" in a while loop. I intend to perform a division operation and want to unroll the loop if I achieve the required accuracy level.
For example,
format long g
a = (4);
b = (6);
T = numerictype('Signed', true,...
'WordLength', 64,...
'FractionLength', 60);
quotient = divide(T,a,b)
quotient =
0.666666666666667
and I want to stop the division process when a certain fractional bit accuracy, say 0.6667, is reached.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 16 Aug. 2022

1 Stimme

coder.unroll() by itself cannot do that. coder,unroll() always unrolls the number of times given in the for loop. The loop limits do not need to be constant inside the function being unrolled, but if not then during the code generation phase, the limit must be given as a numeric constant. If you had two different limits that you wanted to unroll to, then you would have to generate code twice, once for each limit.

6 Kommentare

Walter Roberson
Walter Roberson am 16 Aug. 2022
You could do a numeric analysis of the algorithm to determine the worst-case needed to achieve the desired precision over the valid input range, and code that number as the number of loops. coder.unroll() has no ability to stop early if a goal is reached.
Also, coder.unroll() must be placed on the line immediately before a for loop. You cannot call coder.unroll() just before a function call, with the expectation that loops in the function call will be unrolled to the desired depth. coder.unroll() is only for loops directly inside your own code.
Life is Wonderful
Life is Wonderful am 16 Aug. 2022
Verschoben: Bruno Luong am 16 Aug. 2022
So a coder.Unroll should be supplied from outside rather than during runtime.
Division consumes many cycles, and the number of iterations depends on the limit accuracy; if I reach the desired accuracy, I no longer need to iterate; otherwise, I am wasting cycles.
So the plan is to supply coder.unroll with a constant so that I can use the benefit.
It would be great if you could share a fictitious example to expand on the above example.
The limit for unrolling cannot be dynamic. Multiple copies of the code are generated and the loop is completely replaced.
MATLAB does not generate code along the lines of
K = 1; Z(K) = sin(X(K));
K = 2; Z(K) = sin(X(K));
K = 3; Z(K) = sin(X(K));
if n > 3
for K = 4 : n
Z(K) = sin(X(K));
end
end
That is, you are not providing some kind of promise of a minimum number of loops and unrolling to that count. coder.unroll always fully unrolls according to the constant count passed during code compilation phase.
coder.unroll is unable to handle the situation you are describing. It always does the full number of loops.
If you knew the maximum number of iterations then you could potentially hand-unroll, something along the lines of
remainder = a;
precision = inf;
divisor = 0;
while true
[divisor, remainder, precision] = divide_one_step(divisor, remainder, b);
if precision <= target; break; end
[divisor, remainder, precision] = divide_one_step(divisor, remainder, b);
if precision <= target; break; end
%more stuff
end
and keep repeating those two lines right there the maximum number of iterations that will ever be needed.
Will this give any improvement compared to a for loop that is not unrolled? Probably not. Unrolling has efficiency gains when there are no conditional branches in the code, which allows for more possibilities about keeping values in registers, or consolidating operations (e.g., instead of adding one to a value 8 times, add 8 to it once at the end.)
Life is Wonderful
Life is Wonderful am 16 Aug. 2022
Yup, I agree. Thanks a lot for your efforts and time.
So coder.unroll is ruled out .
Do you have any suggestions for division optimization?
Walter Roberson
Walter Roberson am 16 Aug. 2022
I seem to recall that the CORDIC division algorithm stops early if it reaches its goal.
Life is Wonderful
Life is Wonderful am 16 Aug. 2022
Thank you very much for your kind assistance. !!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Test Model Components finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2022a

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by