필터 지우기
필터 지우기

How to convert dependent equation to independent for parallel computing (parfor)

조회 수: 1 (최근 30일)
I am trying to run my code using 'parfor' but getting error due to dependent term. How to resolve that.
The code similar to below. I am getting error for third loop expression
image(: , : , i) = image(: , : , i) + image(: , : , i -1);
parfor ix = 1:nx
% expression to calculate image_c and image_d
for i = 1:nt
image(:,:,i) = image_c(:,:,i) +image_d(:,:,i);
end
for i=2:nt
image(:,:,i) = image(:,:,i) +image(:,:,i-1);
end
end

답변 (1개)

Walter Roberson
Walter Roberson 2019년 2월 11일
You cannot do that in parallel.
What you can do is roughly
total = zeros(size(image));
parfor i = 1 : nt
total = total + image(:,:,i);
end
MATLAB recognizes total as being a "reduction variable" and will take care of doing the relevant additions and locking.
  댓글 수: 5
Walter Roberson
Walter Roberson 2019년 2월 11일
why bother to take a cumulative sum of snapshot_i but then only extract the last layer? Why not just sum(snapshot_i,3) ?
Walter Roberson
Walter Roberson 2019년 2월 11일
Also it looks like
snapshot_i = snapshot_c .* snapshot_d;
with no loop needed ?

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by