1
$\begingroup$

I am using Mathematica 12.x and have a compiled function, returning many things. I would like to run several instances of this compiled function, one for each core of my CPU, and then collect all of the results. After looking through SE page here I am not sure how to accomplish this.

My function is something like this:

{out1, out2, out3, out4, out5, out6} = compiledFunction[<integer args>]

How should I go about coding the ParallelEvaluate[] to enable the behavior I want?

I would assume something like:

DistributeDefinitions[compiledFunction];
ParallelEvaluate[ SetSystemOptions[ "ParallelOptions" -> "ParallelThreadNumber" -> $ProcessorCount]];
ParallelEvaluate[{out1, out2, out3, out4, out5, out6} = compiledFunction[<integer args>]];

But this does not put all of the thread's instances of functions into the output variables. If I could, I could create out1a, out1b, ..., etc and force them together as out1 = {out1a, out1b, ...} but I am not sure how to do that, and if there's a better way?

$\endgroup$

1 Answer 1

0
$\begingroup$

I think what you want would be something like:

{out1, out2, out3, out4, out5, out6} = Join @@@ Transpose[
   ParallelEvaluate[compiledFunction[<integer args>]]
];

ParallelEvaluate returns a list of length equal to the number of kernels and each kernel returns a list of length 6, so all you need to do is transpose the dimensions of ParallelEvaluate and join the results together.

Note that if you do:

ParallelEvaluate[{out1, out2, out3, out4, out5, out6} = compiledFunction[<integer args>]];

then all the assignments to out1 etc. will be done in the parallel kernels. In other words: those variables will not get a value in your main session (the master kernel).

$\endgroup$
3
  • $\begingroup$ Wouldn't there also need to be some flattening or partitioning? Since out1, out2, etc are just lists, the final joined output is a list of lists i.e. {{...}, {...}, ...} then ParallelEvaluate[] will return different? I guess I am not positive what will be returned by this. $\endgroup$ Commented Nov 24, 2020 at 17:26
  • $\begingroup$ @HaloArchive Good point, but that's not difficult to do. See the update to the answer. If that's not sufficient, I think you should update your question with an example of the compiled function (just something simple, doesn't have to be the real thing) and the sort of thing you want to end up with. $\endgroup$ Commented Nov 24, 2020 at 17:59
  • $\begingroup$ Thank you for the edit, it seems that for my application this is working. Marking this as the correct answer. $\endgroup$ Commented Nov 24, 2020 at 20:22

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.