I need to run a simple ParallelTable:

  {tab1[[i1]], tab2[[i2]], tab3[[i3]], tab4[[i4]],
  {i1, 1, n1}, {i2, 1, n2}, {i3, 1,n3}, {i4, 1, n4}

The computation, however, is parallelized only on the outermost index i1 and not on all of them. The problem is that I have more cores than n1 and so I'm not computing this table as fast as I had hoped. Is there a simple way to run on as many cores as possible without having to rewrite the table in a flattened version?

  • $\begingroup$ Actually this looks like an excellent candidate for flattening. Do you really need the higher dimensional array in the output, or will a flat one do as well? (Sorry, I don't know the answer to your actual question. But this looks like something I would have flattened before parallelization even if I don't know about this limitation of ParallelTable. You can use Tuples to make the parameter list.) $\endgroup$
    – Szabolcs
    Jun 13, 2012 at 13:32
  • $\begingroup$ Do you have a license for all of the cores? A standard installation runs on max 4 cores if I'm not mistaken. $\endgroup$ Jun 13, 2012 at 13:37
  • $\begingroup$ the ni are generally all equal to 2 in the specific case i'm dealing with $\endgroup$
    – Valerio
    Jun 13, 2012 at 13:50
  • $\begingroup$ @Szabolcs Could you please give me an example on how to use Tuples to run a flattened version of the table? thanks $\endgroup$
    – Valerio
    Jun 13, 2012 at 13:52

2 Answers 2


If you don't need a high-dimensional tensor as the output (from the code it seems you don't, since you're including all parameters in each output), I recommend you use a flattened table instead. This will make sure that the available cores can be used optimally.

Tuples gives you an easy way to parallelize this calculation. Assuming that the length of tab1 is n1, etc.,

ParallelMap[Append[#, fun @@ #]&, Tuples[{tab1, tab2, tab3, tab4}]]

will do what you need.

I typically use this approach (though I like to define fun to take a list as one argument instead of a number of arguments).

  • $\begingroup$ Very keen, thanks! $\endgroup$ Jul 11, 2021 at 16:58

One solution that I can see is nesting a ParallelTable inside a Table.

Original code (I have four cores, so I let the loop index stop at 3 for this demo):

ParallelTable[Labeled[Framed[{i, j}], $KernelID], {i, 3}, {j, 6}]// TableForm

Mathematica graphics

Indeed, only 3 cores have been used

Table[ParallelTable[Labeled[Framed[{i, j}], $KernelID], {j, 6}], {i, 3}] // TableForm

Mathematica graphics

Now all four are used.


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.