主要内容

生成 MEX 代码以加速弹球仿真

此示例说明如何使用生成的 MEX 函数来加速 MATLAB® 算法执行。它使用 codegen 命令为使用多个 MATLAB 文件的复杂应用程序生成 MEX 函数。您可以使用 codegen 来检查您的 MATLAB 代码是否适用于代码生成,而且在许多情况下,您还可以用它来加快您 MATLAB 算法的执行速度。您可以运行 MEX 函数以检查运行时错误。

前提条件

此示例没有任何前提条件。

关于 run_balls 函数

run_balls.m 函数接受单个输入以指定要仿真的弹球数量。仿真运行并对球的弹跳绘图,直到弹球能量耗尽,并返回所有球的状态(位置)。

type run_balls
% balls = run_balls(n)
% Given 'n' number of balls, run a simulation until the balls come to a
% complete halt (or when the system has no more kinetic energy).
function balls = run_balls(n) %#codegen

coder.extrinsic('fprintf');

%   Copyright 2010-2013 The MathWorks, Inc.

% Seeding the random number generator will guarantee that we get
% precisely the same simulation every time we call this function.
old_settings = rng(1283,'V4');

% The 'cdata' variable is a matrix representing the colordata bitmap which
% will be rendered at every time step.
cdata = zeros(400,600,'uint8');

% Setup figure windows
im = setup_figure_window(cdata);

% Get the initial configuration for 'n' balls.
balls = initialize_balls(cdata, n);

energy = 2; % Something greater than 1
iteration = 1;
while energy > 1
    % Clear the bitmap
    cdata(:,:) = 0;
    % Apply one iteration of movement
    [cdata,balls,energy] = step_function(cdata,balls);
    % Render the current state
    cdata = draw_balls(cdata, balls);
    iteration = iteration + 1;
    if mod(iteration,10) == 0
        fprintf(1, 'Iteration %d\n', iteration);
    end
    refresh_image(im, cdata);
end
fprintf(1, 'Completed iterations: %d\n', iteration);

% Restore RNG settings.
rng(old_settings);

生成 MEX 函数

首先,使用命令 codegen 后跟要编译的 MATLAB 文件的名称,生成 MEX 函数。传递示例输入 (-args 0) 以指示将使用双精度类型的输入调用生成的 MEX 函数。

codegen run_balls -args 0
Code generation successful.

run_balls 函数调用其他 MATLAB 函数,但在调用 codegen 时您只需指定入口函数。

默认情况下,codegen 在当前文件夹中生成名为 run_balls_mex 的 MEX 函数。这允许您测试 MATLAB 代码和 MEX 函数,并将结果进行比较。

比较结果

先后运行原始 run_balls 函数和生成的 MEX 函数并进行计时。

tic, run_balls(50); t1 = toc;
Iteration 10
Iteration 20
Iteration 30
Iteration 40
Iteration 50
Iteration 60
Iteration 70
Iteration 80
Iteration 90
Iteration 100
Iteration 110
Iteration 120
Iteration 130
Iteration 140
Iteration 150
Iteration 160
Iteration 170
Iteration 180
Iteration 190
Iteration 200
Iteration 210
Iteration 220
Iteration 230
Iteration 240
Iteration 250
Iteration 260
Iteration 270
Iteration 280
Completed iterations: 281
tic, run_balls_mex(50); t2 = toc;

Figure MATLAB Coder Bouncing Balls contains an axes object. The hidden axes object contains an object of type image.

Iteration 10
Iteration 20
Iteration 30
Iteration 40
Iteration 50
Iteration 60
Iteration 70
Iteration 80
Iteration 90
Iteration 100
Iteration 110
Iteration 120
Iteration 130
Iteration 140
Iteration 150
Iteration 160
Iteration 170
Iteration 180
Iteration 190
Iteration 200
Iteration 210
Iteration 220
Iteration 230
Iteration 240
Iteration 250
Iteration 260
Iteration 270
Iteration 280
Completed iterations: 281

Figure MATLAB Coder Bouncing Balls contains an axes object. The hidden axes object contains an object of type image.

估计的加速情况为:

fprintf(1, 'Speed up: x ~%2.1f\n', t1/t2);
Speed up: x ~1.3