Call Generated Code Using C Caller Blocks
This example shows how to incorporate the generated C code by using MATLAB® Coder™ into a Simulink® model then call the code from the Simulink model by using a C Caller block.
Generate C Code in MATLAB using MATLAB Coder
squeezenet_predict
Entry-Point Function
The squeezenet_predict
function uses the imagePretrainedNetwork
(Deep Learning Toolbox) function to load a pretrained SqueezeNet network into a persistent dlnetwork
object. On subsequent calls, the function reuses the object. The entry-point function creates a dlarray
object. The input and output of the function are both single
data type. For more information, see Code Generation for dlarray.
type squeezenet_predict.m
function out = squeezenet_predict(in) %#codegen % Copyright 2018-2024 The MathWorks, Inc. dlIn = dlarray(in,'SSC'); persistent dlnet; if isempty(dlnet) dlnet = imagePretrainedNetwork('squeezenet'); end dlOut = predict(dlnet, dlIn); out = extractdata(dlOut); end
Create Code Configuration Object
Create a code configuration object, cfg
, for a standalone executable. Set the target language to C and use the GenCodeOnly
property to specify that the code generator only produces source code. To leverage the AVX2 intrinsics for the generated code, set the InstructionSetExtension
property to AVX2
. This example generates single thread code and disables the use of the OpenMP library.
cfg = coder.config('exe'); cfg.TargetLang = 'C'; cfg.EnableOpenMP = false; cfg.GenCodeOnly = true; cfg.InstructionSetExtensions = 'AVX2';
Generate the C code
Generate C code for the squeezenet_predict
function.
codegen -config cfg squeezenet_predict -args ones(227,227,3,1,'single')
Code generation successful.
Call Generated Code From Simulink Model
Next, call the generated C code in the Simulink model.
Open the CallGeneratedCode
model. This model reads an input image by using the Image From File (Computer Vision Toolbox) block with the File name parameter set to pepper.png
image and the Output data type parameter set to single
. The model resizes the input image by using the Resize (Computer Vision Toolbox) block with these parameters:
Specify —
Number of output rows and columns
Number of output rows and columns —
[227 227]
Interpolation method —
Nearest neighbor
The model then calls the function squeezenet_predict
using the C Caller block.
open_system('CallGeneratedCode');
Configure Model for Simulation
Next, configure the model to use the generated code for simulation by setting these configuration parameters on the Simulation Target pane.
Include headers
Specify the statements to include the generated header files. For this example, use:
#include "squeezenet_predict.h" #include "squeezenet_predict_data.h" #include "squeezenet_predict_initialize.h"
Include directories
Specify the folder that contains the generated code. You can use absolute path name or relatvie path name. For this example, use the path name relative to the current folder:
codegen\exe\squeezenet_predict
Source files
Specify the generated C code files, including the relative paths. You can use this code to find all the C code files
files = dir(fullfile(fullfile('codegen','exe','squeezenet_predict','*c'))) string({files.name})'
For this example, use:
codegen\exe\squeezenet_predict\callPredict.c codegen\exe\squeezenet_predict\squeezenet_predict.c codegen\exe\squeezenet_predict\squeezenet_predict_data.c codegen\exe\squeezenet_predict\squeezenet_predict_initialize.c codegen\exe\squeezenet_predict\squeezenet_predict_terminate.c codegen\exe\squeezenet_predict\conv2dDirectOptimizedColMajor.c codegen\exe\squeezenet_predict\pool.c codegen\exe\squeezenet_predict\rtGetInf.c codegen\exe\squeezenet_predict\rtGetNaN.c codegen\exe\squeezenet_predict\rt_nonfinite.c codegen\exe\squeezenet_predict\SoftmaxLayer.c
Compiler flags
Specify the compiler flags to use to support the code generation configuration. This example uses this compiler flag /arch:AVX2
for the Windows platform for the code configuration object that has InstructionSetExtension
set to an instruction set such as AVX2
.
/arch:AVX2
On the Linux platform, use the compiler flag -mavx2
.
Configure the Model for Code Generation
Configure the model to use the generated code for code generation by setting these configuration parameters on the Code Generation pane:
Build configuration — Set to
Specify
C++ Compiler — Add the compiler flag
-fopenmp
C++ Linker — Add the linker flag
-liomp5
Configure the C Caller block by setting these port properties in the Port specification table:
Set the Dimension of
in
port to be[227 227 3]
Set the Scope of
out
port to beOutput
Simulate the model. The model uses the generated code that you previously generated from the squeezenet_predict
entry-point function.
See Also
C Caller | coder.config
| coder.CodeConfig
| coder.EmbeddedCodeConfig