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!
P
preallocated? I believeparfor
doesn't like variables which change size/shape. – nkjtdatacoord
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.. – powderim_w
,ncell
andpos
are - variables? I'm guessing thepredict
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 thepredict
part viaparfor
. – nkjt