跳ねるボールのシミュレーションを高速化する MEX コードの生成
この例では、生成した MEX 関数を使って MATLAB® アルゴリズムの実行を高速化する方法を示します。複数の MATLAB ファイルを使用する複雑なアプリケーションのための MEX 関数を、codegen
コマンドを使って生成します。codegen
を使用すると、MATLAB コードがコード生成に適しているかどうかをチェックし、多くの場合 MATLAB アルゴリズムを高速化することができます。MEX 関数を実行して、実行時エラーをチェックできます。
必要条件
この例には必要条件はありません。
関数 run_balls
について
関数 run_balls.m
は、シミュレートする跳ねるボールの数を指定するために 1 つの入力引数をもちます。シミュレーションは、エネルギーが存在しなくなり、すべてのボールの状態 (位置) が返されるまで実行され、跳ねるボールがプロットされます。
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
を使用して MEX 関数を生成し、コンパイルする MATLAB ファイルの名前を指定します。サンプル入力 (-args 0
) を渡して、生成した MEX 関数が double 型の入力を指定して呼び出されることを示します。
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;
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
高速化の推定値:
fprintf(1, 'Speed up: x ~%2.1f\n', t1/t2);
Speed up: x ~2.0