主要内容

本页采用了机器翻译。点击此处可查看最新英文版本。

使用 ValueStore 监控批处理作业

此示例显示如何使用 ValueStore 监控批处理作业的进度。

batchSvdCode 函数查找随机矩阵的奇异值,并将结果存储在具有唯一键的 ValueStore 对象中。此外,该函数还计算批处理作业的进度并将进度存储在 ValueStore 对象中。

type batchSvdCode
function 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

当作业向 updateWaitbar 对象添加条目时,ValueStore 回调函数运行。在此示例中,您将作业配置为在每个 ValueStore 循环迭代中向 for 对象添加两个条目。

type updateWaitbar
function 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

另请参阅

|

主题