1

I am trying to implement some logic which can be simplified in the example below:

count = 5;
value = 0;
parfor i = 1:2
    if i == 1
        for u = 0:count
            %Do dome work
            pause(5);
            value = value + 1;
        end
    else
        while true
           disp(value)
        end
    end
end

I wanted to run two loops in parallel which share a certain variable(s). Above is the closest I could get. I realized if I used the parfor as shown, I can call each on its own worker. Unfortunately, I am getting the error The PARFOR loop cannot run due to the way variable 'value' has been used

Anyone with an idea how I can achieve the above successfully?

Kochez
  • 673
  • 4
  • 10
  • What's the purpose of `value`? Are you expecting the "same" `value` counter to be incremented by either loop? That smells like bad code structure and shouldn't be required, each iteration of the parallel loop should operate independently – Wolfie Dec 22 '22 at 12:11
  • @Wolfie I am new to Matlab. But essentially, `value` should be editable in one loop while being used in another. I am not stuck on using parfor. Just looking for a way I can try to implement such logic. – Kochez Dec 22 '22 at 12:17
  • 2
    I think this might be an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) - whilst I appreciate you've made a minimal example, I think some more context about what you're ultimately trying to achieve would help us propose a solution. You cannot easily use a `parfor` like this to try and emulate running one code block out-of-process, the `parfor` is typically for running the _same_ code on many similar inputs. I think you'd have to manually save and load to disk to pass between iters, which is asking for issues. – Wolfie Dec 22 '22 at 13:14
  • 1
    You haven’t simplified the logic, you have simplified what you think is a solution for implementing your logic. Please describe in higher-level language what you need to accomplish. Maybe what you need is a timer object? – Cris Luengo Dec 22 '22 at 14:32

1 Answers1

3

It's hard to tell what you actually want to do here, but parfor does not look like a good fit. You might be better off using spmd, which is designed to allow worker-to-worker communication. However, note that the communication methods used inside spmd are essentially "two-sided" - i.e. both sender and receiver must be involved. Something like this:

spmd
    if labindex == 1
        for u = 0:count
            %Do dome work
            pause(5);
            value = value + 1;
            labSend(value, 2); % send value to other worker
        end
        labSend([], 2) % tell it to finish
    elseif labindex == 2
        value = labReceive(1);
        while ~isempty(value)
            % do stuff

            % get updated "value"
            value = labReceive(1);
        end
    end
end
Edric
  • 23,676
  • 2
  • 38
  • 40
  • How does this dependency affect the execution of the `while` loop for lab 2? Would each iteration wait to receive a new `value` from lab 1 before continuing? – Wolfie Dec 22 '22 at 13:20
  • 1
    @Wolfie yes lab 2 has to wait for the new value. If that's not OK, then there are other options using (the relatively recent) `ValueStore`. That might work, but you give up some determinism that way. – Edric Dec 22 '22 at 14:24
  • @Wolfie thanks for this answer. Let me check out how to use the `ValueStore`. – Kochez Dec 28 '22 at 18:18