Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

...

...

...

...

...

目次

Table of Contents

MATLABのバージョン:MATLAB R2022a

提供:windows, Mac

※R2022bではないので注意

...

はじめに

ラボラトリでは並列処理を行うクラスター環境 MATLAB Parallel Serverが利用できます。R2019a 以前は、MATLAB Parallel Server は MATLAB Distributed Computing Server という名称でした。現時点では最大24ワーカーとなっています。サーバは NVIDIA Testa T4 ×1 に接続されており、通常の並列処理だけでなく、GPUによるスケールアウトも可能です。

Info

MATLAB R2022aのみの提供です。クライアント側もR2022aをご利用ください。

準備1-MATLAB R2022aのダウンロード

  1. MathWorksアカウント画面へMathWorksアカウントにサインインします。アカウントがない場合は作成してください。

  2. Image Removed

    MATLAB(Individual)の右の下矢印を押して「ダウンロード」へMATLAB(Individual)の右の下矢印を押してダウンロード画面へ

    Image Added
  3. ※この際に画面左のリリースを選択からR2022aを選択ダウンロードで「リリースを選択」の「さらに表示」からR2022aを選択します。

    Image RemovedWindows用ダウンロードを押してMATLAB Image AddedImage Added
  4. {Windows, Mac, Linux}用ダウンロードを押してMATLAB R2022aのダウンロードを開始します。

準備2-

...

起動後は以下の順でライセンスと保存先の設定をします。

...

電子メール

...

インストール時の設定

インストール時には以下の順でライセンスと保存先の設定をします。

  1. MathWorksアカウント(電子メールアドレスとパスワードの入力)へのサインイン

  2. ライセンス許諾の条件への同意

  3. ライセンスの選択 MATLAB (Individual) を選択

  4. ユーザーの確認

  5. 保存先フォルダーの選択(デフォルトのままで問題ありません)

...

  1. Parallel Serverを使用するために、インストール時の製品の選択でMATLABの他にParallel Computing

...

  1. Toolboxを選択してください

    Image Modified

...

  1. Image Added
  2. ハンズオンセミナーに参加される場合は、Parallel Computing Toolbox に加え、Deep Learning Toolbox も選択してください。

    Image Added
  3. 「次へ」を押して「選択の確認」に進み、「インストールの開始」をクリックしてください。

    Image Added

準備3-Parallel Computing Toolbox基本設定

「基本設定」の「Parallel Computing Toolbox」の「推奨される並列プールでのワーカー数」について、デフォルトでは「12」となっていますが「4」に変更します。

...

準備4-クラスター構成

Info

クラスタープロファイルは学内でのみ使用可能です。学外の場合はVPN接続で使用してください。

1. クラスタープロファイルマネージャーの表示

MATLAB のホームタブで、並列メニューの[クラスタの作成と管理クラスターの作成と管理]を選択します。

...

2. クラスタープロファイルの追加

クラスタプロファイルの追加 クラスタープロファイルの追加 > MATLAB Job Scheduler をクリックします。

...

追加したプロファイルはダブルクリックすることで、名前を変更が可能です。追加したプロファイルはダブルクリックすることで、名前の変更が可能です。
デフォルトのままMJSProfile1でも使用可能です。

3. クラスタープロファイルの編集

プロファイルを選択し、ツールバーの[編集]をクリックして編集します。

...

このクラスターの説明
Description

(例)lab2022

MATLABジョブスケジューラを実行しているマシンのホスト名
Host

a9.media.hosei.ac.jp

MATLABジョブスケジューラにアクセスするためのユーザー名
Username

lab2022に登録しているユーザー名 (ラボラトリアカウントが分からない https://media-hosei.atlassian.net/wiki/x/BYDsv )

各ワーカーで使用する計算スレッドの数
NumThreads

1 デフォルト

ライセンス番号
LisenceNumber

<none> デフォルト

[完了]をクリックします。二枚目の画像は、設定後のMATLAB Job Schedulerクラスタ・プロファイルを示します。Schedulerクラスタープロファイルを示します。

4.

...

クラスタープロファイル「ワーカー」の編集

プロファイルを選択し、スクロールし「ワーカー」を表示しツールバーの[既定に設定編集] を選択して、このプロファイルをデフォルトにします。をクリックして編集します

...

...

クラスタープロファイルの検証をクリックします。直後、ユーザー名とパスワードを聞かれるため、入力します。

...

クラスタの検証が成功すると、以下のように全ての段階のステータスが”パス”になります。

...

※クラスタープロファイルは学内でのみ使用可能です。学外の場合はVPN接続で使用してください。

解析するファイルの準備

Coding files>Block Process On Large Imageファイルのinput_img.ngをC:\Users\ユーザー名\Documents\MATLABに移す。

移動したファイルが画像左の現在のフォルダーに表示されていれば成功。

...

クラスタープロファイルの切り替え(localとa9 Server)

localとa9 Serverの切り替えは、それぞれのクラスタープロファイルについて既定の設定を切り替えて行います。

...

並列処理ありとなしの切り替え

並列処理ありとなしの切り替えは、コマンドにおける'UseParallel',trueを'UseParallel',falseに書き換える。

  • 並列処理あり

Code Block
tic % Start stopwatch timer
% Input image
input_img = "input_img.jpg"; % Add image path
% Initialize Edge detection function
fun = @(block_struct) edge(block_struct.data,"canny");
% Covert source image from RGB to GRAY
input_image= rgb2gray(imread(input_img));
% Perform Parallel Block Process
result = blockproc(input_image,[25 25],fun, ...
   'UseParallel',true);
toc % Terminate stopwatch timer 
% Show ouput image
imshow(result)
  • 並列処理なし

Code Block
tic % Start stopwatch timer
% Input image
input_img = "input_img.jpg"; % Add image path
% Initialize Edge detection function
fun = @(block_struct) edge(block_struct.data,"canny");
% Covert source image from RGB to GRAY
input_image= rgb2gray(imread(input_img));
% Perform Parallel Block Process
result = blockproc(input_image,[25 25],fun, ...
   'UseParallel',false);
toc % Terminate stopwatch timer 
% Show ouput image
imshow(result

並列処理の例

例1:大きな画像に対するブロック処理

  1. 並列処理ありの場合

    Code Block
    tic % Start stopwatch timer
    % Input image
    input_img = "input_img.jpg"; % Add image path
    % Initialize Edge detection function
    fun = @(block_struct) edge(block_struct.data,"canny");
    % Covert source image from RGB to GRAY
    input_image= rgb2gray(imread(input_img));
    % Perform Parallel Block Process
    result = blockproc(input_image,[25 25],fun, ...
       'UseParallel',true);
    toc % Terminate stopwatch timer 
    % Show ouput image
    imshow(result)
  2. Image Removed

    並列処理なしの場合

    Code Block
    tic % Start stopwatch timer
    % Input image
    input_img = "input_img.jpg"; % Add image path
    % Initialize Edge detection function
    fun = @(block_struct) edge(block_struct.data,"canny");
    % Covert source image from RGB to GRAY
    input_image= rgb2gray(imread(input_img));
    % Perform Parallel Block Process
    result = blockproc(input_image,[25 25],fun, ...
       'UseParallel',false);
    toc % Terminate stopwatch timer 
    % Show ouput image
    imshow(result
    Image Removed

処理時間の比較(単位:秒)

...

並列処理なし

...

並列処理あり

...

windows

(Local)

...

windows

(Local)

8 workers

...

Red Hat Enterprise Linux

(a9 Server)

12 workers

...

11.97

...

3.30

...

2.79

例2:グローバルミニマムの探索

  1. 並列処理ありの場合

    Code Block
    tic % Start stopwatch timer
    % Consider a function with several local minima.
    fun = @(x) x.^2 + 4*sin(5*x);
    fplot(fun,[-10,10])
    rng default % For reproducibility
    opts = optimoptions(@fmincon,'Algorithm','sqp');
    problem = createOptimProblem('fmincon','objective',...
        fun,'x0',3,'lb',-5,'ub',5,'options',opts);
    ms = MultiStart('UseParallel', true);
    %To search for the global minimum, run MultiStart on 2000 instances of the problem using the fmincon 'sqp' algorithm.
    [x,f] = run(ms,problem,2000)
    toc % Terminate stopwatch timer
    Image Removed

  2. 並列処理なしの場合

    Code Block
    tic % Start stopwatch timer
    % Consider a function with several local minima.
    fun = @(x) x.^2 + 4*sin(5*x);
    fplot(fun,[-10,10])
    rng default % For reproducibility
    opts = optimoptions(@fmincon,'Algorithm','sqp');
    problem = createOptimProblem('fmincon','objective',...
        fun,'x0',3,'lb',-5,'ub',5,'options',opts);
    ms = MultiStart('UseParallel', false);
    %To search for the global minimum, run MultiStart on 2000 instances of the problem using the fmincon 'sqp' algorithm.
    [x,f] = run(ms,problem,2000)
    toc % Terminate stopwatch timer

処理時間の比較(単位:秒)

...

並列処理なし

...

並列処理あり

...

windows

(Local)

...

windows

(Local)

8 workers

...

Red Hat Enterprise Linux

(a9 Server)

12 workers

...

6.20

...

1.70

...

1.52

例3:SVM分類器の最適化

  1. 並列処理ありの場合

    Code Block
    tic % Start stopwatch timer 
    load ionosphere % Load the ionosphere data set.
    rng default
    % Find hyperparameters that minimize five-fold cross-validation loss by using automatic hyperparameter optimization. For reproducibility, set the random seed and use the 'expected-improvement-plus' acquisition function.
    Mdl = fitcsvm(X,Y,'OptimizeHyperparameters','auto', ...
    'HyperparameterOptimizationOptions',struct('UseParallel',true))
    toc % Terminate stopwatch timer
    Image Removed

  2. 並列処理なしの場合

    Code Block
    tic % Start stopwatch timer 
    load ionosphere % Load the ionosphere data set.
    rng default
    % Find hyperparameters that minimize five-fold cross-validation loss by using automatic hyperparameter optimization. For reproducibility, set the random seed and use the 'expected-improvement-plus' acquisition function.
    Mdl = fitcsvm(X,Y,'OptimizeHyperparameters','auto', ...
    'HyperparameterOptimizationOptions',struct('UseParallel',false))
    toc % Terminate stopwatch timer

処理時間の比較(単位:秒)

...

並列処理なし

...

並列処理あり

...

windows

(Local)

...

windows

(Local)

8 workers

...

Red Hat Enterprise Linux

(a9 Server)

12 workers

...

32.38

...

8.23

...

7.86

例4:並列処理によるデータのクラスタリング

  1. 並列処理ありの場合

    Code Block
    Mu = bsxfun(@times,ones(20,300),(1:20)'); % Gaussian mixture mean
    rn300 = randn(300,300);
    Sigma = rn300'*rn300; % Symmetric and positive-definite covariance
    Mdl = gmdistribution(Mu,Sigma); % Define the Gaussian mixture distribution
    
    rng(1); % For reproducibility
    X = random(Mdl,10000);
    % Specify the options for parallel computing.
    stream = RandStream('mlfg6331_64');  % Random number stream
    options = statset('UseParallel',1,'UseSubstreams',1,...
        'Streams',stream);
    % Cluster the data using k-means clustering. Specify that there are k = 200 clusters in the data and increase the number of iterations. Typically, the objective function contains local minima. Specify 10 replicates to help find a lower, local minimum.
    
    tic; % Start stopwatch timer
    [idx,C,sumd,D] = kmeans(X,200,'Options',options,'MaxIter',10000,...
        'Display','final','Replicates',10);
    toc % Terminate stopwatch timer
    Image Removed
  2. 並列処理なしの場合

    Code Block
    Mu = bsxfun(@times,ones(20,300),(1:20)'); % Gaussian mixture mean
    rn300 = randn(300,300);
    Sigma = rn300'*rn300; % Symmetric and positive-definite covariance
    Mdl = gmdistribution(Mu,Sigma); % Define the Gaussian mixture distribution
    
    rng(1); % For reproducibility
    X = random(Mdl,10000);
    % Specify the options for parallel computing.
    stream = RandStream('mlfg6331_64');  % Random number stream
    options = statset('UseParallel',false,'UseSubstreams',1,...
        'Streams',stream);
    % Cluster the data using k-means clustering. Specify that there are k = 200 clusters in the data and increase the number of iterations. Typically, the objective function contains local minima. Specify 10 replicates to help find a lower, local minimum.
    
    tic; % Start stopwatch timer
    [idx,C,sumd,D] = kmeans(X,200,'Options',options,'MaxIter',10000,...
        'Display','final','Replicates',10);
    toc % Terminate stopwatch timer

処理時間の比較(単位:秒)

...

並列処理なし

...

並列処理あり

...

windows

(Local)

...

windows

(Local)

8 workers

...

Red Hat Enterprise Linux

(a9 Server)

12 workers

...

8.45

...

9.90

...

11.00

例1~4の並列処理の有無、Localとa9 Serverの比較

...

  • 例1~3について

    • 並列処理なし(Local):処理時間が長いが、振れ幅が少ない。

    • 並列処理あり(Local):処理時間が短いが、振れ幅が大きい

    • 並列処理あり(a9 Server):処理時間が短く、振れ幅も少ない

  • 例4について

    • 並列処理なしの方が処理時間が短くなった。

例1:大きな画像に対するブロック処理

例2:グローバルミニマムの探索

例3:SVM分類器の最適化

例4:並列処理によるデータのクラスタリング

並列処理ありと並列処理なしの違い

  • 並列処理あり

    • コマンドにおいて'UseParallel',true

    • 処理中の際に左下のアイコンが緑色になる。

      Image Removed
  • 並列処理なし

    • コマンドにおいて'UseParallel',false

    • 処理中の際に左下のアイコンが青色になる。

...

ジョブを実行するワーカーの数の範囲

[1 4]

[完了]をクリックします。

5. クラスタープロファイルを既定に設定

[既定に設定] を選択して、プロファイルをデフォルトにします。

...

6. クラスタープロファイルの検証

プロパティの隣の検証のタブに移動し、クラスタープロファイルの「検証(V)」をクリックします。

...

ラボラトリのパスワードを入力します。

...

クラスターの検証が成功すると、以下のように全ての段階のステータスが”パス”になります。

...

Info

クラスタープロファイルは学内でのみ使用可能です。学外の場合はVPN接続で使用してください。