5
$\begingroup$

I have a function of several parameters, funct(a,b,c,d,f,...). Mathematica deals with this function fine but it takes a minute or so to make a data point. I want to plot graphs of funct using code like

Plot[
    {
    funct(a1,b,c,d,x,...),
    funct(a2,b,c,d,x,...),
    funct(a3,b,c,d,x,...)
    },  
    {x, -10, 10}, PlotPoints -> 10, MaxRecursion -> 3}, ect..
]

Again, Mathematica has no problem doing this but it takes an Age. I want to make Plot deal with the plot points in parallel, or perhaps each curve in parallel, and then combine them into a single plot to speed up the process. Is this possible?

ps. I can think of a work around using something like Show[Parallize@Table[Plot[... but that is parallizing the Table function, I would like to know if I can do it all in Plot if possible.

$\endgroup$
  • $\begingroup$ Hi @ChrisRochester, probably if you share the code of your function we can actually offer and benchmark performance of different solutions. $\endgroup$ – rhermans Oct 14 '14 at 18:38
  • $\begingroup$ I think your workaround Show@ParallelTable[Plot[...],...] is a good choice since Plot doesn't have parallelization itself. $\endgroup$ – ybeltukov Oct 14 '14 at 20:31
  • $\begingroup$ @ChrisRochester, please consider taking the tour so you learn the basic rules of the site. Once you gain enough reputation by making good questions you will be able help asigning reputation by voting up and down both questions and answers. Your question has been answered, if its answered to satisfaction, then consider accepting the best one for you. $\endgroup$ – rhermans Oct 15 '14 at 7:43
4
$\begingroup$

You can use ParallelCombine for this task:

plot = Plot[#, {x, -10, 10}, PlotPoints -> 10] &;

funcs = {Sin[x], Cos[x], Sinc[x]};

g = ParallelCombine[plot, funcs, Show]

enter image description here

Be aware that the expressions in funcs are not held in this example. Consider using Formal Symbols for the plot variables.

You can add styling with post-processing, e.g.:

restylePlot[g, {Red, Black, Blue}]

enter image description here

(See the link above for code for restylePlot.)

$\endgroup$
3
$\begingroup$

Plot by itself is not parallelizable using Parallelize.

You can plot each curve in a different kernel using ParallelTable and then Show the results together

Show[
 ParallelTable[
  Plot[
   Sin[a x]
   , {x, 0, Pi}
   , PlotRange -> {-1, 1}
   ], {a, {1, 2, 3}}]]

You may need to use DistributeDefinitions so the sub-kernels know the definitions of your custom functions.

Otherwise you could calculate the points into a table and then use ListPlot

ListPlot[
 ParallelTable[
  {x, Sin[a x]}
  , {a, {1, 2, 3}}
  , {x, 0, Pi, Pi/100}
  ]
 , Joined -> True
 ]

You could also try speeding up your function by using Compile , or pre-calculating the parts of it or by using memoization or coarse numerical approximation with N

If you share the code of your function we could test further.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.