# Using Multiple Kernels When Making Plots

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?

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!

• Maybe this post can Help. Jan 29, 2014 at 3:42
• Hi, thanks for the suggestion, but I've already made sure that the definition of the function has been passed into the sub kernels, which is what that post is about. For example, I can run the code to make one plot in a sub kernel via ParallelSubmit and WaitAll, but I can't figure out how to get several plots being evaluated in sub kernels at the same time. Jan 29, 2014 at 4:01
• Any reason why you are not using ParallelTable or ParallelMap directly? Jan 29, 2014 at 10:15
• Belated welcome to MSE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! Apr 4, 2015 at 22:11

The problem is that the argument to WaitAll should be a list of your ParallelSubmit "jobs".

As in the Basic Examples of the documentation for ParallelSubmit, after first defining your function(s) and distributing the definitions, do something like:

ps = Table[ParallelSubmit[{i}, RegionPlot[f[x, y, i] > 5, {x, -10, 10}, {y, -10, 10}]], {i, 1, 2}]


Followed by

results = WaitAll[ps]


If you can't use Table or ParallelTable then simply use:

ps1 = ParallelSubmit[funct1[v11, v22]]
ps2 = ParallelSubmit[funct2[v21, v22]]


Then

results = WaitAll[{ps1,ps2}]

• Thanks; I forgot to mention the problem I ran into when doing this and my simplified example doesn't recreate it: I have two variables I am substituting into the function f, and I can't get that work in the table. I will edit the question to clarify. Thanks! Jan 29, 2014 at 17:20
• @LaurenPearce Don't you just add the EvaluationObjects returned by ParallelSubmit to a list given to WaitAll? See my edit. Jan 29, 2014 at 17:47

I would just use ParallelTable or ParallelMap

f[x_, y_, z_] := x^2 + y^2 + z^2;

With[{if = f},
ParallelTable[
RegionPlot[if[x, y, max] > 5, {x, -10, 10}, {y, -10, 10}]
, {max, 4}]
]