How to import objects and models in parallel implementation

조회 수: 3 (최근 30일)
Ayush
Ayush 2015년 2월 9일
댓글: Sean de Wolski 2015년 2월 9일
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.

답변 (1개)

Edric Ellis
Edric Ellis 2015년 2월 9일
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
Ayush
Ayush 2015년 2월 9일
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 2015년 2월 9일
Ayush, workerobjwrapper should help with that. It allows you to build it once and reuse it.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by