0
votes

I have a quite time consuming task that I perform in a for loop. Each iteration is completely independent from the others so I figured out to use the parfor loop and benefit from the i7 core of my machine.

The serial loop is:

 for i=1 : size(datacoord,1)

    %P matrix: person_number x z or
    P(i,1) = datacoord(i,1); %pn
    P(i,4) = datacoord(i,5); %or
    P(i,3) = predict(Barea2, datacoord(i,4)); %distance (z)

    dist = round(P(i,3)); %round the distance to get how many cells
    x = ceil(datacoord(i,2) / (im_w / ncell(1,dist)));
    P(i,2) = pos(dist, x); %x

end

Reading around about the parfor, the only doubt it had is that i use dist and x as indexes which are calculated inside the loop, i heard that this could be a problem. The error I get from matlab is about the way P matrix is used though. How is it? If i remember correcly from my parallel computing courses and I interpret correcly the parfor documentation, this should work by just switching the for with the parfor.

Any input would be greatly appreciated, thanks!

1
Is P preallocated? I believe parfor doesn't like variables which change size/shape.nkjt
If you provide code that compiles as is, with maybe some example or rand data, that makes it much easier to diagnose.andrelucas
Yes, P is preallocated, the exact error is: "In a PARFOR loop, P is indexed in different ways, potentially causing dependencies between iterations". As for some data, datacoord is a (:,5) matrix with (int, int, int, int, int) matrix, since the problem persists even if you leave the first 2 lines of the loop I wouldn't go further with the data since it gets quite messy expecially with the predict thing..powder
Can you confirm what im_w, ncell and pos are - variables? I'm guessing the predict part is the one that's taking a long time - in that case you ought to be able to take a lot of the other stuff out of the loop and just do the predict part via parfor.nkjt
Try replacing "P(i,3) = predict..." with a simpler one for testing, like "P(i,3) = datacoord(i,4);". If it works fine now, then the problem is with the way you are using state-space model inside PARFOR.Divakar

1 Answers

1
votes

Unfortunately, in a PARFOR loop, 'sliced' variables such as you'd like P to be cannot be indexed in multiple different ways. The simplest solution is to build up a single row, and then make a single assignment into P, like this:

parfor i=1 : size(datacoord,1)

    %P matrix: person_number x z or
    P_tmp    = NaN(1, 4);
    P_tmp(1) = datacoord(i,1); %pn
    P_tmp(4) = datacoord(i,5); %or
    P_tmp(3) = predict(Barea2, datacoord(i,4)); %distance (z)

    dist = round(P_tmp(3)); %round the distance to get how many cells
    x = ceil(datacoord(i,2) / (im_w / ncell(1,dist)));
    P_tmp(2) = pos(dist, x); %x
    P(i, :) = P_tmp;
end