필터 지우기
필터 지우기

Error when running function with batch()

조회 수: 10 (최근 30일)
Haris K.
Haris K. 2024년 5월 30일
댓글: Haris K. 2024년 5월 30일
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!

채택된 답변

Edric Ellis
Edric Ellis 2024년 5월 30일
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.
  댓글 수: 1
Haris K.
Haris K. 2024년 5월 30일
I added the loop as a sub-function inside batchfun and it works fine, exactly as you said. Thank you for the help Edric!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Parallel Computing Fundamentals에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by