Hello,everyone! Nowadays I am trying to do some parallel computing on Matlab.I get a question and the codes below may explain it.
a=1:1000;b=zeros(10,1);for i=1:1000 j=mod(i,10); b(j+1)=b(j+1)+a(i);end
In this example,I want to calculate a sum to a in every ten numbers,and save the results in an array b. And as it can be seen,it works well in a serial program. However,when I try to do it in parallel(below),an error appear:
a=1:1000;b=zeros(10,1);matlabpool local;parfor i=1:1000 j=mod(i,10); b(j+1)=b(j+1)+a(i);endmatlabpool close;"Error: The variable b in a parfor cannot be classified."
I think variable b is a reduction variable,because in line 5 of the code,it has the form "X=X+expr" (expr is any expression). But the examples of reduction variables in the "product help" of Matlab are all simple variable,while b is an array.So I turn to "sliced variable" for help.It is a pity that the index of b is not i(the loop variable).So maybe Matlab cannot make sure what kind of variable the "_b_" is. So,my question is:How to achieve my goal(sum a in a sliced way) in parallel? In other words, how to modify the code in parallel so as to get the same results as that in serial way? Your answer or idea is important for me.Thank you in advance!
Best Answer