parfor loop , how should i use

Hi,
this is part of my code, it is my frist time to use par for.
the first question, how to fix this error such that The variable Evaluation in a parfor cannot be classified.
second shoud i use parfor loop for eac for loop or that one time as my code?
Thanks in advance.
parfor i=1:iteration
for k=1:swarm_size
zz=1;
for j=1 :centers
[fbest,best]=min(Evaluation);
gbest= swarm(best,:);
new_pos =swarm(k,:);
v(k,zz:zz+1)=v(k,zz:zz+1)+c1*rand*(Pbest(k,zz:zz+1)-swarm(k,zz:zz+1))+c2*rand*(gbest(1,zz:zz+1)-swarm(k,zz:zz+1));
new_pos(1,zz:zz+1)=swarm(k,zz:zz+1)+v(k,zz:zz+1);
zo=1; lo=1;
for Io=1: centers
C3C(:,Io) = f(Invpoints(:,1), new_pos(1,zo),Invpoints(:,2), new_pos(1,zo+1));
zo=zo+2;
end
fnewpos= sum(min(C3C,[],2));
for go=1: centers
C4C(:,go) = f(Invpoints(:,1), swarm(k,lo),Invpoints(:,2), swarm(k,lo+1));
lo=lo+2;
end
fswarm = sum(min(C4C,[],2));
zz=zz+2;
end
end
min(Evaluation)
end

16 Kommentare

The variable Evaluation is not changed inside your loop. It is not clear why you do not do the
[fbest, best] = min(Evaluation);
once, before the parfor ?
Walter Roberson
Walter Roberson am 22 Mär. 2019
It looks to me as if v is plausibly an output variable. You should explicitly initialize it inside for k to make it easier for parfor to figure out that it is intended to be local.
You do not index anything with the variable of your parfor loop. What is your intended output?
Hassan
Hassan am 22 Mär. 2019
in the part i calculate an objective function "Evaluation"
in the second part which i need to more faster by using parfor, i find a new solution for the problem and if it best than the value in "Evaluation" i update "Evaluation" if not do nothing
In what you posted, you do not change Evaluation .
If you were to do
Evalution = min(Evaluation, some_new_value);
then that would generally be permitted, as it is a "min reduction". However to get it to work you might perhaps not be able to test Evaluation.
I am unclear as to whether Evaluation is a scalar or a vector? If it is a vector then it is not obvious what it means for a value to be better than the value in Evaluation ?
Hassan
Hassan am 22 Mär. 2019
sorry the first part to calculate "Evaluation " is not mentioned here;
the code worked after i add this line after parfor
ParEvaluation= Evaluation; Parswarm= swarm; Parv=v; ParPbest= Pbest;
is it resanable to you?
my second ask, for the too many nested "for loop" i only use one parfor or parfor for each "for loop"?
Walter Roberson
Walter Roberson am 22 Mär. 2019
Since we do not have your actual code, it is difficult to say whether your iterations are independent of each other. Going only by the code you have posted, I suspect your for i loop needs to be serial, as it looks like you are doing iterative pso algorithm work, and for pso each iteration starts from where the previous iteration ended, rather than each iteration being independent of the others.
If I am correct that your for i needs to be serial, then my suspicion is that using parfor for your k loop (swarm size) will require more communications overhead than you gain in efficiency by having the swarm members processed in parallel. If you are doing pso, then you would be better off vectorizing the work on the smarm members.
Hassan
Hassan am 23 Mär. 2019
yes, this is a PSO code, in the frist part i generate the swarm and then evaluate it,
the actual code "which i attached" and i need to use parfor to speed up the calcualtions,
could you consider that the above code is the actual code, mainly i have three loops (1) iterations, (2) swarm_size (3) centers "problem diminsions"
your ask about independent , serial, and vectorizing the work of the swarm NOT clear for me, i still ask how should i use parfor to optimize the above code, " Kindly, colud you explain your points by examplying my code "
If the above were your actual code, then I would optimize it by replacing
parfor i=1:iteration
.... bunch of code
end
with
... bunch of code, done only once, with no parfor loop.
Hassan
Hassan am 23 Mär. 2019
Dear Walter,
Thanks for your patience, and i appreciate your support
i made some changes for the code to get ride of this error
Error: The temporary variable Evaluation in a parfor is uninitialized.
See Parallel for Loops in MATLAB, "Uninitialized Temporaries".
however the code is still not working and i get the same error,
parfor i=1:iteration
ParEvaluation= Evaluation; Parswarm= swarm; Parv=v; ParPbest= Pbest;
Evaluation=[]; swarm=[]; Pbest=[]; v=[];
for k=1:swarm_size
zz=1;
for j=1 :centers
[fbest,best]=min(ParEvaluation); gbest= Parswarm(best,:);
new_pos = Parswarm(k,:);
Parv(k,zz:zz+1)=Parv(k,zz:zz+1)+c1*rand*(ParPbest(k,zz:zz+1)-Parswarm(k,zz:zz+1))+c2*rand*(gbest(1,zz:zz+1)- Parswarm(k,zz:zz+1));
new_pos(1,zz:zz+1)= Parswarm(k,zz:zz+1)+ Parv(k,zz:zz+1);
zo=1; lo=1; C3C=[]; C4C=[];
for Io=1: centers
C3C(:,Io) = f(Invpoints(:,1), new_pos(1,zo),Invpoints(:,2), new_pos(1,zo+1));
zo=zo+2;
end
fnewpos= sum(min(C3C,[],2));
for go=1: centers
C4C(:,go) = f(Invpoints(:,1), Parswarm(k,lo),Invpoints(:,2), Parswarm(k,lo+1));
lo=lo+2;
end
fswarm = sum(min(C4C,[],2));
if fnewpos < fswarm, Parswarm(k,zz:zz+1)=new_pos(1,zz:zz+1); end
if fnewpos < ParEvaluation (k), ParPbest(k, zz:zz+1)=new_pos(1,zz:zz+1); ParEvaluation (k)= fnewpos; end
zz=zz+2;
end
end
Evaluation= ParEvaluation; swarm= Parswarm; v=Parv; Pbest= ParPbest;
min(ParEvaluation)
end
Walter Roberson
Walter Roberson am 23 Mär. 2019
Why are you not simply using particleswarm() ?
Hassan
Hassan am 24 Mär. 2019
i need to run my code on parfor
Walter Roberson
Walter Roberson am 24 Mär. 2019
Was using parfor given as part of the project definition?
Hassan
Hassan am 25 Mär. 2019
for large-scale problem the run time about 10 hours, parfor will be a good soultion to reduce the time.
Walter Roberson
Walter Roberson am 25 Mär. 2019
That is an assumption not a requirement .
Your code is difficult to understand, but when I looked at it earlier, it looked to me that it was likely that using parfor will slow you down rather than improving performance.
What range of values do you expect for swarm_size ?
Hassan
Hassan am 25 Mär. 2019
Swarm Size=100
Iterations = 100
Dimensions = 5 : 100
Walter Roberson
Walter Roberson am 25 Mär. 2019
I am not sure what Dimensions corresponds to in your code?
What kind of value does centers have?
Your code appears to run j=1:centers and within that loop runs for Io = 1:centers, and also for go=1:centers . That suggests a run time on the order of swarm_size * centers^2 -- or possibly more as it is not clear whether f is a function or a 4D array being indexed.
Your code would really benefit from comments; it is difficult to understand what you are doing and why.

Antworten (0)

Diese Frage ist geschlossen.

Produkte

Version

R2018a

Gefragt:

am 22 Mär. 2019

Geschlossen:

am 20 Aug. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by