User`s guide
1 Getting Started
1-6
Introduction to Parallel Solutions
In this section...
“Interactively Run a Loop in Parallel” on page 1-6
“Run a Batch Job” on page 1-8
“Run a Batch Parallel Loop” on page 1-9
“Run Script as Batch Job from the Current Folder Browser” on page 1-11
“Distribute Arrays and Run SPMD” on page 1-12
Interactively Run a Loop in Parallel
This section shows how to modify a simple for-loop so that it runs in parallel. This
loop does not have a lot of iterations, and it does not take long to execute, but you can
apply the principles to larger loops. For these simple examples, you might not notice an
increase in execution speed.
1
Suppose your code includes a loop to create a sine wave and plot the waveform:
for i = 1:1024
A(i) = sin(i*2*pi/1024);
end
plot(A)
2
You can modify your code to run your loop in parallel by using a parfor statement:
parfor i = 1:1024
A(i) = sin(i*2*pi/1024);
end
plot(A)
The only difference in this loop is the keyword parfor instead of for. When the loop
begins, it opens a parallel pool of MATLAB sessions called workers for executing
the iterations in parallel. After the loop runs, the results look the same as those
generated from the previous for-loop.