Main Content

Monitor Batch Jobs with ValueStore

This example shows how to monitor the progress of a batch job by using ValueStore.

The batchSvdCode function finds the singular values of random matrices and stores the results in the ValueStore object with a unique key. Additionally, the function calculates the progress of the batch job and stores the progress in the ValueStore object.

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

The updateWaitbar callback function runs when the job adds an entry to the ValueStore object. In this example, you configure the job to add two entries to the ValueStore object in every for-loop iteration.

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

Create a wait bar.

w = waitbar(0,'Please wait ...');

Run a batch job on workers using the default cluster profile. Retrieve the ValueStore object on the client while the job is still running. Show the progress of the job.

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);

Use delete to close the wait bar after the job is completed.

delete(w)

Get the entry value specified by the key "Result 1" from the ValueStore object.

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

See Also

|

Related Topics