I'm working on a complicated function that calls several subfunctions (within the same file). To pass data around, the setappdata
/getappdata
mechanism is used occasionally. Moreover, some subfunctions contain persistent
variables (initialized once in order to save computations later).
I've been considering whether this function can be executed on several workers in a parallel pool, but became worried that there might be some unintended data sharing (which would otherwise be unique to each worker).
My question is - how can I tell if the data in global
and/or persistent
and/or appdata
is shared between the workers or unique to each one?
Several possibly-relevant things:
- In my case, tasks are completely parallel and their results should not affect each other in any way (parallelization is done simply to save time).
- There aren't any temporary files or folders being created, so there is no risk of one worker mistakenly reading the files that were left by another.
- All
persistent
and appdata-stored variables are created/assigned within subfunction of theparfor
.
I know that each worker corresponds to a new process with its own memory space (and presumably, global
/persistent
/appdata
workspace). Based on that and on this official comment, I'd say it's probable that such sharing does not occur... But how do we ascertain it?
This is quite straightforward to test, and we shall do it in two stages.
Step 1: Manual Spawning of "Workers"
First, create these 3 functions:
Next we boot up 2 MATLAB instances (representing two different workers of a parallel pool), then run
q52623266_W1
on one of them, wait for it to finish, and runq52623266_W2
on the other. If data is shared, the 2nd instance will print something. This results (on R2018b) in:Which means that data is not shared. So far so good, but one might wonder whether this represents an actual parallel pool. So we can adjust our functions a bit and move on to next step.
Step 2: Automatic Spawning of Workers
Running the above we get:
Which again means that data is not shared. Note that in the second step we used
spmd
, which should function similarly toparfor
for the purposes of this test.