MATLAB Answers

0

Is it possible to organize parfor outputs based on the "thread" number?

Asked by Jack Keller on 22 Apr 2019
Latest activity Answered by Walter Roberson
on 22 Apr 2019
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

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?
I should've specified some of that my apologies. All the user cares about is that the workers' outputs are outputted together. So if worker 2 is displayed first, that is fine, as long as all of worker 2's output is together. It could go Worker 2, Worker 1, Worker 3, Worker 5, Worker 4 for the output as long as everyone's outputs is togther.
The user wants this done in real time so they can monitor the workers because the real intentions are that the workers will have tasks that take minutes to complete. I suggested post-processing to them and was told that is not what they want.
I hope those answer your questions.

Sign in to comment.

2 Answers

Answer by Sean de Wolski
on 22 Apr 2019

parfor ii = 1:10
t = getCurrentTask
disp(t.ID)
end

  1 Comment

I apologize for my ignorance, but how does this help me or what does this mean? I tested it with 3 threads and it outputted 1, 2, 1 in that order on separate lines.

Sign in to comment.


Answer by 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

Sign in to comment.