Skip to main content

performance tuning - ParallelTable behaves differently when it is run inside a package


I've been butting my head against some very weird behaviour by ParallelTable, and its interaction with packages, over the past few weeks, and making relatively little headway towards even reproducing the weird behaviour in a stable way. I just managed to crystallize some of this behaviour in a clean format and I would like some help understanding what's going on.


Consider, as an example of a relatively heavy calculation that one might want to parallelize, the following sum:


AbsoluteTiming[
Table[

Sum[
BesselJ[0, 10^-9 k]/(n + 1.6^k), {k, 0, 10000}
]
, {n, 0, 12}]
]

(* {5.67253, {etc.}} *)

If I parallelize this, even for something this small, it gets faster:


AbsoluteTiming[

ParallelTable[
Sum[
BesselJ[0, 10^-9 k]/(n + 1.5^k), {k, 0, 10000}
]
, {n, 0, 12}]
]

(* {1.89187, {etc.}} *)

(Minor change in the denominator to avoid what looks like caching.) OK, so far so good. Now, suppose I wish to make this calculation into part of a package, which might look like this:



BeginPackage["package`"];
function::usage = "function[x] is a function to calculate stuff";
RunInParallel::usage = "RunInParallel is an option for function which determines whether it runs in parallel or not.";
Begin["Private`"];

Options[function] = {RunInParallel -> False};

function[x_, OptionsPattern[]] := Block[{TableCommand, SumCommand},
Which[
OptionValue[RunInParallel] === False,

TableCommand = Table; SumCommand = Sum;,
OptionValue[RunInParallel] === True,
TableCommand = ParallelTable; SumCommand = Sum;,
True, TableCommand = OptionValue[RunInParallel][[1]];
SumCommand = OptionValue[RunInParallel][[2]];
];
TableCommand[
SumCommand[
BesselJ[0, 10^-9 k]/(n + x^k), {k, 0, 50000}
]

, {n, 0, 12}]
]

End[];
EndPackage[];

In particular, I have given it the option RunInParallel to decide whether to use a normal Table or a parallelized one. If I run it like this, however, I get much worse timings:


AbsoluteTiming[function[1.1, RunInParallel -> True]]
AbsoluteTiming[function[1.2, RunInParallel -> False]]


(* {31.465, {etc.}} *)
(* {34.5198, {etc.}} *)

Note here that (i) both versions are much slower than their non-packaged cousins, and (ii) all the speedup from the parallelization is gone.


To try and probe this a bit further, I tried to add some functionality to let me extract the calculation and then run it separately. That is, running


function[1.3, RunInParallel -> {Inactive[ParallelTable], Inactive[Sum]}]]

returns the calculation that it would have run, but with the Table and Sum wrapped in Inactive statements:


Inactive[ParallelTable][
Inactive[Sum][

BesselJ[0, Private`k/1000000000]/(1.3^Private`k + Private`n)
, {Private`k, 0, 50000}]
, {Private`n, 0, 12}]

I can then simply pop them open with a corresponding Activate statement. However, when I do this,


AbsoluteTiming[Activate[function[1.9, RunInParallel -> {Inactive[ParallelTable], Inactive[Sum]}]]]
AbsoluteTiming[Activate[function[1.8, RunInParallel -> {Inactive[Table], Inactive[Sum]}]]]

(* {11.7112, {etc.}} *)
(* {35.7969, {etc.}} *)


the timings come out as something else entirely, yet again. I'm a bit baffled about why the calculation is slower through the package than outside it, but mostly it's the parallelization that bothers me: why isn't the in-package parallelization able to work as well as the Activate[Inactive] route? Why was the parallelization lost in the first place? Did I fall into a bug or something?


Any help in understanding this will be welcome.


(All of this run, by the way, on a 4-core 4-thread Intel Core i5-2500 with 4GB RAM, MM v10.4 over Ubuntu 15.10.)




Comments

Popular posts from this blog

plotting - Filling between two spheres in SphericalPlot3D

Manipulate[ SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, Mesh -> None, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], {n, 0, 1}] I cant' seem to be able to make a filling between two spheres. I've already tried the obvious Filling -> {1 -> {2}} but Mathematica doesn't seem to like that option. Is there any easy way around this or ... Answer There is no built-in filling in SphericalPlot3D . One option is to use ParametricPlot3D to draw the surfaces between the two shells: Manipulate[ Show[SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], ParametricPlot3D[{ r {Sin[t] Cos[1.5 Pi], Sin[t] Sin[1.5 Pi], Cos[t]}, r {Sin[t] Cos[0 Pi], Sin[t] Sin[0 Pi], Cos[t]}}, {r, 1, 2 - n}, {t, 0, Pi}, PlotStyle -> Yellow, Mesh -> {2, 15}]], {n, 0, 1}]

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]

plotting - Mathematica: 3D plot based on combined 2D graphs

I have several sigmoidal fits to 3 different datasets, with mean fit predictions plus the 95% confidence limits (not symmetrical around the mean) and the actual data. I would now like to show these different 2D plots projected in 3D as in but then using proper perspective. In the link here they give some solutions to combine the plots using isometric perspective, but I would like to use proper 3 point perspective. Any thoughts? Also any way to show the mean points per time point for each series plus or minus the standard error on the mean would be cool too, either using points+vertical bars, or using spheres plus tubes. Below are some test data and the fit function I am using. Note that I am working on a logit(proportion) scale and that the final vertical scale is Log10(percentage). (* some test data *) data = Table[Null, {i, 4}]; data[[1]] = {{1, -5.8}, {2, -5.4}, {3, -0.8}, {4, -0.2}, {5, 4.6}, {1, -6.4}, {2, -5.6}, {3, -0.7}, {4, 0.04}, {5, 1.0}, {1, -6.8}, {2, -4.7}, {3, -1.