matlab global variables and parallel pooling

2019-04-13 05:28发布

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 :)

1条回答
贼婆χ
2楼-- · 2019-04-13 06:01

Had the same issue; I was unable to use Global variables within the parallel loop (parfor or using spmd) 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.

% Store global variables to be reused in parallel workers
global Var1
save('temp_global_parallel','Var1');

% Parallel pool functions
parpool(4)
spmd
    someFunctions();
    anotherFunction();
end

% Optionally: delete to avoid the bug as explained below
delete('temp_global_variable');

And then inside the function that uses the Global variable:

global Var1
if isempty(Var1)
    load('temp_global_parallel')
end

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).

查看更多
登录 后发表回答