Problem's variable used by workers

5 views (last 30 days)
Andrea Stevanato
Andrea Stevanato on 30 May 2018
Commented: Andrea Stevanato on 31 May 2018
I'm seeing documentation of parfor loop and i'm reading that i can't use global and persistent variables in parfor loop, so how i can share a "problem's variable" between workers? In my code there is a parfor loop that call some function, in this function that call other function (etc, etc) i need to read a "problem's variable", how i can do it without global and without pass variable between all function call? For exemple:
parfor i = 1:size(...)
var = function1()
end
function result = function1()
var2 = function2()
end
function result = function2()
global varsProblems;
%something else
end
  3 Comments
Andrea Stevanato
Andrea Stevanato on 31 May 2018
I got an error using parallel.pool.Constant. In the script i create a struct with parallel.pool.Constant value as follow:
globalVar = who('global');
for index = 1:size(globalVar,1)
constant.(globalVar{index}) = parallel.pool.Constant(eval(globalVar{index}));
end
The struct was created correclty but into parfor-loop, when i call function that use this variable i can't access to constant struct. The code is the follow:
mainScript.m
parfor i = 1:tot
result = myFunction();
end
myFunction.m()
% some code here
var = otherFunction();
otherFunction.m
if ~isempty(getCurrentWorker)
global numberOfWorkers;
global numberOfTasks;
else
numberOfWorkers = constant.numberOfWorkers.Value;
numberOfTasks = constant.numberOfTasks.Value;
end
% some code here
The error that I receive is the follow: Undefined variable "constant" or class "constant.numberOfWorkers.Value".
Andrea Stevanato
Andrea Stevanato on 31 May 2018
An UndefinedFunction error was thrown on the workers for 'costant'. This may be because the file containing
'costant' is not accessible on the workers. Specify the required files for this parallel pool using the command:
addAttachedFiles(pool, ...). See the documentation for parpool for more details.
Undefined function or variable 'costant'.

Sign in to comment.

Answers (0)

Categories

Find more on Parallel Computing Fundamentals 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!