function workerSvdCode(models)
% Get the ValueStore of the current job
store = getCurrentValueStore;
for i = 1:numel(models)
% Store simulation results in the ValueStore object
pause(1)
key = strcat("result_",num2str(i));
store(key) = svd(rand(models(i)));
store("progress") = i/numel(models);
end
end
当向 ValueStore 对象添加条目时,将执行以下回调函数。
type handleNewEntry
function handleNewEntry(store,key)
if strcmp(key,"progress")
fprintf("Progress update: %.2f %%\n",store(key)*100);
else
fprintf("Result %s added\n",key);
end
end
使用默认集群配置文件在工作单元上运行批处理作业。
models = [8,16,32,20];
c = parcluster;
job = batch(c,@workerSvdCode,0,{models});
在作业仍在运行时检索客户端上的 ValueStore 对象。显示作业的进度。
store = job.ValueStore;
store.KeyUpdatedFcn = @handleNewEntry;
wait(job);
Result result_1 added
Progress update: 25.00 %
Result result_2 added
Progress update: 50.00 %
Result result_3 added
Progress update: 75.00 %
Result result_4 added
Progress update: 100.00 %