I want my script to run faster, so the purpose is, to use my cores simultaneously. The problem is that i somehow miss a layer of globality. I want my globals to be persistent within some functions, but i want them to be different in each loop-call.
what i want to do:
parfor i:T
createData() % global variables are being created
useData() % several functions need access to global vars
end
I thankful for any idea, to make this loop work simultaneously, subject to keeping my variables global. Thanks for your advise :)
Had the same issue; I was unable to use Global variables within the parallel loop (
parfor
or usingspmd
) as they turned out as empty when triggered.Instead of rewriting the entire code, I did a quick fix by storing the needed Global variables before the parallel pool and then loading them in the relevant functions if they are empty. In this way I keep my Global variables logic and only load them if they are in a parallel pool.
And then inside the function that uses the Global variable:
Caution: The disadvantage is off course that if the Global variable is really empty, then you would not detect it. You could solve this by deleting the .mat file after the parallel loop.
Second caution: I would not recommend storing big variables (in any case do not them as Global variables) as this might lower the speed significantly in each loop. Storing and loading variables is bad practice in general. Instead, try to store only things like constants or some parameters. In my case I was storing a string with the current path extension (which is less than 1kb).