using variables in a parfor loop
조회 수: 6 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
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
Walter Roberson
2019년 11월 10일
If so then,
parfor i=1:cores
v=d(:,i);
vc = v;
v=v(v>0);
v = v + 1./v; %optimized out loop
vc(1:length(v)) = v;
d(:,i)=v;
end
Note that if your non-zeros are not consecutive that this can result in entries that were 0 being populated. [1 2 0 3] would be replaced with [1+1/1, 2+1/2, 3+1/3, 3] for example. It is not obvious that is what you would want.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!