ValueStore によるバッチ ジョブの監視
この例では、ValueStoreを使用してバッチ ジョブの進行状況を監視する方法を示します。
関数 batchSvdCode は、乱数行列の特異値を求め、結果を ValueStore オブジェクトに一意のキーを付けて格納します。さらに、この関数はバッチ ジョブの進行状況を計算し、進行状況を ValueStore オブジェクトに格納します。
type batchSvdCodefunction batchSvdCode(size)
% Get the ValueStore of the current job.
store = getCurrentValueStore;
for i = 1:numel(size)
% Store results in the ValueStore object.
pause(1) % Use pause to simulate a nontrivial calculation.
key = strcat("Result ",num2str(i));
store(key) = svd(rand(size(i)));
store("progress") = i/numel(size);
end
end
ジョブによって ValueStore オブジェクトにエントリが追加されると、コールバック関数 updateWaitbar が実行されます。この例では、for ループの反復ごとに 2 つのエントリを ValueStore オブジェクトに追加するようにジョブを構成します。
type updateWaitbarfunction updateWaitbar(w,store,key)
% Update a waitbar using the ValueStore property.
if strcmp(key,"progress")
% Check if the waitbar is a reference to a deleted object.
if isvalid(w)
progress = store(key);
if progress==1
waitbar(progress,w,"Job Completed");
else
% Update the waitbar
waitbar(progress,w);
end
end
else
waitbar(store("progress"),w,("Please wait... " + key + " added"))
end
ウェイト バーを作成します。
w = waitbar(0,'Please wait ...');
既定のクラスター プロファイルを使用して、ワーカー上でバッチ ジョブを実行します。ジョブの実行中に、クライアント上で ValueStore オブジェクトを取得します。ジョブの進行状況を表示します。
size = [8 16 32 20];
c = parcluster;
job = batch(c,@batchSvdCode,0,{size});
store = job.ValueStore;
store.KeyUpdatedFcn = @(store,key) updateWaitbar(w,store,key);
wait(job);ジョブが完了したら、delete を使用してウェイト バーを閉じます。
delete(w)
キー "Result 1" で指定されたエントリ値を ValueStore オブジェクトから取得します。
val1 = store("Result 1")val1 = 8×1
4.3318
1.2988
1.1040
0.8813
0.5711
0.3991
0.2092
0.1048
delete(job)
clear job