11
$\begingroup$

I thought of a possible answer to Monitor doesn't work with ParallelTable but it doesn't work as I hoped it would.

This accumulates print cells instead of deleting them as intended. Can it be fixed?

ParallelTable[
  NotebookDelete[x]; x = PrintTemporary[i];
  Pause[1]; i,
  {i, 1, 15}
]

I thought that x would be local to each kernel, and this would print four cells (for four cores) that would be deleted and replaced with each iteration.

I then thought that the value of x was lost between iterations, but this also fails to delete the print cells:

ParallelTable[
  x = PrintTemporary[i]; NotebookDelete[x];
  Pause[1]; i,
  {i, 1, 15}
]
$\endgroup$

1 Answer 1

12
$\begingroup$

This will display a list that's updated as long as the calculation runs, and vanishes afterwards:

(* Pattern that translates the kernel's ID to
   a number from 1 to $KernelCount *)
kernels = ParallelTable[$KernelID -> i, {i, $KernelCount}];
SetSharedVariable[kernels]; (* for Mathematica 7 *)

(* This is the list that will display each kernel's current operation *)
SetSharedVariable[currentNumber]
currentNumber = ConstantArray[0, Length@kernels];

PrintTemporary["Current calculations:"];
PrintTemporary@Dynamic[currentNumber];

(* Start the computation *)
ParallelTable[
    Pause@RandomReal[{0, .25}]; (* Long calculation *)
    currentNumber[[$KernelID /. kernels]] = i,
    {i, 100},
    Method -> "FinestGrained"
]

The immediate output looks like this:

Current calculations:

{15, 24, 16, 23, 25, 29, 27, 20}

Now wait for ParallelTable to finish, and the above will disappear, leaving only the result:

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...}

You can modify the Dynamic statement according to your needs of course, such as adding a // Column to the argument to print it nicer etc.

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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