How can I run this loop in parallel ?

3 visualizaciones (últimos 30 días)
Angelo Charry
Angelo Charry el 1 de Abr. de 2019
Editada: Agnish Dutta el 9 de Abr. de 2019
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

Respuestas (1)

Agnish Dutta
Agnish Dutta el 9 de Abr. de 2019
Editada: Agnish Dutta el 9 de Abr. de 2019
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.

Categorías

Más información sobre Parallel for-Loops (parfor) en Help Center y File Exchange.

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by