Search code examples
arraysmatlabfile-ioparallel-processingparfor

How does MATLAB assign values to a variable and print to file in a parfor loop


I have a parfor loop that loops over i = 1:250, gets three values from an array according to the index. Then a calculation is being made within the parfor loop using those 3 values. I then store the results in the variables theta1, theta2, and theta3, and immediately print them to a file. I am parallelizing because the calculations take too much time, and for each index, they can be done independently. I guess MATLAB wouldn't necessarily divide the work by having workers compute independent iterations, but it could divide a given iteration between workers. In either case, would the value assignment to the variables or the printing to the file be affected by the order in which the operations are done? Is it possible that the results be printed in a different order like:

theta1 from i = 1
theta1 from i = 2
theta2 from i = 2
theta3 from i = 2
theta2 from i = 1
...

instead of the desired order, which is:

theta1 from i = 1 
theta2 from i = 1
theta3 from i = 1
theta1 from i = 2
theta2 from i = 2
...

Would it be healthier to collect all the results in a cell array and then print them at the very end?


Solution

  • Having the outer parfor loop means that the values of i the function looks at is not guaranteed to be 1, 2, 3, etc. However, the theta1, theta2, theta3 variables INSIDE the parfor loop will be calculated in their order inside their inner parfor loop. So the only thing you are guaranteed is that for a given value of i, theta1 will be done first, then theta2, then theta3. The workers themselves (the i loops) are interleaved, so it is completely possible that you get the result

    theta1 from i = 1 
    theta1 from i = 2 
    theta2 from i = 2
    theta3 from i = 2
    theta2 from i = 1
    ...
    

    In any case, printing to a file inside the parfor loop can cause problems, since it could be that two workers are trying to write to the file at the same time. This would mean that you'd "lose" results from some of your computations. The MATLAB way (if there is such a thing...) makes it better to save all the results from your parfor loops in an array or cell array (regular arrays are better for memory reasons, since not the entire cell array needs to be shared across all parfor loops; but cell arrays are easier to wrap your head around for prototyping) and print it to a file at the end.