Is it possible to organize parfor outputs based on the "thread" number?
10 views (last 30 days)
Show older comments
I was given a side project by a professor reagarding outputting text inside parfor loops in order by "thread" number. The user wants to be able to monitor the status of the threads as they are working. For example, if in the parfor loop there are five steps to be done and a messaged is displayed every time the thread finishes a step, how can I make it so that the output is grouped by the thread number instead of in the order that it gets completed? I am of the mind that this is not possible but thought this was a good place to start after finding minimal resources regarding this.
There is code below that I was given to test on. The user wants the output to be like:
"Thread 1 complete step 1"
"Thread 1 completed step 2"
"Thread 2 completed step 1"
"Thread 2 completed step 2"
etc.
Instead of:
"Thread 3 complete step 1"
"Thread 1 completed step 2"
"Thread 3 completed step 2"
"Thread 2 completed step 1"
etc.
They also want it in a GUI format, but that's something I could figure out. What I don't know is if the output they want is even possible to do while the program is running.
N = 10; %How many threads should I run?
pt = 4; %pause time randomness.
min_pt = 1;
parfor ii=1:N
t_starts(ii) = tic();
pause(rand()*pt+min_pt);
my_str = sprintf('thread %d completed step 1, took %f',ii,toc(t_starts(ii)));
disp(my_str);
pause(rand()*pt+min_pt);
tmp = randn();
my_str=sprintf('thread %d completed step 2 and found %f, took %f',ii,randn(),toc(t_starts(ii)));
disp(my_str);
pause(rand()*pt+min_pt);
my_str=sprintf('thread %d completed step 3, took %f',ii,toc(t_starts(ii)));
disp(my_str);
pause(rand()*pt+min_pt);
tmp2 = randn()-tmp;
if tmp2<0
my_str=sprintf('thread %d completed step 4, %f better than step 2, took %f',ii,-tmp2,toc(t_starts(ii)));
else
my_str=sprintf('thread %d completed step 4, %f worse than step 2, took %f',ii,tmp2,toc(t_starts(ii)));
end
disp(my_str);
pause(rand()*pt + min_pt);
my_str=sprintf('thread %d completed step 5, took %f',ii,toc(t_starts(ii)));
disp(my_str);
end
2 Comments
Steven Lord
on 22 Apr 2019
Let's say that worker 1's work takes much longer than worker 2's work.
Step 1 for worker 2 finishes, and the status message gets written to the screen and/or log file.
Step 2 for worker 2 finishes next, and the status message gets written to the screen and/or log file.
Now step 1 for worker 1 finishes. Should MATLAB erase the messages already displayed on screen and/or in the log file, write the status message for worker 1, then rewrite the erased messages?
Do you need to do this in or close to real-time? Or is post-processing the messages acceptable?
Answers (2)
Walter Roberson
on 22 Apr 2019
Use a parallel queue or pollable queue https://www.mathworks.com/help/parallel-computing/send.html#bvl84az-pollablequeue to send status information back from the workers to the client. The client can organize it however it wants, including by adjusting a uicontrol style text specific to the worker.
0 Comments
See Also
Categories
Find more on Parallel for-Loops (parfor) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!