How do you run the same function multiple times and at the same time, but with different parameters?

I have had a look at ParallelEvaluate[] but it is meant to run the same function multiple times but with the same parameters.

For instance: I have the following function: f[x_]: = a*x+b*x Now I would like to run this function on multiple kernels at the same time with different parameters for a and b...


(*Define function in current notebook with default kernel*)    
f[x_]: = a*x+b*x

RunParallel[{ (*pseudocode*)
{a=1,b=1,f[1]}, (*send to kernel1*)
{a=2,b=2,f[1]}, (*send to kernel2*)
{a=3,b=3,f[1]}, (*send to kernel3*)
{a=4,b=4,f[1]}  (*send to kernel4*)
  • $\begingroup$ How about including the parameters to the arguments of the function? This way, the definitions would localized. Much more robust and debuggable coding style. $\endgroup$ Commented Apr 14, 2018 at 18:21
  • $\begingroup$ Alternatively, you may use something like Block[{a=1,b=1},f[x]]... $\endgroup$ Commented Apr 14, 2018 at 18:23
  • $\begingroup$ @Henrik Schumacher The syntax of ParallelEvaluate doesn't permit to call in parallel the same function with different agruments (say f[1] and f[2] in parallel).I think the only way to launch the same function with different parameters withParallelEvaluate is to use a shared variable (or some Random.. in the kernels). $\endgroup$
    – andre314
    Commented Apr 14, 2018 at 18:31
  • $\begingroup$ @andre thanks for the comment. Would you mind to explain in more details how to do it? I am just beginning to get to know parallel evaluation. $\endgroup$
    – james
    Commented Apr 14, 2018 at 18:36
  • $\begingroup$ @HenrikSchumacher I agree, but I cannot change the function's input parameter, unfortunately. $\endgroup$
    – james
    Commented Apr 14, 2018 at 18:37

2 Answers 2


One may use lists of parameters for each kernel in conjunction with $KernelID. On each kernel, $KernelID simply evaluates on a number of the kernel. So all we have to do is to design the code in a way that it depends on $KernelID. In this case, we use $KernelID in order to index into the lists of parameters (indexing with [[ ]], the short form of Part).

alist = {9, 8, 7, 6};
blist = {10, 20, 30, 40};
f[x_] := a x + b x

 Block[{a = alist[[$KernelID]], b = blist[[$KernelID]]},

{19, 28, 37, 46}

The drawback of this method is that the outcome depends on the number of kernels...

A better approach might be to use, e.g., ParallelTable. Here an example that hopefully illuminates what is going on:

f = {x, a, b} \[Function] a x + b x;
alist = {9, 8, 7, 6};
blist = {10, 20, 30, 40};

 Row[{"Kernel ", $KernelID, " computes ", i -> f[1, alist[[i]], alist[[i]]], "."}],
 {i, 1, Length[alist]}

{Row[{"Kernel ", 4, " computes ", 1 -> 18, "."}], Row[{"Kernel ", 3, " computes ", 2 -> 16, "."}], Row[{"Kernel ", 2, " computes ", 3 -> 14, "."}], Row[{"Kernel ", 1, " computes ", 4 -> 12, "."}]}

A further, more functional oriented way could be ParallelMap:

 abpair \[Function] Row[{"Kernel ", $KernelID, " computes ",  f[1, abpair[[1]], abpair[[2]]], "."}],
 Transpose[{alist, blist}]

{Row[{"Kernel ", 4, " computes ", 19, "."}], Row[{"Kernel ", 3, " computes ", 28, "."}], Row[{"Kernel ", 2, " computes ", 37, "."}], Row[{"Kernel ", 1, " computes ", 46, "."}]}

  • $\begingroup$ Thank you very much. I get: {2, 4} as a result? Would you mind to comment your code? I don't understand the [[$KernelID]] part... how do you assign the function with different parameters to new kernels ? Thanks! $\endgroup$
    – james
    Commented Apr 14, 2018 at 18:42
  • $\begingroup$ You obtain {2,4} because you have two kernels running (a processor with two cores), and $KernelID is the identification of each kernel... $\endgroup$ Commented Apr 14, 2018 at 18:45
  • $\begingroup$ @JoséAntonioDíazNavas I cannot go beyond that with my computer then ? ... I am asking because I can define a multitude of new kernels using "Evaluation" -> "Kernel Configuration Options" $\endgroup$
    – james
    Commented Apr 14, 2018 at 18:49
  • $\begingroup$ I think not, although I am not an expert. As far I know, you can define the machine in which you can run your NoteBook. Then, for that machine you could use as many cores as it has. However, I am afraid no beyond...I mean, I think we cannot run the same NoteBook in many machines (each one with many cores)...Therefore, I cannot run my calculation at my home computer and at the same time in the one at my office... $\endgroup$ Commented Apr 14, 2018 at 18:53
  • $\begingroup$ @HenrikSchumacher Thank you very much for your answer and your annotation. This is really helpful! :) $\endgroup$
    – james
    Commented Apr 14, 2018 at 18:56

I guess that you begin to discover parallel computing with Mathematica by reading the tutorial

That's the way I discovered it.

The problem is that the first instruction that this tutorial introduces is ParallelEvaluate.

It's misleading : ParallelEvaluate is seldom used and, when reading the tutorial, it takes time
to understand that it is not destinated to evaluate the same function with different parameters in parallel.

Fortunely there are many other instructions to do what you want.

Here are two solutions :

Solution without risks, robust, conform to functional programming (recommended)

f[{x_,a_,b_}]=a x + b x;

{2, 4, 6, 8}

It's prudent to verify that the computing was really done in parallel (as opposed to a premature evaluation of the whole expression in the main kernel) :

f[{x_,a_,b_}]=a x + b x;
ParallelMap["Kernel "<>ToString[$KernelID]<>" : "<>ToString[f[#]]&,

{"Kernel 4 : 2", "Kernel 3 : 4", "Kernel 2 : 6", "Kernel 1 : 8"}

Other solution, more close to your code

f[x_]=a x + b x;

enter image description here


enter image description here

{2, 4, 6, 8}

  • $\begingroup$ Thank you very much for your comment. Would you mind pointing me or any other newbie to an instruction that will do the job? Maybe with a small example? That would be very helpful for anyone trying to learn how to do it. Thank you very much. $\endgroup$
    – james
    Commented Apr 14, 2018 at 18:53
  • $\begingroup$ Thanks a lot! Looking forward seeing it. $\endgroup$
    – james
    Commented Apr 14, 2018 at 19:06
  • $\begingroup$ Thank you very much for your detailed and nicely visualized answer ! +1 Can you tell me: 1. Why the first solution is better than the second ? 2. What does WaitAll do ? Thank you very much ! $\endgroup$
    – james
    Commented Apr 15, 2018 at 9:24
  • $\begingroup$ great answer +1 $\endgroup$
    – henry
    Commented Apr 15, 2018 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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