4

I want to parallelize block2 for each block1 and parallerlize outer loop too.

previous code:

    for i=rangei
        <block1>
        for j=rangej
            <block2> dependent on <block1>
        end
    end

changed code:

    parfor i=rangei
        <block1>
        parfor j=rangej
            <block2> dependent on <block1>
        end
    end

how much efficient can this get and will the changed code do the right thing? Is the changed code valid for my requirements?

dksahuji
  • 171
  • 1
  • 6

4 Answers4

5

In MATLAB, parfor cannot be nested. Which means, in your code, you should replace one parfor by a for (the outer loop most likely). More generally, I advise you to look at this tutorial on parfor.

Franck Dernoncourt
  • 77,520
  • 72
  • 342
  • 501
4

parfor cannot be nested. In nested parfor statements, only the outermost call to parfor is paralellized, which means that the inner call to parfor only adds unnecessary overhead.

To get high efficiency with parfor, the number of iterations should be much higher than the number of workers (or an exact multiple in case each iteration takes the same time), and you want a single iteration to take more than just a few milliseconds to avoid feeling the overhead from paralellization.

parfor i=rangei
    <block1>
    for j=rangej
        <block2> dependent on <block1>
    end
end

may actually fit that description, depending on the size of rangei. Alternatively, you may want to try unrolling the nested loop into a single loop, where you iterate across linear indices.

Jonas
  • 74,690
  • 10
  • 137
  • 177
  • Actually, one can observe that parfor loop iterations are chunked - so each iteration need not be so tiny. There are, of course, plenty of other factors in play, mostly to do with the overhead of transferring the data. – Edric Oct 06 '13 at 01:46
1

The following code uses a single parfor loop to implicitly manage two nested loops. The loop1_index and loop2_index are the ranges, and the loop1_counter and loop2_counter are the actual loop iterators. Also, the iterators are put in reverse order in order to have a better load balance, because usually the load of higher range values is bigger than those of smaller values.

loop1_index = [1:5]
loop2_index = [1:4]

parfor temp_label_parfor = 1 : numel(loop1_index) * numel(loop2_index)
    [loop1_counter, loop2_counter] = ind2sub([numel(loop1_index), numel(loop2_index)], temp_label_parfor)
    loop1_counter = numel(loop1_index) - loop1_counter + 1;
    loop2_counter = numel(loop2_index) - loop2_counter + 1;
end
imriss
  • 1,815
  • 4
  • 31
  • 46
0

You can't use nested parfor, From your question it seems that you are working on a matrix( with parameter i,j), try using blockproc, go through this link once blockproc

arduinolover
  • 176
  • 2
  • 5