目次
はじめに
ラボラトリでは並列処理を行うクラスター環境 MATLAB Parallel Serverが利用できます。現時点では最大24ワーカーとなっています。サーバは NVIDIA Testa T4 ×1 に接続されており、通常の並列処理だけでなく、GPUによるスケールアウトも可能です。
MATLAB R2022aのみの提供です。クライアント側もR2022aをご利用ください。
準備1-MATLAB R2022aのダウンロード
MathWorksアカウント画面へ
MATLAB(Individual)の右の下矢印を押してダウンロード画面へ
※この際に画面左のリリースを選択からR2022aを選択します。{Windows, Mac, Linux}用ダウンロードを押してMATLAB R2022aのダウンロードを開始します。
準備2-起動後の設定
起動後は以下の順でライセンスと保存先の設定をします。
電子メール
ライセンスの選択
ユーザーの確認
保存先フォルダーの選択
Parallel Serverを使用するには、インストール時の製品の選択ではMATLABの他にParallel Computing Toolboxを選択してください。
準備3-クラスター構成
MATLAB Parallel Server はVPNを含む学内ネットワークのみの利用となっています。
1. クラスタープロファイルマネージャーの表示
MATLAB のホームタブで、並列メニューの[クラスタの作成と管理]を選択します。
2. クラスタープロファイルの追加
クラスタープロファイルの追加 > MATLAB Job Scheduler をクリックします。
追加したプロファイルはダブルクリックすることで、名前の変更が可能です。
デフォルトのままMJSProfile1でも使用可能です。
3. クラスタープロファイルの編集
プロファイルを選択し、ツールバーの[編集]をクリックして編集します。
このクラスターの説明 | (例)lab2022 |
---|---|
MATLABジョブスケジューラを実行しているマシンのホスト名 | a9.media.hosei.ac.jp |
MATLABジョブスケジューラにアクセスするためのユーザー名 | lab2022に登録しているユーザー名 |
各ワーカーで使用する計算スレッドの数 | 1 デフォルト |
ライセンス番号 | <none> デフォルト |
[完了]をクリックします。二枚目の画像は、設定後のMATLAB Job Schedulerクラスタープロファイルを示します。
4. クラスタープロファイルを既定に設定
[既定に設定] を選択して、プロファイルをデフォルトにします。
5. クラスタープロファイルの検証
プロパティの隣の検証のタブに移動し、クラスタープロファイルの「検証(V)」をクリックします。
ラボラトリのパスワードを入力します。
クラスターの検証が成功すると、以下のように全ての段階のステータスが”パス”になります。
クラスタープロファイルは学内でのみ使用可能です。学外の場合はVPN接続で使用してください。
6. クラスタープロファイルの切り替え(localとa9 Server)
localとa9 Serverの切り替えは、それぞれのクラスタープロファイルについて既定の設定を切り替えて行います。
MATLAB での動作確認-クラスタープロファイルの検証
設定したクラスタープロファイルの検証を行います。AOBA-Bのクラスタープロファイルを選択していることを確認し、検証項目5つ目の「並列プールテスト」だけチェックを外します。そして右下の「検証」ボタンをクリックして検証を実行します。
「プールジョブテスト」までの4つの検証をパスすれば問題ありません。
並列処理の例
並列処理について、以下の5つの例を紹介します。
例1:大きな画像に対するブロック処理
例2:グローバルミニマムの探索
例3:SVM分類器の最適化
例4:並列処理によるデータのクラスタリング
例5:オフロードジョブのテスト
例1の大きな画像に対するブロック処理で用いる画像ファイルおよび、例1~例4で用いるMATLAB Codeは以下のCoding filesの内容になります。
以下の画像は例1で用いる画像になります。ファイルの場所はCoding files\Block Process On Large Image\input_img.pngになります。
Coding files内のMATLAB Codeを実行するためには、二通りの方法があり、
対話形式でMATLAB Code内のコマンドを転記する。
C:\Users\ユーザー名\Documents\MATLABにinput_img.pngをコピーする必要があります。MATLAB Codeファイルを現在の作業フォルダーに移動し、実行を選択する。
C:\Users\ユーザー名\Documents\MATLAB以下にCoding filesをコピーする必要があります。
※現在のファイルにおいて、文字が黒色のものが実行可能、灰色のものは実行が不可能です。
対話形式でMATLAB Codeを実行する方法
コマンドウィンドウにてMATLAB Codeの内容を転記する。
この方法ではMATLAB Codeファイルを作業フォルダーに移動させる必要がない。
現在のフォルダーからMATLAB Codeを実行する方法
以下の場合、現在の作業フォルダーはC:\Users\ユーザー名\Documents\MATLABであるため、C:\Users\ユーザー名\Documents\MATLAB\Coding files\Block Process On Large Image内のMATLAB Codeファイルは実行できませんが、
C:\Users\ユーザー名\Documents\MATLAB\Coding files\Block Process On Large Imageに移動することにより、MATLAB Codeファイルを実行可能になります。
例の内容
例については、コマンドウィンドウに直接コマンドを記入する対話形式での手順を記載しております。
例1:大きな画像に対するブロック処理
input_img.pngが下記画像左の「現在のフォルダー」に表示されていることを確認してください。
並列処理ありの場合
BlockProcessLargeImageExample.m(デフォルト)
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)
並列処理なしの場合
BlockProcessLargeImageExample.m('UseParallel',true→'UseParallel',false)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)
処理時間の比較(単位:秒)
並列処理なし | 並列処理あり | |
---|---|---|
windows (Local) | windows (Local) 8 workers | Red Hat Enterprise Linux (a9 Server) 12 workers |
11.97 | 3.30 | 2.79 |
例2:グローバルミニマムの探索
並列処理ありの場合
ClusterDataUsingParallelComputing.m(デフォルト)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
並列処理なしの場合
ClusterDataUsingParallelComputing.m('UseParallel',true→'UseParallel',false)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分類器による最適化
並列処理ありの場合
OptimizeAnSVMClassifier.m(デフォルト)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
並列処理なしの場合
OptimizeAnSVMClassifier.m('UseParallel',true→'UseParallel',false)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:並列処理によるデータのクラスタリング
並列処理ありの場合
searchGlobalMinimum.m(デフォルト)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
並列処理なしの場合
searchGlobalMinimum.m('UseParallel',true→'UseParallel',false)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 |
例5:オフロードジョブのテスト
batch でのオフロード処理をテストします。parallelServerSample.m というファイルを作り、現在の作業フォルダーに移動させます。
以下のファイルはparallelServerSample.m、また、以下のコマンドはその内容になります。
parallelServerSample.m
n = 200; A = 500; a = zeros(n); parfor i = 1:n a(i) = max(abs(eig(rand(A)))); end
MATLAB のコマンドウィンドウから、batch コマンドを使用してジョブを実行します。Pool オプ ションに使用するワーカー数から 1 を引いた値を入れます。ラボラトリでは24ワーカーまでを推奨します。
job1 = batch('parallelServerSample', 'Pool', 3, 'AutoAddClientPath',false);
ジョブ投入後、MATLAB の「並列」メニューから「ジョブの監視」をクリックします。
クラスターサーバに投入したジョブの一覧が表示されます。処理が進むに連れ「状態」欄が 「queued」、「running」、「finished」と遷移します。
ステータスがfinishedになったら、ジョブモニターで右クリック「変数の読み込み」をクリックすることでクラスターサーバに投げた処理結果を取得できます。または、以下のコマンドで処理結果を取得することもできます。
wait(job1);
fetchOutputs(job1);
並列処理ありと並列処理なしの違い
並列処理あり
コマンドにおいて'UseParallel',true
処理中の際に左下のアイコンが緑色になる。
並列処理なし
コマンドにおいて'UseParallel',false
処理中の際に左下のアイコンが青色になる。