using variables in a parfor loop

2 ビュー (過去 30 日間)
Marc Laub
Marc Laub 2019 年 11 月 9 日
編集済み: Marc Laub 2019 年 11 月 10 日
Hey, I am very new to the parallel computing and i got some trouble.
So basicly i have so example code very similar to this one:
a = (normrnd(1,1,[100000,1]))+5;
i=0;
while i<=10000
for j=1:length(a)
a(j)=a(j)+1/a(j);
end
i=i+1;
end
Inside the nested for loop are many more alculations which takes a huge amount of time, so i basically want to cut a into as much pieces as i have physical cores, so in this case 4 *20000 and then do the same calculations of thos 20000 arrays parrallel.
But I direclty get some error cause i cannot use my input variable from outside the parfor loop because of classification (Code without the outside while loop)
parfor i=1:cores
v=d(:,i);
v=v(v>0);
lim=size(v,1);
for j=1:lim
v(j)=v(j)+1/v(j);
end
d(1:length(v),i)=v;
end
Shouldnt my input d (20000x4 array) not work as an broadcast variable as defined in https://de.mathworks.com/help/parallel-computing/troubleshoot-variables-in-parfor-loops.html ?
Or am I missanderstanding something very basic in parallel computing?
Many thanks in advance
Best regards

採用された回答

Daniel M
Daniel M 2019 年 11 月 9 日
I think it's because you're using d as the source and the sink in your loop, and you're assigning to d in a way that can make practically anything happen to the size of d, on different iterations. For example, what if on the first iteration d(:,i) contains no zeros, but on the second one it contains all zeros. Then 1:length(v) is changing every iteration and makes assignment to d impossible to determine a priori.
  2 件のコメント
Marc Laub
Marc Laub 2019 年 11 月 10 日
編集済み: Marc Laub 2019 年 11 月 10 日
d can contain zeros and if so its only the last element, in cases when my total number in a cannot be eveny distributed on my workers without reminder. So in the case of 101 elements i want to calculate 26 on worker 1 and 25 on the other 3.
It is not important in which order the 25 or 26 elements in each arrays are calculatet, but it is very important taht i know a prior wihc elements are calculated by which worker.
So i want to be basically be like this:
num_workers =4;
a = (normrnd(1,1,[100000,1]))+5;
i=0;
while i<=10000
b=randperm(length(a),length(a));
a=a(b);
d=reshape(a,[25000,num_workers]);
for k=1:num_workers
c=d(:,k);
for j=1:length(c)
c(j)=c(j)+1/c(j);
end
d(:,k)=c;
end
a=reshape(d,numel(d),1);
i=i+1;
end
but with the inner for loop k=1:4 run parallel instead of serial since the 4 arrays in d are independend.
How is this possible?
Edit: I think spmd is what i should be looking for but for beginners the examples or help is not very detailed i think.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Find more on Loops and Conditional Statements in Help Center and File Exchange

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by