Parallel in fmincon: getappdata & indexing problem
조회 수: 1 (최근 30일)
이전 댓글 표시
I want to parallelize the finite differences calculations of fmincon. Doing that gives an error and I've isolated the two causes.
I've recreated the issue below, where the code inside the parfor is from my objective function for fmincon.
If I run fmincon without 'UseParallel',true then it works fine.
If I run fmincon with 'UseParallel',true, then I get the same errors as are generated using the parfor example below.
I understand why I get the error but I really need a workaround as using setappdata/getappdata between fmincon iterations helps with speed.
--------------
Cause #1 - grabbing part of a vector set from getappdata
Cause #2 - the indexing of x00_i in the code below.
x00_i=zeros(2,2,1);
x00=ones(2,2);
setappdata(0,'x00',x00)
parpool
parfor ll=1:1:2
%%%%%%%%cause 1
x1= getappdata(0,'x00');
x=x1(1,:);
%%%%%%%%
%%%%%%%%cause 2
for ii= 1:1:2
y=x(ii);
x00_i(ii,:,:)=y;
end
%%%%%%%%
end
FOLLOW-UP: If someone has a similar issue as this, I found a workaround for my case. I save x00 to multiple files indexed by the number of workers and then load/save it within each parallel loop by referring to the worker with: "t = getCurrentTask(); t.ID" . Labindex doesn't work as fmincon's parallel is akin to parfor, not spmd.
That way I can read and write and update it between iterations. In my case it doesn't matter if the worker number switches around.
In the end Walter's suggestions did not really help but it may help others.
댓글 수: 0
채택된 답변
Walter Roberson
2018년 8월 10일
댓글 수: 8
Walter Roberson
2018년 8월 11일
Work around:
Before fmincon, use parfevalOnAll to initialize the starting values.
Use an fmincon OutputFcn that does whatever calculations are needed to figure out what the new parameters should be, and have it run parfevalOnAll to update values on all of the workers.
Inside the objective function, pull values from the place they were written by the parfevalOnAll.
Note that it is legal to use global for this purpose. global is not disabled inside parallel workers: the issue with global is that the global values are in no way synchronized between workers.
Instead of using global, another approach would be to store UserData against the parallel task object; see https://www.mathworks.com/help/distcomp/parallel.task.html
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Parallel Computing Fundamentals에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!