Parallel computing with shared variables, problem with struct
11 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Edric Ellis
2019-7-18
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 个评论
Edric Ellis
2019-8-1
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!