Best practice for passing large number of constants to function that runs in parfor
5 views (last 30 days)
Show older comments
Hi everybody,
I have the following setup and I've been asking myself for a while how to handle this "the best way":
- I set/calculate a large number of constants sequentially and define a high-dimensional grid
- I then want to use those constants and the grid in a parfor loop
- Each parfor worker optimizes a function stored in an external .m file for some point on the grid
- That function uses most constants and the grid
Now what would be the best practice to use that large number of variables within the function file on each of the workers? None of the variables change within the function or the parfor loop. My first hunch would be to use globals (that's what they are for, I guess), but MATLAB doesn't allow me to do that. The two solutions I could think of is to pass all variables to the function as parameters or to all put them in a struct and pass the struct. Both of those solutions work - However, both make my code much less readable and annoy me everytime I have to work with the code. Are there any better ways of doing that?
On a sidenote/feedback, I really can't understand why MATLAB doesn't allow me to access global variables on the workers without changing them. I am a big boy and don't need MATLAB restricting my possibilities in unneccessary ways.
Thank you for suggestions,
Tobias
3 Comments
Answers (2)
Matt J
on 22 Feb 2015
Edited: Matt J
on 22 Feb 2015
Either method you describe would work, and is preferable to using global variables. I don't know what you find annoying about bundling your variables into a struct. It's always good to collect related variables together in a common object, whether a matrix for homogenous data or a struct/cell for heterogeneous data, and that's true regardless of whether it's to serve parfor or something else. It just makes the data easier to carry around and manipulate simultaneously.
If the annoyance is from the need to access your variables using dot-indexing syntax, you could unpack them into individual variables inside the workspace of your mfunction, using this FEX file for instance,
2 Comments
Matt J
on 23 Feb 2015
Edited: Matt J
on 23 Feb 2015
You need to decide at which point in the code you should pack variables into the struct and when to unpack them.
Why does the timing matter, exactly? Why not just pack the variables into the struct at the very end of the function that created them -- the point when you would need to transport them elsewhere -- and unpack them all at the beginning of the function that uses them as input? It can't be an issue of memory consumption, as far as I can see. You've already allocated memory to the variables as globals in the parent function that created them.
As for number of lines, the structvars command that I linked you to can spread multiple packing/unpacking statements across a single line so that the total number of lines is reduced, e.g.,
>> s.a=1; s.b=2; s.c=3; s.d=4;
>> structvars(2,s)
ans =
a = s.a; c = s.c;
b = s.b; d = s.d;
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!