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);
create edge detection model
create a simulink model and insert two matlab function blocks from the user-defined functions library.
add a constant block and set its value to
0.4
.add a from multimedia file block from the computer vision toolbox™ library.
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
.add two video viewer blocks from the computer vision toolbox library to the model.
double-click on one of the matlab function blocks. a default function signature appears in the matlab function block editor.
define a function called
sobel
, which implements the sobel edge detection algorithm. the function header declaresgrayimage
andthreshold
as an argument to thesobel
function, withedgeimage
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
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.
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
connect these blocks as shown in the diagram. save the model as
edgedetection.slx
.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.
configure model for gpu acceleration
model configuration parameters determine the acceleration method used during simulation.
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.
parameter setting effect on generated code type fixed-step
maintains a constant (fixed) step size. solver discrete (no continuous states)
applies a fixed-step integration technique for computing the state derivative of the model. fixed-step size auto
simulink chooses the step size. on the simulation target pane, enable gpu acceleration parameter.
note
the language parameter is automatically set to
c
.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.
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
.
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) |
slbuild
(simulink)