run batch parallel jobs
run a batch job
to offload work from your matlab® session to run in the background in another session, you can use the command inside a script.
to create the script, type:
edit mywave
in the matlab editor, create a
for
-loop:for i = 1:1024 a(i) = sin(i*2*pi/1024); end
save the file and close the editor.
use the
batch
command in the matlab command window to run your script on a separate matlab worker:job = batch('mywave')
batch
does not block matlab and you can continue working while computations take place. if you need to block matlab until the job finishes, use thewait
function on the job object.wait(job)
after the job finishes, you can retrieve and view its results. the
load
command transfers variables created on the worker to the client workspace, where you can view the results:load(job,'a') plot(a)
when the job is complete, permanently delete its data and remove its reference from the workspace:
delete(job) clear job
batch
runs your code on a local worker or a cluster worker,
but does not require a parallel pool.
you can use batch
to run either scripts or functions. for
more details, see the reference page.
run a batch job with a parallel pool
you can combine the abilities to offload a job and run a loop in a parallel
pool.
this example combines the two to create a simple batch
parfor
-loop.
to create a script, type:
edit mywave
in the matlab editor, create a
parfor
-loop:parfor i = 1:1024 a(i) = sin(i*2*pi/1024); end
save the file and close the editor.
run the script in matlab with the
batch
command. indicate that the script should use a parallel pool for the loop:job = batch('mywave','pool',3)
this command specifies that three workers (in addition to the one running the batch script) are to evaluate the loop iterations. therefore, this example uses a total of four local workers, including the one worker running the batch script. altogether, there are five matlab sessions involved, as shown in the following diagram.
to view the results:
wait(job) load(job,'a') plot(a)
the results look the same as before, however, there are two important differences in execution:
the work of defining the
parfor
-loop and accumulating its results are offloaded to another matlab session bybatch
.the loop iterations are distributed from one matlab worker to another set of workers running simultaneously (
'pool'
andparfor
), so the loop might run faster than having only one worker execute it.
when the job is complete, permanently delete its data and remove its reference from the workspace:
delete(job) clear job
run script as batch job from the current folder browser
from the current folder browser, you can run a matlab script as a batch job by browsing to the file’s folder, right-clicking
the file, and selecting run script as batch job. the
batch job runs on the cluster identified by the default cluster profile. the
following figure shows the menu option to run the script file
script1.m
:
running a script as a batch from the browser uses only one worker from the
cluster. so even if the script contains a parfor
loop or
spmd
block, it does not open an additional pool of workers
on the cluster. these code blocks execute on the single worker used for the batch
job. if your batch script requires opening an additional pool of workers, you can
run it from the command line, as described in run a batch job with a parallel pool.
when you run a batch job from the browser, this also opens the job monitor. the job monitor is a tool that lets you track your job in the scheduler queue. for more information about the job monitor and its capabilities, see .