AutomaticCCodeGenerationfromMATLABMarcBarberis–ApplicationEngineeringGroup,MathWorksInc.AGENDAAGENDA•QuickDemo•BenefitsofAutomaticCCodeGeneration•In-DepthExample•ComparisonbetweenMATLABCoderandMATLABCompiler•ComparisonbetweenMATLABCoderandMATLABCompiler•Fixed-PointDesign•ConclusionDemo:UsingGeneratedCCodeinaStand-AloneCProjectWhytranslateMATLABtoC?IntegrateMATLABalgorithmsw/existingCenvironmentusingsourcecodeorstaticlibrariesPrototypeMATLABalgorithmsondesktopsasstandaloneexecutablesPrototypeMATLABalgorithmsondesktopsasstandaloneexecutablesAccelerateuser-writtenMATLABalgorithmsImplementC/C++codeonprocessorsorhand-offtosoftwareengineersChallengeswithManualTranslationfromMATLABtoC/C++•Separatefunctionalandimplementationspecification–Leadstomultipleimplementationsthatareinconsistent–Hardtomodifyrequirementsduringdevelopment–Hardtomodifyrequirementsduringdevelopment–DifficulttokeepreferenceMATLABcodeandCcodein-sync•Manualcodingerrors•TimeconsumingandexpensiveAutomaticTranslationofMATLABtoCWithMATLABCoder,designengineerscan•MaintainonedesigninMATLABWithMATLABCoder,designengineerscan•MaintainonedesigninMATLAB•MaintainonedesigninMATLAB•DesignfasterandgettoC/C++quickly•Testmoresystematicallyandfrequently•SpendmoretimeimprovingalgorithmsinMATLAB•MaintainonedesigninMATLAB•DesignfasterandgettoC/C++quickly•Testmoresystematicallyandfrequently•SpendmoretimeimprovingalgorithmsinMATLABImplementationConstraintsElementbyelementmultiplyElementbyelementmultiplyElementbyelementmultiplylogicalintegerfunctiona=foo(b,c)a=b*c;voidfoo(constdoubleb[15],ElementbyelementmultiplyDotproductMatrixmultiplyElementbyelementmultiplyDotproductMatrixmultiplyElementbyelementmultiplyDotproductMatrixmultiplyintegerrealcomplex…Cdoublefoo(doubleb,doublec){returnb*c;}voidfoo(constdoubleb[15],constdoublec[30],doublea[18]){inti0,i1,i2;for(i0=0;i03;i0++){for(i1=0;i16;i1++){a[i0+3*i1]=0.0;for(i2=0;i25;i2++){a[i0+3*i1]+=b[i0+3*i2]*c[i2+5*i1];C}a[i0+3*i1]+=b[i0+3*i2]*c[i2+5*i1];}}}}ImplementationConstraints•Polymorphism•Polymorphism•Memoryallocation•Processingmatrices&arrays•Fixed-pointdatatypes7LinesofMATLAB107LinesofC7LinesofMATLAB107LinesofC107LinesofC107LinesofCIn-DepthDemoofMATLABCoder•CoderUI•CoderUI•CodeGenerationoptions•Generatecode•BrowsethroughreportSupportedMATLABLanguageFeaturesandFunctions•Broadsetoflanguagefeaturesandfunctions/systemobjectssupportedfor•Broadsetoflanguagefeaturesandfunctions/systemobjectssupportedforcodegenerationMatricesandArraysDataTypesProgrammingConstructsFunctions•Matrixoperations•N-dimensionalarrays•Subscripting•Frames•Persistentvariables•Globalvariables•Complexnumbers•Integermath•Double/single-precision•Fixed-pointarithmetic•Characters•Structures•Numericclasses•Variable-sizeddata•Arithmetic,relational,andlogicaloperators•Programcontrol(if,for,while,switch)•MATLABfunctionsandsub-functions•Variablelengthargumentlists•FunctionhandlesSupportedalgorithms•400MATLABoperatorsandfunctions•200Systemobjectsfor•Signalprocessing•Variable-sizeddata•Systemobjects•Classes•Signalprocessing•Communications•ComputervisionCodeGenerationReadinessToolInstantfeedbackoncodegenerationcomplianceofyourMATLABcodecomplianceofyourMATLABcodeProvidesestimateofeffortneededtogenerateCcodefromyourMATLABcodeonascaleof1to5ProvidesalistofissuesthatneedtoberesolvedinonereportGivesdetailedinformationonunsupportedfunctionsOtherDeploymentOptionsDeployingApplicationswithMATLABCompiler•Shareapplications•Shareapplications–DesktoporWebsoftwarecomponentsMATLABCompilerMATLABBuilderNEMATLABBuilderEXMATLABBuilderJA–SupportsfullMATLABlanguageandmosttoolboxes–RequiresMCR•Freerun-timelibraryJavaExcel.NETWebCOM.exe.dllBuilderNEBuilderEXBuilderJA•Freerun-timelibrary•Royalty-freedeploymentChoosingtheRightDeploymentSolutionMATLABCoderandMATLABCompilerOutputPortableandreadableCsourcecodeExecutableorsoftwarecomponent/libraryMATLABCompilerMATLABCoderCsourcecodecomponent/libraryMATLABsupportSubsetoflanguageSometoolboxesFulllanguageMosttoolboxesGraphicsRuntimerequirementNoneMATLABCompilerRuntime(MCR)LicensemodelRoyalty-freeRoyalty-freeFixedPointDesign:MotivationConsiderationFixedPointFloatingPointRAMandROMconsumptionSmallLargeExecutiontimeFasterSlowerHardwarepowerconsumptionLowHighDevelopmenttimeLongShortDevelopmenttimeLongShortImplementationcomplexityMorecomplex.Controlofwordlength,roundingmode,saturation...LessErrorProneHardertodevelop.MorepronetoprogrammingerrorsEasiertodevelopFixedPointDesign:PitfallsArithmeticPitfallsArithmeticPitfalls–Introducesquantizationerrors–WordlengthandFractionLengthmustbespecifiedForeveryvariable–DegradationmustbeanalyzedInteger+signfractionalL-NNQuantizationoverflowInteger+signfractionalLQuantizationoverflowFixedPointDesign:Pitfalls•FixedPointCPitfalls•FixedPointCPitfalls–Nonativefixed-pointmathlibraries–Nobuilt-inoverflow/underflowchecks–Notoolstodetermineoptimalintegerandfractionalbits–Novisualizationoffloatingandfixed-pointrepresentationsInteger+signfractionalL-NNQuantizationoverflowInteger+signfractionalLQuantizationoverflowFixed-PointT