필터 지우기
필터 지우기

How can I run this loop in parallel ?

조회 수: 1 (최근 30일)
Angelo Charry
Angelo Charry 2019년 4월 1일
편집: Agnish Dutta 2019년 4월 9일
Hi, I would like to run this loop in parallel while only keeping the values of Y (result of the simulation countain position, velocity, time) associated with the lowest value of score (witch is a fitness function). It's also very important that the indexs of score correspond exactly to the indexs of population (initials conditions).
for n=1:nbr_individus
Y=simulation(population(n,:),P);
score(n) = 2*norm(Y(end,2:4)'-[P.tXo;P.tyo;terrain(P.tXo,P.tyo)])^2+0.2*Y(end,1)^2;
if score(n)<record_generation
Y_record_generation=Y;
record_generation = score(n);
end
end
I'm new to parallel computing...
thanks

답변 (1개)

Agnish Dutta
Agnish Dutta 2019년 4월 9일
편집: Agnish Dutta 2019년 4월 9일
Since you have mentioned that you are new to parallel programming in MATLAB, I suggest going through the following resources:
Regarding the issue at hand, I suggest using "parfor" from the parallel computing toolbox to execute the above loop using multiple workers. The first example in the "Convert a for-Loop Into a parfor-Loop" section of the below document illustrates how a series loop operation may be parallelized.
In this case, the variables "Y_record_generation" and "record_generation" are being shared across all iterations. You will have to make sure that each iteration is totally independent of all the others.
One way to do this could be to make modifcations to your code as shown below:
parfor n=1:nbr_individus
Y_record_generation(n) = simulation(population(n,:),P);
record_generation(n) = 2*norm(Y(end,2:4)'-[P.tXo;P.tyo;terrain(P.tXo,P.tyo)])^2+0.2*Y(end,1)^2;
end
You can then calculate the "Y_record_generation" value corresponding to the minimum "record_generation" value as:
Y_record_generation = Y_record_generation(record_generation == min(record_generation));
For further reference, consider going through the following document:
There are certain caveats to using "parfor" which are mentioned in the "Tips" section of the documentation provided above.

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by