4
$\begingroup$

I have a question about monitoring parallel processes. Let's suppose that I am running the following code with 5 active Kernels.

ParallelDo[For[i = 1, i <= 5, i++, Print[i]], {k, 1, 5, 1}]

Every kernel will pick a number from 1 to 5 of the ParallelDo loop command and will execute the internal loop For individually. How can I monitor with a ProgressIndicator bar every single Kernel evaluation status simultaneously?

$\endgroup$
  • $\begingroup$ Are you looking for something different than Parallel Kernel Status? $\endgroup$ – Karsten 7. Mar 5 '16 at 1:47
  • $\begingroup$ Yes, I was looking for a way to monitor the evolution of the parameter "i" in the "For" cycle, independently for every Kernel. At the moment I am doing it by using the "Print" command, the problem is that it prints a new line every time, while I would like some kind of a monitor that auto-update himself without printing a new line every time. Looking on other questions I found that is exactly what "ProgressIndicator" does, but I am not able to use it the way I wanted. Do you have any suggestion on how to do so? Thanks. $\endgroup$ – Mattia Mar 5 '16 at 5:44
  • $\begingroup$ You should have a look at this: mathematica.stackexchange.com/a/1562/21606 $\endgroup$ – Lukas Mar 5 '16 at 10:05
  • $\begingroup$ Thanks! Exactly what I was looking for $\endgroup$ – Mattia Mar 5 '16 at 19:17
3
$\begingroup$

To avoid that somebody might miss the reference in my comment, I would like to rephrase the solution which is given in this answer by @David.

Basically, one sets up a list - currentStep - which holds the current value of the iterators on every kernel (or some combination that gives the desired output) and is printed temporarily as a Dynamic construct. By this, one avoids huge amounts of Print cells in the notebook that need to be deleted afterwards.

Mainly, the correct assignment in the body needs to be like currentStep[[$KernelID /. kernels]] = i where i is the iterator. For the example given in the OP (where the iterator of the inner For loop needs to be monitored) we can proceed like this:

kernels = ParallelTable[$KernelID -> i, {i, $KernelCount}];
currentStep = ConstantArray[0, Length@kernels];
SetSharedVariable[kernels, currentStep];

PrintTemporary["Current computation on each kernel:"];
PrintTemporary@Dynamic[currentStep//Column];

ParallelDo[For[i = 1, i <= 5, i++, Pause[.25]; currentStep[[$KernelID/.kernels]] = "Kernel: " <> ToString[$KernelID] <> " -> i=" <> ToString[i]], {k, 1, 20, 1}] 
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.