Codegen removes dead code while translating to C/C++

I tried using Matlab Code/codegen tool to translate following Matlab script to C (by wrapping it as a Matlab function named test).
$ matlab -nojvm -nosplash -batch "codegen -I . -config:lib -c test"
% line added by me
function test
randn('seed',0);
n = 2;
m = 2*n;
A1 = [1 1; 1 -1; -1 1; -1 -1];
A2 = [1 0; -1 0; 0 1; 0 -1];
b1 = 2*ones(m,1);
b2 = [5; -3; 4; -2];
fprintf(1,'Hello world...');
end
But I see that the generated C code only contains printf statement.
I want codegen to translate dead (unused) Matlab code also. How can I achieve this? I did not see any codegen option to control this behavior.
void test(void)
{
printf("Hello world...");
fflush(stdout);
}

댓글 수: 3

I want codegen to translate dead (unused) Matlab code also.
Why? They have no impact on the result of this code, so why shouldn't MATLAB Coder get to optimize them away? What are you hoping to achieve by including them in your generated code?
I am trying to understand how different Matlab code elements gets translated into C. Is there a knob to control this behavior?
You could modify the code so it returns those variables. That way they're not unused and so I believe couldn't be optimized away.

댓글을 달려면 로그인하십시오.

답변 (1개)

Gojo
Gojo 2024년 9월 8일
편집: Gojo 2024년 9월 8일

0 개 추천

Hey Niranjan,
I understand that you wish to control the optimizations being performed while generating the code. You can do so by following the below mentioned steps:
  • Change the level of optimizations being performed while generating code. You can achieve this by modifying the settings in Model Configuration Pane and selecting the optimization level for your models by referring to the following documentation: https://www.mathworks.com/help/releases/R2024a/ecoder/ref/level.html
  • The optimizations being performed are drastically reduced in the debugging mode. In order to generate the code using the MATLAB Coder with the debug option, Use the "coder.CodeConfig" object to modify the configuration parameters. For more information on changing the configuration parameters for the optimizations being performed, refer to the "BuildConfiguration" property from the following documentation: https://www.mathworks.com/help/releases/R2024a/coder/ref/coder.codeconfig.html
I hope this helps!

댓글 수: 1

Niranjan
Niranjan 2024년 9월 9일
편집: Niranjan 2024년 9월 9일
Hi Shubham,
Thanks for your inputs. I did try the option that you suggested, but it still does not preserve input Matlab code as it is. The command that I used is as follows:
matlab -batch "cfg = coder.config('lib','ecoder',false); cfg.BuildConfiguration='Debug'; codegen -I . -config cfg -c test

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Problem-Based Optimization Setup에 대해 자세히 알아보기

제품

릴리스

R2024a

질문:

2024년 9월 6일

편집:

2024년 9월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by