Parallel processing for asynchronous plotting

조회 수: 3 (최근 30일)
André C. D.
André C. D. 2019년 7월 8일
댓글: André C. D. 2019년 7월 15일
Hello,
I have a script that plots two different 3D graphs continuously as follows:
while(condition)
mex_function() %continuous communication with a C++ MEX function, which provides the variables a, b, c, x, y, z, etc.
subplot(1,2,1)
function1(a,b,c) %simple plotting function
subplot(1,2,2)
function2(x,y,z) %time-consuming plotting function
end
Because the second subplot takes so long, and the first one needs to be updated in real time, I tried to rewrite this using parfor. These plotting functions do not interact with each other.
parfor i = 1:2
if i == 1
% code containing variables required by mex_function
while(condition)
mex_function()
f1 = figure;
function1(a,b,c)
end
else
% code containing variables required by mex_function
while(condition)
mex_function()
f2 = figure;
function2(x,y,z)
end
end
end
The MEX function saves and gets the variables using matlabPtr->setVariable(u"var_name",variable) and variable = matlabPtr->getVariable(u"variable_name"), respectively
I get the error message that my MEX function can't get the variables declared in the 'base' workspace. I am not familiar with how/where parallel workers get their variables, so does anyone have any suggestions on how to solve this issue?
As an alternative, is there a way of running two scripts completely separately in parallel?
Thank you in advance.

채택된 답변

Edric Ellis
Edric Ellis 2019년 7월 9일
One of the restrictions of parfor is that all variable access in the body of the loop must be "transparent". This is to ensure that the parfor machinery knows which variables to transfer from the client to the workers and back again.
If possible, I would recommend restructuring your MEX function to work with explicit input and output arguments.
A second option would be to hide your calls to your MEX function inside a MATLAB function that sets up the base workspace as required. (This is required because matlabPtr->getVariable() access the base workspace). Maybe something like this:
function mexWrapper(aVal,bVal,cVal)
% Set up the base workspace
assignin('base', 'a', aVal);
assignin('base', 'b', bVal);
assignin('base', 'c', cVal);
mex_function();
... % more stuff
end
% Then, call in parfor like this
parfor i = 1:2
if i == 1
mexWrapper(a,b,c);
else
...
end
end
Note however when workers manipulate figures and other graphical objects, they are not visible at the client. (You can create graphics on workers and then save/print them to file from there).
  댓글 수: 4
Edric Ellis
Edric Ellis 2019년 7월 15일
What I meant was that instead of using setVariable and getVariable, you should write your MEX function to accept input parameters and return output parameters using the argument list.
André C. D.
André C. D. 2019년 7월 15일
Thank you, it's (more or less) working now!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by