Main Content

Simulation Acceleration by Using GPU Coder

您可以使用GPU编译器™加快执行Simulink的执行金宝app®在nvidia的模型®GPU。GPU加速的计算遵循异质编程模型。应用程序的高度并知部分映射到核心并行地执行数千个GPU核心,而顺序代码的其余部分仍在CPU上运行。

要执行GPU加速仿真,请使用Simulink模拟应用程序的计算密集部分金宝appMATLAB Function(金宝appSimulink)blocks. When you simulate a model that contains aMATLAB Function块,软件分区并生成CUDA®MATLAB®可执行(MEX)代码并将此代码与Simulink模型集成。金宝app

这basic steps for simulation acceleration by using GPU Coder are:

  • Create or open a model.

  • Configure the model for GPU acceleration by selecting theSolver语言以及其他GPU的配置参数。

  • 运行GPU加速模型。

Example: Sobel Edge Detection

Sobel边缘检测算法是一种简单的边缘检测算法,其在灰度图像上执行二维空间梯度操作。该算法强调了对应于输入图像的边缘的高空间频率区域。

Sobel Edge算法计算水平梯度(H)和垂直梯度(V.) of the input image by using two orthogonal filter kernels (K.andK.')。在过滤操作之后,算法计算梯度幅度并应用阈值以找到被认为是边缘的图像的区域。

k =单([1 2 1; 0 0 0; -1 -2 -1]);H= conv2(single(grayImage),k,'same'); V = conv2(single(grayImage),k','same'); E = sqrt(H.*H + V.*V); edgeImage = uint8((E > threshold) * 255);

Matlab Peppers.png测试图像及其边缘检测到输出。

创建边缘检测模型

  1. 创建一个Simul金宝appink模型并插入两个MATLAB Function街区来自User-Defined Functionslibrary.

  2. Add aConstantblock and set its value to0.4

  3. Add aFrom Multimedia File街区来自计算机Vision Toolbox™library.

  4. 打开Block Parametersdialog for theFrom Multimedia File块并设置File name参数到rhinos.avi

    设定图像信号参数到One multidimensional signal

  5. 加二视频查看器街区来自电脑视觉工具箱library to the model.

    Simulink model containing blocks for implementing edge detection algorithm.

  6. Double-click on one of theMATLAB Functionblocks. A default function signature appears in theMATLAB Function块编辑器。

  7. Define a function calledSobel.那which implements the Sobel edge detection algorithm. The function header declaresgrayImageandthresholdas an argument to theSobel.功能,有edgeImage作为返回值。将编辑器文档保存到文件。

    functionedgeImage = sobel(grayImage,threshold)%#codegen.% Define Kernel for Sobel edge detectionk =单([1 2 1; 0 0 0; -1 -2 -1]);% Detect EdgeH= conv2(single(grayImage),k,'same'); V = conv2(single(grayImage),k','same'); E = sqrt(H.*H + V.*V); edgeImage = uint8((E > threshold) * 255);结尾

  8. 打开块参数MATLAB Function堵塞。在这方面代码生成tab, selectReusable function为了功能包装范围。

    If the功能包装parameter is set to any other value, CUDA kernels may not get generated.

  9. Modify the otherMATLAB Function块implement the RGB to grayscale conversion prior to the Sobel edge detection operation. Set the功能包装parameter of theMATLAB FunctionReusable function

    function灰= rgb2gray(RGB)%#codegen.% Convert color image to grey image灰色=(0.2989 * DOUBLE(RGB(::,:,1))+......0.5870 *双(RGB(:,:,2))+......0.1140 *双(RGB(:,:,3))));结尾
  10. Connect these blocks as shown in the diagram. Save the model asedgeDetection.slx

    Simulink model showing connection between the blocks.

  11. To test the model for errors, simulate the model in the Simulink Editor. On the toolstrip, click

    To see all video frames during simulation, disable theSimulation > Drop Frames to improve Performance选项视频查看器堵塞。

    Edge detected output from the Video Viewer block.

配置GPU加速模型

模型配置参数确定模拟过程中使用的加速度方法。

  1. 打开配置参数对话框。打开Solverpane. To compile your model for acceleration and generate CUDA code, configure the model to use a fixed-step solver. This table shows the solver configuration for this example.

    范围 Setting 对生成的代码影响
    Type 固定步骤 保持常数(固定)步长。
    Solver discrete (no continuous states) 应用一个固定步骤集成技术来计算模型的状态衍生物。
    固定步长 auto Simulink chooses the step size.

    显示求解器选项的配置参数对话框的快照。

  2. 在这方面Simulation Targetpane, enableGPU加速范围。

    笔记

    语言parameter is automatically set toC ++

  3. GPU Coder specific options are now visible in the仿真目标> GPU加速度pane. For the purposes of this example, you can use the default values for all the GPU-specific parameters.

    GPU加速窗格在模型的配置参数对话框中。

  4. 要保存并关闭“配置参数”对话框,请单击OK

    你也可以使用set_param(金宝appSimulink)function to configure the model parameters programmatically in the MATLAB command Window.

    set_param('edgedetection''gpuacceleration''在');

构建GPU加速模型

要构建和模拟GPU加速模型,请选择on theSimulationtab or use the following MATLAB command:

SIM('edgedetection');

该软件首先检查以查看CUDA代码是否已为模型编译。如果以前创建的代码,则软件运行模型。如果先前未构建代码,则软件首先生成并编译CUDA代码,然后运行模型。代码生成工具将生成的代码放在名为的工作文件夹的子文件夹中slprj/_slprj/edgeDetection

Edge detected output from the Video Viewer block.

Limitations

  • GPU代码生成MATLAB FunctionStateFlow中的块®charts is not supported.

  • WhenGPU加速已启用,代码生成器不支持金宝appImport custom code为了importing custom authored CUDA source files (*.cu). Instead, usecoder.ceval在 - 的里面MATLAB Function堵塞。

  • MATLAB Function块不支持来自MATLAB语言的金宝app所有数据类型。对于支持金宝app的数据类型,请参阅块文档。

也可以看看

职能

Related Topics