You have a computational task that calculates the maximum absolute eigenvalue of a
2-by-2 submatrix extracted from a large matrix. Initially, you implement this task using a
for-loop. To accelerate the computation, you convert the
for-loop into a parfor-loop, and run the
parfor-loop on a pool with six process workers. When you compare
the execution times, the parfor-loop takes significantly longer than
the serial for-loop. You can use the Pool Dashboard to
investigate why the execution time for the parfor-loop is much larger
than the serial for-loop.
Open the Pool Dashboard and start a pool of process workers.
Save the parfor-loop code as a script named
parforPoolDashboard.m. In the MATLAB Command Window, enter edit parforPoolDashboard. Then
copy this code into the new file and save it.
To start collecting monitoring data, in the
section of the Pool Dashboard, type parforPoolDashboard
in the Enter code to run and monitor box box and click
Run and Monitor. When the code completes, the Pool
Dashboard displays the monitoring results.
Before R2026a: To start collecting monitoring data, in the
Monitoring section of the Pool
Dashboard, select Start Monitoring. Switch to the
MATLAB Command Window and enter parforPoolDashboard. To
visualize the monitoring data when the code completes, return to the Pool
Dashboard, and in the Monitoring section, select
Stop. The Pool Dashboard displays the monitoring
results.
Review the monitoring data.
The Timeline graph shows a visual representation of the time
the workers and client spend running the parfor-loop and
transferring data. Dark blue indicates time spent running the
parfor-loop, light blue represents time spent sending data, and
magenta represents time spent receiving data. You can observe that the workers spend the
first two to three seconds of the computation receiving data from the client. Some
workers also spend a considerable amount of time waiting to receive data from the
client.
The Worker Summary table below the
Timeline graph summarizes the information in the
Timeline graph. To view the whole table, click the three dots on
the right of the Worker Summary table and select
.
The workers spend a short time running the computations compared to transferring
data. You can observe that the client sends a total of 4.47 GB of data to the workers
and the workers each receive 762.95 MB of data while executing the
parfor-loop. The parfor-loop requires all
the workers to receive a copy of the input data, which introduces data transfer
overheads to the computation that the for-loop does not have. The
high parallel overhead dominates the computing time and this indicates the
for-loop does not benefit from conversion into a
parfor-loop.
However, if you need to run a parfor-loop multiple times using
the same set of data, you can optimize the parfor-loop by
transferring the input data to the workers only once using a Constant
object. This is a one off cost, and the workers have access to the data until you clear
the Constant object.
To see how using a Constant object optimizes the
parfor-loop, modify the parforPoolDashboard
script and rerun it with the Pool Dashboard. In the MATLAB Command Window, enter edit parforPoolDashboard. Delete
the existing code, then copy and paste this code into the script and save the
file.
Type parforPoolDashboard into the Enter code to run and
monitor box box and click Run and Monitor. When the
code completes, the Pool Dashboard updates the displayed monitoring
results.
Before R2026a: To start collecting monitoring data, in the
Monitoring section of the Pool
Dashboard, select Start Monitoring. Switch to the
MATLAB Command Window and enter parforPoolDashboard. To
visualize the monitoring data when the code completes, return to the Pool
Dashboard, and in the Monitoring section, select
Stop. The Pool Dashboard displays the monitoring
results.
To view the monitoring data for only the parfor-loop that uses
the Constant object, in the Parallel Constructs
panel, select the last row in the table. When you use the Constant
object to transfer data to the workers before you run the
parfor-loop, the workers only spend time running the computations.