# ParallelTable[ParallelTable] vs Table[ParallelTable]: inexplicable differences & nonlinear scaling in computation time

I have something that looks a lot like the following:

h[d1_,d2_,rn_,cn_]:=Flatten[d1[[
Max[1,1+rn];;Min[rows,rows+rn],
Max[1,1+cn];;Min[cols,cols+cn]
]]].
Flatten[d2[[
Max[1,1-rn];;Min[rows,rows-rn],
Max[1,1-cn];;Min[cols,cols-cn]
]]]/
((rows-Abs[rn])(cols-Abs[cn]));
ParallelTable[
w=0.02x;
a=(Pi/10)y;
f=Abs[t+w Exp[I a]s];
g=ParallelTable[h[f,f,rn,cn],{rn,-10,10},{cn,-10,10}];
total=Total[Total[g]];
total,{x,0,3},{y,0,3}]


$h$ is simply the correlation of two matrices $d_1$ and $d_2$ of the same size, one of which is displaced by $r_n$ rows and $c_n$ columns, weighted by the overlap area (yes, I know of ListCorrelate; that's beside the point). $rows$ and $cols$ are the number of rows and columns in the matrices, respectively. $s$ and $t$ are matrices of size $rows\times cols$, so obviously $f$ is also a matrix of that size. $g$ is an $11\times11$ matrix. Finally, $total$ is the sum of all the elements of $g$.

The whole thing should result in an $4\times4$ matrix with all the values of $total$ for different values of $x$ and $y$ (or, equivalently, for different values of $w$ and $a$).

When I run this, I get the following message:

ParallelTable::subpar: Parallel computations cannot be nested; proceeding with sequential evaluation.

(The message appears a few times per processor core before a "further output of ParallelTable::subpar will be suppressed" message appears.) However, the thing runs in under 4 seconds. If I change the first ParallelTable to Table, the error doesn't appear anymore but the thing takes about 10 seconds.

As far as I understand, the error comes up because I have a ParallelTable nested inside of another ParallelTable and what Mathematica does is turn one of them into Table. If this is the case, though, why does it take longer to run when I manually change one of them into Table?

Edit: I read here that the error results in the second ParallelTable being turned into Table. I suppose the stuff outside the second ParallelTable takes longer to evaluate than the stuff inside it (and thus it's better for the stuff outside to be parallelised than for the stuff inside to be parallelised); that would explain the computation-time difference. However, I still have another question:

Why does it take really long (it's been going on for half an hour; who knows how long it's got left) when I do this $11\times11$ times instead of $4\times4$ times (i.e. when I let $x$ and $y$ run up to 10)? If $4\times4=16$ takes under 4 seconds, I'd imagine $11\times11=121$ to take $4\times121/16=30$ seconds at most.

Thanks for any insight into this.

• The main issue is probably not the stuff outside the inner ParallelTable, but rather the overhead of starting a parallel computation… Regarding your other issue: I cannot reproduce the timing behavior you describe. Can you provide example data for gradhLr?? – Lukas Lang May 10 '18 at 14:32
• @BrettChampion They are exact numbers. – Rain May 10 '18 at 14:32
• @Mathe172 I'd already started parellel computation when I made these tests. As for gradhLr, that's a typo (f was initially called that); I'll edit my post to fix it. – Rain May 10 '18 at 14:33
• @Rain There will still be some overhead - although a big part of that is indeed only done once for the first parallel computation – Lukas Lang May 10 '18 at 14:35
• Can you provide s and t? – Brett Champion May 10 '18 at 14:44