MATLAB Answers

Parallel computing with shared variables, problem with struct

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

Accepted Answer

Edric Ellis
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

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!