How can I run this loop in parallel ?
6 views (last 30 days)
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).
score(n) = 2*norm(Y(end,2:4)'-[P.tXo;P.tyo;terrain(P.tXo,P.tyo)])^2+0.2*Y(end,1)^2;
record_generation = score(n);
I'm new to parallel computing...
Agnish Dutta on 9 Apr 2019
Edited: Agnish Dutta on 9 Apr 2019
Since you have mentioned that you are new to parallel programming in MATLAB, I suggest going through the following resources:
What Is Parallel Computing? - https://www.mathworks.com/help/parallel-computing/what-is-parallel-computing.html
Choose a Parallel Computing Solution - https://www.mathworks.com/help/parallel-computing/choosing-a-parallel-computing-solution.html
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.
Interactively Run a Loop in Parallel Using parfor - https://www.mathworks.com/help/parallel-computing/interactively-run-a-loop-in-parallel.html
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:
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;
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:
Documentation for "parfor" - https://www.mathworks.com/help/parallel-computing/parfor.html
There are certain caveats to using "parfor" which are mentioned in the "Tips" section of the documentation provided above.