simulation acceleration by using gpu coder -凯发k8网页登录

main content

simulation acceleration by using gpu coder

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 using (simulink) blocks. when you simulate a model that contains a matlab function block, the software partitions and generates cuda® matlab® executable (mex) code and integrates this code with the simulink model.

the basic steps for simulation acceleration by using gpu coder are:

  • create or open a model.

  • configure the model for gpu acceleration by selecting the solver, language, and other gpu-specific configuration parameters.

  • run the gpu accelerated model.

example: sobel edge detection

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) of the input image by using two orthogonal filter kernels (k and k'). 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 = single([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 test image and its edge detected output.

create edge detection model

  1. create a simulink model and insert two matlab function blocks from the user-defined functions library.

  2. add a constant block and set its value to 0.4.

  3. add a from multimedia file block from the computer vision toolbox™ library.

  4. open the block parameters dialog for the from multimedia file block and set the file name parameter to rhinos.avi.

    set the image signal parameter to one multidimensional signal.

  5. add two video viewer blocks from the computer vision toolbox library to the model.

    simulink model containing blocks for implementing edge detection algorithm.

  6. double-click on one of the matlab function blocks. a default function signature appears in the matlab function block editor.

  7. define a function called sobel, which implements the sobel edge detection algorithm. the function header declares grayimage and threshold as an argument to the sobel function, with edgeimage as the return value. save editor document to file.

    function edgeimage  = sobel(grayimage,threshold)   %#codegen
    % define kernel for sobel edge detection
    k = single([1 2 1; 0 0 0; -1 -2 -1]);
    % detect edge
    h = conv2(single(grayimage),k, 'same');
    v = conv2(single(grayimage),k','same');
    e = sqrt(h.*h   v.*v);
    edgeimage = uint8((e > threshold) * 255);
    end
    
  8. open the block parameters for the matlab function block. on the code generation tab, select reusable function for function packaging parameter.

    if the function packaging parameter is set to any other value, cuda kernels may not get generated.

  9. modify the other matlab function block to implement the rgb to grayscale conversion prior to the sobel edge detection operation. set the function packaging parameter of the matlab function block to reusable function.

    function gray = rgb2gray(rgb)   %#codegen
    % convert color image to grey image
    gray = (0.2989 * double(rgb(:,:,1))   ...
        0.5870 * double(rgb(:,:,2))   ...
        0.1140 * double(rgb(:,:,3)));
    end
  10. connect these blocks as shown in the diagram. save the model as edgedetection.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 run.

    to see all video frames during simulation, disable the simulation > drop frames to improve performance option of the video viewer block.

    edge detected output from the video viewer block.

configure model for gpu acceleration

model configuration parameters determine the acceleration method used during simulation.

  1. open the configuration parameters dialog box. open the solver pane. 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.

    parametersettingeffect on generated code
    typefixed-stepmaintains a constant (fixed) step size.
    solverdiscrete (no continuous states)applies a fixed-step integration technique for computing the state derivative of the model.
    fixed-step sizeautosimulink chooses the step size.

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

  2. on the simulation target pane, enable gpu acceleration parameter.

    note

    the language parameter is automatically set to c .

  3. gpu coder specific options are now visible in the simulation target > gpu acceleration pane. for the purposes of this example, you can use the default values for all the gpu-specific parameters.

    gpu acceleration pane on the configuration parameters dialog of the model.

  4. to save and close the configuration parameters dialog box, click ok .

    you can also use the (simulink) function to configure the model parameters programmatically in the matlab command window.

    set_param('edgedetection','gpuacceleration','on');
    

build gpu accelerated model

to build and simulate the gpu accelerated model, select run on the simulation tab or use the following matlab command:

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 called slprj/_slprj/edgedetection.

edge detected output from the video viewer block.

limitations

  • gpu code generation for matlab function blocks in stateflow® charts is not supported.

  • when gpu acceleration is enabled, the code generator does not support import custom code for importing custom authored cuda source files (*.cu). instead, use inside the matlab function block.

  • the matlab function block does not support all the data types from the matlab language. for supported data types, refer to the block documentation.

see also

functions

  • (simulink) | (simulink) | (simulink) | (simulink) | (simulink) | (simulink) | (simulink) | (simulink) | (simulink)

related topics

网站地图