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

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 件のコメント

Matt J
Matt J 2023 年 4 月 5 日
編集済み: Matt J 2023 年 4 月 5 日
What is the loop trying to do? In what way is it supposed to be parallel? The loop variable isim is never used at all.
Ive J
Ive J 2023 年 4 月 5 日
Are you aware that labindex is always 1 in a parfoor loop? Maybe you can explain what you're trying to do...
Patrick Mboma
Patrick Mboma 2023 年 4 月 5 日
I just simplified the code so that I can post it. The isim variable is irrelevant for the point here. It could be anything. The problem is with the "counts" variable
Matt J
Matt J 2023 年 4 月 5 日
You need to explain what you are trying to do with the counts variable. As shown, it doesn't make sense.
Matt J
Matt J 2023 年 4 月 5 日
編集済み: Matt J 2023 年 4 月 5 日
Here are the different types of variable classifications that a parfor variable can have. Which type is count supposed to be? It cannot be a broadcast variable if you are going to assign to it.
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 日
This is interesting. Could you, please, tell me how to integrate that with the rest of the code? Note I also need to get the correct labindex, which unfortunately is always 1, as someone pointed out, inside parfor loops.
Matt J
Matt J 2023 年 4 月 6 日
編集済み: Matt J 2023 年 4 月 6 日
Within spmd, labindex should work as expected.
N=20;
spmd
counts=0;
workerID=[];
for i=drange(1:N)
counts=counts+1;
workerID=labindex;
end
end
counts{:}
ans =
5
ans =
5
ans =
5
ans =
5
workerID{:}
ans =
1
ans =
2
ans =
3
ans =
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 件)

カテゴリ

ヘルプ センター および File ExchangeParallel for-Loops (parfor) についてさらに検索

タグ

質問済み:

2023 年 4 月 5 日

編集済み:

2023 年 4 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by