8
$\begingroup$

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];
];
$\endgroup$
  • $\begingroup$ Try setting counter equal to zero in the Module initialization... $\endgroup$ – kale Jun 16 '15 at 13:18
12
$\begingroup$

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}]]
]
|improve this answer|||||
$\endgroup$
  • $\begingroup$ Yes, this indeed solves the issue. Thanks. I think for the ProgressIndicator to work correctly it would be better to have counter*xstep + xmin. $\endgroup$ – NOhs Jun 16 '15 at 13:56
  • $\begingroup$ @MrZ Sure, that would be the more general solution if your xstep is not equal to 1. $\endgroup$ – kale Jun 16 '15 at 14:50
2
$\begingroup$

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];
|improve this answer|||||
$\endgroup$
  • $\begingroup$ 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 ^^. $\endgroup$ – NOhs Jun 16 '15 at 16:02

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.