How to convert dependent equation to independent for parallel computing (parfor)
2 views (last 30 days)
Show older comments
Abhishek Saini on 11 Feb 2019
Commented: Walter Roberson on 11 Feb 2019
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);
image(:,:,i) = image(:,:,i) +image(:,:,i-1);
You cannot do that in parallel.
What you can do is roughly
total = zeros(size(image));
parfor i = 1 : nt
total = total + image(:,:,i);
MATLAB recognizes total as being a "reduction variable" and will take care of doing the relevant additions and locking.
Also it looks like
snapshot_i = snapshot_c .* snapshot_d;
with no loop needed ?
Find more on Parallel for-Loops (parfor) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!