Parallel processing for asynchronous plotting

7 views (last 30 days)
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.

Accepted Answer

Edric Ellis
Edric Ellis on 9 Jul 2019
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 Comments
Edric Ellis
Edric Ellis on 15 Jul 2019
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. on 15 Jul 2019
Thank you, it's (more or less) working now!

Sign in to comment.

More Answers (0)

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!