Вы находитесь на странице: 1из 44

Latest Features in code generation

Michael Donnenfeld, Application Engineer Code Generation Systematics

November 2013

2012 The MathWorks, Inc. 1

C/C++ Coders
MATLAB Coder - Code from MATLAB Portable code for numerical algorithms Desktop applications (standalone, library) Simulink Coder - Code from Simulink

MATLAB and Simulink

Algorithm and System Design

Rapid prototyping or HIL applications Real-time machines (xPC Target) Embedded Coder Production optimized code Embedded applications MCU and DSP (fixed or float) Code verification (in-the-loop) Target-specific support (APIs and examples)

All coders generate portable code (ANSI/ISO C) by default.


Latest Features in MATLAB Coder

November 2013

2012 The MathWorks, Inc. 4

fprintf, fopen, and fclose in MATLAB Coder

Log data in generated code

load and coder.load

Access data stored in MAT-files
Use load to read data from MAT-files from within generated MEX-files only Use coder.load to read data from MAT-files and embed them as constants

Nonpersistent Handle Objects

Use more programming idioms with Handle objects
Create an object and pass it to a subfunction Create an object and assign it to a property of another object Allocate and use an object repeatedly inside a loop Create an object in a function and return it

Structures Passed by Reference to Entry-Point Functions

Reduce data copies for better performance

MEX C Code generation for parfor

Generate code from parfor loops in MEX C code

Simplify integration with existing C code Runtime errors handled using setjmp/longjmp rather than try/catch

Build Tool Integration

Allows users to choose different toolchains and build configurations
A new panel Toolchain for toolchain-related settings.
Toolchain pulldown allows you select registered toolchains. User can add their own. Build Configuration pulldown allows to switch easily among different configurations

Correspondingly, three new coder.config properties are added:

Toolchain BuildConfiguration CustomToolchainOptions


Optimize X = [X C]
codegen -config:lib grow_a_vec_pattern -args {} -report

This is a typical pattern of growing a vector but it generates data copies in previous releases R2013a generates optimized code, improves RAM/ROM and speed
for (i = 0; i < 10; i++) { ...................... for (i0 = 0; i0 < loop_ub; i0++) { b_x_data[i0] = (int8_T)x_data[i0]; } b_x_data[y_size[1]] = (int8_T)(1 + i); ....................... for (i0 = 0; i0 < x_size_idx_1; i0++) { x_data[i0] = (real_T)b_x_data[i0]; } ...................... }

coder.varsize(x',[1,10]); . for i = 1 : 10 x = [x i]; end


for (i = 0; i < 10; i++) { x_data[y_size[1]] = 1.0 + (real_T)i; x_size[1]++; }





For Use with Embedded Coder


Static Code Metrics Report

See estimated code metrics at code generation time

Target-independent analysis Report showing:
Lines of code Global RAM estimate Stack size estimate Cyclomatic complexity estimate


Code Replacement Library Report

Find code replacement functions used in generated code
Improves discoverability of code replacement library usage in generated code Provides traceability of used function back to MATLAB function that triggered the replacement
See next slide


Code Replacement Library Traceability

Discover why code replacement functions were not used in generated code
Aids in debugging code replacement library entries Provides information for why CRL entry was not used for replacement by a particular MATLAB function


Latest Features in MATLAB Coder

November 2013

2012 The MathWorks, Inc. 16

Code Generation Support for Statistics Toolbox functions

Use 100+ Statistics Toolbox functions
betacdf betainv betapdf betarnd betastat binocdf binoinv binopdf binornd binostat cdf chi2cdf chi2inv chi2pdf chi2rnd chi2stat evcdf evinv evpdf evrnd evstat expcdf expinv exppdf exprnd expstat fcdf finv fpdf frnd fstat gamcdf gaminv gampdf gamrnd gamstat geocdf geoinv geomean geopdf geornd geostat gevcdf gevinv gevpdf gevrnd gevstat gpcdf gpinv gppdf gprnd gpstat harmmean hygecdf hygeinv hygepdf hygernd hygestat icdf iqr kurtosis logncdf logninv lognpdf lognrnd lognstat mad mnpdf moment nancov nanmax nanmean nanmedian nanmin nanstd nansum nanvar nbincdf nbininv nbinpdf nbinrnd nbinstat ncfcdf ncfinv ncfpdf ncfrnd ncfstat nctcdf nctinv nctpdf nctrnd nctstat ncx2cdf ncx2rnd ncx2stat normcdf norminv normpdf normrnd normstat pdf poisscdf poissinv poisspdf poissrnd poisstat prctile quantile randg random raylcdf raylinv raylpdf raylrnd raylstat skewness tcdf tinv tpdf trnd tstat unidcdf unidinv unidpdf unidrnd unidstat unifcdf unifinv unifpdf unifrnd unifstat wblcdf wblinv wblpdf wblrnd wblstat zscore


Code Generation Support for Phased Array System Toolbox functions

Use 80+ functions
aictest albersheim ambgfun aperture2gain az2broadside azel2phitheta azel2phithetap at azel2uv azel2uvpat azelaxes beat2range billingsleyicm broadside2az cart2sphvec cbfweights circpol2pol dechirp delayseq depressionang dop2speed dopsteeringvec effearthradius espritdoa fspl gain2aperture global2localcoord grazingang horizonrange lcmvweights local2globalcoord mdltest mvdrweights noisepow npwgnthresh phitheta2azel phitheta2azelpat phitheta2uv phitheta2uvpat physconst pol2circpol polellip polloss polratio polsignature pulsint radareqpow radareqrng radareqsnr radarvcd radialspeed range2beat range2bw range2time rangeangle rdcoupling rocpfa rocsnr rootmusicdoa rotx roty rotz sensorcov sensorsig shnidman speed2dop sph2cartvec spsmooth steervec stokes stretchfreq2rng surfacegamma surfclutterrcs systemp time2range unigrid uv2azel uv2azelpat uv2phitheta uv2phithetapat val2ind


Code Generation Support for Phased Array System Toolbox functions

Use 50+ System objects
phased.CosineAntennaElement phased.CrossedDipoleAntennaElement phased.CustomAntennaElement phased.CustomMicrophoneElement phased.IsotropicAntennaElementIsotropic phased.OmnidirectionalMicrophoneElement phased.ShortDipoleAntennaElement phased.ULA phased.URA phased.ConformalArray phased.PartitionedArray phased.ReplicatedSubarray phased.SteeringVector phased.ArrayGain phased.ArrayResponse phased.ElementDelay phased.Collector phased.Radiator phased.WidebandCollector phased.LinearFMWaveformLinear phased.PhaseCodedWaveform phased.RectangularWaveform phased.SteppedFMWaveform phased.FMCWWaveform phased.MatchedFilter phased.Transmitter phased.ReceiverPreamp phased.PhaseShiftBeamformer phased.LCMVBeamformer phased.MVDRBeamformer phased.SubbandPhaseShiftBeamformer phased.FrostBeamformer phased.TimeDelayBeamformer phased.TimeDelayLCMVBeamformer phased.SteeringVector phased.SumDifferenceMonopulseTracker phased.SumDifferenceMonopulseTracker2D phased.BeamscanEstimator phased.BeamscanEstimator2D phased.MVDREstimator phased.MVDREstimator2D phased.RootMUSICEstimator phased.RootWSFEstimator phased.ESPRITEstimator phased.BeamspaceESPRITEstimator phased.STAPSMIBeamformer phased.DPCACanceller phased.ADPCACanceller phased.AngleDopplerResponse phased.CFARDetector phased.MatchedFilter phased.RangeDopplerResponse phased.StretchProcessor phased.TimeVaryingGain phased.FreeSpace phased.RadarTarget phased.ConstantGammaClutter phased.BarrageJammer phased.Platform


Multicore parfor Support for Standalone Code Generation

Use parfor to generate parallel C/C++ code using OpenMP

Requires C/C++ compiler supporting OpenMP rand works with parfor

>> coderdemo_contrast_enhancer


Generate Built-In C Types

Improved readability of the generated code

Use built-in C types in the generated code Make generated code look more like handwritten code Revert to old behavior if desired


Code Generation Support for 64-Bit Data Types

Use int64 and uint64 data types for code generation

Generate multiword code for hardware target not having 64bit integer C type
Leverage long long C Type


For Use with Embedded Coder


Software-in-the-Loop (SIL) Verification

Verify numerical behavior of generated source code through software-in-the-loop testing
Reuse MATLAB tests to exercise standalone source code compiled for host computer Integrate SIL verification with the existing Project verification tool and commandline utility coder.runTest Step through generated code in Microsoft Visual Studio debugger during SIL testing when using coder.runTest

Embedded Coder and Simulink Coder Features for R2013a

November 2013

2012 The MathWorks, Inc. 26

User-directed Buffer merging at Root IO

Facilitates user-guided in-place optimization in the generated code

Allows Reusable (CSC) buffer specification at Simulink model root IO Reusable buffers are reused in the generated code (buffers are merged for a pair of specified root input and output) Feature-controlled

abc = Simulink.Signal; abc.CoderInfo.StorageClass = 'Custom'; abc.CoderInfo.CustomStorageClass = 'Reusable';

Reuse of abc



Easily Run Generated Code in the Microsoft Visual C++ Source Level Debugger
Step through the generated code in the MSVC debugger during SIL simulations
Supports top-model and Model block SIL Automatically compiles generated code with debug symbols Automatically launches MSVC and adds breakpoints at entry point functions in generated code


Reduce verification time by simulating SIL components together

Simulate Multiple SIL Model Blocks Within a Top Model Multiple Model blocks can now simulate in SIL mode at the same time Reduces the number of simulations required to simulate all components in SIL mode.


Easily Review Function Execution Times for Code Running on Embedded Hardware
Comprehensive measurement of function call execution times
Call-site instrumentation to measure execution time of functions in the generated code Now includes initialization, shared utility and math library functions Configurable units for reporting of measured execution times

Default units are




Create Comparison Plots for Function Execution Times

View code execution times in Simulation Data Inspector

Results automatically imported into Simulation Data Inspector Use extensive capabilities to plot execution times and to manage and compare runs Faster alternative to the command line API



Define an Execution Profiling Timer via Code Replacement Library (CRL)

Use the Code Replacement Tool to define a hardwarespecific timer
CRL is now the recommended approach to specify a timer for execution profiling You can use the command line API or the crtool graphical user interface



Code Replacement Library (CRL) used to enable custom multi-core targeting

Used with the Concurrent Execution modeling capability Customize data transfer code to the specific RTOS environment Functions Mutex Destroy Mutex Init Mutex Lock Mutex Unlock Semaphore Destroy Semaphore Init Semaphore Post Semaphore Wait
void mReusableRTB_SubsystemTID0(real_T rtu_In1, rtB_Subsystem_mReusableRTB *localB, rtDW_Subsystem_mReusableRTB *localDW) { int8_T wrBufIdx; /* RateTransition: '<S1>/IntegF2S' */ mySemTake((void**)&(localDW->IntegF2S_SEMAPHORE)); wrBufIdx = (int8_T)(localDW->IntegF2S_LstBufWR + 1); if (wrBufIdx == 3) { wrBufIdx = 0; } mySemGive((void**)&(localDW->IntegF2S_SEMAPHORE)); }

Embedded Coder and Simulink Coder Features for R2013b

November 2013

2012 The MathWorks, Inc. 35

C99 long long available in Code Generation

Generate code for 64-bit data types on 32-bit targets
Exploit C compilers long long (when available) Enhance simulation accuracy Speed up Windows64 accelerated simulation 64-bit multiword on 40-bit DSPs
/* rtwtypes.h */ typedef long long int64_T; /* Model step function */ void add3_64bit_step(void) { Y = U + 3ULL; }

Lean Generated C Code Using Long Long

~100 lines with just 32 bit long

~3 lines Code with 64 bit long long


Single Default Data Type Option for Underspecified Signals

Specify default floating-point data type for under-specified signals
Option to set up default data type in a Simulink model as single instead of double Controls data type propagation rules to use double or single for inherited data types Avoid the introduction of double data types for embedded designs that target singleprecision processors

Avoid Stowaway Doubles

Mixed single precision and integer (or fixed-point) now always limit math to singles (avoid doubles).

Math Mixed Single and Int32

Old Design: Stowaway doubles

yPB2 = (real32_T)((real_T)U5 * U6); ySF2 = (real32_T)((real_T)U5 * U6);

New Design: Strict Singles

yPB2 = (real32_T)U5 * U6; ySF2 = (real32_T)U5 * U6;


Model Advisor Check for Single-Precision Designs

Finds use of doubles that may be hidden deep in model


System Function with Return Argument

Subsystem function may return one of its scalar outputs as a return argument when it is packaged to generate non-reusable function with arguments.

void sys(real_T in1, real_T in2, real_T *out) { *out = in1 + in2; } Void model(void) { sys(U.In1, U.In2, &rtb_sum); Y.out = rtb_sum + U.In3; }

real_T sys(real_T in1, real_T in2) { return in1 + in2; } void model(void) { rtb_sum = sys(U.In1, U.In2); Y.out = rtbsum + U.In3; }


Simplified Makefile generation for ert.tlc and grt.tlc targets

Toolchain Build Configuration


ARM Cortex-M Optimized Code

Example: Code Replacement Library enables FIR filter code to use ARM CMSIS DSP library: arm_fir_f32()

Provides order of magnitude performance improvements for ARM Cortex-M cores

Includes STMicroelectronics Discovery Board basic blockset (ADC, GPIO)


New Targets in R2013a and R2013b

Educational Simulink targets

Raspberry Pi Gumstix Overo

Embedded Coder targets

ARM Cortex-M STM32F4-Discovery board (ARM Cortex-M4)

Xilinx Zynq (ARM Cortex-A9)

HDL Coder FPGA targets

Xilinx Zynq (FPGA fabric) Xilinx-based Software-Defined Radio


Average Global RAM Reduction Since R2008b (Using 70+ Industry Models)
Global RAM Usage

Relative to R2008b

Code Report


Unnecessary Data Copy Reduction Since R2008b (Using 70+ Industry Models)
Data Copy Measurement

MathWorks has developed a target-independent method to measure data copies, which helps prioritize our code efficiency efforts

Relative to R2008b

Thank you