How to use a matrix in parfor?

13 次查看(过去 30 天)
Lukas Kozubik
Lukas Kozubik 2018-3-21
评论: Rik 2022-11-15
Hello,
I have troubles with using parfor. I have program similar to this (heat transfer):
A=rand(20,20,2);
d=rand(20);
parfor i=2:19
for j=2:19
A(i,j,2)=d(i)*A(i+1,j,1)+d(i+1)*A(i-1,j,1)+d(i-1)*A(i,j+1,1)+A(i,j-1,1);
end
end
There is always error like: The PARFOR loop cannot run due to the way variable 'A' is used .
tank you very much Lukas
  3 个评论
ahmad eldeeb
ahmad eldeeb 2021-11-6
What if I want to calculate A_new=A_old+v?
Matt J
Matt J 2021-11-7
编辑:Matt J 2021-11-7
I'm skeptical that parfor is going to make things faster here. Try the following simpler test. The times shown are what I get with a 12 worker default local profile.
N=2000;
A=rand(N,N);
d=rand(N,1);
B=A(:,:,1);
C=B;
tic
for j=2:N-1
for i=2:N-1
C(i,j)=d(i)*B(i,j-1);
end
end
toc %Elapsed time is 0.014457 seconds.
tic
parfor j=2:N-1
for i=2:1999
C(i,j)=d(i)*B(i,j-1);
end
end
toc %Elapsed time is 1.889115 seconds.

请先登录,再进行评论。

回答(1 个)

Namnendra
Namnendra 2022-6-19
The different iterations in a parfor loop work independently. So, if "i" is the iterating variable in a parfor loop, you can't use A[i]=A[i+1], because the order of different iterations of loop can vary. You can call a function instead to perform the necessary operation.

类别

Help CenterFile Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by