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

6 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Matt J
Matt J am 26 Okt. 2012
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 Kommentare
Matt J
Matt J am 29 Okt. 2012
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Chris A
Chris A am 26 Okt. 2012
Bearbeitet: Chris A am 27 Okt. 2012
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 Kommentare
Tanguy
Tanguy am 27 Okt. 2012
Bearbeitet: Tanguy am 27 Okt. 2012
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 am 28 Okt. 2012
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;

Melden Sie sich an, um zu kommentieren.


Matt J
Matt J am 26 Okt. 2012
Bearbeitet: Matt J am 26 Okt. 2012
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

Kategorien

Mehr zu Parallel Computing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by