0
votes

Hi I want to run the code as below using PARFOR. When I try it says that :

Valid indices for 'A_x' and 'A_y' are restricted in PARFOR loops. Explanation For MATLAB to execute parfor loops efficiently, the amount of data sent to the MATLAB workers must be minimal. One of the ways MATLAB achieves this is by restricting the way variables can be indexed in parfor iterations. The indicated variable is indexed in a way that is incompatible with parfor. Suggested Action

Fix the indexing. For a description of the indexing restrictions, see “Sliced Variables” in the >Parallel Computing Toolbox documentation:

 N=eveninteger;
 H=zeros(N);
 V=zeros(N);
 A_x=zeros(N);
 A_y=zeros(N);
    parfor i=1:N;
      for j=1:N;
        if H(i,j)==-2;
          t=0.3;
          As_x=t*(j-i)/a;
          As_y=t*(j-i)/a;


       elseif H(i,j)==-3;
           t=0.8;
          As_x=t*(j-i)/(a*sqrt3);
          As_y=t*(j-i)/(a*sqrt3);

      elseif i==j

               As_x=i;
               As_y=i;
      else
          t=0;
          As_x=0;
          As_y=0;

        end
      for p=1:N/2
        for q=N/2+1:N

             A_x(p,q)=A_x(p,q)+As_x*(V(i,p)*V(j,q));
             A_y(p,q)=A_y(p,q)+As_y*(V(i,p)*V(j,q));


        end
      end
end

end

I could not find solution. Could you offer me a solution. Thanks in advance. Erico

1
Your code is incomplete, at least an "end" is missing. Please indent your code properly, use rightlick->"Smart Indent" in the editor. - Daniel
never tried it but having a bunch of for loops nested within a parfor loop seems awkward. And it seems some of them (nested for loops) could be replaced by vectorized expressions. - Hoki

1 Answers

1
votes

It looks like you're trying to perform a "reduction" on A_x and A_y using +. You might be able to work around this by doing something like the following:

parfor i = 1:N
  A_x_tmp = zeros(N);
  A_y_tmp = zeros(N);
  for p=1:N/2
    for q=N/2+1:N
      A_x_tmp(p,q) = A_x_tmp(p,q) + ...
      A_y_tmp(p,q) = A_y_tmp(p,q) + ...
    end
  end
  A_x = A_x + A_x_tmp;
  A_y = A_y + A_y_tmp;
end

In that way, PARFOR will understand the reduction operations on A_x and A_y.