title: How to nest multiple parfor loops
body: <p><code>parfor</code> is a convenient way to distribute independent iterations of intensive computations among several "workers". One meaningful restriction is that <code>parfor</code>-loops cannot be nested, and invariably, that is the answer to similar questions like <a href="https://stackoverflow.com/questions/13444745/converting-nested-for-loops-to-parfor-loop-matlab">there</a> and <a href="https://stackoverflow.com/questions/19193542/using-parfor-in-matlab-for-nested-loops">there</a>.</p>
  
  <p><strong>Why parallelization across loop boundaries is so desirable</strong></p>
  
  <p>Consider the following piece of code where iterations take a highly variable amount of time on a machine that allows 4 workers. Both loops iterate over 6 values, clearly hard to share among 4.</p>
  
  <pre><code>for row = 1:6
      parfor col = 1:6
          somefun(row, col);
      end
  end
  </code></pre>
  
  <p>It seems like a good idea to choose the inner loop for <code>parfor</code> because individual calls to <code>somefun</code> are more variable than iterations of the outer loop. But what if the run time for each call to <code>somefun</code> is very similar? What if there are trends in run time and we have three nested loops? These questions come up regularly, and people go to <a href="https://stackoverflow.com/questions/19844249/predicting-runtime-of-parallel-loop-using-a-priori-estimate-of-effort-per-iteran">extremes</a>.</p>
  
  <p><strong>Pattern needed for combining loops</strong></p>
  
  <p>Ideally, <code>somefun</code> is run for all pairs of <code>row</code> and <code>col</code>, and workers should get busy irrespectively of which iterand is being varied. The solution should look like</p>
  
  <pre><code>parfor p = allpairs(1:6, 1:6)
      somefun(p(1), p(2));
  end
  </code></pre>
  
  <p>Unfortunately, even if I knew which builtin function creates a matrix with all combinations of <code>row</code> and <code>col</code>, MATLAB would complain with an error <em>The range of a parfor statement must be a row vector.</em> Yet, <code>for</code> would not complain and nicely iterate over columns. An easy workaround would be to create that matrix and then index it with <code>parfor</code>:</p>
  
  <pre><code>p = allpairs(1:6, 1:6);
  parfor k = 1:size(pairs, 2)
      row = p(k, 1);
      col = p(k, 2);
      somefun(row, col);
  end
  </code></pre>
  
  <p>What is the builtin function in place of <code>allpairs</code> that I am looking for? Is there a convenient <em>idiomatic pattern</em> that someone has come up with?</p>