How could I process a global variable in parfor
3 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
Walter Roberson
2016-1-19
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 个评论
Walter Roberson
2016-1-22
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.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!