MATLAB: How to display the progress of a ‘parfor’ or ‘parfeval’ loop in MATLAB R2017a and newer

dataqueueParallel Computing Toolboxparallel.pool.dataqueueparfevalparforprogressstatuswaitbar

I have written some code which uses the MATLAB Parallel Computing Toolbox 'parfor' and 'parfeval' constructs. Since my code takes a long time to execute, I would like it to show some indication of the progress of the code such as the number of 'parfor' iterations completed, or the number of 'parfeval' function calls completed. How can I do this in MATLAB R2017a and newer? 

Best Answer

Building a 'waitbar' during a 'parfor' loop
1, The status of a 'parfor' loop can be displayed during execution using a data queue. The 'parallel.pool.DataQueue' function was added in MATLAB R2017a in order to send data from a MATLAB parallel worker to the MATLAB Desktop client: 
https://www.mathworks.com/help/releases/R2017a/distcomp/parallel.pool.dataqueue.html
In particular, the functionality of 'parallel.pool.DataQueue' can be used to display a 'waitbar' on the MATLAB Desktop client while a 'parfor' loop is executing on the parallel workers. Please refer to the following example in the 'parallel.pool.DataQueue' documentation page for more information:
https://www.mathworks.com/help/releases/R2017a/distcomp/parallel.pool.dataqueue.html#bvo5uvj-1
The above example builds a 'parallel.pool.DataQueue' object in the MATLAB client and workers. Then, the 'afterEach' function is used to register a callback function which gets executed on the MATLAB Desktop client whenever new data is available in the queue. Finally, the 'send' function is used within the 'parfor' loop to provide data to the 'parallel.pool.DataQueue'. 
Building a 'waitbar' during 'parfeval' execution
The following documentation page provides an example of using the 'waitbar' function to display the progress of a 'parfeval' computation: 
https://www.mathworks.com/help/releases/R2016b/distcomp/examples/parfeval-blackjack.html
This example can be opened in MATLAB using: 
>> openExample('distcomp/paralleldemo_blackjack_parfeval')
In the above example, note that the 'waitbar' is created and updated on the MATLAB Desktop client, while the computations are running on the MATLAB parallel workers. At the start of the code, 100 'parfeval' computations are queued. The 'waitbar' and plots are updated as each 'parfeval' computation is completed.