Memory issues in generated for loops
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Jonah Caplan
le 10 Août 2015
Réponse apportée : Jonah Caplan
le 12 Août 2015
The simple Matlab code:
function y = fcn(u)
y = u;
for i = single(0:0.15:1000-1)
y = y + (i+1)^u;
end
generates the following C:
loop.c:
int32_T i;
static const real32_T b[6661] = { 0.0F, 0.15F, 0.3F, 0.45F, 0.6F, 0.75F, 0.9F,
1.05F, 1.2F, 1.35F, 1.5F, 1.65F, 1.8F, 1.95F, 2.1F, 2.25F, 2.4F, 2.55F, 2.7F,
2.85F, 3.0F, 3.15F, 3.3F, 3.45F, 3.6F, 3.75F, 3.9F, 4.05F, 4.2F, 4.35F, 4.5F,
4.65F, 4.8F, 4.95F, 5.1F, 5.25F, 5.4F, 5.55F, 5.7F, 5.85F, 6.0F, 6.15F, 6.3F,
6.45F, 6.6F, 6.75F, 6.9F, 7.05F, 7.2F, 7.35F, 7.5F, 7.65F, 7.8F, 7.95F, 8.1F,
8.25F, 8.4F, 8.55F, 8.7F, 8.85F, 9.0F, 9.15F, 9.3F, 9.45F, 9.6F, 9.75F, 9.9F,
...
};
for (i = 0; i < 6661; i++) {
/* '<S1>:1:3' */
/* '<S1>:1:4' */
rtb_y += (real32_T)pow(b[i] + 1.0F, for_loop_U->In1);
/* '<S1>:1:3' */
}
The loop consumes a lot of memory due to the high number of iterations, but I can't find a simple way to force the generator not to use arrays (optimizing for ROM efficiency is already turned on). I should be able to reduce the memory footprint at the price of re-calculating i*0.15 with floating point hardware.
Another similar example:
function y = fcn(u)
y = u;
for i = single(0:0.15:1000000-1)
y = y - u*(i+1);
end
loop.h:
typedef struct {
real_T y[6666661];
} DW_for_loop_T;
loop.c:
for_loop_DW->y[0] = 0.0;
for_loop_DW->y[6666660] = 999999.0;
for (k = 0; k < 3333329; k++) {
kd = (((real_T)k) + 1.0) * 0.15;
for_loop_DW->y[k + 1] = kd;
for_loop_DW->y[6666659 - k] = 999999.0 - kd;
}
for_loop_DW->y[3333330] = 499999.5;
/* '<S1>:1:3' */
for (k = 0; k < 6666661; k++) {
/* '<S1>:1:3' */
/* '<S1>:1:4' */
rtb_y -= (((real32_T)for_loop_DW->y[k]) + 1.0F) * for_loop_U->In1;
/* '<S1>:1:3' */
}
This time, despite the fact that y is ultimately a single, the intermediate values (for_loop_DW->y and kd) are doubles, which consumes twice as much memory.
0 commentaires
Réponse acceptée
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur MATLAB Coder dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!