FOR to PARFOR - cannot make it right

1 view (last 30 days)
Hello everyone,
I am having troubles of adjusting FOR loop codes below to PARFOR loop. Hopefully, you can help me out. Thank you so much in advance.
for i = 1:size(t2,1)-1
x = find(t1.v1>t2.v1(i) & t1.v1<=t2.v1(a+1));
t1.v2(x(t1.v2(x)~=t2.v2(a))) = 0;
end
PS: I am not sure if I give out enough information, please let me know if you need me to clear out anything.
  2 Comments
Joseph Cheng
Joseph Cheng on 8 Jul 2014
I do not think this is a case where you can use PARFOR. in the t1.v2() line you maybe indexing the same values in x in parallel loops. what is the error it gives you when you try this?
Hai Nguyen
Hai Nguyen on 8 Jul 2014
Thank you for responding. Actually I got 3 warning when I used PARFOR instead of FOR.
  • The PARFOR loop cannot run due to the way variable 't1' used
  • Valid indices for 't1' are restricted in PARFOR loop
  • The entire array or structure 't2' is broadcast variable. This might result in unnecessary communication overhead.

Sign in to comment.

Accepted Answer

Edric Ellis
Edric Ellis on 9 Jul 2014
Edited: Edric Ellis on 9 Jul 2014
There are several problems here, but I think the biggest problems you need to overcome is that the result of a PARFOR loop needs to be expressed as a sliced or a reduction variable, and that the iterations of the loop need to be provably order-independent. Simply put, a sliced output is one where MATLAB can see from the text of your program that each loop iteration assigns into a slice of an array, and the slice depends on the loop variable. So, the following is a sliced output:
x = zeros(10);
parfor idx = 1:10
x(:, idx) = rand(10, 1);
end
In this case, each iteration of the loop assigns into a column slice of x. In your case, the indexing expression into t1.v2 is problematic firstly because it is not a simple 'sliced' form, and secondly because the indexing expression depends on previous values in t1.v2.
You might be able to run the first expression in the loop as a PARFOR, and then perform the assignment in an ordinary FOR loop later - but this will probably not get you any improvement in performance.
parfor i = 1:size(t2,1)-1
xc{i} = find(t1.v1>t2.v1(i) & t1.v1<=t2.v1(a+1));
end
for i = 1:size(t2,1)-1
x = xc{i};
t1.v2(x(t1.v2(x)~=t2.v2(a))) = 0;
end

More Answers (0)

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!