Reduction function called in a parfor loop cannot have more than two variables?

I'm sort of confused about the transparency policy in parfor loop. For example, this works:
a=zeros(10,1);
parfor i = 1:10
d=2;
x=(1:10)'+i;
a=mymax(a,x);
end
where mymax is a function:
function y=mymax(a,x)
b=[a;x];
[~,idx]=sort(b,'descend');
y=b(idx(1:10));
But this doesn't work (even if I replace mymax(a,x,d) with mymax(a,x,2)):
a=zeros(10,1);
parfor i = 1:10
d=2;
x=(1:10)'+i;
a=mymax(a,x,d);
end
where mymax is a function:
function y=mymax(a,x,d)
b=[a;x];
[~,idx]=sort(b,'descend');
y=b(idx(1:10))+d;
I've no idea why this doesn't work... It doesn't violate the transparency policy does it? How can I fix it? Is there any workaround for me to pass the constant d to the function mymax? Thanks a lot in advance!

 채택된 답변

The problem here is to do with both the number of arguments to your function, and how you're using it with respect to the outputs. In the case where things work, i.e.
parfor ...
a = mymax(a, b);
end
the parfor machinery is treating the variable a as a reduction variable, and the function mymax as a reduction operation. As described in that reference page, the reduction function must be a binary associative and commutative function.
Once you start having more than 2 arguments, parfor cannot treat the function as a reduction function. You can still call the function for sliced outputs, e.g.
parfor idx = ...
a(idx) = someFcn(1,2,3,4,5);
end
Or, if you still want a reduction function - the reduction reference (somewhat indirectly) suggests the following, which should work:
fcn = @(a,b) mymax(a,b,3)
parfor ...
a = fcn(a,x);
end
Note that fcn must be defined outside the parfor loop, and not modified inside it.

댓글 수: 1

Li Xie
Li Xie 2015년 12월 4일
편집: Li Xie 2015년 12월 4일
Hi Edric,
Thank you so much for your help! I eventually created a handle like you suggested and it worked. However, when a and b are structure arrays I ran into strange problems again... please see my post here
Any help is very much appreciated!
Li

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

질문:

2015년 12월 2일

편집:

2015년 12월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by