主要内容

Code Generation from Simulink Models with GPU Coder

GPU编码器™产生优化的CUDA®simulink的代金宝app码®models containingMatlab功能块。您可以使用生成的代码和可执行文件在nvidia上快速原型设计®GPUs. Code generation reports and traceability enable you to view and analyze the generated code. The basic steps for CUDA code generation by using GPU Coder are:

  • 创建或打开模型。

  • 通过选择来配置代码生成模型solver,language,工具链, and other GPU-specific configuration parameters.

  • 构建模型。

示例: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);

MATLAB peppers.png test image and its edge detected output.

Create Edge Detection Model

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

  2. 添加A.持续的块并将其值设置为0.4.

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

  4. Open the块参数dialog box for the来自多媒体文件block and set theFile nameparameter torhinos.avi..

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

  5. Add twoVideo Viewerblocks from theComputer Vision Toolboxlibrary to the model.

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

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

  7. 定义一个调用的函数sobel,实现Sobel边缘检测算法。函数头声明grayImage临界点作为一个争论sobel功能, withedgeImage.as the return value. Save Editor document to file.

    功能edgeImage = Sobel(灰度,阈值)%#codegen%定义Sobel边缘检测的内核k =单([1 2 1; 0 0 0; -1 -2 -1]);% Detect Edgeh = 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..

    Simulink model showing connection between the blocks.

  11. 要测试错误模型,请在Simulink编辑器中模拟模型。金宝app在ToolStrip上,单击Run.

    要在模拟期间查看所有视频帧,请禁用Simulation > Drop Frames to improve Performanceoption of theVideo Viewer堵塞。

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

Configure Model for Code Generation

模型配置参数为代码生成和构建过程提供了许多选项。

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

    Parameter 环境 Effect on Generated Code
    类型 Fixed-step 维护常数(固定)步长,这是代码生成所必需的
    求解器 离散(没有连续的状态) Applies a fixed-step integration technique for computing the state derivative of the model
    Fixed-step size 汽车 Simulink chooses the step size

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

  2. 在这方面Code Generationpane, set theSystem target filetoGRT.TLC..

    You can also use the Embedded Coder®target fileert.tlc.

  3. 设定LanguagetoC++.

  4. Select生成GPU代码.

  5. 在这方面Code Generationpane, selectGenerate code only.

  6. 选择Toolchain. For Linux®平台,选择NVIDIA CUDA | gmake (64-bit Linux). For Windows®系统,选择NVIDIA CUDA(W / Microsoft Visual C ++ 20xx)|nmake(64位窗口).

  7. 在这方面Code Generation > Interfacepane, disableMAT-file logging.

  8. 在这方面Code Generation > Reportpane, selectCreate code generation reportOpen report automatically.

  9. When you enable the生成GPU代码参数,GPU编码器特定的选项出现在代码> GPU代码窗格。

    对于此示例,您可以使用GPU的默认值代码> GPU代码窗格。

    GPU代码面板的配置参数dialog of the model.

  10. Click好的要保存并关闭“配置参数”对话框。

    You can use theset_param.在Matlab中以编程方式配置模型参数的功能®Command Window.

    set_param('edgeDetection','世代pucode','CUDA');

为模型生成CUDA代码

  1. 在Simuli金宝appnk编辑器中,打开金宝appSimulink编码器应用程序。

  2. Generate code.

Messages appear in the Diagnostics Viewer. The code generator produces CUDA source and header files, and an HTML code generation report. The code generator places the files in abuild folder,一个名为的子文件夹edgeDetection_grt_rtw在您当前的工作文件夹下。

你可以找到CUDA内核_eML_blk_kernel _eml_blk_kernel_c功能s. The information within the triple chevrons is the execution configuration for the kernel.

限制

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

  • 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

相关话题