Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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}
]
share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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