# Save values from a parfor-loop

5 visualizzazioni (ultimi 30 giorni)
dominik ballreich il 13 Gen 2012
Hello,
I've got a question regarding my little parfor-example.
Y=rand(100,3)
matlabpool open
parfor p=1:2
for q=1:2
for e=1:3
Z=sum(Y(:,e)+p*q)
end
end
end
matlabpool close
For every combination of p and q there are 3 different values of Z which I would like to save within a matrix. For example in this manner:
1 1 1 1 1 1
1 1 1 2 2 2 . . .
Z(1) Z(2) Z(3) Z(4) Z(5) Z(6)
If it was not a parfor-loop but a simple for-loop, I would try something ike this:
Y=rand(100,3)
z=1
for p=1:2
for q=1:2
for e=1:3
Z=sum(Y(:,e)+p*q)
end
ergebnis(1,z)=p
ergebnis(2,z)=q
ergebnis(3,z)=Z
z=z+1
end
end
But of course that's not possible when working with parfor-loops.
It would be great, if someone helped me out.
Thank's a lot
Domninik
##### 2 CommentiMostra NessunoNascondi Nessuno
dominik ballreich il 13 Gen 2012
It would be also ok for me, if it was possible just to save only the Z(1)...Z(12) in a row-vector. Without the p and q.
Alex il 13 Gen 2012
As a recommendation, if you know the size of a matrix beforehand, always initialize it before the loop. Like in your example, ergebnis = zeros(3,12), should be placed before your second loop example. This prevents memory loops that increase in size.

Accedi per commentare.

### Risposte (2)

Alex il 13 Gen 2012
It looks like parfor sends one variable to each parallel operation, meaning that the variable references must be clear to begin with.
I.e. Z(i) = #, Z(i,j) = #, and Z(i,j,k) = # are valid but Z(i + j) is not valid.
So, that leaves you with two choices, that I know of.
1. Calculate Z as a 2x2x3 matrix
Y=rand(100,3)
Z = zeros([2,2,3]);
matlabpool open
parfor p=1:2
for q=1:2
for e=1:3
Z=sum(Y(:,e)+p*q)
end
end
end
matlabpool close
Now, you have a 2x2x3 matrix that you would need to deference into a vector using tools such as repmat or loops.
2. calculate z as a vector
The other option is to have a single Parallel loop and d3ereference the index's that you want within the loop. An example follows.
Y =rand(10,3);
Z = zeros(1,12);
parfor i = 1:12
l = mod(i,3);
if(l == 0);
l = 3;
end
j = floor( ( i -l) / 6);
k = floor( ( i - l - j) / 3);
Z( i) = sum(Y(:,l)+j*k)
end
matlabpool close
This leaves you with your desired Z vector. However, the inner loop calculations are messier.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

dominik ballreich il 13 Gen 2012
Hallo Alex,
thank you very much for your help. I tried to implement the idea of your first example into my problem. intervall=zeros([length(kaufsignal),length(verkaufsignal),length(massmooth),anzahl_bootstraps]) <-would be Z = zeros([2,2,3]);
intervall=zeros([length(kaufsignal),length(verkaufsignal),length(massmooth),anzahl_bootstraps]); matlabpool open
parfor p=1:length(kaufsignal)
for q=1:length(verkaufsignal)
for u=1:length(massmooth)
%Bootstrap-Sektion
for e=1:anzahl_bootstraps
x=verkaufsignal(1,p);y=verkaufsignal(1,q);z=massmooth(1,u);
R=Rsi(Y(:,e), z);
R=R';
vermoegen=zeros(long,1);
drin=3;
vermoegen(1)=kapital;
for i=2:long
if R(i)>x && R(i-1)<=x &&drin~=1;
drin=1;
vermoegen(i)=vermoegen(i-1)*t_k;
elseif R(i)<y && R(i-1)>=y &&drin~=0;
drin=0;
vermoegen(i)=vermoegen(i-1)*t_k;
elseif drin==1
vermoegen(i)=vermoegen(i-1)*(Y(i,e)/Y(i-1,e));
elseif drin==0
vermoegen(i)=vermoegen(i-1)*(1+(1-(Y(i,e)/Y(i-1,e))));
else vermoegen(i)=vermoegen(i-1);
end
end
Renditen_Vermoegen=[0;log(vermoegen(2+einpendeln:end)./vermoegen(1+einpendeln:end-1))];
%Performancemessung
mittelwert=mean(Renditen_Vermoegen.*weight);
intervall(p,q,u,e)=mittelwert;
end
end
end
end
matlabpool close
But Matlab tells me "The variable intervall in a parfor cannot be classified." (intervall)
##### 2 CommentiMostra NessunoNascondi Nessuno
dominik ballreich il 13 Gen 2012
I know that the code looks weird, but in my view it's similar to the little example. Running the code without the line
"intervall(p,q,u,e)=mittelwert"
works perfectly. So the variable "mittelwert" gets overwritten on each part of the loop. So the only really problem is to save these values.
All the best
Dominik
Alex il 20 Gen 2012
I don't know what the issue is. The following works for me.
Y=rand(100,3)
Z = zeros([2,2,3]);
matlabpool open
parfor p=1:2
for q=1:2
for e=1:3
Z(p,q,e)=sum(Y(:,e)+p*q)
end
end
end
matlabpool close

Accedi per commentare.

### Categorie

Scopri di più su Parallel for-Loops (parfor) in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by