Parallel computing with shared variables, problem with struct
12 views (last 30 days)
Show older comments
Patrizio Graziosi
on 17 Jul 2019
Commented: Patrizio Graziosi
on 1 Aug 2019
Hi all,
I need to parallelize a code that has four nested for-loops inside which a script runs (tau_calc), that calls other scripts (like tau_ADP_v2) according to input information. These scripts need to have access to the whole workspace that has around 30 variables plus a large struct ‘state_ID’ (2 to 3 Gb).
I should parallelize on the id_E index, or [id_E,id_n] , but I cannot figure out how to pass everything to the parfor, especially the large struct and how to save temporary variables to write the state_ID struct. I understand that inside a parfor it cannot be written in the separate workers. The two scripts I attach are working correctly in serial version.
I’m in an impasse and cannot get out of it. I really need of support…
Thanks
Patrizio
0 Comments
Accepted Answer
Edric Ellis
on 18 Jul 2019
I must admit I didn't look at your code in great detail - but I did get the distinct impression that there's a lot going on there. The script tau_calc_short has a very high degree of "cyclomatic complexity" - in other words, it has lots of deeply nested control structures. The script tau_ADP_v2 has quite a few copies of near-identical computations which again are highly complex.
Now, none of that means that you can't run that stuff as one giant parfor loop, but it isn't going to make life easy. In particular, parfor needs to be able to prove that your loop iterations are independent. The parfor machinery doesn't care about the complexity of your code - but if it refuses to run your loop, it will probably be difficult for you to follow its reasoning.
Therefore, my main advice to you is: try to restructure your code into more self-contained functions. Done correctly, this will let you compartmentalise the complexity, so that the high-level computation is more digestible to the human reader. Once this is done, it will be much more feasible to work out how to apply parfor, since it will be more obvious where the independent (and thus parallelisable) portions are. Sorry that there aren't any simple answers for this sort of case.
3 Comments
Edric Ellis
on 1 Aug 2019
Whether parfor starts from the complete beginning again depends on the release of MATLAB. (I can't remember when we changed that to only re-run the failing portions - but it might well be pretty recent, i.e. R2019a or R2018b). If your workers are crashing like that, hopefully there are some crash dumps around which will help you diagnose things further.
More Answers (0)
See Also
Categories
Find more on Parallel for-Loops (parfor) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!