필터 지우기
필터 지우기

Problem with parfor loop due to some variable that cannot be classified.

조회 수: 1 (최근 30일)
I am unable to specify a parfor loop that works due to some variable that matlab cannot classify
I tried to implement the following function
function somefunction(N,NWorkers)
counts=zeros(1,actualNworkers);
P=myprogress(N,actualNworkers);
parfor (isim=1:N,NWorkers)
local_labindex=1;
if NWorkers
local_labindex=labindex;
end
counts(local_labindex)=counts(local_labindex)+1;
send(P,[local_labindex,counts(local_labindex)])
end
end
But Matlab complains that "The PARFOR loop cannot run due to the way counts is used"
When this failed, I tried to use a cell array instead of a vector. But that didn't work either. Finally I tried to implement a nested function as shown below but it still does not work
function somefunction(N,NWorkers)
counts=zeros(1,actualNworkers);
P=myprogress(N,actualNworkers);
parfor (isim=1:N,NWorkers)
local_labindex=1;
if NWorkers
local_labindex=labindex;
end
o=increment(local_labindex);
send(P,[local_labindex,o])
end
function o=increment(index)
counts(index)=counts(index)+1;
o=counts(index);
end
end
But this time the error message is : "The nested INCREMENT cannot be called from within a PARFOR loop"
Is there any workaround this problem?
  댓글 수: 7
Patrick Mboma
Patrick Mboma 2023년 4월 5일
I have looked at that list but I could not find a solution to my problem that's why I posted it. I need to keep track of the number of iterations performed by each worker
Patrick Mboma
Patrick Mboma 2023년 4월 5일
편집: Patrick Mboma 2023년 4월 5일
As to what I am trying to do with the counts variable, I would like to take the sum to evaluate how far along the process has come.

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

채택된 답변

Matt J
Matt J 2023년 4월 5일
편집: Matt J 2023년 4월 5일
It might be easier just to use for-drange loop.
spmd
counts=0;
for i=drange(1:N)
counts=counts+1;
end
end
counts{:}
  댓글 수: 4
Patrick Mboma
Patrick Mboma 2023년 4월 6일
I guess this could work. One last question, if I may : Would this code work even if there are no workers?
Matt J
Matt J 2023년 4월 6일
편집: Matt J 2023년 4월 6일
You could try and see. Even if it does work, it might be advisable to check first to see if a pool is open before doing anything. If no pool is open, the task becomes trivial.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by