# How to parallel this nested table efficiently?

I have an 8 core CPU and want to parallel evaluate the following nested Table

Table[Table[expr[i,j], {i,1,10}], {j,1,4}]


But there is a problem, the time cost of evaluating expr[i,j] increases with the value of variable i. If expr[1,j] takes 5min, expr[2,j] will take 10min and expr[10,j] will take 3hours. Now you see, no matter where I will put Parallel, in the outer Table or in the inner Table, the efficiency will not change.

The best way would be to first evaluate the most time consuming terms expr[10,1], expr[10,2], expr[10,3], expr[10,4] and other expressions with less time cost just throw onto the remaining core one by one. I naively tried several parallel order, for example

ParallelTable[expr[i,j], {i,10,1,-1}, {j,1,4}]


but this will not use 4 cores out of my 8 cores. The question is what is the best way to parallelize this nested table evaluation?

• After writing my (updated) answer it seemed strangely familiar. I believe we have a duplicate: (20713) -- please review that Q&A; if you agree this is a duplicate I will close and delete my answer. Aug 26, 2013 at 2:15
• @Mr.Wizard yeah the problem is alike. But I have a question. You suggest in the comment to use f @@ # & to get f[i,j]. What if my function is f[parameter1,i,parameter2,j]? Aug 26, 2013 at 2:56
• If I understand you would want f[parameter1, #, parameter2, #2]& @@ # & or perhaps better in that case: f[parameter1, #[[1]], parameter2, #[[2]] ]& Aug 26, 2013 at 3:01
• @Mr.Wizard Damn! How could I be so stupid. Just a little trick. Why it just can't come into my mind. Thank you, Mr. Wizard Aug 26, 2013 at 4:00
• Don't be so hard on yourself. I make lots of mistakes, and miss or forget plenty of good "tricks." Aug 26, 2013 at 4:42

This is a similar question to Efficient way to utilise Parallel features to make use of many cores.

If the times for each evaluation of expr are long, even if not nearly as long as you describe, you will not benefit from queuing multiple operations per kernel. Instead an algorithm that merely waits for a free kernel is appropriate. As the documentation for Parallelize states:

Method -> "FinestGrained" is suitable for computations involving few subunits whose evaluations take different amounts of time. It leads to higher overhead, but maximizes load balancing.

Combining this with a variation Szabolcs's Tuples and ParallelMap method:

ParallelMap[Labeled[Pause[RandomReal[{0, 0.1}]]; {#[[2]], #[[1]]}, $KernelID] &, Tuples@Range@{4, 10}, Method -> "FinestGrained"] ~Partition~ 10  • I am afraid that setting "finestGrained" is the same. Try ParallelTable[Labeled[Framed[i, j],$KernelID], {j, 1, 4}, {i, 1, 10}, Method -> "FinestGrained"] Aug 26, 2013 at 1:39
• @matheorem Hm... I see. I didn't actually try this before answering; that's always poor. Let me look at this again. Aug 26, 2013 at 1:47
• @matheorem Answer updated. Aug 26, 2013 at 1:57
• What does pause do? Aug 26, 2013 at 2:10
• I think my question maybe just a little different to that "Efficient way to utilise Parallel features to make use of many cores". Because in my case, the Method -> "FinestGrained" is essential to guarantee evaluation order. Aug 26, 2013 at 12:19

the solution that I use is:

ParallelMap[(#/.ft->f)&, Flatten[Table[ft[a,b],{a,1,10},{b,1,10}],1]]


where f is the function I want to evaluate in parallel on my 80 kernels and ft is some previously undefined symbol. I find this solution a bit ugly but practical.

• typically, the command would look like: ParallelMap[(#/.ft->f)&, Flatten[Table[{a,b,ft[a,b]},{a,1,10},{b,1,10}],1]] which then allows me to build an interpolating function from the results. Dec 23, 2020 at 20:21