How to transform these three nested FOR loops into a PARFOR loop?

3 次查看(过去 30 天)
Hi All,
I am trying to modify the three FOR nested loops below to allow the use of the PARFOR command.
A=sparse(m,n)
for j=1:N
for t1=1:T
for t2=t1:T % yes, t1 not 1, this is not a typo
A(f(j,t1,t2),g(j,t1,t2)) = h(j,t1,t2);
end
end
end
f, g and h are linear functions of j, t1 and t2; N>>T.
Replacing the outer FOR loop by a PARFOR loop obviously doesn't do it since A is not a sliced variable as it is right now because of the indexing.
Is there any way to do this?
Thanks in advance,
--Tanguy

采纳的回答

Matt J
Matt J 2012-10-26
Similar to Chris', I guess, but perhaps a little more readable/generalizable
A=sparse(m,n);
parfor i=1:n*T*T
[j,t1,t2] = ind2sub([N,T,T], i);
A(f(j,t1,t2),g(j,t1,t2)) = h(j,t1,t2);
end
  11 个评论
Matt J
Matt J 2012-10-29
Yes, a different approach will be required. First though, Accept-click this answer (since you say it covers the 3-loop problem in your original post) and start a new post for your new question.

请先登录,再进行评论。

更多回答(2 个)

Chris A
Chris A 2012-10-26
编辑:Chris A 2012-10-27
Here is a possible solution:
A=sparse(m,n)
indexes=tril(reshape(1:T*T, T, T));
u1=indexes(indexes>0);
v1=[0:T*T:N*T*T-1];
indexes1=kron(ones(numel(u1),1), v1);
indexes2=kron(u1,ones(1,numel(v1)));
indexes=indexes1+indexes2;
for i=1:numel(indexes),
n=indexes(i),
j=floor((n-1)/(T*T))+1;
t1=mod(floor((n-1)/T),T)+1;
t2=mod(n-1,T)+1;
if (t2 < t1),
error('Incorrect index.');
end
A(f(j,t1,t2),g(j,t1,t2)) = h(j,t1,t2);
end
  6 个评论
Tanguy
Tanguy 2012-10-27
编辑:Tanguy 2012-10-27
tempT = K. Yes the code above runs without error. The error arises when I try to assign values to A in the parfor loop:
A(rw, cl) = 1;
It says the variable A in parfor cannot be classified, which makes sense according to what they say on this page (see paragraph on Form of Indexing): http://www.mathworks.com/help/distcomp/advanced-topics.html#bq_tcng-1
Does this mean there is no way we can parallel split this FOR loop? I don't know...
Chris A
Chris A 2012-10-28
See if this works.
N = 100; %in reality this number is much larger
T = 24;
K = (T+1)*T - sum([1:T]); %constant I use in the loop
A=sparse(N*T*(T+1)/2,(N-1)*T+T);
indexes=tril(reshape(1:T*T, T, T));
u1=indexes(indexes>0);
v1=(0:T*T:N*T*T-1);
indexes1=kron(ones(numel(u1),1), v1);
indexes2=kron(u1,ones(1,numel(v1)));
indexes=indexes1+indexes2;
B=zeros(numel(indexes),3);
parfor i=1:numel(indexes),
n=indexes(i);
j=floor((n-1)/(T*T))+1;
t1=mod(floor((n-1)/T),T)+1;
t2=mod(n-1,T)+1;
% if (t2 < t1),
% error('Incorrect index.');
% end
rw = (j-1)*tempT+(T+1)*(t1-1)-sum([1:(t1-1)])+t2-t1+1; %f
cl = (j-1)*T+t1; %g
% A(rw, cl) = 1; %h=1 to simplify
B(i,:)=[rw cl 1];
end
A(sub2ind(size(b), B(:,1), B(:,2)))=1;

请先登录,再进行评论。


Matt J
Matt J 2012-10-26
编辑:Matt J 2012-10-26
A=sparse(m,n);
[J,T1,T2]=ndgrid(1:N, 1:T, 1:T);
parfor i=1:numel(J)
j=J(i); t1=T1(i); t2=T2(i);
A(f(j,t1,t2),g(j,t1,t2)) = h(j,t1,t2);
end

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by