How to import objects and models in parallel implementation

1 view (last 30 days)
I am using parallel computation toolbox to run two functions simultaneously like this:
funList = {@f1,@f2};
parpool(2);
spmd
labBarrier
funList{labindex}()
end
function [] = f1()
for k=1:10
labSend(rand(1,3),2,k);
end
end
function [] = f2()
for k=1:10
labReceive(1,k)
end
end
The problem is I can't call any objects from my workspace using evalin function in neither f1 nor f2 nor I can send variables to my base workspace using assignin function.
I need to call serial port object from base workspace and get output from f2() in base workspace in real time. Is there any way to do it except creating a new object inside these functions.

Answers (1)

Edric Ellis
Edric Ellis on 9 Feb 2015
The parallel pool workers are separate MATLAB processes, so you might well need to create new serial port objects on the workers. I'm not too familiar with the serial port object in MATLAB, but it sounds like you can't have multiple objects referring to the same underlying port, so you probably need to ensure you only build the object on one worker, something like this:
% build the serial port object
spmd
if labindex == 1
s = serial('COM1');
else
s = [];
end
end
% Use the serial port object on lab 1
spmd
if labindex == 1
f1(s);
else
f2();
end
end
  2 Comments
Ayush
Ayush on 9 Feb 2015
Thanks for your reply but I am doing the same thing already. The problem with this method is every time I call f1() I need to initialize serial port which takes around 10-15 seconds and I am doing processing in f2() so I need real time update of f2() output either in base workspace or in any other way through which I can access it's output in real time.
Sean de Wolski
Sean de Wolski on 9 Feb 2015
Ayush, workerobjwrapper should help with that. It allows you to build it once and reuse it.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!