arrays - How does MATLAB assign values to a variable and print to file in a parfor loop -
i have parfor
loop loops on i = 1:250
, gets 3 values array according index. calculation being made within parfor
loop using 3 values. store results in variables theta1
, theta2
, , theta3
, , print them file. parallelizing because calculations take time, , each index, can done independently. guess matlab
wouldn't divide work having workers compute independent iterations, divide given iteration between workers. in either case, value assignment variables or printing file affected order in operations done? possible results printed in different order like:
theta1 = 1 theta1 = 2 theta2 = 2 theta3 = 2 theta2 = 1 ...
instead of desired order, is:
theta1 = 1 theta2 = 1 theta3 = 1 theta1 = 2 theta2 = 2 ...
would healthier collect results in cell array , print them @ end?
having outer parfor loop means values of i
function looks @ not guaranteed 1, 2, 3, etc. however, theta1
, theta2
, theta3
variables inside parfor loop calculated in order inside inner parfor loop. thing guaranteed given value of i
, theta1
done first, theta2
, theta3
. workers (the i
loops) interleaved, possible result
theta1 = 1 theta1 = 2 theta2 = 2 theta3 = 2 theta2 = 1 ...
in case, printing file inside parfor loop can cause problems, since 2 workers trying write file @ same time. mean you'd "lose" results of computations. matlab way (if there such thing...) makes better save results parfor loops in array or cell array (regular arrays better memory reasons, since not entire cell array needs shared across parfor loops; cell arrays easier wrap head around prototyping) , print file @ end.
Comments
Post a Comment