Error when running function with batch()
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi. I am using batch() to run a function with a par-for loop inside. When I run it as a script (instead of a function), everything works fine. However, when I replace the script with the equivalent function, I get an error. Here are the details:
%(A) Using batch() to call script. This works fine!
job = batch(parcluster('local'), 'batchscript','Pool',5);
%(B) Using batch() to call function. This produces an error.
jm=6;
job = batch(parcluster('local'),'batchfun',0,{jm},'Pool',5);
The script (batchscript.m) I am running in A, looks something like that:
jm = 6;
run('Paths.m') %This creates 'pth' using 'jm'
parfor vh = 1:length(alld)
parfun01(vh,jm,pth)
end
The function (batchfun.m) I am running in B, looks something like that:
function batchfun(jm)
run('Paths.m') %This creates 'pth' using 'jm'
parfor vh = 1:length(alld)
parfun01(vh,jm,pth)
end
They reason why I decided to turn this from a scrip to a function was because I wanted to pass jm as an input, instead of always changing the script. However, I cannot understand why it works perfectly fine as a script, but not as a function.
The error I am getting when running B) is the following:
getReport(job.Tasks(1).Error)
ans =
'Error using batchfun (line 66)
The source code (E:\Analysis\batchfun.m) for the parfor-loop that is trying to
execute on the worker could not be found.
Caused by:
Unrecognized function or variable 'pth'.
Error using remoteParallelFunction (line 84)
Worker unable to find file.
Unrecognized function or variable 'pth'.'
Anyone could please help me with what's wrong? Thanks!
0 commentaires
Réponse acceptée
Edric Ellis
le 30 Mai 2024
This is nothing to do with batch, and everything to do with workspace transparency. Basically when a parfor loop exists inside a function, it treats any names it sees that are not definitively variables as if they are functions. In other words, the problem is that the function itself does not contain any statements that assign to pth directly.
You could fix this either by converting Paths to be a function returning values as output arguments, or you could push the parfor loop into a function which has specific input arguments which the parfor analysis can understand, like this:
function batchfun(jm)
run('Paths.m') %This creates 'pth' using 'jm'
callParfor(alld, jm, pth)
end
function callParfor(alld, jm, pth)
% All variables accessed here are input arguments to this function, and
% so parfor will understand correctly that they are variables.
parfor vh = 1:length(alld)
parfun01(vh,jm,pth)
end
end
parfor when running in a script (or at the MATLAB command-line) has different behaviour that is less efficient but can see what variables actually exist immediately prior to running the loop.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Parallel Computing Fundamentals dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!