parfor with wrong indexing. Trying to assign output to parts of vector

6 views (last 30 days)
Hi,
I'm trying to run a parfor loop, but I don't know how to index one of the variables. The code is the following
tt=data(:,1);
xi=zeros(max(size(tt)),1);
parfor t=1:max(time)
xik=xi0(tt==t,1);
tk=nj(time==t,1);
ik=idt(time==t,1);
all_kk=z(time==t,:);
agg_kk=data(tt==t,4);
error=1;
while error>tol
xi0k=xik;
j=ll(xi0k,tk,ik,d,zz);
xik=xi0k+log(agg_kk)-log(j);
error=max(abs(xik-xi0k));
end
xi(tt==t,1)=xik;
end
Before the 'while' loop, the code loads 'pieces of data' that correspond to the part of each vector associated with 't'. Then, I use the while loop for each of these t's separately. The first issue is that because of the way in which I'm assigning the part of each vector associated with t, I get the message "The variable is indexed, but not slide". Could anybody suggest how to avoid having the whole vector being sent to each worker? Second, and much more important, the last part of the code takes each piece xik associated with each t, and (tries to) put it into the part that it corresponds, according to t, in the vector xi; and this is not allowed, at least in this form, for a parfor loop. Does anybody have a suggestion of how to deal with this?
Thanks,

Accepted Answer

Edric Ellis
Edric Ellis on 26 Mar 2014
At the moment, the PARFOR loop is not order-independent because of the way that you're assigning into "xi". PARFOR loops are required to be order-independent - and more than that, MATLAB must be able to deduce from the text of the loop that it is order-independent. It's far from clear from your program that you guarantee to write each element of "xi" from only one PARFOR loop iteration.
In cases like this, you might be able to refactor things slightly so that you produce a sliced output that you can then work with later. For example, you might re-write your loop like so:
xi_tmp = cell(max(time), 1); % store loop results
parfor t = 1:max(time)
...
xi_tmp{t} = xik; % keep result of xik for this iteration
end
% post-process outside PARFOR to build up xi.
for t = 1:max(time)
xi(tt = t, 1) = xi_tmp{t};
end
There's an explanation of what sliced variables are in the documentation. It's not clear to me that you can easily slice those variables - but it might not matter, it's just a warning that you are incurring extra communication.

More Answers (0)

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!