主要内容

Code Generation from Simulink Models with GPU Coder

GPU CODER™生成优化的CUDA®Simulink的代万博1manbetx码®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. 添加一个持续的阻止并将其值设置为0.4.

  3. 添加一个来自多媒体文件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.

    万博1manbetxSimulink模型包含用于实现边缘检测算法的块。

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

  7. 定义一个称为的函数sobel,实现SOBEL边缘检测算法。功能标题声明grayImage临界点作为对sobel功能, with边缘as the return value. Save Editor document to file.

    功能边缘= sobel(grayImage,threshold)%#codegen%定义索贝尔边缘检测的内核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编辑器中模拟该模型。万博1manbetx在工具条上,单击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或自定义系统目标文件。

    对于GPU代码生成,自定义目标文件必须基于grt.tlc或者ert.tlc. For information on developing a custom target file, see自定义系统目标文件(Simulink Coder).

  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位窗户).

    使用自定义系统目标文件时,必须为工具链方法设置构建控件。要了解有关自定义目标工具链方法的更多信息,请参见万博1manbetx使用自定义目标的支持工具链方法(Simulink Coder).

  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',“ generategpucode','cuda');

为模型生成CUDA代码

  1. 在Simuli万博1manbetxnk编辑器中,打开万博1manbetxSimulink编码器应用程序。

  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在您当前的工作文件夹下。

您可以在_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®图表不支持。万博1manbetx

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

  • 对于GPU代码生成,自定义目标文件必须基于grt.tlc或者ert.tlc.

See Also

Functions

相关话题