Codegen bug - infinite "while" loop generated in place of simple "for" loop

4 visualizaciones (últimos 30 días)
It looks that I encountered the codegen bug. I have a MEX application with multiple entries, i.e. I run the Matlab code as skeleton that calls a few MEX files. For one of them the code apparently ends up in the infinite loop. When I do not run the MEX file for that function (in the coder.runTest() I do not select that function to run as MEX file), so I simply run .m function instead - to code behaves fine.
Also, apparently since the codegen wrongly decides to generate the infinite loop it does not generate any code after that infinite loop. That "for" loop that gets generated as infinite "while" loop is a simple one - one line "printf" (see below). If I simply comment out the loop, the code gets generated, on the first glance it looks OK - but its behaviour is odd, it does not work as from Matlab - yet to debug it in C environment, to provide more details. By the way, for almost 6 months I have been generating the code which was working fine, but suddenly I have plenty of problems. I also encountered other problems where my generated code does not behave as Matlab code, but I am focused right now to only this problem with infinite loop being wrong generated, since that one seems a simple to see.
If this atracts the attention from Matlab supports I am happy to send relevant files.
Matlab "for" loop code:
% why codegen here generates infinite while loop?
for channelNr = 1: numOfTrackedChannels
fprintf('Chan%2u: Nk_ms: %u, codePhase_chips: %4f, pseudoRanges_ms: %4f, fractPseudoRanges_ms: %4f, predictedPseudoranges_ms: %4f, satClockBias_ms: %4f\n',...
uint32(channelNr), uint32(Nk_ms(channelNr)), codePhase_chips(channelNr), pseudoRanges_ms(channelNr), fractionalPseudoRanges_ms(channelNr), predictedPseudoranges_ms(channelNr), satClockBias_ms(channelNr));
end
Generated "while" infinite loop: (
/* why codegen here generates infinite while loop? */
/* 'calcUserPos:58' for channelNr = 1: numOfTrackedChannels */
while (1) {
/* 'calcUserPos:59' fprintf('Chan%2u: Nk_ms: %u, codePhase_chips: %4f, pseudoRanges_ms: %4f, fractPseudoRanges_ms: %4f, predictedPseudoranges_ms: %4f, satClockBias_ms: %4f\n',... */
/* 'calcUserPos:60' uint32(channelNr), uint32(Nk_ms(channelNr)), codePhase_chips(channelNr), pseudoRanges_ms(channelNr), fractionalPseudoRanges_ms(channelNr), predictedPseudoranges_ms(channelNr), satClockBias_ms(channelNr)); */
d0 = muDoubleScalarRound(Nk_ms_data[0]);
if (d0 < 4.294967296E+9) {
if (d0 >= 0.0) {
u2 = (uint32_T)d0;
} else {
u2 = 0U;
}
} else if (d0 >= 4.294967296E+9) {
u2 = MAX_uint32_T;
} else {
u2 = 0U;
}
c_print_processing(u2, codePhase_chips_data[0], pseudoRanges_ms_data[0],
fractionalPseudoRanges_ms_data[0],
predictedPseudoranges_ms, satClockBias_ms_data[0],
&validatedHoleFilling_f2, &validatedHoleFilling_f3,
&validatedHoleFilling_f4, &validatedHoleFilling_f5,
&validatedHoleFilling_f6, &validatedHoleFilling_f7);
mexPrintf("Chan%2u: Nk_ms: %u, codePhase_chips: %4f, pseudoRanges_ms: %4f, fractPseudoRanges_ms: %4f, predictedPseudoranges_ms: %4f, satCl"
"ockBias_ms: %4f\n", 1U, validatedHoleFilling_f2,
validatedHoleFilling_f3, validatedHoleFilling_f4,
validatedHoleFilling_f5, validatedHoleFilling_f6,
validatedHoleFilling_f7);
}
  5 comentarios
Dusko Vujadinovic
Dusko Vujadinovic el 4 de En. de 2022
I attached the vecNormProblem.zip file that contains all relevant files to run this to demonstrate the problem.
The script is runVecNormProblem.m, the input data is in input_data.mat, the small code that demonstrates the problem is in vecNormProblem.m the code generation configuration scrpit is codegen_mex_vecNormProblem.m.
The generated .c code with infinite loop is in vecNormProblem.c file.
1. Copy all to the same folder
2. Unzip
3. run codegen_mex_vecNormProblem.m from the command line
4. Find the vecNormProblem.c in automatically opened report to see the infinite loop, at the end of the generated code:
channelNr = 1;
while ((uint32_T)channelNr <= numOfChannels) {
/* 'vecNormProblem:22' fprintf('distance: %4f\n',distance(channelNr)); */
mexPrintf("distance: %4f\n", dv0[0]);
channelNr = 2;
}
The input parameter numOfChannels = 6, but the loop counter channelNr does not incremented above 2. It is interesting that if instead of numOfChannels, which value is passed to the function, we use hardcoded value - the generated code is fine.
Sumit Ghosh
Sumit Ghosh el 10 de En. de 2022
A bug has been created for the development team.
Someone will update this post once some progress is made.

Iniciar sesión para comentar.

Respuesta aceptada

Sumit Ghosh
Sumit Ghosh el 10 de En. de 2022
This problem is fixed in R2018b and later.
You can try in R2018b or later. If you can't upgrade, please reach out to Technical Support.

Más respuestas (0)

Categorías

Más información sobre MATLAB Coder en Help Center y File Exchange.

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by