Main Content

Use the GPU to Simulate an MPC Controller in Simulink

This example shows how to generate CUDA® code and use the GPU to compute optimal MPC moves in Simulink®.

GPU Coder is required to run this example for both simulation and code generation.

Create Plant Model and Design MPC Controller

Use a double integrator as a plant.

plant = tf(1,[1 0 0]);

Create an MPC object for the plant with a sampling time of 0.1 seconds, and prediction and control horizon of 10 and 3 steps, respectively.

mpcobj = mpc(plant, 0.1, 10, 3);
-->The "Weights.ManipulatedVariables" property is empty. Assuming default 0.00000.
-->The "Weights.ManipulatedVariablesRate" property is empty. Assuming default 0.10000.
-->The "Weights.OutputVariables" property is empty. Assuming default 1.00000.

Limit the manipulated variable between –1 and 1.

mpcobj.MV = struct('Min',-1,'Max',1); 

Control the Plant Model in Simulink

Create a Simulink closed loop simulation using the MPC Controller block, with the mpcobj object passed as a parameter, to control the double integrator plant. For this example, open the pre-existing gpudemo Simulink model.

open_system('gpudemo')

Accelerate Simulation Using NVIDIA GPU

To simulate the model using GPU acceleration, open the Configuration Parameters dialog box by clicking Model Settings. Then, in the Simulation Target section, select Generate acceleration.

You can now run the model by clicking Run or by using the MATLAB® command sim. Before running the simulation the model will generate CUDA code from the Simulink model and compile it to obtain a MEX executable. When the model is simulated, this file is called and the simulation is performed on the GPU.

sim('gpudemo')
-->Converting the "Model.Plant" property to state-space.
-->Converting model to discrete time.
   Assuming no disturbance added to measured output channel #1.
-->The "Model.Noise" property is empty. Assuming white noise on each measured output.

After the simulation, the plots of the two scopes show that the manipulated variable does not exceed the limit and the plant output tracks the reference signal after approximately 3 seconds.

Generate Code for NVIDIA GPU

To generate code that runs on NVIDIA GPU, open the Configuration Parameters dialog box by clicking Model Settings. Then, in the Code Generation section, select Generate GPU code.

You can now generate code by using the "rtwbuild" command, which also produces a "Code Generation Report".

slbuild('gpudemo');
### Starting build procedure for: gpudemo
### Generating code and artifacts to 'Model specific' folder structure
### Generating code into build folder: C:\Users\rchen\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\rchen.Bdoc23aOct\mpc-ex60861417\gpudemo_grt_rtw
### Invoking Target Language Compiler on gpudemo.rtw
### Using System Target File: W:\25\rchen.Bdoc23aOct\matlab\rtw\c\grt\grt.tlc
### Loading TLC function libraries
........
### Initial pass through model to cache user defined code
..
### Caching model source code
...............................................................................
### Writing header file gpudemo_types.h
### Writing source file gpudemo.cu
.
### Writing header file gpudemo_private.h
### Writing header file gpudemo.h
### Writing header file rtwtypes.h
### Writing header file multiword_types.h
### Writing header file rtGetNaN.h
### Writing source file rtGetNaN.cu
.
### Writing header file rt_nonfinite.h
### Writing source file rt_nonfinite.cu
### Writing header file rtGetInf.h
### Writing source file rtGetInf.cu
### Writing header file rtmodel.h
### Writing source file gpudemo_data.cu
.
### TLC code generation complete (took 5.268s).
### Creating HTML report file index.html
### Saving binary information cache.
### Using toolchain: NVIDIA CUDA (w/Microsoft Visual C++ 2019) | nmake (64-bit Windows)
### Creating 'C:\Users\rchen\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\rchen.Bdoc23aOct\mpc-ex60861417\gpudemo_grt_rtw\gpudemo.mk' ...
### Successful completion of code generation for: gpudemo
### Simulink cache artifacts for 'gpudemo' were created in 'C:\Users\rchen\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\rchen.Bdoc23aOct\mpc-ex60861417\gpudemo.slxc'.

Build Summary

Top model targets built:

Model    Action           Rebuild Reason                                    
============================================================================
gpudemo  Code generated.  Code generation information file does not exist.  

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 33.118s

Here, the entire Simulink model is generated to run on the GPU. To deploy only the MPC block on the GPU, you can create a model having only the MPC block inside. Typically in embedded control modules, the deployed model contains the controller block plus a few interface blocks for input/output signals.

See Also

Objects

Blocks

Related Examples

More About