1
votes

I have this example code that works well with simple loop:

for i=1:n
   y = function1(X(1:i-1))
   X(i) = function2(y)
end

My output is X. I know that parfor clears the variables each loop iteration. Can someone help me to parallelize it with parfor using X inside the loop? Is there a way to communicate the output variable X to the workers at each loop iteration? Thanks.

1

1 Answers

2
votes

MATLAB doesn't clear all variables in each iteration, there is some intelligence behind that: Look at the following parfor loop:

parfor k=1:n
    x = function1(a(k));
    y(k) = function2(a(k));
end

First it is important to know that the iteration order is not fix, so there is no order in which the iterations are performed. That is why you can't know what value x will have after this loop. So MATLAB decided to clear this variable, to prevent possible problems. For y on the other hand it is obvious what value it will have after the loop, so this will work.

In your case this means that you can't use the value of y outside the loop, but you can use X. The problem is something else:

X(i) depends on X(1:i-1). That means that the iterations have to be in the order from 1 to n. parfor can not possibly do that. Assuming you have a Quad-Core CPU, so you will have 4 workers. Assume iterations 1 to 4 are started simultaneously on one worker each. Iterations 2 to 4 would have to wait for iteration 1 to be finished, then iterations 3 to 5 would wait for 2 to finish, and so on.

You will either have to stay with a for loop, or find a way to change your functions such that they don't depend on the previous values.