How could I process a global variable in parfor

3 Ansichten (letzte 30 Tage)
Dingqiao Zhu
Dingqiao Zhu am 19 Jan. 2016
Kommentiert: Walter Roberson am 22 Jan. 2016
I'm using Robotic Toolbox System in Matlab to process real-time data which are caught from a robot(which is using ROS too).
The situation is, if I caught data from a robot, I have to use a global variable to keep the real-time data. However, parfor can't process a global variable because this method are designed forbidding dependency between different loop. Can I convert global variable to local variable?

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 19 Jan. 2016
No. You cannot communicate between the workers of a parfor in order to update a variable.
As well, parfor requires that the results be order-independent, that you can run the loops in any order and get the same result (to within roundoff.) If you have a variable that might update part-way through the run then that breaks order independence, as the iterations run first would have used the previous value but the later loops would have used the new values.
If you need to update a variable while parallel processing is happening, you need to use SPMD and labsend() / labreceive()
  2 Kommentare
Dingqiao Zhu
Dingqiao Zhu am 22 Jan. 2016
Thank you very much! I know that an updated variable can't be processed inside a parfor. So I'm wondering, could I convert the global variable to a local variable outside parfor and than sent it to parfor? Just like pseudo code showing below:
main()
{
global Data;
while(1)
{
get_Data_Through_Callback(Data);
convert_Global_To_Local(Data);
parfor(data);
}
}
Dose the convert function or method exist?
By the way, you've mentioned spmd and labsend/labreceive. If I use spmd to parallel process an updated variable, shall I specifically distinguish one worker to receive data at all time and then broadcast to other workers? Or I can do the updating work in the Matlab client and then broadcast to all of the workers? Because specially distinguish one worker to update a variable seems a bit wasteful
Thank you!
Walter Roberson
Walter Roberson am 22 Jan. 2016
Except that instead of the initial parfor shown there, you should use parfevalOnAll to execute the setup function once on each worker.
Note that this will not work if the global variable is mentioned directly in the body of the parfor. If you are doing that then change the name of the variable in the direct text and above the parfor assign that new name the value from the global variable.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by