How can I prevent the calculation of intermediary results while code generation with the embedded coder?
조회 수: 6 (최근 30일)
이전 댓글 표시
For example I have created a Simulink subsystem with the following M-Function.
function y = fcn(u1,u2)
%#codegen
p1 = 0.13285;
p2 = 999.1;
p3 = 9.806;
p4 = 132.535;
p = p1*p2*p3;
y = p2*p4*u1 - p*u2;
end
After running code generation with "C++ Code --> Build this Subsystem" the Embedded Coder create the following code (extract from Subsystem.cpp).
/* Model step function */
void Subsystem_step(void)
{
/* Outport: '<Root>/Out1' incorporates:
* Inport: '<Root>/In1'
* Inport: '<Root>/In2'
* MATLAB Function: '<S1>/MATLAB Function'
*/
/* MATLAB Function 'Subsystem/MATLAB Function': '<S2>:1' */
/* '<S2>:1:11' */
Subsystem_Y.Out1 = 132415.7185 * Subsystem_U.In1 - 1301.5546456099999 *
Subsystem_U.In2;
}
As you can see, the coder calculates intermediary results without using the absolute values in the output equation. For complex systems the inaccuracy of the calculation generates a divergence that produces instability. What chances in the configuration parameters are necessary to prevent this simplification of calculation while code generation?
current configuration: * system target file: ert.tlc (Visual C/C++ Solution File) * language: C++ * compiler optimization level: optimization off * prioritized objectives: safety precation, debugging * code replacement library: C++ (ISO) * shared code placement: auto * parentheses level: maximum
댓글 수: 4
Walter Roberson
2013년 5월 31일
There is no difference between precalculation of p1*p2*p3 and run-time calculation, provided the architecture stays constant.
Are you compiling on one brand of processor (e.g., Intel) but executing on another (e.g., AMD) ? If so then Yes, in such a case you could get a one bit difference for p1*p2*p3 (unless the rounding settings were different for the two systems.)
There can be a difference between p1*p2*p3*u2 and u2*p1*p2*p3 due to round-off, as order of operations is important in floating point. MATLAB uses left-to-right evaluation for expressions of the same precedence.
If these kinds of differences are critical you should be considering using the fixed-point toolbox.
채택된 답변
Mike Hosea
2013년 5월 31일
편집: Mike Hosea
2013년 6월 1일
I think you have misapprehended the source of your problem. The exact mathematical result p1*p2*p3 is 1301.55464561. This number does not have an exact representation in 64-bit IEEE double precision binary floating point (for that matter, neither do p1, p2, and p3). However, the closest floating point number is, in fact, 1301.5546456099999, since the next larger one is 1301.5546456100003. If you are observing a divergence on account of this optimization, then it would probably have to involve the use of 80-bit extended precision registers or some such. On x86 architectures with C compilers these are notoriously difficult to predict or control.
댓글 수: 4
Walter Roberson
2013년 6월 2일
Note that is at the C (or C++) level, not the MATLAB level.
And for completeness in case you are doing some odd cross-compiling: http://publib.boulder.ibm.com/infocenter/zos/v1r13/topic/com.ibm.zos.r13.bpxbd00/fedecsetround.htm
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Deployment, Integration, and Supported Hardware에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!