9

I am using a lot of ParallelTable and want to avoid wrapping it in a Monitor call each time. Since each of the calls inside ParallelTable will already consume considerable time, I do not mind the overhead of the monitoring.

Here is what I tried (which isn't working):

monitoredParallelTable[expr_, {x_,xmin_,xmax_,xstep_}]:=Module[
    {counter,tmp},
    SetSharedVariable[counter];
    Monitor[
        tmp=ParallelTable[counter++;expr,{x,xmin,xmax,xstep}],
        Row[{ProgressIndicator[counter,{xmin,xmax}]}]];
    Return[tmp];
];
NOhs
  • 869
  • 7
  • 13

2 Answers2

13

As a more complete answer to my comment above, Mathematica doesn't necessarily assume a variable is a number upon introduction. Initialize the counter variable equal to zero and then the increment will be handled correctly for the shared variable.

monitoredParallelTable[expr_, {x_,xmin_,xmax_,xstep_}]:=Module[{counter = 0,tmp},
SetSharedVariable[counter];
Monitor[
    tmp=ParallelTable[counter++;expr,{x,xmin,xmax,xstep}],
    ProgressIndicator[counter,{xmin,xmax}]]
]
kale
  • 10,922
  • 1
  • 32
  • 69
  • Yes, this indeed solves the issue. Thanks. I think for the ProgressIndicator to work correctly it would be better to have counter*xstep + xmin. – NOhs Jun 16 '15 at 13:56
  • @MrZ Sure, that would be the more general solution if your xstep is not equal to 1. – kale Jun 16 '15 at 14:50
3

This works for any table dimension. It also sends the $Assumptions to all parallel kernels (otherwise a simplification inside the parallel table will not be done properly).

MonitoredTimedParallelTable[expression_,indexesDescriptors__]:=Module[{tuplesLength,counter,printTemporaryCell,tableResult},
    tuplesLength=Length[Tuples[Map[Range[Rest[#]/.List->Sequence]&,{indexesDescriptors}]]];
    counter=0;
    SetSharedVariable[counter];
    printTemporaryCell=PrintTemporary[Dynamic[NumberForm[Clock[{0,Infinity}],{Infinity,1}],TrackedSymbols:>{},UpdateInterval->0.01]];
    With[{parallelKernelAssumptions=$Assumptions},ParallelEvaluate[$Assumptions=parallelKernelAssumptions]];
    tableResult=Monitor[ParallelTable[counter++;expression,indexesDescriptors],ToString[counter]<>"/"<>ToString[tuplesLength]];
    NotebookDelete[printTemporaryCell];
    tableResult
]
SetAttributes[MonitoredTimedParallelTable,HoldAll];
Giovanni F.
  • 1,911
  • 13
  • 20
  • Very nice solution indeed. However, the HoldAll does slow down some of my computations drastically. I have not found a decent way around it except explicitly placing Evaluate@ in front of the mentionend expressions inside the MonitoredTimedParallelTable call. This however, seems a bit uncomfortable ^^. – NOhs Jun 16 '15 at 16:02