Fast, Effective Code Generation in a Just-In-Time Java C ompiler
C Code Generation
-
Upload
ahmed58seribegawan -
Category
Documents
-
view
283 -
download
2
Transcript of C Code Generation
-
1 2012 The MathWorks, Inc.
Latest Features in code generation
November 2013
Michael Donnenfeld,
Application Engineer
Code Generation
Systematics
-
2
C/C++ Coders
MATLAB Coder - Code from MATLAB
Portable code for numerical algorithms
Desktop applications (standalone, library)
Simulink Coder - Code from Simulink
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.
C/C++
Gen
era
te
MATLAB and Simulink
Algorithm and System Design
Verify
-
4 2012 The MathWorks, Inc.
Latest Features in MATLAB Coder
November 2013
-
5
fprintf, fopen, and fclose in
MATLAB Coder
Log data in generated code
-
6
load and coder.load
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
Access data stored in
MAT-files
-
7
Nonpersistent 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
Use more programming idioms with Handle objects
-
8
Structures Passed by Reference to
Entry-Point Functions
Reduce data copies for better performance
-
9
MEX C Code generation for parfor
Simplify integration with existing C code
Runtime errors handled using setjmp/longjmp rather than
try/catch
Generate code from parfor
loops in MEX C code
-
10
Build Tool Integration
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
Allows users to choose
different toolchains and build
configurations
-
11
Optimize X = [X C]
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];
} ......................
}
for (i = 0; i < 10; i++) { x_data[y_size[1]] = 1.0 + (real_T)i;
x_size[1]++; }
R2012b
R2013a
coder.varsize(x',[1,10]); .
for i = 1 : 10 x = [x i];
end
codegen -config:lib grow_a_vec_pattern -args {} -report
concat
assign
-
12
For Use with Embedded Coder
-
13
Static Code Metrics Report
Target-independent analysis
Report showing:
Lines of code
Global RAM estimate
Stack size estimate
Cyclomatic complexity estimate
See estimated code metrics
at code generation time
-
14
Code Replacement Library Report
Improves discoverability of code replacement library usage in
generated code
Provides traceability of used function back to MATLAB
function that triggered the
replacement
Find code replacement
functions used in generated
code
See next slide
-
15
Code Replacement Library Traceability
Aids in debugging code replacement library entries
Provides information for why CRL entry was not used for
replacement by a particular
MATLAB function
Discover why code
replacement functions were
not used in generated code
-
16 2012 The MathWorks, Inc.
Latest Features in MATLAB Coder
November 2013
-
17
Code Generation Support for
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
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
nbinrnd
nbinstat
ncfcdf
ncfinv
ncfpdf
ncfrnd
ncfstat
nctcdf
nctinv
nctpdf
nctrnd
nctstat
ncx2cdf
ncx2rnd
ncx2stat
normcdf
norminv
normpdf
normrnd
normstat
Use 100+ Statistics Toolbox functions
-
18
Code Generation Support for
Phased Array System Toolbox 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
Use 80+ functions
-
19
Code Generation Support for
Phased Array System Toolbox functions
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
Use 50+ System objects
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
-
20
Multicore parfor Support for
Standalone Code Generation
Requires C/C++ compiler supporting OpenMP
rand works with parfor
Use parfor to generate parallel C/C++ code using OpenMP
>> coderdemo_contrast_enhancer
-
22
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
-
23
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 64-
bit integer C type
Leverage long long C Type
-
24
For Use with Embedded Coder
-
25
Software-in-the-Loop (SIL) Verification
Reuse MATLAB tests to exercise standalone source code compiled for
host computer
Integrate SIL verification with the existing Project verification tool and command-line utility coder.runTest
Step through generated code in Microsoft Visual Studio debugger during SIL testing when using coder.runTest
Verify numerical behavior of
generated source code through
software-in-the-loop testing
-
26 2012 The MathWorks, Inc.
Embedded Coder and Simulink Coder
Features for R2013a
November 2013
-
28
User-directed Buffer merging at Root IO
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
Facilitates user-guided
in-place optimization in the
generated code
slfeature(ReuseReusableCSCAtRootIO, 1)
abc = Simulink.Signal; abc.CoderInfo.StorageClass = 'Custom';
abc.CoderInfo.CustomStorageClass = 'Reusable';
Reuse of abc
-
29
Easily Run Generated Code in
the Microsoft Visual C++ Source
Level Debugger
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
Step through the generated
code in the MSVC debugger
during SIL simulations
-
30
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.
-
31
Easily Review Function Execution Times
for Code Running on Embedded Hardware
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
Comprehensive measurement
of function call execution
times
rtwdemo_sil_topmodel
Default units are
nanoseconds
-
32
Create Comparison Plots for Function
Execution Times
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
View code execution times in
Simulation Data Inspector
rtwdemo_sil_topmodel
-
33
Define an Execution Profiling Timer via
Code Replacement Library (CRL)
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
Use the Code Replacement
Tool to define a hardware-
specific timer
rtwdemo_custom_pil_script
-
34
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: '/IntegF2S' */
mySemTake((void**)&(localDW->IntegF2S_SEMAPHORE));
wrBufIdx = (int8_T)(localDW->IntegF2S_LstBufWR + 1);
if (wrBufIdx == 3) {
wrBufIdx = 0;
}
mySemGive((void**)&(localDW->IntegF2S_SEMAPHORE));
}
-
35 2012 The MathWorks, Inc.
Embedded Coder and Simulink Coder
Features for R2013b
November 2013
-
36
/* Model step function */
void add3_64bit_step(void)
{
Y = U + 3ULL;
}
/* rtwtypes.h */
typedef long long int64_T;
C99 long long available in Code Generation
Exploit C compilers long long (when available)
Enhance simulation accuracy
Speed up Windows64 accelerated simulation
64-bit multiword on 40-bit DSPs
Generate code for 64-bit data
types on 32-bit targets
-
37
Lean Generated C Code Using Long Long
~100 lines with just 32 bit long
~3 lines Code with 64 bit long long
-
38
Single Default Data Type Option for
Underspecified 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 single-
precision processors
Specify default floating-point
data type for under-specified
signals
-
39
Avoid Stowaway Doubles
Mixed single precision and integer (or fixed-point)
now always limit math to singles (avoid doubles).
New Design: Strict Singles yPB2 = (real32_T)U5 * U6;
ySF2 = (real32_T)U5 * U6;
Old Design: Stowaway doubles yPB2 = (real32_T)((real_T)U5 * U6);
ySF2 = (real32_T)((real_T)U5 * U6);
Math Mixed Single and Int32
-
40
Model Advisor Check for Single-Precision Designs
Finds use of doubles that
may be hidden deep in model
-
41
System Function with Return Argument
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;
}
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;
}
Subsystem function may return one
of its scalar outputs as a return
argument when it is packaged to
generate non-reusable function
with arguments.
R2013a R2013b
-
42
Simplified Makefile generation for ert.tlc
and grt.tlc targets
Toolchain
Build Configuration
-
43
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)
-
44
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
-
45
Average Global RAM Reduction Since R2008b
(Using 70+ Industry Models)
Rela
tive to
R2
00
8b
Code Report
Global RAM Usage
-
46
Unnecessary Data Copy Reduction Since
R2008b (Using 70+ Industry Models)
MathWorks has developed a target-independent method to measure data copies, which helps prioritize our code efficiency efforts
Rela
tive to
R2
00
8b
Data Copy Measurement
-
47
Thank you