主要内容

使用GPU编码器的仿真加速

You can use GPU Coder™ to speed up the execution of your Simulink®model on NVIDIA®GPUs. GPU-accelerated computing follows a heterogeneous programming model. Highly parallelizable portions of the application are mapped into kernels that execute on thousands of GPU cores in parallel, while the remainder of the sequential code still runs on the CPU.

To perform GPU-accelerated simulation, model the compute intensive portions of your application in Simulink by usingMATLAB功能(Simulink)块。当您模拟一个包含一个的模型时MATLAB功能block, the software partitions and generates CUDA®MATLAB®executable (MEX) code and integrates this code with the Simulink model.

使用GPU编码器的仿真加速的基本步骤是:

  • 创建或打开模型。

  • 通过选择求解器,Language, and other GPU-specific configuration parameters.

  • Run the GPU accelerated model.

示例:Sobel边缘检测

The Sobel edge detection algorithm is a simple edge detection algorithm that performs a 2-D spatial gradient operation on a grayscale image. This algorithm emphasizes the high spatial frequency regions that correspond to the edges of the input image.

The Sobel edge algorithm computes the horizontal gradient (H) and the vertical gradient (V)通过使用两个正交过滤器内核(输入图像)(kk'). After the filtering operation, the algorithm computes the gradient magnitude and applies a threshold to find the regions of the images that are considered to be edges.

k =单个([1 2 1; 0 0 0; -1 -2 -1]);h = conv2(单个(灰色图),k,'相同的');v = conv2(单个(灰色图),k',,'相同的');E = SQRT(H.*H + V.*V);edgeimage = uint8((E>阈值) * 255);

MATLABpeppers.png test image and its edge detected output.

Create Edge Detection Model

  1. Create a Simulink model and insert twoMATLAB功能blocks from the用户定义的函数图书馆。

  2. 添加一个持续的阻止并将其值设置为0.4.

  3. 添加一个来自多媒体文件block from theComputer Vision Toolbox™图书馆。

  4. Open the块参数对话来自多媒体文件block and set the文件名parameter torhinos.avi.

    设置Image signalparameter to一个多维信号.

  5. Add twoVideo Viewerblocks from theComputer Vision Toolbox图书馆到模型。

    金宝appSimulink模型包含用于实现边缘检测算法的块。

  6. 双击其中一个MATLAB功能块。默认函数签名出现在MATLAB功能Block Editor.

  7. 定义一个称为的函数sobel,实现SOBEL边缘检测算法。功能标题声明灰色图临界点作为对sobel功能, with边缘的返回值。保存编辑文档文件。

    功能edgeimage = sobel(灰色图,阈值)%#codegen%定义索贝尔边缘检测的内核k =单个([1 2 1; 0 0 0; -1 -2 -1]);%检测边缘h = conv2(单个(灰色图),k,'相同的');v = conv2(单个(灰色图),k',,'相同的');E = SQRT(H.*H + V.*V);edgeimage = uint8((E>阈值) * 255);end

  8. Open the block parameters for theMATLAB功能堵塞。在Code Generation选项卡,选择可重复使用的功能forFunction packagingparameter.

    如果是Function packaging参数设置为任何其他值,CUDA内核可能不会生成。

  9. 修改对方MATLAB功能在SOBEL边缘检测操作之前,将RGB实现为灰度转换。设置Function packaging参数MATLAB功能block to可重复使用的功能.

    功能gray = RGB2gray(RGB)%#codegen%将颜色图像转换为灰色图像gray = (0.2989 * double(RGB(:,:,1)) +...0.5870 * double(RGB(:,:,2)) +...0.1140 * double(RGB(:,:,3)));end
  10. 如图所示,连接这些块。将模型保存为edgedetection.slx.

    金宝appSimulink模型显示了块之间的连接。

  11. 要测试模型是否错误,请在Simulink编辑器中模拟该模型。金宝app在工具条上,单击Run.

    要在模拟过程中查看所有视频帧,请禁用仿真>掉落帧以提高性能option of theVideo Viewer堵塞。

    边缘从视频查看器块检测到输出。

Configure Model for GPU Acceleration

Model configuration parameters determine the acceleration method used during simulation.

  1. Open the Configuration Parameters dialog box. Open the求解器窗格。要编译您的模型以进行加速并生成CUDA代码,请配置模型以使用固定步骤求解器。该表显示了此示例的求解器配置。

    Parameter 环境 Effect on Generated Code
    类型 Fixed-step Maintains a constant (fixed) step size.
    求解器 离散(没有连续状态) Applies a fixed-step integration technique for computing the state derivative of the model.
    Fixed-step size 汽车 金宝appSimulink选择步骤大小。

    Snapshot of the configuration parameters dialog showing solver options for simulation.

  2. 模拟目标窗格,启用GPU accelerationparameter.

    Note

    TheLanguage参数自动设置为C++.

  3. GPU编码器特定选项现在可以在模拟目标> GPU Acceleration窗格。出于本示例的目的,您可以使用所有GPU特定参数的默认值。

    GPU Acceleration pane on the configuration parameters dialog of the model.

  4. To save and close the Configuration Parameters dialog box, click好的.

    You can also use theset_param.(Simulink)函数以在MATLAB命令窗口中以编程方式配置模型参数。

    set_param('edgeDetection','GPUAcceleration','on');

Build GPU Accelerated Model

To build and simulate the GPU accelerated model, selectRun在这方面模拟选项卡或使用以下MATLAB命令:

sim('edgeDetection');

The software first checks to see if CUDA code was previously compiled for the model. If code was created previously, the software runs the model. If code was not previously built, the software first generates and compiles the CUDA code, and then runs the model. The code generation tool places the generated code in a subfolder of the working folder calledslprj/_slprj/edgedetection.

边缘从视频查看器块检测到输出。

限制

  • GPU code generation forMATLAB功能blocks in Stateflow®图表不支持。金宝app

  • 什么时候GPU accelerationis enabled, the code generator does not support导入自定义代码用于导入自定义作者的CUDA源文件(*.cu)。而是使用CODER.CEVALinside theMATLAB功能堵塞。

  • TheMATLAB功能block does not support all the data types from the MATLAB language. For supported data types, refer to the block documentation.

See Also

Functions

相关话题