Bug introduced in 8.0 or earlier and fixed in 9.0.0
I'm having trouble with parallel evaluation of compiled functions. Here is a simple example illustrating the problem:
testwm = Compile[ {{x, _Real}, {n, _Integer}},
Module[ {sum, inc}, sum = 1.0; inc = 1.0;
Do[inc = inc*x/i; sum = sum + inc, {i, n}]; sum],
CompilationTarget -> "WVM"];
Map[(testwm[1.5, 2000000] + $KernelID) &,
Range[2 $ProcessorCount]] // AbsoluteTiming
(*
==> {1.4290817, {4.48169, 4.48169, 4.48169, 4.48169, 4.48169,
4.48169, 4.48169, 4.48169, 4.48169, 4.48169, 4.48169, 4.48169}}
*)
ParallelMap[(testwm[1.5, 2000000] + $KernelID) &,
Range[2 $ProcessorCount]] // AbsoluteTiming
(*
==> {1.4210813, {10.4817, 10.4817, 9.48169, 9.48169, 8.48169,
8.48169, 7.48169, 7.48169, 6.48169, 6.48169, 5.48169, 5.48169}}
*)
ParallelEvaluate[testwm = Compile[ {{x, _Real}, {n, _Integer}},
Module[ {sum, inc}, sum = 1.0; inc = 1.0;
Do[inc = inc*x/i; sum = sum + inc, {i, n}]; sum],
CompilationTarget -> "WVM"];];
ParallelMap[(testwm[1.5, 2000000] + $KernelID) &,
Range[2 $ProcessorCount], DistributedContexts -> None] // AbsoluteTiming
(*
==> {0.2760158, {10.4817, 10.4817, 9.48169, 9.48169, 8.48169,
8.48169, 7.48169, 7.48169, 6.48169, 6.48169, 5.48169, 5.48169}}
*)
ParallelEvaluate[ClearAll[testwm]];
From this example it's clear that DistributeDefinitons
is not working properly, since ParallelMap
offered no speed-up. It does not help even if I add an explicit call to DistributeDefinitons
. If I compile the function on each kernel, then I get the expected speed increase.
Is this (another) bug or am I doing something wrong?
I'm using mma 8.0.1 on win7 64 bit.
PS: for non compiled functions everything works as expected:
testnc[x_, n_] :=
Module[ {sum, inc}, sum = 1.0; inc = 1.0;
Do[inc = inc*x/i; sum = sum + inc, {i, n}]; sum];
Map[(testnc[1.5, 10000] + $KernelID) &,
Range[2 $ProcessorCount]] // AbsoluteTiming
(*
==> {0.5660324, {4.48169, 4.48169, 4.48169, 4.48169, 4.48169,
4.48169, 4.48169, 4.48169, 4.48169, 4.48169, 4.48169, 4.48169}}
*)
ParallelMap[(testnc[1.5, 10000] + $KernelID) &,
Range[2 $ProcessorCount]] // AbsoluteTiming
(*
==> {0.1210069, {10.4817, 10.4817, 9.48169, 9.48169, 8.48169,
8.48169, 7.48169, 7.48169, 6.48169, 6.48169, 5.48169, 5.48169}}
*)
ParallelMap
, there is a time lag to launch the kernels. After that, I get the speed-up fromParallelMap
of the kind that I would expect (roughly 5-fold on 6 cores). I am on Win 7 64 bit M8.0. $\endgroup$DistributeDefinitions[testwm];ParallelTable[{$KernelID, OwnValues[testwm]}, {i, 1, 4}]
, it appears that the definitions do get distributed $\endgroup$t = Compile[{}, $KernelID]; ParallelTable[t[], {i, 1, 4}]
$\endgroup$DistributeDefinitions
doesn't seem to likeCompiledFunctions
: after compiling, tryDistributeDefinitions[testwm]; ParallelEvaluate@HoldForm[testwm // Evaluate]
and you'll see the slave kernels still don't know abouttestwm
. In contrast,With[{testwmcopy = testwm}, ParallelEvaluate[testwm = testwmcopy]];
works. Don't have time to look into this now but I suspect a bug inDistributeDefinitions
(it wouldn't be the first...). FWIW behaviour in version 7 is as it should be. $\endgroup$