1
$\begingroup$

I want to know how many tasks each kernel has accomplished with the monitor.

My Attemption:

fun=#^2&;input=Range[10^3];

SetSharedVariable[Finished]
Monitor[Finished={};
    ParallelMap[AppendTo[Finished,{fun[#],ToString@$KernelID}]&,input];
    Print@bar;First/@Finished,
    bar=Evaluate[Column@Table[
        Row@{"Kernel-"<>n<>": ",ProgressIndicator[n/Length[input]]," finished tasks: ",n},
        {n,Union[Last/@Finished]}]]/.CountsBy[Finished,Last]
]//Total//AbsoluteTiming

ParallelMap[fun,input]//Total//AbsoluteTiming

Map[fun,input]//Total//AbsoluteTiming

enter image description here

The code is hundred times slower than without monitor.

How can I optimize the monitor?

In other tasks like fun = PrimeQ[2^# - 1] &; input = Range[30000, 30100];, these code works better.

$\endgroup$
  • $\begingroup$ Don't use AppendTo. It has to copy the whole list in every call. Moreover, writing to any shared variable should be avoided as much as possible because that will force the kernels into waiting for others to finish their write operation. $\endgroup$ – Henrik Schumacher May 5 '18 at 14:03
  • $\begingroup$ Oh, and this Dynamic bogus will also involve the FrontEnd, and there is hardly any better way than slowing your computations down like communicating with the FrontEnd... ;) $\endgroup$ – Henrik Schumacher May 5 '18 at 14:08
  • $\begingroup$ Replacing Row@{"Kernel-"<>n<>": ",ProgressIndicator[n/Length[input]]," finished tasks: ",n} by Row@{"Kernel-", n, ": ", ProgressIndicator[n/Length[input]], " finished tasks: ", n} also had some positive effect. $\endgroup$ – Henrik Schumacher May 5 '18 at 14:15

Your Answer

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

Browse other questions tagged or ask your own question.