Difficulty in parallelizing Packages using DistributeDefinitions
Context
I am trying to parallelize some parameter estimation using MCMC.
As a test run I expect it to find the width of a Gaussian.
If I use the built in Mathematica function FindDistributionParameters, it parallelizes nicely:
Table[x = RandomVariate[NormalDistribution[0, 2/10], 50000];
FindDistributionParameters[x, NormalDistribution[0, s]], {i,
40}] // Timing
{0.324758, {{s -> 0.200035},…}
runs much slower than
ParallelTable[x = RandomVariate[NormalDistribution[0, 2/10], 50000];
FindDistributionParameters[x, NormalDistribution[0, s]], {i,
40}] // Timing
{0.016444, {{s -> 0.199899}, …}
If I now use a MCMC package (following this answer)
<<MCMC.m
Unprotect[Monitor];
Monitor = # &;
Protect[Monitor];
DistributeDefinitions[MCMC];
For some reason this (non parallel) implementation is as slow
Table[x = RandomVariate[NormalDistribution[0, 2/10], 500];
f[s_] = LogLikelihood[NormalDistribution[0, s], x];
MCMC[f[s], {{s, 0.1, 0.4, Reals}}, 1000]["BestFitParameters"]
, {i, 4}] // Timing
{2.26298,{{s->0.188522},{s->0.208759},{s->0.208809},{s->0.1983}}
as that (parallel) implementation:
ParallelTable[x = RandomVariate[NormalDistribution[0, 2/10], 500];
f[s_] = LogLikelihood[NormalDistribution[0, s], x];
MCMC[f[s], {{s, 0.1, 0.4, Reals}}, 1000]["BestFitParameters"]
, {i, 4}] // Timing
{3.38599,{{s->0.199691},{s->0.200183},{s->0.206191},{s->0.201176}}
Question
Why does
ParallelTablenot scale up on this problem given that the different jobs are fully independent?
PS: note that an alternative option could involve parallelizing the function itself, following e.g. this answer


Needs[]as well asParallelNeeds[]to have the definitions distributed correctly to the Kernels. – gwr Mar 03 '17 at 15:58Min::nordandFrontEndObject::notavail(albeit getting the correct result). Do you get the same messages? – gwr Mar 03 '17 at 16:17