f



Help with Parallel Matlab

Long story short, here is the criteria I need.
1) I need two modules running in parallel. One function must activate these two They don't need to START at the same time but both neither return info nor end. They run pretty much infinitely
1.1) I would prefer if the One function also ran separate from the above two in it's own loop but i can deal with a 3'rd function also not ending.

2) I need a shared variable between two of the modules. 1 Module will place information there while the other just reads it, but as they are running parallel.

That's it. I can do this in pretty much any other language but matlab I've been having issues with. Problem is I absolutely need to use matlab for one module.

If this is impossible to accomplish all in matlab then can i instead run this in C and call one matlab module with separate thread?

Sample code below.
function mainParallelTest()
persistent counter;
counter = 0;
parfor i=0:2
    
   if (i == 0)
       ImageProccessorParallelTest();
   elseif(i == 1)
       EventProcessorParallelTest();
   elseif(i == 2)
       prevCounter = 0;
        tempCounter = 1;
       while(true)
           tempCounter = counter;
           if (tempCounter == prevCounter)
               %disp('hi')
           end    
           prevCounter = tempCounter;
       end
   end
end
0
Alex
3/22/2016 6:19:03 PM
comp.soft-sys.matlab 211264 articles. 26 followers. lunamoonmoon (257) is leader. Post Follow

1 Replies
824 Views

Similar Articles

[PageSpeed] 39

"Alex Cruikshank" <cruiksam@gmail.com> writes:

> Long story short, here is the criteria I need.  
>
> 1) I need two modules running in parallel. One function must activate
> these two They don't need to START at the same time but both neither
> return info nor end. They run pretty much infinitely 
>
> 1.1) I would prefer if the One function also ran separate from the
> above two in it's own loop but i can deal with a 3'rd function also
> not ending.
>
> 2) I need a shared variable between two of the modules. 1 Module will
> place information there while the other just reads it, but as they are
> running parallel.

The primary mechanism for communicating between MATLAB workers executing
in parallel is labSend and labReceive - which operate within an SPMD
block. It sounds like you might want to do something a little bit like
this. In this example, worker 1 sends 'work' to workers 2 and 3, and
then gets them to quit by sending them empty 'work'. Workers 2 and 3
just consume work until there's no more.


parpool('local', 3);
spmd
  if labindex == 1
    for idx = 1:2:100
      labSend(idx, 2); % send work to 2
      labSend(idx + 1, 3); % send work to 3
    end
    labSend([], 2); % tell 2 to quit
    labSend([], 3); % tell 3 to quit
  elseif labindex == 2 || labindex == 3
    % here 2 and 3 are responding to 'work' in the same way.
    done = false;
    results = {};
    while ~done
      work = labReceive(1);
      if isempty(work)
        done = true;
      else
        results{end+1} = max(abs(eig(rand(work))));
      end
    end
  end
end
      
Cheers,
Edric.
0
Edric
3/23/2016 12:55:34 PM
Reply: