I am running a notebook that makes multiple plots. I define a function and then I want to plot that function for different choices of parameters, using RegionPlot
. Given the complexity of the function (which is why I'm not copying the code here), it takes ~2 hours to make each plot, and I need about 20 separate plots. Since the plots are completely independent of each other, this seems like the sort of thing that should be parallelized. This is my goal.
I've figured out how to launch the subkernels and how to move the definition of the function to the subkernels using DistributeDefinitions
. (Sorry if that's not quite the right terminology.) I've been trying to use the subkernels like this (pseudo-code):
ParallelSubmit[RegionPlot[* code with function[x,y,z] here *]]
ParallelSubmit[RegionPlot[* code with function[a,b,c] here *]];
However, it seems to me that the ParallelSubmit
doesn't actually get evaluated until WaitAll
is called, and I can only find a way to apply WaitAll
to one of the two instances above; hence, I still end up evaluating them sequentially, which is not what I wanted.
So my question is this: what is the easiest way to parallelize the production of plots (for different parameters) in Mathematica?
Thank you in advance for all your help!
It occurred to me that perhaps an example will help to clarify:
f[x_,y_,z_]:=x^2+y^2+z^2;
DistributeDefinitions["Global`"];
(* I do have a back tick on the Mathematica code but I'm revising this on my phone which
can't type one. *)
ParallelSubmit[RegionPlot[f[x,y,1]>5,{x,-10,10},{y,-10,10}]];
ParallelSubmit[RegionPlot[f[x,y,2]>5,{x,-10,10},{y,-10,10}]];
How can I run WaitAll
so both ParallelSubmits
evaluate at the same time?
* Clarification: * I should have realized that my "simplified" example is amenable to using ParallelTable, whereas (so far as I know) my actual function is not. The reason being that I have two variables that I want to substitute, and I don't want all combinations of them. Let me try to explain with a revised version:
f[x_,y_,z_,w_]:=x^2+y^2+z^2+w^2;
DistributeDefinitions["Global`"];
ParallelSubmit[RegionPlot[f[x,y,1,1]>5,{x,-10,10},{y,-10,10}]];
ParallelSubmit[RegionPlot[f[x,y,2,2]>5,{x,-10,10},{y,-10,10}]];
When you run a table with the indices like {i,1,2},{j,1,2}
, you get four entries: i=1,j=1
, i=1,j=2
, i=1,j=2
and i=2,j=2
. This is not what I want; it would increase the number of plots made dramatically. I tried making a table with {{i,j},{1,1},{2,2}}
, but that gives the expected error that iterator does not have the expected bounds.
Thanks for all the help!
ParallelTable
orParallelMap
directly? $\endgroup$