Is it possible to disp some text to main console while executing code in background? I have an infinite function that is started by parfeval but it's difficult to track what's going on there and simply putting disp in code shows nothing (I guess that's beceause parallel worker has its own console output).
MATLAB: Disp in background thread
parallel computing
Related Solutions
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.
I don't think it'll be beneficial nor possible. I get an error like this, since you can't have multiple pools running. Matlab won't know which pool to distribute which data:
parpool(16) p = gcp(); parpool(4) q = gcp() Error using parpool (line 104) Found an interactive session. You cannot have multiple interactive sessions open simultaneously. To terminate the existing session, use 'delete(gcp('nocreate'))'.
This seems like the simplest use of parfor for 20 cores:
parpool(20) parfor i = start:last % p
B(i) = func2(A(i)); end
If this was for working around the 16-core limit when using parpool, this limit was removed in R2014a. See notes here for R2014a, 1st line: https://www.mathworks.com/help/releases/R2017a/distcomp/release-notes.html?rntext=&startrelease=R2014a&endrelease=R2017a
Best Answer